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

            利用棧存儲中綴表達式的各個元素

            例如:
            1 2 + 3 *
            3 3 *
            9

             1 // 后綴表達式求解結(jié)果
             2 
             3 #include <iostream>
             4 #include <stack>
             5 #include <string>
             6 #include <sstream>
             7 #include <algorithm>
             8 using namespace std;
             9 
            10 void printPost(const stack<string>& post)
            11 {
            12     stack<string> temp(post);
            13     while (!temp.empty())
            14     {
            15         cout << temp.top() << ' ';
            16         temp.pop();
            17     }
            18     cout << endl;
            19 }
            20 
            21 void clearPost(stack<string>& post)
            22 {
            23     while (!post.empty())
            24     {
            25         post.pop();
            26     }
            27 }
            28 
            29 void getPost(stack<string>& post)
            30 {
            31     clearPost(post);
            32     string t;
            33     stack<string> temp;
            34     while (cin >> t)
            35     {
            36         temp.push(t);
            37     }
            38     while (!temp.empty())
            39     {
            40         post.push(temp.top());
            41         temp.pop();
            42     }
            43 }
            44 
            45 double computePost(const stack<string>& rhs)
            46 {
            47     stack<string> post(rhs);
            48     double d1, d2;
            49     double dd;
            50     string optor;
            51     string temp;
            52     while (post.size() >= 3)
            53     {
            54         d1 = atof(post.top().c_str());
            55         post.pop();
            56         d2 = atof(post.top().c_str());
            57         post.pop();
            58         optor = post.top();
            59         post.pop();
            60         switch (optor[0])
            61         {
            62         case '+':
            63             dd = d1 + d2;
            64             break;
            65         case '-':
            66             dd = d1 - d2;
            67             break;
            68         case '*':
            69             dd = d1 * d2;
            70             break;
            71         case '/':
            72             dd = d1 / d2;
            73             break;
            74         default:
            75             break;
            76         }
            77         if (post.empty())
            78         {
            79             break;
            80         }
            81         stringstream ss;
            82         ss << dd;
            83         ss >> temp;
            84         post.push(temp);
            85         printPost(post);
            86     }
            87     return dd;
            88 }
            89 
            90 int main()
            91 {
            92     stack<string> post;
            93     cout << "Input:" << endl;
            94     getPost(post);
            95     printPost(post);
            96     cout << computePost(post) << endl;
            97     return 0;
            98 }


            posted on 2011-06-25 19:10 unixfy 閱讀(414) 評論(0)  編輯 收藏 引用
            久久久久国产一区二区三区| 久久这里只有精品18| 欧美综合天天夜夜久久| 国产精品日韩欧美久久综合| 色狠狠久久综合网| 久久久久亚洲精品天堂| 久久高清一级毛片| 久久久久av无码免费网| 婷婷久久综合九色综合98| 久久播电影网| 久久人人爽人人爽人人片AV不 | 欧美777精品久久久久网| 亚洲国产成人乱码精品女人久久久不卡 | 一本久久知道综合久久| 久久99国产精品久久久| 久久久精品久久久久影院| 97精品伊人久久大香线蕉app| 国产精品99久久久久久www| 漂亮人妻被中出中文字幕久久 | 欧美粉嫩小泬久久久久久久| 久久久老熟女一区二区三区| 亚洲精品成人网久久久久久| 999久久久国产精品| 国产∨亚洲V天堂无码久久久| 亚洲日本va午夜中文字幕久久| 久久亚洲国产午夜精品理论片| 中文字幕久久波多野结衣av| 久久93精品国产91久久综合| 久久99精品久久久久婷婷| 乱亲女H秽乱长久久久| 老男人久久青草av高清| 一本一道久久a久久精品综合| 久久成人精品| 久久久久香蕉视频| 久久久久亚洲AV综合波多野结衣| 91久久福利国产成人精品| 久久婷婷国产麻豆91天堂| 青青草国产精品久久| 超级碰久久免费公开视频| 久久国产免费观看精品| 久久91精品久久91综合|