C++课程设计后缀表达_第1页
C++课程设计后缀表达_第2页
C++课程设计后缀表达_第3页
C++课程设计后缀表达_第4页
C++课程设计后缀表达_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

1、C与C+课程设计报告设计题目: 专 业 班 级 学 生 学 号 指导教师 起止时间 2013年 秋季 学期 一、具体任务 题目:后缀表达式求值基本要求:使用模板设计后缀表达式求值,模拟一个计算器操作。要求定义一个计算器类,模拟后缀表达式的计算过程。例如:四则运算式32*6-15+35,后缀表达 式为32 6 15 - * 35 + 。 二、软件环境win7下的VC+6.0三、算法思想使用一个存放操作数的栈,求值过程顺序扫描后缀表达式,每次遇到操作数便将它压入堆栈;遇到运算符,则从栈中弹出两个操作数进行计算,然后再把结果压入堆栈。这样,等到扫描结束时,留在栈顶的整数就是所求表达式的值。四、源代码

2、#include<iostream>#include<string>#include<cmath>#include<cstring>#include<cstdlib>#define LEN sizeof(Stack)#define MaxSize 100using namespace std;struct stufloat dataMaxSize;int top;*st;structchar ch;int pri;lpri= '=',0,'(',1,'*',5,'/',5

3、,'+',3,'-',3,')',6 ,rpri= '=',0,'(',6,'*',4,'/',4,'+',2,'-',2,')',1 ;char postexpMaxSize;/后缀表达式int index=0;/用来启示录POSTEXP原素位置char exMaxSize;/表达式int count;/计算位数typedef struct stackchar chMaxSize;int top;Stack;void InitStac

4、k(Stack *&s)s=(Stack*)malloc(LEN);s->top=-1;void Free(Stack *&s)free(s);int change_into_dig(int i,int l,char e)int j,power=0;int s=0;count=1;for(j=i+1;j<l && (ej>=48 && ej<=57 );j+ ) count+;j-;/整数的个位while(j>=i)s=s+(ej-48)*pow(10,power);j-;power+;return s;void P

5、ushStack(Stack *&s,int elem)if(s->top=MaxSize-1)cout<<"full"<<endl;elses->top+;s->chs->top=elem;int compare(char ex,char op)int pri_ex,pri_op;for(int i=0;i<7;i+)if(ex=rprii.ch)pri_ex=rprii.pri;if(op=lprii.ch) pri_op=lprii.pri;if(pri_ex>pri_op)return 1;else

6、 if(pri_ex=pri_op)return 0;elsereturn -1;void Pop(Stack *&s)if(s->top=-1)cout<<"empty"<<endl;elses->top-;void tran(Stack *&op) int l,i,k,s,value;l=strlen(ex);for(i=0;i<l;i+)if(exi>=48 && exi<=57)/数字count=0;for(int j=i;exj>=48 && exj<

7、=57 && j<l;j+)postexpindex=exj; index+;count+;i=i+count-1;postexpindex+='#'else if(exi='s'|exi='c'|exi='t')count=0;postexpindex+=exi;for(k=i+3;exk>=48&&exk<=57&&k<l;k+)postexpindex+=exk;count+;i=i+count-1+3; postexpindex+='#'

8、;elseswitch(compare(exi,op->chop->top)case 1:PushStack(op,exi);break;case 0:Pop(op);break;case -1:postexpindex+=op->chop->top;op->top-;i-; while(op->chop->top!='=')postexpindex+=op->chop->top-;postexpindex='0'float compvalue()st=(struct stu*)malloc(sizeof(s

9、truct stu) );st->top=-1;int value;int i,l;l=strlen(postexp);for(i=0;i<l;i+)if(postexpi>=48&&postexpi<=57)value=change_into_dig(i,l,postexp);st->top+;st->datast->top=value;i=i+count; else if(postexpi='s'|postexpi='c'|postexpi='t')value=change_into_

10、dig(i+1,strlen(postexp),postexp);cout<<value<<endl;float tri_value;switch(exi)case 's':tri_value=sin(value);break;case 'c':tri_value=cos(value);break;case 't':tri_value=tan(value);break;cout<<tri_value<<endl;st->top+;st->datast->top=tri_value;

11、i=i+count+1;elsechar c=postexpi;int v1=st->datast->top-,v2=st->datast->top-,v;switch(c)case '+':v=v1+v2;st->data+st->top=v;break;case '-':v=v2-v1;st->data+st->top=v;break;case '*':v=v1*v2;st->data+st->top=v;break;case '/':if(v1=0)cout<<"v1 can't be zero"<<endl;return 0;v=v2/v1;st->data+st->top=v;break;return st->datast->top;int main()Stack *op;InitStack(op);op-&

温馨提示

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

评论

0/150

提交评论