【数据结构】7-2 Infix to Postfix Conversion (40 分)

Convert the infix expression to postfix expression.

输入格式:
1 line. A correct expression include +,-,*,/,(,) and integer. Each integer is less than 10 and more than 0.
输出格式:
2 line. The first line gives the result of expression. Keep 2 digits after the decimal point. The second line gives the postfix expression. Use a blank space to divide every number and symbol. There is a blank space at the end of the line.

输入样例:
在这里给出一组输入。例如:
2*(3+4)/5
输出样例:
在这里给出相应的输出。例如:
2.80
2 3 4 + * 5 /

#include<stdio.h>
#include<stdlib.h>
struct node{
	char element;
	struct node* next;
};
typedef struct node *PtrToNode;
typedef PtrToNode Stack;
struct Node{
	float element;
	struct Node* next;
};
typedef struct Node* nstack;

int isEmpty(Stack s);
int isEmptyn(nstack ns);
void makeEmpty(Stack s);
void makeEmptyn(nstack ns);
Stack createStack();
nstack createStackn();
char pop (Stack s);
float popn (nstack ns);
void push (char x, Stack s);
void pushn (float x, nstack ns);
char top(Stack s);

main(){
//	Stack s=(Stack)malloc(sizeof(struct node));
//	s->next=NULL;
	Stack s=createStack();
	char str[100],re[100],tmp;
	int i=0,j=0,len=0;
	scanf("%s",str);//输入
	
	//中缀表达式转后缀表达式 
	while(str[i]!='\0'){
		if(str[i]>'0' && str[i]<='9'){
			//数字直接存入结果数组中 
			re[j++]=str[i];
		}
		else if(str[i]=='('){
			//(直接压栈 
			push(str[i],s);
		}
		else if(str[i]==')'){
			//)弹栈直至遇到(
			tmp=pop(s); 
			while(tmp!='('){
				re[j++]=tmp;
				tmp=pop(s);
			}
		}
		else if(str[i]=='+' || str[i]=='-'){
			//+-优先级较低,弹栈至遇到(或空栈,(还要压栈压回去
			if(!isEmpty(s)){
				//如果不是空栈 
				tmp=pop(s);
				while(tmp!='('){
					re[j++]=tmp;
					if(!isEmpty(s)){
						tmp=pop(s);
					}else{
						break;
					}
				}
				if(tmp=='('){
					push(tmp,s);
				}
			}
			push(str[i],s);
		}
		else if(str[i]=='*' || str[i]=='/'){
			//优先级高,只有乘除才弹栈 
			if(!isEmpty(s)){
				//不是空栈
				tmp=pop(s);
				while(tmp=='*' || tmp=='/'){
					re[j++]=tmp;
					if(!isEmpty(s)){
						tmp=pop(s);
					}else{
						break;
					}
				}
				if(tmp=='+' || tmp=='-' || tmp=='('){
					push(tmp,s);
				} 
			}
			push(str[i],s);
		}
		else{
			printf("\nWrong format\n");
		}
		i++;
	}
	while(!isEmpty(s)){
		tmp=pop(s);
		re[j++]=tmp;
	}
	len=j;
	//计算结果 
	float right=0,left=0,n=0;
	nstack ns=(nstack)malloc(sizeof(struct Node));
	ns->next=NULL;
	for(j=0;j<len;j++){
		switch(re[j]){
			case '+':
				right=popn(ns);
				left=popn(ns);
				pushn(right+left,ns);
				break;
			case '-':
				right=popn(ns);
				left=popn(ns);
				pushn(left-right,ns);
				break;
			case '*':
				right=popn(ns);
				left=popn(ns);
				pushn(left*right,ns);
				break;
			case '/':
				right=popn(ns);
				left=popn(ns);
				pushn(left/right,ns);
				break;
			default:
				n=re[j]-'0';
				pushn(n,ns);
				break;
		}
	}
	//输出结果
	n=popn(ns);
	printf("%.2lf\n",n);
	for(j=0;j<len;j++){
		printf("%c ",re[j]);
	} 
}

int isEmpty(Stack s){
	return s->next == NULL;
}
int isEmptyn(nstack ns){
	return ns->next==NULL;
}
void makeEmpty(Stack s) {
	if (s == NULL){
		printf("\nMust use CreateStack first\n");
	}else{
		while (!isEmpty(s)){
			pop(s);
		}
	}
}
void makeEmptyn(nstack ns){
	if(ns==NULL){
		printf("\nMust use CreateStack first\n");
	}else{
		while(!isEmptyn(ns)){
			popn(ns);
		}
	}
}
Stack createStack(){
	Stack s;
	s = (Stack)malloc (sizeof (struct node));
	if (s==NULL) {
		printf("\nOut of space!!\n");
	}
	s->next=NULL; 
	makeEmpty(s);
	return s;
}
nstack createStackn(){
	nstack ns;
	ns=(nstack)malloc(sizeof(struct Node));
	if(ns==NULL){
		printf("\nOut of space!!\n");
	}
	ns->next=NULL;
	makeEmptyn(ns);
	return ns;
}
char pop (Stack s) {
	char tmp;
	PtrToNode first;
	if (isEmpty(s)) {
		printf("\nempty stack\n");
	} else {
		first = s->next;
		s->next = s->next->next;
		tmp=first->element;
		free(first);
	}
	return tmp;
}
float popn (nstack ns) {
	float tmp;
	nstack first;
	if (isEmptyn(ns)) {
		printf("\nempty stack\n");
	} else {
		first = ns->next;
		ns->next = ns->next->next;
		tmp=first->element;
		free(first);
	}
	return tmp;
}
void push (char x, Stack s) {
	PtrToNode temp;
	temp = (PtrToNode)malloc(sizeof(struct node));
	if (temp == NULL) {
		printf("\nout of space!!\n");
	} else {
		temp->element = x;
		temp ->next = s->next;
		s->next = temp;
	}
	temp=NULL;
	free(temp);
}
void pushn (float x, nstack ns) {
	nstack temp;
	temp = (nstack)malloc(sizeof(struct Node));
	if (temp == NULL) {
		printf("\nout of space!!\n");
	} else {
		temp->element = x;
		temp ->next = ns->next;
		ns->next = temp;
	}
	temp=NULL;
	free(temp);
}
char top(Stack s) {
	if (!isEmpty(s)) {
		return s->next->element;
	}
	printf("\nEmpty stack\n");
	return 0;
}

因为要先输出计算结果再输出后缀表达式,只能用两种结构体建立栈,比较麻烦,想不到好方法解决这个问题,希望有大佬指点。

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值