利用栈求表达式的值_第1页
利用栈求表达式的值_第2页
利用栈求表达式的值_第3页
利用栈求表达式的值_第4页
利用栈求表达式的值_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、题目:利用栈求表达式的值一.设计任务和目标编写程序实现表达式求值,即验证某算术表达式的正确性, 若正确,则计算 该算术表达式的值。主要功能描述如下:1、从键盘上输入表达式。2、分析该表达式是否合法:(1)是数字,则判断该数字的合法性。若合法,则压入数据到堆栈中。(2)是规定的运算符,则根据规则进行处理。在处理过程中,将计算该表 达式的值。(3)若是其它字符,则返回错误信息。主要功能描述如下:1、从键盘上输入表达式。2、分析该表达式是否合法:(1)是数字,则判断该数字的合法性。若合法,则压入数据到堆栈中。(2)是规定的运算符,则根据规则进行处理。在处理过程中,将计算该表 达式的值。(3)若是其它

2、字符,则返回错误信息。程序应包括以下几个功能函数void initstack();初始化堆栈int Make_str();语法检查并计算int push_operate(i nt operate): 将操作码压入堆栈int push_num(double num):将操作数压入堆栈int procede(int operate): 处理操作码in t cha nge_op nd(in t operate):将字符型操作码转换成优先级 int push_opnd(int operate):将操作码压入堆栈int pop_opnd();将操作码弹出堆栈int caculate(interru_op

3、nd):简单计算 +,-,*,/ double pop_num():弹出操作数程序如下:#i nclude #i nclude #i nclude #defi ne MAXLEN 100typedef structchar op;int level;opt;typedef structp;char push(op_stack *s,opt c)p=0;s-top-;return i;elsereturn error;void clear(op_stack *s) _while(*exp = O & *exp = 9 | *exp =.)tmpi+=*exp+;dtmp=atof(tmp);Dp

4、ush(&ds,dtmp);len g=strle n(tmp);for(i=0;ile ng;i+)tmpi=O;i=0;evel =switch(pop(&os).op)case %:dpoptmp=Dpop(&ds);dpoptmp=(float)(i nt)Dpop(&ds)%(i nt)dpoptmp);Dpush(&ds,dpoptmp);break;case *:dpoptmp=Dpop(&ds)*Dpop(&ds);Dpush(&ds,dpoptmp);break;case /:dpoptmp=Dpop(&ds);dpoptmp=Dpop(&ds)/dpoptmp;Dpush(

5、&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(!lsEmpty(&os) | peek(&os).level =switch(pop(&os).op)case %:dpoptmp=Dpop(&ds);dpoptmp=(float)(i nt)Dpop(&ds)%(i

6、nt)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,dpoptm

7、p);break;push(&os,R);*exp+;break;case1*1.if(!lsEmpty(&os) | peek(&os).level =switch(pop(&os).op)case %:dpoptmp=Dpop(&ds);dpoptmp=(float)(i nt)Dpop(&ds)%(i nt)dpoptmp); Dpush(&ds,dpoptmp);break;dpoptmp=Dpop(&ds)*Dpop(&ds);Dpush(&ds,dpoptmp); break;case /:dpoptmp=Dpop(&ds);dpoptmp=Dpop(&ds)/dpoptmp;Dp

8、ush(&ds,dpoptmp); break;push(&os,M);*exp+;break;case /:if(!IsEmpty(&os) | peek(&os).level =switch(pop(&os).op)case %:dpoptmp=Dpop(&ds);dpoptmp=(float)(i nt)Dpop(&ds)%(i nt)dpoptmp);Dpush(&ds,dpoptmp);break;case *:dpoptmp=Dpop(&ds)*Dpop(&ds);Dpush(&ds,dpoptmp);break;case /:dpoptmp=Dpop(&ds);dpoptmp=D

9、pop(&ds)/dpoptmp;Dpush(&ds,dpoptmp);break;push(&os,D);*exp+;break;case %:if(!lsEmpty(&os) | peek(&os).level =switch(pop(&os).op)case %:dpoptmp=Dpop(&ds);dpoptmp=(float)(i nt)Dpop(&ds)%(i nt)dpoptmp); Dpush(&ds,dpoptmp);break;dpoptmp=Dpop(&ds)*Dpop(&ds);Dpush(&ds,dpoptmp); break;case /:dpoptmp=Dpop(&

10、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)(i nt)Dpop(&ds)%(i nt)dpoptmp);Dpush(&ds,dpoptmp);break;case *:dpoptmp=Dpop(&ds)*Dpop(&ds);Dpush(&

11、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(&0S);p)case %:dpoptmp=Dpop(&ds);dpoptmp=(float)(i nt)Dpop(&ds)%(i nt)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 -:dpopt

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论