LISP~~~~~

#include<iostream>
#include<vector>
#include<stack>
#include<string>

using namespace std;

void Lisp(string str)
{
    vector<string> st;
    stack<int>res;
    bool flag = true;
    int n = str.size();

    for(int i = n - 1; i >= 0;)
    {
        if(str[i] == ')' || str[i] == ' ' || str[i] == '(')
        {
            i--;
        }
        else if(str[i] >= 'a' && str[i] <= 'z')
        {
            string op = str.substr(i - 2, 3);
            st.push_back(op);
            i -= 3;
        }
        else
        {
            int spaceIndex = str.rfind(' ', i);
            string num = str.substr(spaceIndex + 1, i - spaceIndex);
            st.push_back(num);
            i = spaceIndex;
        }
    }

    n = st.size();
    for(int j = 0; j < n; j++)
    {
        string s = st[j];
        if(s == "add" || s == "sub" || s == "mul" || s == "div")
        {
            if(res.size() < 2)
            {
                return;
            }
            int result = 0;
            int num2 = res.top();
            res.pop();
            int num1 = res.top();
            res.pop();

            if(s == "add")
            {
                result = num1 + num2;
            }
            else if(s == "sub")
            {
                result = num2 - num1;
            }
            else if(s == "mul")
            {
                result = num1*num2;
            }
            else if(s == "div")
            {
                if(num1 == 0)
                {
                    flag = false;
                }
                else
                {
                    result = num2 / num1;
                }
            }

            res.push(result);
        }
        else
        {
            res.push(atoi(s.c_str()));
        }
    }
    if(!flag)
    {
        cout << "error" << endl;
    }
    else
    {
        cout << res.top() << endl;
    }
}

int main()
{
    string str;
    cout << "please input the string:";
    getline(cin, str);
    Lisp(str);
    return 0;
}
 

相关推荐

  1. LISP~~~~~

    2023-12-08 08:38:01       34 阅读
  2. <span style='color:red;'>list</span>

    list

    2023-12-08 08:38:01      29 阅读
  3. list

    2023-12-08 08:38:01       9 阅读
  4. list

    2023-12-08 08:38:01       10 阅读
  5. LISP入门

    2023-12-08 08:38:01       14 阅读
  6. LISP学习历程

    2023-12-08 08:38:01       13 阅读
  7. lisp学习历程

    2023-12-08 08:38:01       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-08 08:38:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-08 08:38:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-08 08:38:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-08 08:38:01       18 阅读

热门阅读

  1. Spring Kafka常用配置详解

    2023-12-08 08:38:01       32 阅读
  2. c++通过serial库进行上下位机通信

    2023-12-08 08:38:01       38 阅读
  3. 怎样学习AI编程?

    2023-12-08 08:38:01       37 阅读
  4. [rk3308]源码编译

    2023-12-08 08:38:01       37 阅读
  5. 使用python脚本轻松实现ssh免密登陆配置

    2023-12-08 08:38:01       42 阅读
  6. nginx 一键切换停机维护页面 —— 筑梦之路

    2023-12-08 08:38:01       38 阅读