1高级语言程序设计《算术表达式求值》课程设计报告2第一章系统目标算术表达式求值系统可以实现实现对算术四则混合运算表达式求值,并打印求值过程中运算符栈、操作数栈的变化过程。第二章系统分析开始运行时界面如下:你可以输入一个表达式,按E对其进行求值。3第三章系统设计开始输出菜单界面,输入表达式选C清除选E计算,求值选Q确定否是结束,退出4第四章系统实现#includestdio.h#includeconio.h#includestdlib.h#includestring.h#defineN100doublenumStack[N]={0};//操作数栈intnumTop;charopStack[N];//运算符栈intopTop;voidprint_num(doublestr1[],intn){inti;printf(\n操作数栈:\n);for(i=0;in;i++)printf(%g,str1[i]);}voidprint_op(charstr2[],intm){intj;printf(\n运算符栈:\n);for(j=0;jm;j++)printf(%c,str2[j]);}intop(charch)//判断运算符优先级{5if(ch=='+'||ch=='-')return2;if(ch=='*'||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]!='\0'||opTop0){if(str[i]='0'&&str[i]='9')num=num*10+str[i]-'0';elseif(k==1&&str[i]=='-'&&(i==0||op(str[i-1])))k=-1;else{if(i0&&!op(str[i-1])&&str[i]!='('&&str[i-1]!=')')6{numStack[numTop++]=num*k;if(opTop!=0&&numTop!=0)print_num(numStack,numTop);num=0;j=1;k=1;}if(opTop==0||str[i]=='('){opStack[opTop++]=str[i];print_op(opStack,opTop);}elseif(str[i]==')'){while(opTop0&&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]!='(')return0;}else{if(str[i]=='\0'&&numTop==0)return0;while(opTop0&&op(str[i])=op(opStack[opTop-1])){numStack[numTop-2]=result(numStack[numTop-2],opStack[--opTop],numStack[numTop-1]);if(opTop!=0&&numTop!=0)7{print_num(numStack,numTop-1);print_op(opStack,opTop);}numTop--;}if(str[i]!='\0')opStack[opTop++]=str[i];if(opTop!=0&&numTop!=0)print_op(opStack,opTop);}}if(str[i]!='\0')i++;}if(numTop!=1||opTop!=0)return0;return1;}voidmenu(){system(cls);printf(_______________________________\n);printf(Clear(C)|Equal(E)|Quit(Q)\n);printf(-------------------------------\n);}intmain(void){inti=0,j=0,k;8charstr[N]=\0;charnum[N]=\0;charsave[N]=\0;charch;doubletemp;unsignedlongtemp2;menu();printf(inputanexpression,presskey'E'tocompute\n);ch=getch();while(1){if(ch==')'||op(ch)||ch='0'&&ch='9'){str[i++]=ch;str[i]='\0';menu();printf(inputanexpression,presskey'E'tocompute\n);printf(%s,str);if(ch=='-'&&(i==1||op(str[i-2]))||ch='0'&&ch='9'){num[j++]=ch;num[j]='\0';}elsej=0;}if(ch=='C'||ch=='c'){if(strlen(str))str[--i]='\0';menu();9printf(inputanexpression,presskey'E'tocompute\n);printf(%s,str);}if(ch=='E'||ch=='e'){if(compute(str)){printf(\n=%g\n,numStack[0]);j=0;temp=numStack[0];if(temp0){temp=-temp;num[j++]='-';num[j]='\0';}temp2=(unsignedlong)temp;k=1;while(temp2/k=10)k*=10;while(k){num[j++]=temp2/k+'0';num[j]='\0';temp2=temp2%k;k/=10;}temp=temp-(int)temp;if(temp!=0){num[j++]='.';num[j]='\0';temp+=0.0000005;10}for(k=6;k0;k--){if(temp==0)break;temp*=10;num[j++]=(int)temp+'0';num[j]='\0';temp=temp-(int)temp;}}i=0;j=0;str[0]='\0';}if(ch=='Q'||ch=='q'){printf(\nareyousuretoquit?(Y/N)\n);ch=getch();if(ch=='Y'||ch=='y')break;else{menu();printf(inputanexpression,presskey'E'tocompute\n);printf(%s,str);}}ch=getch();}return0;}11第五章系统测试1.先输入:3+2*5后按E求值2.再输入:12/4-5后按E求值3.再输入Q4.输入Y,退出系统。12第六章设计心得收获:经过这次课程设计我重新回忆起许多以前学过的知识,如:栈,字符串等等。也学习到了一些新的知识,如:%g是数值输出时去掉无用的零,等等。疑问:如何进行函数运算,如:log,sin等等。第七章参考文献杨升,数据结构,厦门出版社,2009教师评语和成绩2010年7月