swust oj88: 表达式的转换

原题

88: 表达式的转换
题目描述
平常我们书写的表达式称为中缀表达式,因为它将运算符放在两个操作数中间,许多情况下为了确定运算顺序,括号是不可少的,而后缀表达式就不必用括号了。后缀标记法:书写表达式时采用运算紧跟在两个操作数之后,从而实现了无括号处理和优先级处理,使计算机的处理规则简化为:从左到右顺序完成计算,并用结果取而代之。例如:8-(3+26)/5+4可以写为:8 3 2 6+5/-4+ 其计算步骤为:

(1):8 3 2 6 * + 5 / -4 +

(2):8 3 12 + 5 / - 4 +

(3):8 15 5 / - 4 +

(4):8 3- 4 +

(5):5 4 +

(6):9

编写一个程序,完成这个转换,要求输出的每一个数据间都留一个空格。

输入
就一行,是一个中缀表达式。输入的符号中只有这些基本符号“0123456789±/^()”,并且不会出现形如2-3的格式。表达式中的基本数字也都是一位的,不会出现形如12形式的数字。所输入的字符串不要判错。

输出
**若干个中缀表达式,第I+1行比第I行少一个运算符和一个操作数,最后一行只有一个数字,表示运算结果。运算的结果可能为负数,“/”以整除运算。并且中间每一步都不会超过2^31。

分析:

在最基础的中缀表达式转后缀的基础上,多了3个难点
1 实时更新 利用一个栈和一个数组分别输出
2高位输入数 利用其他标识符来区分
3注意 每行最后 不要空格 不然要pe

代码

#include<iostream> 
#include<stack>
#include<math.h>
#include<string>
#include<string.h>
using namespace std;
int pai(char a){
	switch(a){
		case '(':  return 0;
		case '+':  return 1;
		case '-':  return 1;
		case '*':  return 2;
		case '/':  return 2;
		case '^':  return 3;
		default:  return 4;
	}
}
	char zhuan[10000];string a;stack<char>z;int j=0;//zhuan	
void zhong(){

	for(int i=0;i<a.size();i++){
//shuzhi
	if(a[i]>='0'&&a[i]<='9'){

		//		//数字  用#来识别高位数    //特色点 
		zhuan[j++]=a[i];
		while(a[i+1]>='0'&&a[i+1]<='9'){
			zhuan[j++]=a[++i];
		} 
		zhuan[j++]='#';
	}//4
	
//zhifu
	else if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'||a[i]=='+'||a[i]=='^'){
		if(z.empty()){
			z.push(a[i]);

		}
		else{
			while(!z.empty()){
				
				if(pai(a[i])<=pai(z.top())){
					zhuan[j++]=z.top();
					z.pop();
				}
				else break;
			} 
			z.push(a[i]);
		}		
	}//3
	
//kuohao
	else if(a[i]=='('||a[i]==')'){
		if(a[i]=='('){
			z.push(a[i]);

		}
		else if(a[i]==')'){//?
			if(z.empty())continue;
			while(z.top()!='('){
				zhuan[j++]=z.top();z.pop();
			}
			z.pop();
		}

	}//2

}
	while(!z.empty()){
		zhuan[j++]=z.top();z.pop();
	}//1
}
void shuchu(){
		for(int i=0;i<strlen(zhuan);i++){
		if(zhuan[i]=='#')continue;
		else if(zhuan[i]>='0'&&zhuan[i]<='9'){
		while(zhuan[i]>='0'&&zhuan[i]<='9'){
		cout<<zhuan[i++];	
		}		cout<<" ";		
		}
		if(zhuan[i]!='#'){
		cout<<zhuan[i];if(i!=strlen(zhuan)-1)cout<<" ";				
		}
	}cout<<"\n";
}
	
	stack<int>sta;//存符号 
void jishuan(){

	shuchu();
	for(int i=0;i<strlen(zhuan);i++){
		if(zhuan[i]=='#'){
			continue;
		}
		//数字  用#来识别高位数    //特色点 
		if(zhuan[i]>='0'&&zhuan[i]<='9'){
			int tmp=0;
			tmp=zhuan[i]-'0'+tmp*10;zhuan[i]='#';
			while(zhuan[i+1]>='0'&&zhuan[i+1]<='9'){
			tmp=zhuan[++i]-'0'+tmp*10;	zhuan[i]='#';			
			}
			sta.push(tmp);
		}
		
		else {int n;
			int num2=sta.top();sta.pop();
			int num1=sta.top();sta.pop();
			if(zhuan[i]=='+'){
				n=num1+num2;sta.push(n);
			}
			else if(zhuan[i]=='-'){
				n=num1-num2;sta.push(n);			
			}
			else if(zhuan[i]=='*'){
				n=num1*num2;sta.push(n);				
			}
			else if(zhuan[i]=='/'){
				n=num1/num2;sta.push(n);				
			}
			else if(zhuan[i]=='^'){
				n=pow(num1,num2);sta.push(n);				
			}
			zhuan[i]='#';
		//输出  栈 和 zhuan 相结合 并且每操作一次更新一次 // 特殊点  
			stack<int>st;
			while(!sta.empty()){
			st.push(sta.top());sta.pop();
			}
			while(!st.empty()){
			cout<<st.top();
			if(i!=strlen(zhuan)-1)cout<<" ";
			sta.push(st.top());st.pop();	
			}
		shuchu();
		//输出 		
		}
	}	
	
}
using namespace std;
int main(){
	 cin>>a;
	zhong();
	jishuan();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值