资源描述:
《算术表达式求值演示》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、算术表达式求值演示算术表达式求值演示#include#include#include#include#include#defineERROR0#defineOK1#defineSTACK_INIT_SIZE100//存储空间初始分配量#defineSTACKINCREMENT10//存储空间分配增量#defineOPSETSIZE10charOP[OPSETSIZE]={'+','-','*','/','(',')','#','^','~','='};unsignedcharform[OPS
2、ETSIZE][OPSETSIZE]={{'>','>','<','<','<','>','>','<','<','>'},{'>','>','<','<','<','>','>','<','<','>'},{'>','>','>','>','<','>','>','<','<','>'},{'>','>','>','>','<','>','>','<','<','>'},{'<','<','<','<','<','=','','<','<','<'},{'>','>','>','>','','>','>','>','>','>'},{'<','<','<','<','<','',
3、'=','<','<','<'},{'>','>','>','>','<','>','>','>','<','>'},{'>','>','>','>','<','>','>','>','>','>'},{'<','<','<','<','<','>','>','<','<','>'}};typedefstruct{float*base;//在栈构造之前和销毁之后,base的值为NULLfloat*top;//栈顶指针intstacksize;//当前已分配的存储空间,以元素为单位}SqStack;typedefstruct{char*base;char*top;intstacksi
4、ze;}SqStack_c;intInitStack(SqStack&S){//构造一个空栈S存储数据S.base=(float*)malloc(STACK_INIT_SIZE*sizeof(float));if(!S.base)exit(OVERFLOW);//存储分配失败S.top=S.base;S.stacksize=STACK_INIT_SIZE;returnOK;}//InitStackintInitStack_c(SqStack_c&S){//构造一个空栈S存储运算符S.base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));i
5、f(!S.base)exit(OVERFLOW);//存储分配失败S.top=S.base;S.stacksize=STACK_INIT_SIZE;returnOK;}//InitStack_cintPush(SqStack&S,floate){//插入元素e为新的栈顶元素if(S.top-S.base>=S.stacksize){//栈满,追加存储空间S.base=(float*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(float));if(!S.base)exit(OVERFLOW);//存储分配失败S.top=S.
6、base+S.stacksize;S.stacksize+=STACKINCREMENT;}*S.top++=e;returnOK;}//PushintPush_c(SqStack_c&S,charch){//插入元素ch为新的栈顶元素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.st
7、acksize+=STACKINCREMENT;}*S.top++=ch;returnOK;}//Push_cintPop(SqStack&S,float&e){//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERRORif(S.top==S.base)returnERROR;e=*--S.top;;returnOK;}//popintPop_c(SqStack_c&S,char&ch){//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK,