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

            利用棧存儲(chǔ)中綴表達(dá)式的各個(gè)元素

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

             1 // 后綴表達(dá)式求解結(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 閱讀(421) 評(píng)論(0)  編輯 收藏 引用

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


            婷婷综合久久中文字幕蜜桃三电影| 国产V亚洲V天堂无码久久久| 久久久国产视频| 人妻无码αv中文字幕久久| 国产精品久久久久久久久鸭| 久久久久免费视频| 国产V综合V亚洲欧美久久| 久久久久久久久久久久久久| 久久精品国产精品亚洲精品| 欧美精品国产综合久久| 国产午夜精品久久久久九九| 久久精品www人人爽人人| 亚洲国产成人精品91久久久| 久久精品草草草| 777午夜精品久久av蜜臀| 热久久国产欧美一区二区精品 | 久久久久免费精品国产| 久久99国产亚洲高清观看首页 | 亚洲综合伊人久久综合| 久久综合久久伊人| 久久久久99精品成人片牛牛影视| 99久久777色| 国内精品久久久久久野外| 国内精品人妻无码久久久影院导航 | 久久综合日本熟妇| 伊人久久综合热线大杳蕉下载| 久久久久久亚洲精品成人| 99久久精品国产一区二区 | 狠狠色婷婷久久综合频道日韩 | 97精品伊人久久大香线蕉app| 久久天天躁夜夜躁狠狠躁2022 | 精品久久久无码21p发布| 青青草原综合久久大伊人| 九九精品久久久久久噜噜| 久久99热这里只频精品6| 97视频久久久| 久久久无码精品亚洲日韩蜜臀浪潮| 亚洲午夜久久久久妓女影院| 亚洲国产精品无码久久久蜜芽 | 久久综合亚洲欧美成人| 国产91久久精品一区二区|