您好,欢迎访问三七文档
#includestdio.h#includemalloc.h#includestdlib.h#definestack_init_size100#definestackincrement10#defineOVERFLOW-2#defineFALSE0#defineOK1#defineTRUE1#defineERROR0typedefstruct//定义运算符栈OPTR{char*base;char*top;intstacksize;}soptr;typedefstruct//定义运算数栈OPND{int*base;int*top;intstacksize;}sopnd;voidInitStack_c(soptr&s)//初始化字符栈OPTR{s.base=(char*)malloc(stack_init_size*sizeof(char));if(!s.base)exit(OVERFLOW);s.top=s.base;s.stacksize=stack_init_size;}voidInitStack_i(sopnd&s)//初始化运算数栈OPND{s.base=(int*)malloc(stack_init_size*sizeof(int));if(!s.base)exit(OVERFLOW);s.top=s.base;s.stacksize=stack_init_size;}charGetTop_c(soptrs)//OPTR栈的栈顶元素{chare;if(s.top==s.base)exit(ERROR);e=*(s.top-1);returne;}intGetTop_i(sopnds)//OPTR栈的栈顶元素{inte;if(s.top==s.base)exit(ERROR);e=*(s.top-1);returne;}voidPush_c(soptr&s,chare)//OPTR栈压栈{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;}voidPush_i(sopnd&s,inte)//OPND栈压栈{if(s.top-s.base=s.stacksize)//栈满,增加存储空间{s.base=(int*)realloc(s.base,(s.stacksize+stackincrement)*sizeof(int));if(!s.base)exit(OVERFLOW);s.top=s.base+s.stacksize;s.stacksize+=stackincrement;}*s.top++=e;}voidPop_c(soptr&s,char&e)//OPTR栈出栈{if(s.top==s.base)exit(ERROR);e=*--s.top;}voidPop_i(sopnd&s,int&e)//OPND栈出栈{if(s.top==s.base)exit(ERROR);e=*--s.top;}charRelation[][7]={{'','','','','','',''},{'','','','','','',''},{'','','','','','',''},{'','','','','','',''},{'','','','','','=','',},{'','','','','','',''},{'','','','','','','='}};//定义关系数组charPrecede(charc1,charc2){intoperator1,operator2;switch(c1){case'+':operator1=0;break;case'-':operator1=1;break;case'*':operator1=2;break;case'/':operator1=3;break;case'(':operator1=4;break;case')':operator1=5;break;case'#':operator1=6;break;default:break;}switch(c2){case'+':operator2=0;break;case'-':operator2=1;break;case'*':operator2=2;break;case'/':operator2=3;break;case'(':operator2=4;break;case')':operator2=5;break;case'#':operator2=6;break;default:break;}returnRelation[operator1][operator2];}intOperate(intfront,chartheta,intrear){switch(theta){case'+':returnfront+rear;break;case'-':returnfront-rear;break;case'*':returnfront*rear;break;case'/':returnfront/rear;break;default:break;}}intIn(charc){if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='#')return1;elsereturn0;}voidEvaluteExpression(){sopndOPND;InitStack_i(OPND);soptrOPTR;InitStack_c(OPTR);Push_c(OPTR,'#');charc;c=getchar();intflag=0;/*表示数字字符的变化,当读入的一个字符均属于'0'-'9'时,p值设为1,若下一个字符仍属于'0'-'9'且p的值等于1,那就从数据栈中取出最上面那个元素,在将它乘以10再加上刚才那个字符-48(字符的编码从'0'-'9'依次为48-57)*/while(c!='#'||GetTop_c(OPTR)!='#'){if(!In(c)){if(flag!=0){intswap_i;Pop_i(OPND,swap_i);Push_i(OPND,swap_i*10+c-48);swap_i=GetTop_i(OPND);c=getchar();flag++;}else{Push_i(OPND,c-48);c=getchar();flag++;}}else{flag=0;switch(Precede(GetTop_c(OPTR),c)){case'':Push_c(OPTR,c);c=getchar();break;case'=':charswap_c;Pop_c(OPTR,swap_c);c=getchar();break;case'':chartheta;intb,a;Pop_c(OPTR,theta);Pop_i(OPND,b);Pop_i(OPND,a);Push_i(OPND,Operate(a,theta,b));break;default:break;}//switch}}//whileintx=GetTop_i(OPND);printf(Theresult:%d\n,x);return;}//EvaluteExpressionvoidmain(){EvaluteExpression();return;}
本文标题:表达式求解
链接地址:https://www.777doc.com/doc-7272387 .html