版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 网络安全工程师系统维护与漏洞修复绩效考核表
- 2025年特殊作业监护人学习试题及答案
- 2026年健身教练专业面试题及解答
- 2026年航运公司电管理职位面试题解析
- 商品粉煤灰项目建议书
- 小学数学教育中AI个性化作业设计对数学问题解决能力的影响教学研究课题报告
- 2025年在线问诊平台儿科诊疗服务模式报告
- 2026年教育机构教务主任招聘考试题目
- 2026年中航集团航空服务监督人员面试题库及解析
- 2026年物流规划师职业指南及面试题集
- 智能教育:科技驱动下的学校革新
- 汉字笔画练习字帖
- 酒店住宿和餐饮行业企业安全风险分级管控清单
- 手机靓号抵押借款合同
- GB/T 7044-2013色素炭黑
- GB/T 26059-2010钛及钛合金网板
- 羊的品种课件
- 国家开放大学《机械设计基础》机考试题001-009参考答案
- 《大卫-不可以》绘本
- (完整word版)Word信纸(A4横条直接打印版)模板
- 测试算例-各向同性湍流DNS
评论
0/150
提交评论