1、#include #include#include#include#includeusing namespace std;#define OK 1#define ERROR 0#define OVERFLOW -2#define STACK_SIZE 100#define STACKINCREMENT 10#define OPSIZE 7typedef double ElemType;typedef char SElemType;char opOPSIZE=+,-,*,/,(,),#;char priorOPSIZEOPSIZE=, ,base=(ElemType *)malloc(STACK
2、_SIZE * sizeof(ElemType);if(NULL=S1-base)printf(“Init Failed!n“);exit (OVERFLOW);elseS1-top=S1-base;S1-Stacksize=STACK_SIZE;int StackEmpty(OPND *S1)if(S1-top=S1-base)return 1;elsereturn 0;int StackEmpty(OPTR *S2)if(S2-top=S2-base)return 1;elsereturn 0;void InitStack(OPTR *S2)S2-base=(SElemType *)mal
3、loc(STACK_SIZE*sizeof(SElemType);if(NULL=S2-base)printf(“Init Failed!n“);exit (OVERFLOW);elseS2-top=S2-base;S2-Stacksize=STACK_SIZE;void Push(OPND *S1,ElemType e)if(S1-top-S1-base=S1-Stacksize)S1-base=(ElemType *)realloc(S1-base,(S1-Stacksize+STACKINCREMENT)*sizeof(ElemType);if(!S1-base)exit(OVERFLO
4、W);S1-top=S1-base+S1-Stacksize;S1-Stacksize=S1-Stacksize+STACKINCREMENT;*S1-top=e;S1-top+;void Push(OPTR *S2,SElemType e)if(S2-top-S2-base=S2-Stacksize)S2-base=(SElemType *)realloc(S2-base,(S2-Stacksize+STACKINCREMENT)*sizeof(SElemType);if(!S2-base)exit (OVERFLOW);S2-top=S2-base+S2-Stacksize;S2-Stac
5、ksize+=STACKINCREMENT;*S2-top+=e;ElemType Pop(OPND *S1,ElemType *e)if(!StackEmpty(S1)S1-top=S1-top-1;*e=*S1-top;elsereturn ERROR;return OK;SElemType Pop(OPTR *S2,SElemType *e)if(StackEmpty(S2)return ERROR;else*e=*-S2-top;return OK;ElemType GetTop(OPND *S1,ElemType *e)if(!StackEmpty(S1)*e=*(S1-top-1)
6、;return OK;SElemType GetTop(OPTR *S2,SElemType *e)if(!StackEmpty(S2)*e=*(S2-top-1);return OK;bool Judege(char c,char *op)/判断 C 是否为运算符bool check=false;int i;for(i=0;i:Pop(Pop(Pop(result=Operate(e2,e,e1);Push(break;Pop(return result;int main()char expression80;float result;printf(“输入你要计算的算式:n“);gets(expression);result=EvaluateExpression(expression);printf(“计算结果为:%f“,result);return OK;