C语言用后序求表达式的结果
2015-10-05 15:41
423 查看
#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型
相关文章推荐
- C语言中内存分布及程序运行中(BSS段、数据段、代码段、堆栈)
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- C语言中计算正弦的相关函数总结
- 使用C语言详解霍夫曼树数据结构
- 探讨C语言的那些小秘密之断言
- C语言实现BMP转换JPG的方法
- 深入探讨C语言中局部变量与全局变量在内存中的存放位置
- C语言查找数组里数字重复次数的方法
- C语言泛型编程实例教程
- C语言中使用lex统计文本文件字符数
- 在C语言中转换时间的基本方法介绍