#include<iostream> #include <stdlib.h> using namespace std; typedef int Status; const int TRUE=1; const int FALSE=0; const int OK=1; const int ERROR=0; const int INFEASIBLE=-1; const int overflow=-2; const int STACK_INIT_SIZE=100; const int STACKINCREMENT=10; typedef struct{ char *base; char *top; int stacksize; }SqStack; //构造一个空栈 Status InitStack(SqStack &S) { S.base=(char*)malloc(sizeof(char)*STACK_INIT_SIZE); if(!S.base) exit(overflow); S.top=S.base; S.stacksize=STACK_INIT_SIZE; return OK; } Status GetTop(SqStack S,char &e) { if(S.top==S.base) return ERROR; e=*(S.top-1); return OK; } Status Push(SqStack &S,char e) { if(S.top-S.base>=S.stacksize) { S.base=(char*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char)); if(!S.base)exit(overflow); S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; return OK; } Status Pop(SqStack &S,char &e) { if(S.top==S.base) return ERROR; e=*--S.top; return OK; } Status StackEmpty(SqStack S){ if(S.base==S.top) return TRUE; else return FALSE; } Status ClearStack(SqStack &S) { S.top = S.base ; return TRUE; } Status DestroyStack(SqStack &S) { if(S.base!=NULL) { free(S.base); S.stacksize=0; S.top=NULL; } return TRUE; }
#include"book3_3.h" #define ND 0 //操作数标志 #define TR 1 //操作符标志 //存放运算符的优先顺序的矩阵 op1是竖排的 op2是横排的 e表示错误 const static char Order[7][7]= {{'>','>','<','<','<','>','>'}, // + {'>','>','<','<','<','>','>'}, // - {'>','>','>','>','<','>','>'}, // * {'>','>','>','>','<','>','>'}, // / {'<','<','<','<','<','=','e'}, // ( {'>','>','>','>','e','>','>'}, // ) {'<','<','<','<','<','e','='}}; // # // + - * / ( ) # //比较两个操作符的优先级 op1和op2的顺序不能错 Status compOperator(char op1, char op2, char &result) { int t1=9,t2=9; //找到两个操作符的优先级在矩阵中的位置 switch(op1) { case '+': t1=0; break; case '-': t1=1; break; case '*': t1=2; break; case '/': t1=3; break; case '(': t1=4; break; case ')': t1=5; break; case '#': t1=6; break; default: return ERROR; break; } switch(op2) { case '+': t2=0; break; case '-': t2=1; break; case '*': t2=2; break; case '/': t2=3; break; case '(': t2=4; break; case ')': t2=5; break; case '#': t2=6; break; default: return ERROR; break; } result=Order[t1][t2]; if(result=='e') return ERROR; else return OK; } int decideNDorTR(char t) //判断输入的字符是操作符还是操作数 { if(t>=48&&t<=57) //数字的ascii码范围 { return ND; } else if(t=='+' || t=='-' || t=='*' || t=='/' || t=='(' || t==')' || t=='#') { return TR; } else { printf("error"); return ERROR; } } char myCalculate(char op,char num1,char num2) //字符型的运算符的计算 { char num; switch(op) { case '+': num= num1+ num2; break; case '-': num= num1- num2; break; case '*': num= num1* num2; break; case '/': num= num1/ num2; break; default: printf("error");break; } return num; } //计算表达式的函数 注意运算结果大小不能超过255 char型 char EvaluateExpression(char *p, int length) { SqStack OPTR, OPND; InitStack(OPTR); InitStack(OPND); Push(OPTR,'#'); while(*p != '