青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 183,  comments - 10,  trackbacks - 0

表達式的運算

總共分兩個步驟
·中綴表達式轉換成后綴表達式
·后綴表達式的計算

中綴表達式轉換成后綴表達式:
掃描一遍中綴表達式
操作符棧
左括號和右括號的特殊性
運算符的優先級

后綴表達式的計算:
掃描一遍后綴表達式
操作數棧
當前操作符,操作數棧出棧計算,注意雙目運算符的左右順序

表達式運算的整個過程,利用了兩個棧
·操作符棧
·操作數棧
分別應用于第一和第二步驟中。

在做中綴表達式轉換成后綴表達式的過程中需要注意左括號和右括號的入棧出棧,其他操作符相對左括號和右括號的入棧和出棧。注意左括號的棧內優先級與棧外優先級的區別。


實現:
  1 //
  2 // 表達式運算
  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 閱讀(166) 評論(0)  編輯 收藏 引用
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            一区二区久久久久| 日韩网站在线看片你懂的| 性欧美8khd高清极品| 日韩视频一区二区三区在线播放| 欧美成人四级电影| 国产精品99久久久久久有的能看| 夜夜嗨网站十八久久| 国产精品视频网址| 乱人伦精品视频在线观看| 欧美肥婆在线| 午夜欧美电影在线观看| 久久精品视频在线观看| 亚洲精品欧美一区二区三区| 夜色激情一区二区| 国产精品每日更新| 欧美成ee人免费视频| 欧美日韩视频在线一区二区| 久久精品一二三区| 欧美激情一区在线观看| 欧美一区二区三区在线视频 | 欧美激情按摩| 国产精品成人免费| 蜜桃av综合| 欧美午夜片在线免费观看| 久久久久天天天天| 欧美日韩国产精品一卡| 久久久久国产成人精品亚洲午夜| 免费久久精品视频| 欧美在线观看网站| 欧美精品久久天天躁| 久久―日本道色综合久久| 欧美日本网站| 欧美国产欧美亚洲国产日韩mv天天看完整 | 亚洲综合二区| 欧美.com| 久久久久久久久一区二区| 欧美久久久久久久久| 免费成人av在线| 国产美女精品免费电影| 亚洲精品一二| 亚洲国产高清aⅴ视频| 亚洲女人天堂成人av在线| 99精品视频免费全部在线| 久久精品视频网| 久久精品夜色噜噜亚洲a∨ | 欧美精品v日韩精品v韩国精品v | 一区二区三区国产在线观看| 久久婷婷亚洲| 久久一区二区三区四区| 国产精品亚洲美女av网站| 一本色道久久加勒比精品| 亚洲日本理论电影| 免费高清在线视频一区·| 蜜臀91精品一区二区三区| 国产在线观看一区| 亚洲欧美国产毛片在线| 午夜精品www| 国产精品影音先锋| 亚洲一区二区成人| 亚洲欧美日韩在线| 国产精品久久久久久久电影| 亚洲久久在线| 亚洲一区三区在线观看| 欧美视频在线观看一区| 亚洲视频一区二区免费在线观看| 在线视频亚洲一区| 欧美亚洲成人精品| 亚洲性图久久| 久久精品中文| 影音先锋中文字幕一区| 噜噜噜在线观看免费视频日韩| 欧美v日韩v国产v| 亚洲欧洲日本专区| 欧美日韩国产va另类| 一个色综合av| 久久久99爱| 亚洲电影在线看| 欧美久久久久久久| 亚洲午夜精品在线| 久久久久五月天| 亚洲福利在线观看| 欧美日韩国产欧| 亚洲综合日韩中文字幕v在线| 久久精品99| 亚洲电影自拍| 国产精品久久久一区二区三区 | 亚洲欧洲日本一区二区三区| 一区二区三区精品在线| 国产精品视频免费| 美日韩精品免费| 夜夜嗨一区二区三区| 久久超碰97人人做人人爱| 狠狠色综合网| 欧美国产日韩一区| 亚洲欧美在线网| 亚洲国产另类 国产精品国产免费| 亚洲一二三区在线观看| 国内激情久久| 国产精品s色| 蜜桃av一区二区| 亚洲一区二区三区免费在线观看| 老司机精品久久| 亚洲一区在线视频| 亚洲二区三区四区| 国产精品亚洲第一区在线暖暖韩国| 久久久综合免费视频| 一本色道久久综合亚洲精品高清| 久久久999国产| 亚洲免费网址| 亚洲最新色图| 亚洲国产1区| 国产色视频一区| 国产精品草莓在线免费观看| 老牛影视一区二区三区| 亚洲欧美综合网| 在线视频你懂得一区| 亚洲电影下载| 美女精品在线| 久久不射中文字幕| 亚洲伊人伊色伊影伊综合网| 在线免费不卡视频| 国产一区二区在线免费观看| 欧美亚洲成人免费| 欧美精品一区二区在线播放| 久久综合网hezyo| 久久精品国产久精国产思思| 亚洲欧美日韩精品一区二区| 在线视频你懂得一区| 亚洲美女精品久久| 亚洲人精品午夜在线观看| 欧美不卡三区| 嫩草影视亚洲| 欧美wwwwww| 欧美国产一区二区在线观看 | 亚洲精品国产视频| 亚洲国产欧美一区二区三区久久| 老司机aⅴ在线精品导航| 久久久www成人免费无遮挡大片 | 日韩视频中午一区| 亚洲美女性视频| 日韩视频免费观看高清在线视频 | 欧美午夜精品久久久| 欧美日韩第一区| 欧美视频中文字幕| 国产精品久久久久久久久久妞妞| 欧美视频国产精品| 国产精品久久久久一区| 国产伦精品一区二区三区视频孕妇 | 国产精品久久久久毛片大屁完整版| 欧美日韩精品福利| 欧美日韩在线三区| 国产精品久久久久久久一区探花 | 国产精品一区二区男女羞羞无遮挡 | 欧美亚洲自偷自偷| 欧美一区免费视频| 久热精品视频在线观看| 欧美成人精品一区二区| 欧美久久一级| 国产精品美女999| 国产一区二区视频在线观看 | 在线免费一区三区| 亚洲精品久久久蜜桃| 亚洲特色特黄| 久久精品一区二区国产| 欧美激情免费观看| 国产精品99久久久久久久女警| 一区二区日韩欧美| 久久久国产精品一区二区中文| 老牛国产精品一区的观看方式| 欧美国产精品劲爆| 国产欧美精品一区| 亚洲黄色免费| 亚洲欧美在线aaa| 欧美二区视频| 亚洲一区久久| 欧美不卡福利| 国产日韩欧美制服另类| 亚洲人午夜精品| 久久国产一区二区| 91久久国产精品91久久性色| 亚洲一区免费视频| 欧美本精品男人aⅴ天堂| 国产精品私拍pans大尺度在线| 在线电影一区| 午夜久久资源| 最新亚洲视频| 久久久久久久精| 国产精品久久久爽爽爽麻豆色哟哟| 亚洲国产日韩精品| 欧美在线视频免费播放| 亚洲精品色婷婷福利天堂| 欧美在线视屏| 国产精品区一区二区三区| 亚洲欧洲日韩在线| 久久精品123| 亚洲一区亚洲| 欧美三日本三级三级在线播放| 亚洲第一成人在线| 久久精品在线| 午夜精品久久久久久久久久久久久 |