欢迎来到天天文库
浏览记录
ID:23470989
大小:133.69 KB
页数:12页
时间:2018-11-08
《数据结构课程设计算术表达式求值-计算器》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、高级语言程序设计《算术表达式求值》课程设计报告12第一章系统目标算术表达式求值系统可以实现实现对算术四则混合运算表达式求值,并打印求值过程中运算符栈、操作数栈的变化过程。第二章系统分析开始运行时界面如下:你可以输入一个表达式,按E对其进行求值。12第三章系统设计开始输出菜单界面,输入表达式选C清除选E计算,求值选Q确定否是结束,退出第四章系统实现12#include#include#include#include#defineN100doublenumStack[N]={0};//操作数栈intnu
2、mTop;charopStack[N];//运算符栈intopTop;voidprint_num(doublestr1[],intn){inti;printf("操作数栈:");for(i=0;i3、4、ch=='-')return2;12if(ch=='*'5、6、7、ch=='/')return3;if(ch=='(')return-1;return0;}doubleresult(doublenum1,charop,doublenum2)//计算{if(op=='+')returnnum1+num2;if(op=='-')returnnum1-num2;if(op=='*')returnnum1*num2;if(op=='/')returnnum1/num2;return0;}intcompute(charstr[]){doublenum=0;inti=0,j=1,k=1;numTop=opTop=0;while(str[i]!=' 8、'9、10、opTop>0){if(str[i]>='0'&&str[i]<='9')num=num*10+str[i]-'0';elseif(k==1&&str[i]=='-'&&(i==011、12、op(str[i-1])))k=-1;else{if(i>0&&!op(str[i-1])&&str[i]!='('&&str[i-1]!=')'){12numStack[numTop++]=num*k;if(opTop!=0&&numTop!=0)print_num(numStack,numTop);num=0;j=1;k=1;}if(opTop==013、14、str[i]=='('){op15、Stack[opTop++]=str[i];print_op(opStack,opTop);}elseif(str[i]==')'){while(opTop>0&&opStack[--opTop]!='('){numStack[numTop-2]=result(numStack[numTop-2],opStack[opTop],numStack[numTop-1]);if(opTop!=0&&numTop!=0){print_num(numStack,numTop);print_op(opStack,opTop);}numTop--;}if(opStack[opTop]!=16、'(')return0;}else{if(str[i]==' '&&numTop==0)return0;while(opTop>0&&op(str[i])<=op(opStack[opTop-1])){numStack[numTop-2]=result(numStack[numTop-2],opStack[--opTop],numStack[numTop-1]);if(opTop!=0&&numTop!=0){12print_num(numStack,numTop-1);print_op(opStack,opTop);}numTop--;}if(str[i]!=' ')17、opStack[opTop++]=str[i];if(opTop!=0&&numTop!=0)print_op(opStack,opTop);}}if(str[i]!=' ')i++;}if(numTop!=118、19、opTop!=0)return0;return1;}voidmenu(){system("cls");printf("_______________________________");printf("Clear(C)20、Equal(E)21、Quit(Q)");printf("-----------
3、
4、ch=='-')return2;12if(ch=='*'
5、
6、
7、ch=='/')return3;if(ch=='(')return-1;return0;}doubleresult(doublenum1,charop,doublenum2)//计算{if(op=='+')returnnum1+num2;if(op=='-')returnnum1-num2;if(op=='*')returnnum1*num2;if(op=='/')returnnum1/num2;return0;}intcompute(charstr[]){doublenum=0;inti=0,j=1,k=1;numTop=opTop=0;while(str[i]!='
8、'
9、
10、opTop>0){if(str[i]>='0'&&str[i]<='9')num=num*10+str[i]-'0';elseif(k==1&&str[i]=='-'&&(i==0
11、
12、op(str[i-1])))k=-1;else{if(i>0&&!op(str[i-1])&&str[i]!='('&&str[i-1]!=')'){12numStack[numTop++]=num*k;if(opTop!=0&&numTop!=0)print_num(numStack,numTop);num=0;j=1;k=1;}if(opTop==0
13、
14、str[i]=='('){op
15、Stack[opTop++]=str[i];print_op(opStack,opTop);}elseif(str[i]==')'){while(opTop>0&&opStack[--opTop]!='('){numStack[numTop-2]=result(numStack[numTop-2],opStack[opTop],numStack[numTop-1]);if(opTop!=0&&numTop!=0){print_num(numStack,numTop);print_op(opStack,opTop);}numTop--;}if(opStack[opTop]!=
16、'(')return0;}else{if(str[i]==' '&&numTop==0)return0;while(opTop>0&&op(str[i])<=op(opStack[opTop-1])){numStack[numTop-2]=result(numStack[numTop-2],opStack[--opTop],numStack[numTop-1]);if(opTop!=0&&numTop!=0){12print_num(numStack,numTop-1);print_op(opStack,opTop);}numTop--;}if(str[i]!=' ')
17、opStack[opTop++]=str[i];if(opTop!=0&&numTop!=0)print_op(opStack,opTop);}}if(str[i]!=' ')i++;}if(numTop!=1
18、
19、opTop!=0)return0;return1;}voidmenu(){system("cls");printf("_______________________________");printf("Clear(C)
20、Equal(E)
21、Quit(Q)");printf("-----------
此文档下载收益归作者所有