• <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 - 183,  comments - 10,  trackbacks - 0

            表達(dá)式的運(yùn)算

            總共分兩個(gè)步驟
            ·中綴表達(dá)式轉(zhuǎn)換成后綴表達(dá)式
            ·后綴表達(dá)式的計(jì)算

            中綴表達(dá)式轉(zhuǎn)換成后綴表達(dá)式:
            掃描一遍中綴表達(dá)式
            操作符棧
            左括號(hào)和右括號(hào)的特殊性
            運(yùn)算符的優(yōu)先級(jí)

            后綴表達(dá)式的計(jì)算:
            掃描一遍后綴表達(dá)式
            操作數(shù)棧
            當(dāng)前操作符,操作數(shù)棧出棧計(jì)算,注意雙目運(yùn)算符的左右順序

            表達(dá)式運(yùn)算的整個(gè)過(guò)程,利用了兩個(gè)棧
            ·操作符棧
            ·操作數(shù)棧
            分別應(yīng)用于第一和第二步驟中。

            在做中綴表達(dá)式轉(zhuǎn)換成后綴表達(dá)式的過(guò)程中需要注意左括號(hào)和右括號(hào)的入棧出棧,其他操作符相對(duì)左括號(hào)和右括號(hào)的入棧和出棧。注意左括號(hào)的棧內(nèi)優(yōu)先級(jí)與棧外優(yōu)先級(jí)的區(qū)別。


            實(shí)現(xiàn):
              1 //
              2 // 表達(dá)式運(yùn)算
              3 // Mark
              4 // email: goonyangxiaofang AT 163.com
              5 // QQ   : 591247876
              6 // 2011.6.29
              7 // ( ( 1 + 2 ) * 3 + ( 1 + 10 ) ) / 2
              8 // ???
              9 //
             10 
             11 #include <iostream>
             12 #include <string>
             13 #include <vector>
             14 #include <stack>
             15 #include <map>
             16 #include <cstdlib>
             17 using namespace std;
             18 
             19 map<stringint> operatorPriors;
             20 
             21 void getInfix(vector<string>& infix)
             22 {
             23     infix.clear();
             24     string tmp;
             25     while (cin >> tmp)
             26     {
             27         infix.push_back(tmp);
             28     }
             29 }
             30 
             31 void init()
             32 {
             33     operatorPriors["+"= 10;
             34     operatorPriors["-"= 10;
             35     operatorPriors["*"= 20;
             36     operatorPriors["/"= 20;
             37     operatorPriors["%"= 20;
             38     operatorPriors["("= 30;
             39     operatorPriors[")"= 0;
             40 }
             41 
             42 bool prior(const string& op1, const string& op2)
             43 {
             44     return operatorPriors[op1] > operatorPriors[op2];
             45 }
             46 
             47 bool isOperator(const string& s)
             48 {
             49     return operatorPriors.find(s) != operatorPriors.end();
             50 }
             51 
             52 void print(stack<string> operators)
             53 {
             54     while (!operators.empty())
             55     {
             56         cout << operators.top() << ' ';
             57         operators.pop();
             58     }
             59     cout << endl;
             60 }
             61 
             62 const vector<string>& infixToPostfix(vector<string>& postfix, const vector<string>& infix)
             63 {
             64     postfix.clear();
             65     stack<string> operators;
             66     for (vector<string>::size_type i = 0; i != infix.size(); ++i)
             67     {
             68         if (isOperator(infix[i]))
             69         {
             70             if (operators.empty())
             71             {
             72                 operators.push(infix[i]);
             73             }
             74             else if (operators.top() == "(" && infix[i] != ")" || prior(infix[i], operators.top()))
             75             {
             76                 operators.push(infix[i]);
             77             }
             78             else
             79             {
             80                 if (infix[i] == ")")
             81                 {
             82                     while (operators.top() != "(")
             83                     {
             84                         postfix.push_back(operators.top());
             85                         operators.pop();
             86                     }
             87                     operators.pop();
             88                 }
             89                 else
             90                 {
             91                     postfix.push_back(operators.top());
             92                     operators.pop();
             93                     while (!operators.empty() && !prior(infix[i], operators.top()) && operators.top() != "(")
             94                     {
             95                         postfix.push_back(operators.top());
             96                         operators.pop();
             97                     }
             98                     
             99                     operators.push(infix[i]);
            100                 }
            101             }
            102         }
            103         else
            104         {
            105             postfix.push_back(infix[i]);
            106         }
            107     }
            108     while (!operators.empty())
            109     {
            110         postfix.push_back(operators.top());
            111         operators.pop();
            112     }
            113     return postfix;
            114 }
            115 
            116 double stringToDouble(const string& s)
            117 {
            118     return (atof(s.c_str()));
            119 }
            120 
            121 double evalPost(const vector<string>& post)
            122 {
            123     stack<double> operands;
            124     int a, b;
            125     for (vector<string>::size_type i = 0; i != post.size(); ++i)
            126     {
            127         if (post[i] == "+")
            128         {
            129             b = operands.top();
            130             operands.pop();
            131             a = operands.top();
            132             operands.pop();
            133             operands.push(a + b);
            134         }
            135         else if (post[i] == "-")
            136         {
            137             b = operands.top();
            138             operands.pop();
            139             a = operands.top();
            140             operands.pop();
            141             operands.push(a - b);
            142         }
            143         else if (post[i] == "*")
            144         {
            145             b = operands.top();
            146             operands.pop();
            147             a = operands.top();
            148             operands.pop();
            149             operands.push(a * b);
            150         }
            151         else if (post[i] == "/")
            152         {
            153             b = operands.top();
            154             operands.pop();
            155             a = operands.top();
            156             operands.pop();
            157             operands.push(a / b);
            158         }
            159         else if (post[i] == "%")
            160         {
            161             b = operands.top();
            162             operands.pop();
            163             a =operands.top();
            164             operands.pop();
            165             operands.push(a - b);
            166         }
            167         else
            168         {
            169             // stringstream ss;
            170             // ss << post[i];
            171             // ss >> a;
            172             operands.push(stringToDouble(post[i]));
            173         }
            174     }
            175     return operands.top();
            176 }
            177 
            178 int main()
            179 {
            180     init();
            181     vector<string> infix;
            182     vector<string> postfix;
            183     getInfix(infix);
            184     infixToPostfix(postfix, infix);
            185     for (vector<string>::size_type i = 0; i != postfix.size(); ++i)
            186     {
            187         cout << postfix[i] << ' ';
            188     }
            189     cout << endl;
            190     cout << evalPost(postfix) << endl;
            191     return 0;
            192 }


            posted on 2011-06-29 01:58 unixfy 閱讀(159) 評(píng)論(0)  編輯 收藏 引用

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            国产美女亚洲精品久久久综合| 97久久久精品综合88久久| 久久久国产精华液| 综合久久精品色| 久久精品国产清高在天天线| 亚洲精品国产成人99久久| 色婷婷噜噜久久国产精品12p| 国产69精品久久久久久人妻精品| 久久综合狠狠色综合伊人| 欧美午夜精品久久久久久浪潮| 亚洲精品国精品久久99热一| 88久久精品无码一区二区毛片 | MM131亚洲国产美女久久| 国产成人99久久亚洲综合精品| 亚洲日本va午夜中文字幕久久| 久久久久中文字幕| 久久久久久久久久久| 国産精品久久久久久久| 欧美精品久久久久久久自慰| 久久综合一区二区无码| 青青青国产成人久久111网站| 国产成人精品三上悠亚久久| 久久久精品波多野结衣| 嫩草影院久久国产精品| 人妻少妇久久中文字幕| 中文字幕无码久久人妻| 狠狠精品久久久无码中文字幕| 日本欧美久久久久免费播放网| 久久久久亚洲AV无码观看| 久久精品无码专区免费| 国产精品欧美久久久久天天影视| 99久久超碰中文字幕伊人| 久久天堂AV综合合色蜜桃网 | 无码乱码观看精品久久| 国产亚洲成人久久| 久久国产热这里只有精品| 日韩亚洲欧美久久久www综合网| 国产精品免费看久久久| 东京热TOKYO综合久久精品| 97久久国产亚洲精品超碰热| 久久精品无码一区二区无码|