C语言用后序求表达式的结果

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
# define MaxSize 50
#define chartonumber(x) (x-'0')
#define numbertochar(x) (x+'0')
double op;


typedef struct SqStack{
	double data[MaxSize];
	int top;
}SqStack;

void InitStack(SqStack &S){
	S.top=-1;
}

bool StackEmpty(SqStack S){
	if(S.top==-1)return true;
	else return false;
}

bool Push(SqStack &S,double x){
	if(S.top==MaxSize-1)return false;
	else
	S.data[++S.top]=x;
	return true;
}

bool Pop(SqStack &S,double &x){
	if(S.top==-1)return false;
	x=S.data[S.top--];
	return true;
}

bool GetTop(SqStack S,double &x){
	if(S.top==-1)return false;
	x=S.data[S.top];
	return true;
}


typedef struct LinkNode{
	double data;
	int flag;
	struct LinkNode *next;
}LinkNode;

typedef struct LinkQueue{
	LinkNode *front,*rear;
}LinkQueue;

void InitQueue(LinkQueue &Q){
	Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));
	Q.front->next=NULL;
}

bool IsEmpty(LinkQueue Q){
	if(Q.front==Q.rear)return true;
	else return false;
}


void EnQueue(LinkQueue &Q,double x,int y){
	LinkNode *s;
	s=(LinkNode*)malloc(sizeof(LinkNode));
	s->data=x;
	s->flag=y;
	s->next=NULL;
	Q.rear->next=s;
	Q.rear=s;
}

bool DeQueue(LinkQueue &Q,double &x,int &y){
	if(Q.front==Q.rear)return false;
	LinkNode *p;
	p=Q.front->next;
	x=p->data;
	y=p->flag;
	Q.front->next=p->next;
	if(Q.rear==p)Q.rear=Q.front;
	free(p);
	return true;
}

void calculate(LinkQueue Q){
	SqStack S;
	InitStack(S);
	double num=0.0,arg=0.0;;int flag=0;double fx;int fy;
	while(DeQueue(Q,fx,fy)){
		if(fy==0){
			if(flag==0){num=fx;flag=1;}
			else if(flag==1){arg=fx;flag=2;}
			else if(flag==2){
				Push(S,num);
				num=arg;
				arg=fx;
				flag=2;
			}
		}
		else
		if(fy==1){
			if(flag==1){
				arg=num;
				Pop(S,num);
				flag==2;
			}
				switch((int)fx){
				case '+':num+=arg;flag=1;break;
				case '-':num-=arg;flag=1;break;
				case '*':num*=arg;flag=1;break;
				case '/':num/=arg;flag=1;break;
				default:printf("%s","请重新输入计算式");break;
			}
		}
	}
	printf("%.3f\n",num);
}


LinkQueue midtoend(char string[],int length){
	LinkQueue Q;SqStack S;
	InitQueue(Q);InitStack(S);
	char c;
	for(int i=0;i<length;i++){
		double num=0.0;
		c=string[i];
		int numflag=chartonumber(c);
		if(numflag>=0 && numflag<=9){
			while((numflag>=0 && numflag<=9)){
				num=num*10+numflag;
				c=string[++i];
				if(c=='.'){
					c=string[++i];
					numflag=chartonumber(c);int point=1;
					while((numflag>=0 && numflag<=9)){
						double dn=numflag;
						while(point--){
							dn=dn/10;
						}
						num+=dn;
						point++;
					}
				}else
				numflag=chartonumber(c);
			}
			EnQueue(Q,num,0);
		}
			switch(c){
				case '(':
						Push(S,'(');
						break;
				case '=':
						while(Pop(S,op)){
							EnQueue(Q,op,1);
						}
						break;
				case ')':
						Pop(S,op);
						while(((int)op)!='('){
							EnQueue(Q,op,1);;
							Pop(S,op);
						}
						break;
				case '+':
						while(GetTop(S,op)){
							if(((int)op)!='('){Pop(S,op);EnQueue(Q,op,1);}else break;
						}
						Push(S,'+');
						break;
				case '-':
						while(GetTop(S,op)){
							if(((int)op)!='('){Pop(S,op);EnQueue(Q,op,1);}else break;
						}
						Push(S,'-');
						break;
				case '*':
						while(GetTop(S,op)){
							if(((int)op)=='*'||((int)op)=='/'){Pop(S,op);EnQueue(Q,op,1);}else break;
						}
						Push(S,'*');
						break;
				case '/':
						if(GetTop(S,op)){
							if(((int)op)=='*'||((int)op)=='/'){Pop(S,op);EnQueue(Q,op,1);}
						}
						Push(S,'/');
						break;
				default : printf("%s","不是正确的表达式"); break; 
			}
	}
	return Q;
}


int main(){
	while(true){
		char string[100];
		int i=0;
		gets(string);
		while(string[i]!='\0'){
			i++;
		}
		string[i++]='=';
		int length=i;
		calculate(midtoend(string,length));
	}
	return 0;
}
</pre><pre name="code" class="plain">可以计算double型


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值