• <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 閱讀(416) 評(píng)論(0)  編輯 收藏 引用

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


            精品永久久福利一区二区| 青青青青久久精品国产h| 久久久久久A亚洲欧洲AV冫| 亚洲精品tv久久久久久久久久| 天天做夜夜做久久做狠狠| 亚洲国产成人久久精品99| 久久久无码精品亚洲日韩按摩| 91精品国产色综合久久| 香蕉久久夜色精品国产2020 | 久久w5ww成w人免费| 国产精品伦理久久久久久| 免费一级欧美大片久久网| 国产成人久久精品激情| 久久嫩草影院免费看夜色| 狠狠色婷婷久久一区二区三区| 久久精品国产99久久久香蕉| 国产V综合V亚洲欧美久久| 欧美黑人激情性久久| 亚洲午夜精品久久久久久人妖| 久久人人爽人人爽人人片AV高清 | 成人妇女免费播放久久久 | 久久精品中文字幕无码绿巨人| 99久久夜色精品国产网站| 精品久久久噜噜噜久久久| 97视频久久久| 亚洲国产成人久久综合碰| 国产精品99久久久久久www| 国产精品久久成人影院| …久久精品99久久香蕉国产 | 日日狠狠久久偷偷色综合0| 99久久精品免费| 久久综合久久久| 久久er热视频在这里精品| 久久se精品一区精品二区| 久久精品人人做人人爽电影蜜月| 一本色综合网久久| 亚洲AV无码一区东京热久久| 久久九九兔免费精品6| 一本色道久久综合亚洲精品| 久久久无码精品亚洲日韩按摩| 91视频国产91久久久|