栈应用——中缀转后缀+后缀计算
2017-08-15 12:18
274 查看
中缀表达式(infix expression)即 平时生活中大家对于算式的书写格式( eg: 6*((5+(2+3)*8)+3)
);
后缀表达式(post expression)即 把数字和运算符分开,把运算符的优先级运算内涵到后缀式的数字和运算符的顺序中(故其优点就是,没有必要知道任何优先的规则),一个运算符只对其前边的两个数字起作用(
eg: 652 3+8*+3+*);
中缀转后缀:
·读入表达式,若是数字放入输出字符串;
·若是运算符,则先与栈顶元素比较:
-栈空,直接压入栈;
-否则比较优先级:(优先级顺序:当前(>
*、/> +、-> 栈中 (> ) )
~ 当前=栈顶:出栈后压入当前;
~ 当前<栈顶:出栈 至 当前=栈顶 进行处理 / 出栈,当前不进栈直接放入输出字符串;
~ 当前>栈顶:压入栈;
~ 若是 ) :出栈 至 ( 为止,但是(和)不放入输出字符串。
时间复杂度: O(N)
代码如下:(智障的我只想到了各种if,然后括号只有 () ,运算符只有 +-*/ )
后缀计算:
·见到数字时,压入栈;
·见到运算符时,从栈中弹出两个数 second 运算符 first,后再压入栈;
时间复杂度: O(N)
代码如下:(开始总不对,才意识到,char的数字转换成int,是需要 - ‘0’,得到的时和0,相差的ascii码数值)
这两个是套装,故奉上main()函数,可以一试~:
);
后缀表达式(post expression)即 把数字和运算符分开,把运算符的优先级运算内涵到后缀式的数字和运算符的顺序中(故其优点就是,没有必要知道任何优先的规则),一个运算符只对其前边的两个数字起作用(
eg: 652 3+8*+3+*);
中缀转后缀:
·读入表达式,若是数字放入输出字符串;
·若是运算符,则先与栈顶元素比较:
-栈空,直接压入栈;
-否则比较优先级:(优先级顺序:当前(>
*、/> +、-> 栈中 (> ) )
~ 当前=栈顶:出栈后压入当前;
~ 当前<栈顶:出栈 至 当前=栈顶 进行处理 / 出栈,当前不进栈直接放入输出字符串;
~ 当前>栈顶:压入栈;
~ 若是 ) :出栈 至 ( 为止,但是(和)不放入输出字符串。
时间复杂度: O(N)
代码如下:(智障的我只想到了各种if,然后括号只有 () ,运算符只有 +-*/ )
string infixTOpostfix(const string s) { string post_s; stack mark; for (size_t i = 0; i < s.length(); ++i) { if (isalnum(s[i])) post_s.push_back(s[i]); else { if (mark.empty()) { mark.push(s[i]); } else switch (s[i]) { case '+': case '-': if (mark.top() == '*' || mark.top() == '/') { while (mark.top() == '*' || mark.top() == '/') { post_s.push_back(mark.top()); mark.pop(); } if (mark.top() == '+' || mark.top() == '-') { post_s.push_back(mark.top()); mark.pop(); mark.push(s[i]); } else mark.push(s[i]); } else if (mark.top() == '+' || mark.top() == '-') { post_s.push_back(mark.top()); mark.pop(); mark.push(s[i]); } else mark.push(s[i]); break; case '*': case '/': if (mark.top() == '*' || mark.top() == '/') { post_s.push_back(mark.top()); mark.pop(); } mark.push(s[i]); break; case '(': mark.push(s[i]); break; case ')': while (mark.top() != '(' && !mark.empty()) { post_s.push_back(mark.top()); mark.pop(); } mark.pop(); break; default: break; } } } while (!mark.empty()) { post_s.push_back(mark.top()); mark.pop(); } return post_s; }
后缀计算:
·见到数字时,压入栈;
·见到运算符时,从栈中弹出两个数 second 运算符 first,后再压入栈;
时间复杂度: O(N)
代码如下:(开始总不对,才意识到,char的数字转换成int,是需要 - ‘0’,得到的时和0,相差的ascii码数值)
int postfixCompute(const string s) { int temp1, temp2; stack result; for (size_t i = 0; i < s.length(); ++i) { if (isdigit(s[i]))//与0的ascll码运算 result.push(s[i] - '0'); else { temp1 = result.top(); result.pop(); temp2 = result.top(); result.pop(); switch (s[i]) { cas 4000 e '+': result.push(temp1 + temp2); break; case '-': result.push(temp1 - temp2); break; case '*': result.push(temp1 * temp2); break; case '/': result.push(temp1 / temp2); break; default: break; } } } return result.top(); }
这两个是套装,故奉上main()函数,可以一试~:
//6*((5+(2+3)*8)+3) int main() { string s, result; char c; while (cin >> c) s.push_back(c); result = infixTOpostfix(s); cout << result << " = " << postfixCompute(result); getchar(); }
相关文章推荐
- 栈的应用--算术表达式的求值(中缀转后缀然后计算后缀表达式的值)
- 栈的应用:中缀和后缀表达式的转换及计算
- 栈的应用--表达式计算&中缀转后缀
- 数据结构-栈应用(中缀转后缀并计算结果)
- java 栈的实现以及栈的典型应用--字符平衡,中缀转后缀,后缀计算,迷宫求解等
- 堆栈的应用(2) 中缀算术表达式到后缀(逆波兰记法reverse polish notation)的转换及其计算 C++实现
- 栈的应用(括号匹配、后缀表式计算、中缀转后缀)
- 栈应用之中缀转后缀表达式计算(C++、JAVA)
- 栈的应用2:中缀转后缀及计算
- 堆栈的应用(2) 中缀算术表达式到后缀(逆波兰记法reverse polish notation)的转换及其计算 C++实现
- 中缀算术转后缀算术表达式并计算的函数
- 栈的应用:通过用户输入后缀表达式,利用数据结构栈计算其结果值。
- 栈应用(中缀表达式转后缀表达式并计算后缀表达式的值)
- 【转】算术表达式中缀形式转后缀形式,并基于后缀式和栈进行计算
- 栈应用之将中缀数值表达式转换成后缀表达式
- 栈应用 - 后缀表达式的计算
- 栈应用(中缀表达式转后缀表达式并计算后缀表达式的值)
- java实现中缀转后缀,后缀计算值
- 数据结构 中缀转后缀 后缀转二叉树并打印 后缀计算值
- 中缀表达式转换为后缀表达式,计算后缀表达式