实验报告2.docx

上传人:b****9 文档编号:26201291 上传时间:2023-06-17 格式:DOCX 页数:15 大小:217.01KB
下载 相关 举报
实验报告2.docx_第1页
第1页 / 共15页
实验报告2.docx_第2页
第2页 / 共15页
实验报告2.docx_第3页
第3页 / 共15页
实验报告2.docx_第4页
第4页 / 共15页
实验报告2.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

实验报告2.docx

《实验报告2.docx》由会员分享,可在线阅读,更多相关《实验报告2.docx(15页珍藏版)》请在冰豆网上搜索。

实验报告2.docx

实验报告2

实验报告2

1、需求分析:

说明课题设计的任务;

课题任务是编写一个简易计算器,实现简单计算器的功能,请按照四则运算加、减、乘、除、幂(^)和括号的优先关系和惯例,编写计算器程序。

对于正确的计算表达式应输出结果,对于不正确的表达式应当进行报错,对计算过程中的错误,也应给与提示。

2、概要设计:

说明课题中用到的抽象数据类型的定义、主程序的流程以及各程序模块之间的调用关系;

为实现上述程序功能,应使用两个栈,分别寄存操作数与运算符。

为此,需要栈的抽象数据结构。

程序计算的过程中,每次读入一个元素(数据或算符),每当程序读入的新算符,优先级低于算符栈的栈顶元素,则弹出栈顶元素与数据栈的栈顶的两个元素进行运算,并将结果压入数据栈。

重复进行知道算式运算完毕。

(1)、栈的抽象数据类型定义为:

ADTStack{

数据对象:

D=

数据关系:

R1=

约定端为栈顶,端为栈底。

基本操作:

InitStack(&S)

操作结果:

创建一个空栈S。

GetTop(S,&e)

初始条件:

栈S已存在且非空。

操作结果:

用e返回S的栈顶元素。

Push(&S,e)

初始条件:

栈S已存在。

操作结果:

插入元素e为新的栈顶元素。

Pop(&S,&e)

初始条件:

栈S已存在且非空。

操作结果:

删除S的栈顶元素,并用e返回其值。

In(m,a[])

操作结果:

若m是运算符,返回TRUE。

Precede(m,n)

初始条件:

m,n为运算符。

操作结果:

若m优先级大于n,返回>,反之亦然。

Operation(a,theta,b)

初始条件:

a,b为整数,theta为运算符。

操作结果:

返回a与b运算的结果。

EvaluateExpression(p[])

初始条件:

输入合法的表达式。

操作结果:

返回表达式的值。

}ADTStack

(2)、宏定义

#defineSTACK_INIT_SIZE100

#defineSTACKINCREMENT10

#defineOVERFLOW-2

#defineOK1

#defineERROR0

#defineTRUE1

#defineFALSE0

(3)、主程序流程

首先定义char型数组,将输入的表达式存入。

随后调用EvaluateExpression(expression)函数计算结果,最后输出在屏幕上。

(4)、模块调用关系:

由主函数模块调用输入模块与求值模块。

求值模块调用表达式转化模块与表达式求职模块,计算并返回表达式的值。

最后主程序调用输出模块输出结果。

3、详细设计:

实现程序模块的具体算法;

算法包括输入检查部分和运算部分。

算法检查部分再分为两部分:

1.非法字符检查:

检查算式中是否有非法字符,如有,报错退出。

2.括号匹配检查:

括号匹配检查运用栈,每次读入若为’(‘,则将其加入栈,若读到’)’则令栈顶’(‘出栈,运行结束后应当栈为空。

若栈非空,或运算过程中出现读到’)’而栈顶为’)’均报错退出。

运算部分:

1.符号优先级函数:

定义符号的优先级,同级则为算符栈顶优先级高。

2.符号运算函数:

定义算符作用,输入两个数据与算符进行运算。

3.运算主函数:

主函数调用上边两个函数,并持续进行一下操作。

读入元素,若为数

据元素,则存入数据栈;若为算符元素,则判断:

当前算符元素与当

前算符栈栈顶元素优先级比较,若栈顶元素优先级高,则弹出栈顶元

素,并弹出数据栈的两个栈顶元素,并使这两个元素进行计算,并放

回数据栈栈顶。

直至运算结束。

运算结束后两个栈均应为空栈,若不

是这样,则证明输入有误。

4、调试分析:

调试过程中遇到的问题及解决方法;经验和体会等;

调试过程中结束后,我对大量的输入进行了尝试,期间不断的发现错误,并不断地修正。

并且修正了,除以0的情况应为NaN。

程序也越来越长,主题程序其实并不复杂,但是对于本题这种有较多细节的问题,再不断地调试过程中,代码长度增加了很多。

5、测试结果:

列出测试结果,包括输入和输出;

6、附录:

带注释的源程序。

#include

#include

#include

#include

#include

usingnamespacestd;

doubleD_Operate(doublex,charop,doubley)

