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;
    }
  • 相关阅读:
    js快速排序
    蛇形数组
    大臣的旅费
    1724ROADS
    2738:实数加法
    完整版高精度计算(整理后的)
    2737:大整数除法
    2980:大整数乘法
    2736大整数减法,3180 整数减法
    2981:大整数加法
  • 原文地址:https://www.cnblogs.com/rstz/p/13725297.html
Copyright © 2011-2022 走看看