• <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 閱讀(1249) 評論(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;
            }
            亚洲欧美日韩精品久久亚洲区 | 一本久久a久久精品综合香蕉| 97久久国产亚洲精品超碰热| 久久美女人爽女人爽| 久久亚洲高清综合| www性久久久com| 亚洲精品午夜国产va久久| 国产精品久久亚洲不卡动漫| 久久久久亚洲精品中文字幕| 午夜不卡久久精品无码免费| 26uuu久久五月天| 日韩精品久久无码人妻中文字幕| 精品久久久久一区二区三区| 亚洲精品国产美女久久久| 久久综合五月丁香久久激情| 久久国产精品成人片免费| 久久久久久久综合日本| 久久精品无码一区二区三区| 无码AV波多野结衣久久| 一本一道久久a久久精品综合| 亚洲伊人久久大香线蕉苏妲己| 久久精品国产精品亚洲精品| 国产叼嘿久久精品久久| 蜜臀av性久久久久蜜臀aⅴ| 久久精品综合网| 亚洲精品综合久久| 婷婷久久综合九色综合九七| 久久夜色精品国产亚洲| 久久国产免费观看精品| 97久久天天综合色天天综合色hd| 久久亚洲sm情趣捆绑调教| 亚洲精品乱码久久久久久蜜桃| 久久精品无码一区二区app| 日本免费一区二区久久人人澡| 2021精品国产综合久久| 久久久久久久久久久久中文字幕 | 久久国产乱子伦精品免费强| 久久国产热精品波多野结衣AV| 香蕉久久av一区二区三区| 亚洲国产精品无码久久一区二区| 无码人妻久久一区二区三区蜜桃 |