栈应用之将中缀数值表达式转换成后缀表达式

计算后缀表达式只用到一个栈,因此很多情况下需要将中缀表达式转换成后缀表达式,以方便进行计算。

使用条件:只能出现二元操作符加减乘除,不能出现负号等一元操作符

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;
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值