• <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

            后綴表達式的計算

            表達式運算過程中,需要先做中綴表達式到后綴表達式的轉換。
            這里現對后綴表達式求值進行解答。

            對后綴表達式進行掃描,遇到操作數將操作數壓棧,遇到運算符將操作數出棧,進行運算,將運算的結果壓入到操作數棧中。
            注意,對于雙目運算符,在堆操作數棧出棧的時候要注意,后彈出的操作符為左邊的操作符,不要弄反了。

            之前的做法是錯誤的,把后綴表達式存在一個棧中,只對棧頂操作,對于 a b c + * 這種情況不成立。

            實現如下:

             1 #include <iostream>
             2 #include <vector>
             3 #include <string>
             4 #include <stack>
             5 #include <sstream>
             6 #include <cstdlib>
             7 using namespace std;
             8 
             9 void getPost(vector<string>& post)
            10 {
            11     post.clear();
            12     string tmp;
            13     while (cin >> tmp)
            14     {
            15         post.push_back(tmp);
            16     }
            17 }
            18 
            19 double stringToDouble(const string& s)
            20 {
            21     return (atof(s.c_str()));
            22 }
            23 
            24 double evalPost(const vector<string>& post)
            25 {
            26     stack<double> operands;
            27     int a, b;
            28     for (vector<string>::size_type i = 0; i != post.size(); ++i)
            29     {
            30         if (post[i] == "+")
            31         {
            32             b = operands.top();
            33             operands.pop();
            34             a = operands.top();
            35             operands.pop();
            36             operands.push(a + b);
            37         }
            38         else if (post[i] == "-")
            39         {
            40             b = operands.top();
            41             operands.pop();
            42             a = operands.top();
            43             operands.pop();
            44             operands.push(a - b);
            45         }
            46         else if (post[i] == "*")
            47         {
            48             b = operands.top();
            49             operands.pop();
            50             a = operands.top();
            51             operands.pop();
            52             operands.push(a * b);
            53         }
            54         else if (post[i] == "/")
            55         {
            56             b = operands.top();
            57             operands.pop();
            58             a = operands.top();
            59             operands.pop();
            60             operands.push(a / b);
            61         }
            62         else if (post[i] == "%")
            63         {
            64             b = operands.top();
            65             operands.pop();
            66             a =operands.top();
            67             operands.pop();
            68             operands.push(a - b);
            69         }
            70         else
            71         {
            72             // stringstream ss;
            73             // ss << post[i];
            74             // ss >> a;
            75             operands.push(stringToDouble(post[i]));
            76         }
            77     }
            78     return operands.top();
            79 }
            80 
            81 int main()
            82 {
            83     vector<string> post;
            84     getPost(post);
            85     cout << evalPost(post) << endl;
            86     return 0;
            87 }


            posted on 2011-06-28 23:20 unixfy 閱讀(691) 評論(0)  編輯 收藏 引用
            国产一级做a爰片久久毛片| 久久久久99精品成人片牛牛影视| 国色天香久久久久久久小说| 久久SE精品一区二区| 久久亚洲高清观看| 亚洲国产日韩欧美久久| 久久大香香蕉国产| 久久免费视频1| 国产成人99久久亚洲综合精品| 99久久香蕉国产线看观香| 国内精品久久人妻互换| 久久毛片一区二区| 久久国产乱子伦精品免费午夜| 精品久久久久久国产| 国产成人久久777777| 久久国产高清字幕中文| 少妇久久久久久被弄高潮| 久久夜色撩人精品国产| 国产激情久久久久影院| 久久成人国产精品二三区| 97精品依人久久久大香线蕉97| 日日狠狠久久偷偷色综合免费 | 久久午夜无码鲁丝片| 久久久中文字幕日本| 亚洲伊人久久大香线蕉苏妲己| 久久天天躁狠狠躁夜夜96流白浆 | 伊人久久大香线蕉亚洲五月天| 欧美成a人片免费看久久| 国产成人无码精品久久久免费| 久久精品国产亚洲欧美| 伊人久久综在合线亚洲2019| 久久久一本精品99久久精品66| 99久久夜色精品国产网站 | 久久久国产精品网站| 久久久久久久久无码精品亚洲日韩 | 国产亚洲成人久久| 久久精品国产一区二区| 久久影院亚洲一区| 一日本道伊人久久综合影| 波多野结衣久久| 久久狠狠高潮亚洲精品|