• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天之道

            享受編程的樂趣。
            posts - 118, comments - 7, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            簡單表達式求值

            Posted on 2012-10-08 17:25 hoshelly 閱讀(1255) 評論(0)  編輯 收藏 引用 所屬分類: Programming
            描述
            給一些包含加減號和小括號的表達式,求出該表達式的值。表達式中的數值均為絕對值小于 10 的整數。

            輸入
            第一行為表達式的個數 n,以下 n 行每行有一個表達式。每個表達式的長度不超過 20 個字符。

            輸出
            對每個表達式,輸出它的值。

            樣例輸入
            3
            3-(2+3)
            -9+8+(2+3)-(-1+4)
            0-0
            樣例輸出
            -2
            1
            0

            //對每種情況都要考慮清楚

            #include <cctype>
            #include <iostream>
            #include <string>
            #include <stack>
            #include <map>

            using namespace std;

            int getPrecedence(const char optr) //給各個操作符定義優先級順序,利用map容器
            {
                map<charint> precedTable;
                precedTable['#'] = 0;
                precedTable[')'] = 1;
                precedTable['+'] = 2;
                precedTable['-'] = 2;
                precedTable['*'] = 3;
                precedTable['/'] = 3;
                precedTable['('] = 10;
                return precedTable[optr];
            }

            int calculate(int a, char optr, int b)
            {
                switch (optr) {
                    case '+': return a + b;
                    case '-': return a - b;
                    case '*': return a * b;
                    case '/': return a / b;
                    defaultreturn 0;
                }
            }

            int evaluate(const string& expr)
            {
                stack<int> opnd;  
                stack<char> optr;   
                char last_ch = '\0'; //每次檢查字符時的前一個字符
                for (size_t i = 0; i != expr.size(); ++i) {  
                    const char ch = expr[i];
                    if (isspace(ch)) { //如果是空格,即跳過
                        continue;
                    } else if (isdigit(ch)) { //如果是數字,則壓入操作數棧
                        opnd.push(ch - '0');
                    } else {
                        if ((ch == '-'  || ch == '+') && (last_ch == '\0' || last_ch == '(')) //遇到 '+'、'-',則壓入0進操作數棧
                            opnd.push(0);    //如 7-1,遇'-'則壓入0進棧,,'-'則進操作符棧,遇到下一個數1,計算0-1得-1
                        if (optr.empty() || ch == '(' || (optr.top() == '(' && ch != ')') || getPrecedence(ch) > getPrecedence(optr.top())) {
                            optr.push(ch);
                        } 
                        else 
                        {
                            while (!optr.empty() && optr.top() != '(' && getPrecedence(ch) <= getPrecedence(optr.top())) 
                            {
                                int b = opnd.top(); opnd.pop();
                                int a = opnd.top(); opnd.pop();
                                opnd.push(calculate(a, optr.top(), b));
                                optr.pop();
                            }
                            if (ch == ')')
                                optr.pop();    
                            else
                                optr.push(ch);
                        }
                    }
                    last_ch = ch;
                }
                while (!optr.empty()) {
                    int b = opnd.top(); opnd.pop();
                    int a = opnd.top(); opnd.pop();
                    opnd.push(calculate(a, optr.top(), b));
                    optr.pop();
                }
                int result = opnd.top(); opnd.pop();
                return result;
            }

            int main()
            {
                int n;
                cin >> n;
                while (n-- > 0) {
                    string expr;
                    cin>>expr;
                    cout << evaluate(expr) << endl;
                }
                return 0;
            }
            久久这里只有精品18| 色综合久久中文字幕综合网| 久久亚洲熟女cc98cm| 中文精品99久久国产 | 97久久香蕉国产线看观看| 精品国际久久久久999波多野| 久久免费精品视频| 少妇人妻综合久久中文字幕| 精品国产99久久久久久麻豆| 久久99国产精一区二区三区| 综合久久精品色| 嫩草影院久久国产精品| 久久久久se色偷偷亚洲精品av| 久久99国产精品久久| 亚洲日本久久久午夜精品| 精品午夜久久福利大片| 亚洲国产精品无码久久SM| 91久久精品国产91性色也| 亚洲国产另类久久久精品小说 | 久久久这里有精品中文字幕| 久久久久久亚洲Av无码精品专口| 欧美粉嫩小泬久久久久久久| 99久久精品日本一区二区免费| 91麻豆国产精品91久久久| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区| 欧美亚洲国产精品久久| 久久人人爽人人精品视频| 久久国产成人精品麻豆| 国内高清久久久久久| 久久天天躁狠狠躁夜夜2020一 | 精品久久777| 色8久久人人97超碰香蕉987| 久久天天躁夜夜躁狠狠躁2022| 中文字幕精品久久久久人妻| 久久黄视频| 亚洲国产综合久久天堂 | 91久久成人免费| 香港aa三级久久三级| 亚洲成人精品久久| 久久精品国产亚洲沈樵| 亚洲成人精品久久|