实验三算法表达式C语言实验报告.doc_第1页
实验三算法表达式C语言实验报告.doc_第2页
实验三算法表达式C语言实验报告.doc_第3页
实验三算法表达式C语言实验报告.doc_第4页
实验三算法表达式C语言实验报告.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

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

文档简介

数据结构实验实验项目三栈和队列的应用 计算机科学与技术系0902班 组 长: 雷耀阳 组 员:柴瑞东、李志鹏、张玲春 日 期:实验报告实验类型_综合设计_ 实验室 软件实验室1 1 实验题目求一个数学表达式的值:用户输入一个包含正整数、括号和四则运算符(“+”、 “”、 “*”、 “/”)的算术表达式,计算其结果。2 需求分析首先置操作数栈为空栈,表达式起始符“#”为运算符栈底元素;依次读入表达式中每个字符,若是操数则进操作数栈,若是操作符则和操作符栈顶的运算符进行比较优先权后作相应的操作,直到整个表达式求值完毕(即操作符栈顶元素和当前读入的字符均为“#”)3 概要设计结点结构类型描述如下 typedef structchar *base,*top;int stacksize;sqstack;函数关系:main()result()compute()face()op()4 详细设计实现概要设计中定义的所有的数据类型,对每个操作给出伪码算法。对主程序和其他模块也都需要写出伪码算法。#include #include #include #include #include #define N 100 double numStackN=0; int numTop; char opStackN; int opTop; int op(char ch) if(ch=+|ch=-) return 2; if(ch=*|ch=/) return 3; if(ch=() return -1; return 0; double result(double num1,char op,double num2) if(op=+) return num1+num2; if(op=-) return num1-num2; if(op=*) return num1*num2; if(op=/) return num1/num2; return 0; int compute(char str) double num=0; int i=0,j=1,k=1; int Flag=0; numTop=opTop=0; while(stri!=0|opTop0) if(stri=0&stri0&!op(stri-1)&stri!=(&stri-1!=) numStacknumTop+=num*k; num=0; j=1; Flag=0; k=1; if(opTop=0|stri=() opStackopTop+=stri; else if(stri=) while(opTop0&opStack-opTop!=() numStacknumTop-2=result(numStacknumTop-2,opStackopTop,numStacknumTop-1); numTop-; if(opStackopTop!=() return 0; else if(stri=0&numTop=0) return 0; while(opTop0&op(stri)=0&ch=0&ch=9 ) numj+=ch; numj=0; else j=0; if(ch=S|ch=s) if(strlen(num) face(); printf(%s has been savedn,strcpy(save,num); printf(input an expression,press key E to computen); printf(%s,str); else face(); printf(there is no number to save!n); printf(input an expression,press key E to computen); printf(%s,str); if(ch=R|ch=r) if(strlen(save) face(); printf(input an expression,press key E to computen); printf(%s,strcat(str,save); i+=strlen(save); if(ch=C|ch=c) if(strlen(str) str-i=0; face(); printf(input an expression,press key E to computen); printf(%s,str); if(ch=E|ch=e) if(compute(str) printf(n=%gn,numStack0); j=0; temp=numStack0; if(temp=10) k*=10; while(k) numj+=temp2/k+0; numj=0; temp2=temp2%k; k/=10; temp=temp-(int)temp; if(temp!=0) numj+=.; numj=0; temp+=0.0000005; for(k=6;k0;k-) if(temp=0) break; temp*=10; numj+=(int)temp+0; numj=0; temp=temp-(int)temp; else face(); printf(input an expression,press key E to computen); printf(%s,str); printf(nwrong expression!); i=0; j=0; str0=0; if(ch=Q|ch=q) printf(nare you sure to quit?(Y/N)n); ch=getch(); if(ch=Y|ch=y) break; else face(); printf(input an expression,press key E to computen); printf(%s,str); ch=getch(); #include #include #include #include #include #define N 100 double numStackN=0; int numTop; char opStackN; int opTop; int op(char ch) if(ch=+|ch=-) return 2; if(ch=*|ch=/) return 3; if(ch=() return -1; return 0; double result(double num1,char op,double num2) if(op=+) return num1+num2; if(op=-) return num1-num2; if(op=*) return num1*num2; if(op=/) return num1/num2; return 0; int compute(char str) double num=0; int i=0,j=1,k=1; int Flag=0; numTop=opTop=0; while(stri!=0|opTop0) if(stri=0&stri0&!op(stri-1)&stri!=(&stri-1!=) numStacknumTop+=num*k; num=0; j=1; Flag=0; k=1; if(opTop=0|stri=() opStackopTop+=stri; else if(stri=) while(opTop0&opStack-opTop!=() numStacknumTop-2=result(numStacknumTop-2,opStackopTop,numStacknumTop-1); numTop-; if(opStackopTop!=() return 0; else if(stri=0&numTop=0) return 0; while(opTop0&op(stri)=0&ch=0&ch=9 ) numj+=ch; numj=0; else j=0; if(ch=S|ch=s) if(strlen(num) face(); printf(%s has been savedn,strcpy(save,num); printf(input an expression,press key E to computen); printf(%s,str); else face(); printf(there is no number to save!n); printf(input an expression,press key E to computen); printf(%s,str); if(ch=R|ch=r) if(strlen(save) face(); printf(input an expression,press key E to computen); printf(%s,strcat(str,save); i+=strlen(save); if(ch=C|ch=c) if(strlen(str) str-i=0; face(); printf(input an expression,press key E to computen); printf(%s,str); if(ch=E|ch=e) if(compute(str) printf(n=%gn,numStack0); j=0; temp=numStack0; if(temp=10) k*=10; while(k) numj+=temp2/k+0; numj=0; temp2=temp2%k; k/=10; temp=temp-(int)temp; if(temp!=0) numj+=.; numj=0; temp+=0.0000005; for(k=6;k0;k-) if(temp=0) break; temp*=10; numj+=(int)temp+0; numj=0; temp=temp-(int)temp; else face(); printf(input an expression,press key E to computen); printf(%s,str); printf(nwrong expression!); i=0; j=0; str0=0; if(ch=Q|ch=q) printf(nare you sure to quit?(Y/N)n); ch=getch(); if(ch=Y|ch=y) break; else face(); printf(input an expression,press key E to computen); printf(%s,str); ch=getch(); 5 调试分析1. 实验过程中,发现错误后,经过看书,不断调试后,成功。2. 菜单是借鉴别人的,拿过来经过改动后,才用到本试验中中的。6 使用说明程序名为表达式求值.exe,运行环境为DOS。程序执行后显示_ Save number(S) | Read number(R) | Clear(C) | Equal(E) | Quit(Q) -input an expression,press key E to compute 请选择操作SRCE Q选择:在选择不同的字母后,将实现不同功能。选择Q:退出程序显示:are you sure to quit?(Y/N)输入Y,推出输入N,回到主界面选择C:1.清屏 2.删除上一次输入 选择E:计算输入表达式的结果并显示结果 选择S:保存所输入表达式的结果 如果没有表达式的结果,输出:there is no number

温馨提示

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

最新文档

评论

0/150

提交评论