{

doublea;

switch(op)

{

case'+':

a=x+y;break;

case'-':

a=x-y;break;

case'*':

a=x*y;break;

case'/':

a=x/y;break;

case'^':

a=pow(x,y);break;

}

returna;

}

 

charPrecede(charop1,charop2)

{

if(((op1=='+'||op1=='-')&&(op2=='+'||op2=='-'||op2==')'||op2=='='))||\

((op1=='*'||op1=='/')&&(op2=='+'||op2=='-'||op2=='*'||op2=='/'||op2==')'||op2=='='))\

||(op1=='^'&&(op2=='+'||op2=='-'||op2=='*'||op2=='/'||op2==')'||op2=='=')))

return'>';

if((op1=='('&&op2==')')||(op1=='='&&op2=='='))

return'=';

else

return'<';

}

intillegal_char(strings,inti)

{

intj=0;

while(j

{

if(s[j]>='0'&&s[j]<='9')

j++;

elseif(s[j]=='+'||s[j]=='-'||s[j]=='*'||s[j]=='/'||s[j]=='.'||s[j]=='('||s[j]==')'||s[j]=='^')

j++;

else

{

cout<<"WrongInput!

"<

return0;

}

}

return1;

}

intmatch(strings)

{

inti=0,top=0;

charstack[50];

while(s[i]!

='\0')

{

if(s[i]=='(')

{

stack[top]=s[i];

top++;

}

if(s[i]==')')

if(stack[top-1]=='(')

{

inta=i+1;

stack[top-1]=NULL;

top--;

}

else

{

cout<<"WrongInput!

"<

return0;

}

i++;

}

if(top!

=0)

{

cout<<"WrongInput!

"<

return0;

}

return1;

}

classNUMstack

{

public:

doublenum[1000];

inttop;

voidstart()

{

for(inti=0;i<1000;i++)

num[i]=0;

top=0;

}

voidpush(chara)

{

num[top]=num[top]*10+(a-'0');

}

doublepop()

{

top--;

doublenumber=num[top];

num[top]=0;

returnnumber;

}

doublegetTop()

{returnnum[top-1];}

voidlift()

{top++;}

};

classOPERstack

{

public:

charoper[1000];

inttop;

voidstart()

{

oper[0]='=';

for(inti=1;i<1000;i++)

oper[i]=NULL;

top=1;

}

voidpush(chara)

{

oper[top]=a;

top++;

}

charpop()

{

top--;

charop=oper[top];

oper[top]=NULL;

returnop;

}

chargetTop()

{

returnoper[top-1];

}

};

voidcalculate(stringequation)

{

NUMstacknumber;

OPERstackoper;

number.start();

oper.start();

inti=0,len=0,k;

charp,sig;

doubleyuan1,yuan2;

while(equation[i]!

='\0')

{

len++;

i++;

}

if(equation[len-1]!

='=')

{

cout<<"WrongInput!

"<

return;

}

intle;

le=illegal_char(equation,len-1);

if(le==0)

return;

le=match(equation);

if(le==0)

return;

for(i=0;i

{

if(equation[i]=='!

')

{

yuan1=number.pop();

if(yuan1==0)

{

number.num[number.top]=0;

number.lift();

}

else

{

number.num[number.top]=1;

for(k=1;k<=yuan1;k++)

number.num[number.top]=k*number.num[number.top];

number.lift();

}

}

elseif(equation[i]>='0'&&equation[i]<='9')

{

number.push(equation[i]);

if((equation[i+1]<'0'||equation[i+1]>'9')&&equation[i+1]!

='.')

number.lift();

}

elseif(equation[i]=='.')

{

intx=1;

while(equation[i+x]>='0'&&equation[i+x]<='9')

{

number.num[number.top]+=((equation[i+x]-'0')/pow(10,x));

x++;

}

x--;

number.lift();

i=i+x;

}

elseif(equation[i]=='(')

{

oper.push(equation[i]);

}

else

{

if(oper.top==1)oper.push(equation[i]);

else

{

chartemp1=oper.getTop();

chartemp2;

temp2=equation[i];

p=Precede(temp1,temp2);

if(p=='<')

oper.push(temp2);

if(p=='>'||p=='=')

{

charrep=p;

while((rep=='>'||p=='=')&&(oper.top-1>0))

{

sig=oper.pop();

yuan1=number.pop();

yuan2=number.getTop();

if(sig=='/'&&yuan1==0)

{

cout<<"NaN"<

return;

}

if(sig=='^'&&yuan2<0&&yuan1>0&&yuan1<1&&(static_cast(1/yuan1))%2==0)

{

cout<<"NaN"<

return;

}

{

number.num[(number.top)-1]=D_Operate(yuan2,sig,yuan1);

temp1=oper.getTop();

rep=Precede(temp1,temp2);

}

}

if(equation[i]==')')

oper.pop();

else

oper.push(equation[i]);

}

}

}

}

if(number.num[0]==ceil(number.num[0]))

cout<

else

{cout<

}

intmain()

{

stringequation;

cin>>equation;

calculate(equation);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 生产经营管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1