中缀表达式转后缀表达式代码,编程老师说写的繁琐了,求助
  • 板块题目总版
  • 楼主zyx1130
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/7/18 20:44
  • 上次更新2023/10/27 19:39:14
查看原帖
中缀表达式转后缀表达式代码,编程老师说写的繁琐了,求助
503583
zyx1130楼主2022/7/18 20:44
#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
using namespace std;
string st;
int n,i,top;
int main()
{
	cin>>st;
	for(i=0;i<st.length();i++){
		if(st[i]>='a'&&st[i]<='z'){
			cout<<st[i];
		}
		if(st[i]=='+'||st[i]=='-'){
			if(top==0){
				st[top]=st[i];
				top++;
			}
			else if(st[top-1]=='*'||st[top-1]=='/'){
				while(st[top-1]!='+'||st[top-1]!='-'||st[top-1]!='('){
					cout<<st[top-1];
					top--;
				}
			}
			else{
				st[top]=st[i];
				top++;
			}
		}
		if(st[i]=='*'||st[i]=='/'){
			if(top==0){
				st[top]=st[i];
				top++;
			}
			else{
				st[top]=st[i];
				top++;
			}
		}
		if(st[i]=='('){
			st[top]=st[i];
			top++;
		}
		if(st[i]==')'){
			while(st[top-1]!='('){
				cout<<st[top-1];
				top--;
			}
			top--;
		}
	}
	for(i=top-1;i>=0;i--){
		cout<<st[i];
	}
	return 0;
}
2022/7/18 20:44
加载中...