您好,欢迎访问三七文档
题目:利用栈求表达式的值一.设计任务和目标编写程序实现表达式求值,即验证某算术表达式的正确性,若正确,则计算该算术表达式的值。主要功能描述如下:1、从键盘上输入表达式。2、分析该表达式是否合法:(1)是数字,则判断该数字的合法性。若合法,则压入数据到堆栈中。(2)是规定的运算符,则根据规则进行处理。在处理过程中,将计算该表达式的值。(3)若是其它字符,则返回错误信息。主要功能描述如下:1、从键盘上输入表达式。2、分析该表达式是否合法:(1)是数字,则判断该数字的合法性。若合法,则压入数据到堆栈中。(2)是规定的运算符,则根据规则进行处理。在处理过程中,将计算该表达式的值。(3)若是其它字符,则返回错误信息。程序应包括以下几个功能函数voidinitstack();初始化堆栈intMake_str();语法检查并计算intpush_operate(intoperate):将操作码压入堆栈intpush_num(doublenum):将操作数压入堆栈intprocede(intoperate):处理操作码intchange_opnd(intoperate):将字符型操作码转换成优先级intpush_opnd(intoperate):将操作码压入堆栈intpop_opnd();将操作码弹出堆栈intcaculate(interru_opnd):简单计算+,-,*,/doublepop_num():弹出操作数程序如下:#includestdio.h#includestring.h#includestdlib.h#defineMAXLEN100typedefstruct{charop;intlevel;}opt;typedefstruct//定义操作符栈{optst[MAXLEN];inttop;}op_stack;typedefstruct//定义值栈{doubleD[MAXLEN];inttop;}D_stack;//--------对栈操作的定义-------------optpeek(op_stack*s)//定义看栈顶函数{opterror=;if(s-top=0)returns-st[s-top];elsereturnerror;}intIsEmpty(op_stack*s)//定义判断栈空的函数{if(s-top0)return0;elsereturns-st[s-top].op;}charpush(op_stack*s,optc)//定义入栈函数{s-top++;s-st[s-top]=c;returnc.op;}optpop(op_stack*s)//定义出栈函数{opti;opterror=;if(s-top=0){i=s-st[s-top];s-st[s-top].op='\0';s-top--;returni;}elsereturnerror;}voidclear(op_stack*s)//定义初始化栈{s-top=-1;}//-----------------------------definethevaluestack-----------------------doubleDpeek(D_stack*s)//定义看栈顶函数{if(s-top=0)returns-D[s-top];elsereturn0;}intDIsEmpty(D_stack*s)//定义判断栈空的函数{if(s-top0)return0;elsereturn(int)(s-D[s-top]);}doubleDpush(D_stack*s,doublec)//定义入栈函数{s-top++;s-D[s-top]=c;returnc;}doubleDpop(D_stack*s)//定义出栈函数{doublei;if(s-top=0){i=s-D[s-top];s-D[s-top]='\0';s-top--;returni;}elsereturn0;}voidDclear(D_stack*s)//定义初始化栈{s-top=-1;}doublecalval(char*exp){op_stackos;//定义两个栈D_stackds;chartmp[MAXLEN]=;inti=0,leng;doubledtmp,dpoptmp;optA=;optR=;optM=;optD=;optB=;optMo=;clear(&os);Dclear(&ds);//-----定义初始化结束-----while(*exp!='\0'){while(*exp='0'&&*exp='9'||*exp=='.'){while(*exp='0'&&*exp='9'||*exp=='.'){tmp[i++]=*exp++;}dtmp=atof(tmp);Dpush(&ds,dtmp);leng=strlen(tmp);for(i=0;ileng;i++){tmp[i]='\0';}i=0;}//-------------------------------switch(*exp){case'+':if(!IsEmpty(&os)||peek(&os).levelA.level){push(&os,A);*exp++;}else{while(IsEmpty(&os)&&peek(&os).level=A.level){switch(pop(&os).op){case'%':dpoptmp=Dpop(&ds);dpoptmp=(float)((int)Dpop(&ds)%(int)dpoptmp);Dpush(&ds,dpoptmp);break;case'*':dpoptmp=Dpop(&ds)*Dpop(&ds);Dpush(&ds,dpoptmp);break;case'/':dpoptmp=Dpop(&ds);dpoptmp=Dpop(&ds)/dpoptmp;Dpush(&ds,dpoptmp);break;case'+':dpoptmp=Dpop(&ds)+Dpop(&ds);Dpush(&ds,dpoptmp);break;case'-':dpoptmp=Dpop(&ds);dpoptmp=Dpop(&ds)-dpoptmp;Dpush(&ds,dpoptmp);break;}}push(&os,A);*exp++;}break;case'-':if(!IsEmpty(&os)||peek(&os).levelR.level){push(&os,R);*exp++;}else{while(IsEmpty(&os)&&peek(&os).level=R.level){switch(pop(&os).op){case'%':dpoptmp=Dpop(&ds);dpoptmp=(float)((int)Dpop(&ds)%(int)dpoptmp);Dpush(&ds,dpoptmp);break;case'*':dpoptmp=Dpop(&ds)*Dpop(&ds);Dpush(&ds,dpoptmp);break;case'/':dpoptmp=Dpop(&ds);dpoptmp=Dpop(&ds)/dpoptmp;Dpush(&ds,dpoptmp);break;case'+':dpoptmp=Dpop(&ds)+Dpop(&ds);Dpush(&ds,dpoptmp);break;case'-':dpoptmp=Dpop(&ds);dpoptmp=Dpop(&ds)-dpoptmp;Dpush(&ds,dpoptmp);break;}}push(&os,R);*exp++;}break;case'*':if(!IsEmpty(&os)||peek(&os).levelM.level){push(&os,M);*exp++;}else{while(IsEmpty(&os)&&peek(&os).level=M.level){switch(pop(&os).op){case'%':dpoptmp=Dpop(&ds);dpoptmp=(float)((int)Dpop(&ds)%(int)dpoptmp);Dpush(&ds,dpoptmp);break;case'*':dpoptmp=Dpop(&ds)*Dpop(&ds);Dpush(&ds,dpoptmp);break;case'/':dpoptmp=Dpop(&ds);dpoptmp=Dpop(&ds)/dpoptmp;Dpush(&ds,dpoptmp);break;}}push(&os,M);*exp++;}break;case'/':if(!IsEmpty(&os)||peek(&os).levelD.level){push(&os,D);*exp++;}else{while(IsEmpty(&os)&&peek(&os).level=D.level){switch(pop(&os).op){case'%':dpoptmp=Dpop(&ds);dpoptmp=(float)((int)Dpop(&ds)%(int)dpoptmp);Dpush(&ds,dpoptmp);break;case'*':dpoptmp=Dpop(&ds)*Dpop(&ds);Dpush(&ds,dpoptmp);break;case'/':dpoptmp=Dpop(&ds);dpoptmp=Dpop(&ds)/dpoptmp;Dpush(&ds,dpoptmp);break;}}push(&os,D);*exp++;}break;case'%':if(!IsEmpty(&os)||peek(&os).levelMo.level){push(&os,Mo);*exp++;}else{while(IsEmpty(&os)&&peek(&os).level=Mo.level){switch(pop(&os).op){case'%':dpoptmp=Dpop(&ds);dpoptmp=(float)((int)Dpop(&ds)%(int)dpoptmp);Dpush(&ds,dpoptmp);break;case'*':dpoptmp=Dpop(&ds)*Dpop(&ds);Dpush(&ds,dpoptmp);break;case'/':dpoptmp=Dpop(&ds);dpoptmp=Dpop(&ds)/dpoptmp;Dpush(&ds,dpoptmp);break;}}push(&os,Mo);*exp++;}break;case'(':push(&os,B);exp++;break;case')':while(peek(&os).level!=-2){switch(pop(&os).op){case'%':dpoptmp=Dpop(&ds);dpoptmp=(float)((int)Dpop(&ds)%(int)dpoptmp);Dpush(&ds,dpoptmp);break;case'*':dpoptmp=Dpop(&ds)*Dpop(&ds);Dpush(&ds,dpoptmp);break;case'/':dpoptmp=Dpop(&ds);dpoptmp=Dpop(&ds)/dpoptmp;Dpush(&ds,dpoptmp);break;case'+':dpoptmp=Dpop(&ds)+Dpop(&ds);Dpush(&ds,dpoptmp);break;case'-':dpoptmp=Dpop(&ds);dpoptmp=Dpop(&ds)-dpoptmp;Dpush(&ds,dpoptmp);break;}}pop(&os);//弹出(exp++;break;}}while(IsEmpty(&os)){switch(pop(&os).op){case'%':dpoptmp=Dpop(&ds
本文标题:利用栈求表达式的值
链接地址:https://www.777doc.com/doc-5824301 .html