文档详情

算术表达式求值的运算符优先算法C语言完整代码.docx

发布:2017-06-06约4.3千字共4页下载文档
文本预览下载声明
#includestdio.h#includestdlib.h#includestring.h#define STACK_INIT_SIZE 100#define STACKINCREMENT 10# define TRUE 1# define FALSE 0typedefint Status;typedef char SElemType;typedefstruct {SElemType *base;SElemType *top;intstacksize;}StackChar; //sequence序列typedefstruct {float *base;float *top;intstacksize;}StackFloat; //sequence序列Status InitStack(StackChar **S){//初始化空桟*S=(StackChar *)malloc(sizeof(StackChar));(*S)-base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!(*S)-base) exit(OVERFLOW);(*S)-top=(*S)-base; (*S)-stacksize=STACK_INIT_SIZE;return OK;}//InitStack(S);Status initStack(StackFloat **S){//初始化空桟*S=(StackFloat *)malloc(sizeof(StackFloat));(*S)-base=(float *)malloc(STACK_INIT_SIZE*sizeof(float));if(!(*S)-base) exit(OVERFLOW);(*S)-top=(*S)-base; (*S)-stacksize=STACK_INIT_SIZE;return OK;}//initStack(S);Status Pop(StackChar **S,SElemType *a){if((*S)-top==(*S)-base) {printf(ERROR!\n); return ERROR; }*a=*(--(*S)-top);return OK;}//Pop(S,e);Status pop(StackFloat **S,float *a){if((*S)-top==(*S)-base) {printf(ERROR!\n); return ERROR; }*a=*(--(*S)-top);return OK;}//Pop(S,e);Status Push(StackChar *S,SElemType e){*S-top++=e; return OK;}//Push(S,e);Status push(StackFloat *S,float e){*S-top++=e; return OK;}//push(S,e);SElemTypeGetTop(StackChar S){SElemType e;e=*(S.top-1); return e;}//GetTop(*S,e);float getTop(StackFloat S){float e;e=*(S.top-1); return e;}//GetTop(*S,e);intInOP(char c){//判断c是否为运算符,是返回TRUE,否则返回FALSEswitch (c){case +: return TRUE; case -: return TRUE;case *: return TRUE;case /: return TRUE;case (: return TRUE;case ): return TRUE;case #: return TRUE;default: return FALSE;}}//InOP(b[2])char precede(SElemTypee,char c){//判断e和c的优先级,若e的优先权小于c 返回; ...=; ...;char OP[7]={+,-,*,/,(,),#};switch (e){ case +: if (c==+||c==-||c== )||c==#) return ;else return ; case -: if (c==+||c==-||c== )||c==#) return ;else return ;case *: if (c==+||c==-||c==*||c==/||c== )||c==#) return ;else return ;cas
显示全部
相似文档