#include#include"function.h"using namespace std;Status Initstack(SqStack &s){//构造一个空栈s.base=(elemtype *)malloc(STACK_INIT_SIZE*sizeof(elemtype));if(!s.base)exit(OVER);s.top=s.base;s.stacksize=STACK_INIT_SIZE;return OK;}Status GetTop(SqStack s,elemtype &e){//取得栈顶元素if(s.base==s.top )return ERROR;e=*(s.top-1);return OK;}Status Pop(SqStack &s,elemtype &e){//出栈if(s.base==s.top)return ERROR;e=*--s.top;return OK;}Status Push(SqStack &s,elemtype &e){//进栈if(s.top-s.base>=s.stacksize){s.base=(elemtype *)realloc(s.base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(elemtype));if(!s.base)exit(OVER);s.top=s.base+s.stacksize;s.stacksize=s.stacksize+STACKINCREMENT;}*s.top++=e;return OK;}char Precede(char c1,char c2){//运算符优先级判断char r;switch(c2){case '+':case'-':if(c1=='('||c1=='#')r= '<';else r= '>';break;case'*':case'/':if(c1=='*'||'/'||')')r='>';else r='<';break;case'(':if(c1==')'){cout<<"括号匹配错误"<<endl;exit(ERROR);}else r='<';break;case')':if(c1=='(')r='=';else if(c1=='#')cout<<"语法错误"<<endl;else r='>';break;case'#':if(c1=='#')r='=';else if(c1=='('){cout<<"语法错误"<<endl;exit(ERROR);}else r='>';break;}return r;}bool In(char c){switch(c){case'+':case'-':case'*':case'/':case'(':case')':case'#':return true;break;default:return false;}}elemtype Operate(elemtype a,char theta,elemtype b){elemtype y;switch(theta){case'+':y=a+b;break;case'-':y=a-b;break;case'*':y=a*b;break;case'/':if(b==0)cout<<"除法分母不能为零"<='0'&&c<='9'||c=='.') { int i=0; while(c>='0'&&c<='9'||c=='.') { Data[i]=c; i++; c=getchar(); } Data[i]='/0';//数字没有存满,输入字符串结束符 d=atof(Data);//此处是把数组里的数字,实际上是按字符串;转换为double类型,然后把浮点型数字入栈 Push(OPND,d);//atof函数的形参是指针类型,故用数组名 } else { cout<<"error!输入错误!"<<endl; exit(-1); } GetTop(OPTR,e); }//while GetTop(OPND,e); return e;}//EvaluateExpressionint main(){elemtype result; cout<<"请输入表达式,以#号结束"<<endl; result=EvaluateExpression(); cout<<"结果为:"<<result<<endl; return 0;}根据严蔚敏数据结构P53页编写
数据结构中表达式求值问题
最新推荐文章于 2023-10-21 10:17:09 发布