资源描述:
《算术表达式求值数据结构代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#include#include#includeusingnamespacestd;#defineOK1#defineERROR0#defineOVERFLOW-2#defineSTACK_SIZE100#defineSTACKINCREMENT10#defineOPSIZE7typedefdoubleElemType;typedefcharSElemType;charop[OPSIZE]={'+','-
2、','*','/','(',')','#'};charprior[OPSIZE][OPSIZE]={{'>','>','<','<','<','>','>'},{'>','>','<','<','<','>','>'},{'>','>','>','>','<','>','>'},{'>','>','>','>','<','>','>'},{'<','<','<','<','<','=',''},{'>','>','>','>','','>','>'},{'<','<','<','<','<','','='}};typedefstru
3、ct{ElemType*top;ElemType*base;intStacksize;}OPND;typedefstruct{SElemType*top;SElemType*base;intStacksize;}OPTR;voidInitStack(OPND*S1){S1->base=(ElemType*)malloc(STACK_SIZE*sizeof(ElemType));if(NULL==S1->base){printf("InitFailed!");exit(OVERFLOW);}else{S1->top=S1->bas
4、e;S1->Stacksize=STACK_SIZE;}}intStackEmpty(OPND*S1){if(S1->top==S1->base)return1;elsereturn0;}intStackEmpty(OPTR*S2){if(S2->top==S2->base)return1;elsereturn0;}voidInitStack(OPTR*S2){S2->base=(SElemType*)malloc(STACK_SIZE*sizeof(SElemType));if(NULL==S2->base){printf("In
5、itFailed!");exit(OVERFLOW);}else{S2->top=S2->base;S2->Stacksize=STACK_SIZE;}}voidPush(OPND*S1,ElemTypee){if(S1->top-S1->base>=S1->Stacksize){S1->base=(ElemType*)realloc(S1->base,(S1->Stacksize+STACKINCREMENT)*sizeof(ElemType));if(!S1->base)exit(OVERFLOW);S1->top=S1->
6、base+S1->Stacksize;S1->Stacksize=S1->Stacksize+STACKINCREMENT;}*S1->top=e;S1->top++;}voidPush(OPTR*S2,SElemTypee){if(S2->top-S2->base>=S2->Stacksize){S2->base=(SElemType*)realloc(S2->base,(S2->Stacksize+STACKINCREMENT)*sizeof(SElemType));if(!S2->base)exit(OVERFLOW);S2-
7、>top=S2->base+S2->Stacksize;S2->Stacksize+=STACKINCREMENT;}*S2->top++=e;}ElemTypePop(OPND*S1,ElemType*e){if(!StackEmpty(S1)){S1->top=S1->top-1;*e=*S1->top;}else{returnERROR;}returnOK;}SElemTypePop(OPTR*S2,SElemType*e){if(StackEmpty(S2)){returnERROR;}else{*e=*--S2->top;
8、}returnOK;}ElemTypeGetTop(OPND*S1,ElemType*e){if(!StackEmpty(S1)){*e=*(S1->top-1);}returnOK;}SElemTypeGetTop(OPTR*S2,