栈应用之将中缀数值表达式转换成后缀表达式
2014-03-07 20:36
459 查看
计算后缀表达式只用到一个栈,因此很多情况下需要将中缀表达式转换成后缀表达式,以方便进行计算。
使用条件:只能出现二元操作符加减乘除,不能出现负号等一元操作符
1. 如果中缀表达式以全括号形式表示,则可以使用以下代码实现转换:
代码实现如下:
使用条件:只能出现二元操作符加减乘除,不能出现负号等一元操作符
1. 如果中缀表达式以全括号形式表示,则可以使用以下代码实现转换:
#include <iostream> #include <string> #include <cctype> #include <cassert> #include <stack> using namespace std; bool is_operation(const char& ch) { if(ch == '+' || ch == '-' || ch == '*' || ch== '/') return true; return false; } //将全括号形式的中缀表达式转化成后缀表达式 void infix_to_postfix(istream &ins) { char ch; double num; stack<char> chstack; while(ins && ins.peek() != '\n') { if(ins.peek() == '(') { ins >> ch; chstack.push(ch); } else if(isdigit(ins.peek()) || ins.peek() == '.') { ins >> num; cout << num << " "; } else if(is_operation(ins.peek())) { ins >> ch; chstack.push(ch); } else if(ins.peek() == ')') { ins.ignore(); assert(!chstack.empty()); //表达式右边有多余')',此时栈已为空 assert(is_operation(chstack.top())); //缺少操作符 cout << chstack.top() << " "; chstack.pop(); assert(chstack.top() == '('); // 括号不匹配 chstack.pop(); } else ins.ignore(); } assert(chstack.empty()); // 表达式左边有多余'(' } int main() { infix_to_postfix(cin); cout << endl; return 0; }2. 一般情况下,表达式多数不表示成全括号形式,此时需要使用计算优先级规则进行设计
代码实现如下:
#include <iostream> #include <string> #include <cctype> #include <cassert> #include <stack> using namespace std; bool is_operation(const char& ch) { if(ch == '+' || ch == '-' || ch == '*' || ch== '/') return true; return false; } //比较操作符的优先级 bool precede(const char &ch1, const char& ch2) { if(ch2 =='+' || ch2 == '-') return true; else if(ch1 == '*' || ch1 == '/') return true; return false; } //将中缀表达式转换成后缀表达式 void infix_to_postfix(istream &ins) { char ch; double num; stack<char> chstack; while(ins && ins.peek() != '\n') { if(ins.peek() == '(') { ins >> ch; chstack.push(ch); } else if(isdigit(ins.peek()) || ins.peek() == '.') { ins >> num; cout << num << " "; } //遇到操作符时,循环输出栈顶元素并出栈,直到(1)栈为空(2)遇到'(' (3) 栈中的操作符优先级小于输入的操作符 else if(is_operation(ins.peek())) { while( !chstack.empty() && chstack.top() !='(' && precede(chstack.top(), ins.peek()) ) { cout << chstack.top() << " "; chstack.pop(); } ins >> ch; chstack.push(ch); } //遇到')'时,循环输出栈中元素直到遇到'(' else if(ins.peek() == ')') { ins.ignore(); assert(!chstack.empty()); //表达式右边有多余')',此时栈已为空 while(chstack.top()!= '(' && !chstack.empty()) { cout << chstack.top() << " "; chstack.pop(); } assert(chstack.top() == '('); // 括号不匹配 chstack.pop(); } else ins.ignore(); } while(!chstack.empty()) { assert(chstack.top()!= '('); // 表达式左边有多余'(' cout << chstack.top() << " "; chstack.pop(); } } int main() { infix_to_postfix(cin); cout << endl; return 0; }
相关文章推荐
- 栈的应用之中缀表达式转换为后缀表达式
- (转)前缀、中缀、后缀表达式及转换与应用
- 堆栈的应用(2) 中缀算术表达式到后缀(逆波兰记法reverse polish notation)的转换及其计算 C++实现
- 栈的应用:中缀和后缀表达式的转换及计算
- 堆栈的应用(2) 中缀算术表达式到后缀(逆波兰记法reverse polish notation)的转换及其计算 C++实现
- 栈的应用 平衡符号 后缀表达式 中缀到后缀的转换
- 前缀、中缀、后缀表达式转换
- 中缀后缀表达式的转换
- 八、通过中缀计算表达式转换成后缀计算表达式
- 栈的应用--算术表达式的求值(中缀转后缀然后计算后缀表达式的值)
- 栈的应用实例——中缀表达式转换为后缀表达式
- 数据结构之 栈和队列---算术表达式的转换(前缀+中缀+后缀)
- 中缀表达式到后缀表达式的转换
- 6.从中缀向后缀转换表达式
- 数据结构——从中缀向后缀转换表达式
- 栈的应用--表达式计算&中缀转后缀
- IT笔试题中经常出现的前缀、中缀、后缀表达式转换问题-----阿冬专栏
- 二叉树中缀表达式到后缀表达式的转换
- 算法学习——栈的应用之逆波兰(后缀)表达式转换
- 中缀表达式转换为后缀表达式的算法