zoukankan      html  css  js  c++  java
  • 表达式计算

    输入表达式计算出值

    #include <iostream>
    #include <stack>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    
    stack<int> nums;
    stack<char> ops;
    
    void cal()
    {
        int a = nums.top();
        nums.pop();
        int b = nums.top();
        nums.pop();
        char c = ops.top();
        ops.pop();
        int d = 0;
    
        if (c == '+')
            d = b + a;
        else if (c == '-')
            d = b - a;
        else if (c == '*')
            d = b * a;
        else if (c == '/')
            d = b / a;
        else
            d = pow(b, a); // ^
        nums.push(d);
    }
    
    int main()
    {
        string str;
        cin >> str;
    
        string left;
        for (int i = 0; i < str.size(); i++)
            left += '(';
        str = left + str + ')';
    
        for (int i = 0; i < str.size(); i++)
        {
            if (isdigit(str[i]))
            {
                int j = i, t = 0;
                while (isdigit(str[j]))
                {
                    t = t * 10 + str[j] - '0';
                    j++;
                }
                nums.push(t);
                i = j - 1;
            }
            else
            {
                char c = str[i];
                if (c == '(')
                    ops.push(c);
                else if (c == '+' || c == '-')
                {
                    if (c == '-' && i && !(isdigit(str[i - 1])) && str[i - 1] != ')')
                    {
                        int j = i + 1, t = 0;
                        while (isdigit(str[j]))
                        {
                            t = t * 10 + str[j] - '0';
                            j++;
                        }
                        nums.push(-t);
                        i = j - 1;
                    }
                    else
                    {
                        while (ops.top() != '(')
                            cal();
                        ops.push(c);
                    }
                }
                else if (c == '*' || c == '/')
                {
                    while (ops.top() == '*' || ops.top() == '/' || ops.top() == '^')
                        cal();
                    ops.push(c);
                }
                else if (c == '^')
                {
                    while (ops.top() == '^')
                        cal();
                    ops.push(c);
                }
                else if (c == ')')
                {
                    while (ops.top() != '(')
                        cal();
                    ops.pop();
                }
                else
                    cout << "invalid operator!" << endl;
            }
        }
    
        cout << nums.top() << endl;
        return 0;
    }
  • 相关阅读:
    JSP_EL使用
    Ajax乱码问题
    Myeclipse安装svn插件(link方式)
    JAVA多线程通信
    Java序列化与反序列化(Serializable)
    Java 字符流实现文件读写操作(FileReader-FileWriter)
    Java 字节流实现文件读写操作(InputStream-OutputStream)
    JAVA环境变量配置
    Flex设置外部浏览器
    J2EE5(Servlet2.5)对EL表达式的支持
  • 原文地址:https://www.cnblogs.com/rstz/p/13725297.html
Copyright © 2011-2022 走看看