中缀转后缀输出-普通版

1 如果有结束符的时候,用char

//判断的时候有结束符 
//就单个字符 char判断 

#include<bits/stdc++.h>
#include<ctype.h>
using namespace std;
#define Maxsize 100
typedef struct Stack{
	int data[Maxsize];//0~Maxsize-1 
	int top;
}Stack,*pStack; 

pStack CreateStack(){
	pStack head=new Stack();
	head->top=-1;   
	return head;
}

bool isFull(pStack L){
	if(L->top ==Maxsize-1) return true;
	return false;
}

bool isEmpty(pStack L){
	if(L->top ==-1) return true;
	return false;
}

bool Push(pStack L,char e){
	if(isFull(L)) return false;
	L->data [++(L->top)]=e;
	return true;
}

char Pop(pStack L){
	if(isEmpty(L)) return false;
	return L->data [(L->top )--];
}

int main(){
	pStack head;
	head=CreateStack();

	char str[50];char c,e;
	cin>>c;
	bool isfirst=true;
	bool ismatch=true;
	while(c!='#'){
		while(isdigit(c)||c=='.'){
			if(isfirst) cout<<c;
			else if(!ismatch) cout<<" "<<c;
			else if(ismatch) cout<<c;
			cin>>c;
			if(!isdigit(c)&&c!='.'){
				ismatch=false;
				isfirst=false;
				break;
			}
			isfirst=false;
			ismatch=true;
		}
		if(c==')'){
			e=Pop(head);
			while(e!='('){
				cout<<" "<<e;
				e=Pop(head);
			}
		}
		//如果是+-的话,就把栈顶元素 1弹出判断优先级 2输出或者再放入 
		else if(c=='+'||c=='-'){
			if(isEmpty(head)) Push(head,c); 
			else{
				do{
					e=Pop(head);
					if(e=='('){
						Push(head,'(');
					}
					else cout<<" "<<e;				
				}while(e!='('&&!isEmpty(head));//关键!! 
				Push(head,c);
			}
		}
		else if(c=='*'||c=='/'||c=='('){
			Push(head,c);			
		} 
		else if(c=='#'){
			break;
		}
		else{
			cout<<"用户输入错误"<<endl;
			return -1;
		}		
		cin>>c;
	}
	while(!isEmpty(head)){
		e=Pop(head);
		cout<<" "<<e;
	} 
	return 0;
}

2 判断的时候无结束符

//判断的时候无结束符
//就用string哈哈哈 

#include<bits/stdc++.h>
#include<ctype.h>
using namespace std;
#define Maxsize 100
typedef struct Stack{
	int data[Maxsize];//0~Maxsize-1 
	int top;
}Stack,*pStack; 

pStack CreateStack(){
	pStack head=new Stack();
	head->top=-1;   
	return head;
}

bool isFull(pStack L){
	if(L->top ==Maxsize-1) return true;
	return false;
}

bool isEmpty(pStack L){
	if(L->top ==-1) return true;
	return false;
}

bool Push(pStack L,char e){
	if(isFull(L)) return false;
	L->data [++(L->top)]=e;
	return true;
}

char Pop(pStack L){
	if(isEmpty(L)) return false;
	return L->data [(L->top )--];
}

int main(){
	pStack head;
	head=CreateStack();

	string ss;char e;cin>>ss;
	bool ismatch=true; 
	for(int i=0;ss[i];i++){
		while(isdigit(ss[i])||ss[i]=='.'){
			//是第一次 
			if(i==0){
				cout<<ss[i];
				i++;
			} 
			//不连着  不是第一次 
			else if(!ismatch&&(i!=0)){
				cout<<" "<<ss[i];
				i++;
			} 
			//连着的时候 
			else if(ismatch){
				cout<<ss[i];
				i++;
			} 
			if(!isdigit(ss[i])&&ss[i]!='.'){
				ismatch=false;
				break;
			}
			else{
				ismatch=true;
			} 
		}
		if(ss[i]==')'){
			e=Pop(head);
			while(e!='('){
				cout<<" "<<e;
				e=Pop(head);
			}
		}
		//如果是+-的话,就把栈顶元素 1弹出判断优先级 2输出或者再放入 
		else if(ss[i]=='+'||ss[i]=='-'){
			if(isEmpty(head)) Push(head,ss[i]); 
			else{
				do{
					e=Pop(head);
					if(e=='('){
						Push(head,'(');
					}
					else cout<<" "<<e;				
				}while(e!='('&&!isEmpty(head));//关键!! 
				Push(head,ss[i]);
			}
		}
		else if(ss[i]=='*'||ss[i]=='/'||ss[i]=='('){
			Push(head,ss[i]);			
		} 
	}
	while(!isEmpty(head)){
		e=Pop(head);
		cout<<" "<<e;
	} 
	return 0;
}

PTA上面需要考虑前缀符号的问题,那道题单独记录一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值