PIPIOJ-1292-中缀表达式转后缀表达式II

题目描述
PIPI现在有若干个包含小写英文字母作为操作数 以及 ‘+’, ‘-’, '’, ‘/’, ‘^’ ,’(’,’)‘七种操作符的合法中缀表达式。请你将其转为后缀表达式(逆波兰式)。
PS:’'代表幂运算,23=8
输入
输入一个字符串s,表示中缀表达式。
输出
输出一个字符串表示对应的后缀表达式。
样例输入
a+b
c+(de+f)g
样例输出
abc
+de
f+g*+

// 题目:中缀表达式转后缀表达式 II
// 日期:2021/7/14 
// 详情:有若干个包含小写英文字母作为操作数以及'+','-','*','/','^','(',')'五种操作符的合法中缀表达式。
//		 请你将其转为后缀表达式(逆波兰式)。
// XWQ
#include <bits/stdc++.h>
using namespace std;
void Check(string str){
	stack<char> op;
	int priority[300];
	priority['('] = 0; 	// 1.定义优先级
	priority['+'] = priority['-'] = 1; 
	priority['*'] = priority['/'] = 2;
	priority['^'] = 3;
	for(int i=0; i<str.size(); i++){
		if(str[i]>='a' && str[i]<='z'){
			printf("%c", str[i]);
		}
		else{
			if(op.empty()){
				op.push(str[i]);
			}
			else if(str[i]=='('){
				op.push(str[i]);
			}
			else if(str[i]==')'){	// 2*.比1291多了)步骤 
				while(!op.empty() && op.top()!='('){
					printf("%c", op.top());
					op.pop();
				}
				op.pop();	// 弹出左括号 
			}
			else{
				while(!op.empty() && priority[str[i]]<=priority[op.top()]){	// 2.使用优先级比较 
					printf("%c", op.top());
					op.pop();
				}
				op.push(str[i]);
			}
		}
	}	
	while(!op.empty()){
		printf("%c", op.top());
		op.pop();
	}
}
int main(){
	string str;
	cin >> str;
	Check(str);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值