数据结构课程设计由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“数据结构课程设计全”。
一,课程题目
(算符优先法计算算数表达式)以字符序列的形式从终端输入语法正确的、不含变量的整数表达式。利用教材表3.1(P53)给出的算符优先关系,实现对于算术四则混合运算(加、减、乘、除)表达式的求值。例如:7+(4-2)*3+12/2=19。注:按照四舍五入的方式将四则运算结果取整。
二,程序设计思想
在程序中分别设立一个运算符栈(OPTR 栈),用于存储‘+’,‘-’,‘*’,‘/’,‘#’(‘#’用于判断算术表达式结束),和一个操作数栈(OPND 栈),用于存放整数,输入算式后,先将数字与运算符分开入i栈,若为数字则先用转换函数将char类型的数转换为int型并进入操作数栈,若为运算符则根据教材表3.1(P53)给出的算符优先级关系,判断栈顶运算符和从键盘取得的运算符作优先级比较,若取得的运算符优先级高则进栈,直到取得运算符优先级低的,则将操作数取出作operate运算后存入栈顶,反复操作知道遇到‘#’,则结束运算,输出栈顶元素即为结果。三,程序流程图
四,程序关键代码设计
本次程序设计共调用了12个方法分别是:
InitNumStack,ParseInt,PushNum,PopNum,InitCalStack,PopCal,PushCal,In,GetTopCal,GetTopNum,Preced,Operate。其中
ParseInt方法
int ParseInt(char c[]){ int number[5],i;for(i=0;i
number[i]=(int)(c[i])-48;} i=10000*number[0]+1000*number[1]+100*number[2]+10 *number[3]+number[4];return i;} 为将输入的数字字符型转换为整型的转换函数,通过Ascall表的转换关系,将输入的字符型数字转换为整型。在入栈前进行此方法,以便整型数的计算。Preced,Operate函数
char Preced(char a , char b){ char c[7]={'+','-','*','/','(',')','#'};char d[7][7]={ {'>','>','','>'}, {'>','>','','>'}, {'>','>','>','>','','>'}, {'>','>','>','>','','>'}, {'','>','>','>',' ','>','>'}, {'
c=a+b;return c;} if(theta=='-'){
c=a-b;return c;} if(theta=='*'){
c=a*b;return c;} if(theta=='/'){
c=a/b;return c;} return 0;} 其中preced是判定运算符栈的栈顶运算符C1与读入的运算符C2之间优先关系函数;Opearte为进行二元运算aCb的函数,如果是编译表达式,则产生这个运算的一组相应的指令并返回存放结果的中间变量名;如果是解释执行表达式,则直接进行该运算,并返回运算结果。五,程序源代码以及运行结果
#include #include #include typedef struct{ int *base;int *top;int Stacksize;}SqlNum;void InitNumStack(SqlNum &S){ S.base=(int *)malloc(100*sizeof(int));S.top=S.base;S.Stacksize=100;} int ParseInt(char c[]){ int number[5],i;for(i=0;i
number[i]=(int)(c[i])-48;} i=10000*number[0]+1000*number[1]+100*number[2]+10*number[3]+number[4];return i;} void PushNum(SqlNum &S,int c){ *S.top=c;S.top++;} int PopNum(SqlNum &S){ int c;S.top--;c=*S.top;return c;} typedef struct{ char *base;char *top;int Stacksize;}SqlCal;void InitCalStack(SqlCal &S){ S.base=(char *)malloc(100*sizeof(char));S.top=S.base;S.Stacksize=100;} void PushCal(SqlCal &S,char c){ *S.top=c;S.top++;} char PopCal(SqlCal &S){ char c;S.top--;c=*S.top;return c;}
int In(char c,char s[]){ int i;for(i=0;i
if(c==s[i])
return 1;
return 0;}
char GetTopCal(SqlCal &s){ char c;c=*(s.top-1);return c;} int GetTopNum(SqlNum &s){ int c;c=*(s.top-1);return c;} char Preced(char a , char b){ char c[7]={'+','-','*','/','(',')','#'};char d[7][7]={ {'>','>','','>'}, {'>','>','','>'}, {'>','>','>','>','','>'}, {'>','>','>','>','','>'}, {'','>','>','>',' ','>','>'}, {'
if(b=='+'){
return d[0][0];}
if(b=='-'){
return d[0][1];}
if(b=='*'){
return d[0][2];}
if(b=='/'){
return d[0][3];}
if(b=='('){
return d[0][4];}
if(b==')'){
return d[0][5];}
if(b=='#'){
return d[0][6];} } if(a=='-'){
if(b=='+'){
return d[1][0];}
if(b=='-'){
return d[1][1];}
if(b=='*'){
return d[1][2];}
if(b=='/'){
return d[1][3];}
if(b=='('){
return d[1][4];}
if(b==')'){
return d[1][5];}
if(b=='#'){
return d[1][6];} } if(a=='*'){
if(b=='+'){
return d[2][0];}
if(b=='-'){
return d[2][1];}
if(b=='*'){
return d[2][2];}
if(b=='/'){
return d[2][3];}
if(b=='('){
return d[2][4];}
if(b==')'){
return d[2][5];}
if(b=='#'){
return d[2][6];} } if(a=='/'){
if(b=='+'){
return d[3][0];}
if(b=='-'){
return d[3][1];}
if(b=='*'){
return d[3][2];}
if(b=='/'){
return d[3][3];}
if(b=='('){
return d[3][4];}
if(b==')'){
return d[3][5];}
if(b=='#'){
return d[3][6];} } if(a=='('){
if(b=='+'){
return d[4][0];}
if(b=='-'){
return d[4][1];}
if(b=='*'){
return d[4][2];}
if(b=='/'){
return d[4][3];}
if(b=='('){
return d[4][4];}
if(b==')'){
return d[4][5];}
if(b=='#'){
return d[4][6];} } if(a==')'){
if(b=='+'){
return d[5][0];}
if(b=='-'){
return d[5][1];}
if(b=='*'){
return d[5][2];}
if(b=='/'){
return d[5][3];}
if(b=='('){
return d[5][4];}
if(b==')'){
return d[5][5];}
if(b=='#'){
return d[5][6];} } if(a=='#'){
if(b=='+'){
return d[6][0];}
if(b=='-'){
return d[6][1];}
if(b=='*'){
return d[6][2];}
if(b=='/'){
return d[6][3];}
if(b=='('){
return d[6][4];}
if(b==')'){
return d[6][5];}
if(b=='#'){
return d[6][6];} } return 0;} int Operate(int a,char theta,int b){ int c;if(theta=='+'){
c=a+b;return c;} if(theta=='-'){
c=a-b;return c;} if(theta=='*'){
c=a*b;return c;} if(theta=='/'){
c=a/b;return c;} return 0;} void main(){ SqlCal OPTR;SqlNum OPND;char c,d[5]={'0','0','0','0','0'};int f=0;char op[]={'+','-','*','/','(',')','#'};InitCalStack(OPTR);InitNumStack(OPND);printf(“请输入算式并在尾部添加一个#号n”);c=getchar();PushCal(OPTR,'#');while(c!='#'||GetTopCal(OPTR)!='#'){ if(!In(c,op))
{
d[0]=d[1];
d[1]=d[2];
d[2]=d[3];
d[3]=d[4];
d[4]=c;
c=getchar();f=1;
}
else
{
if(f==1){
PushNum(OPND,ParseInt(d));
d[0]='0';d[1]='0';d[2]='0';d[3]='0';d[4]='0';
f=0;
}
switch(Preced(GetTopCal(OPTR),c))
{
case'
PushCal(OPTR,c);
c=getchar();
break;
case'=':
PopCal(OPTR);
c=getchar();
break;
case'>':
char theta;int a;int b;
theta=PopCal(OPTR);
b=PopNum(OPND);
a=PopNum(OPND);
PushNum(OPND,Operate(a,theta,b));
break;
}
} } printf(“%dn”,GetTopNum(OPND));}
程序运行结果: 六,心得体会
通过这次编程,我发现很多编程过程中的不足与问题,很多问题由于考虑不全面,导致程序运行失败。还有一些小问题,比如字母的大小写,括号的遗漏,语法书写错误等等一些基础错误,也是让我体会很深写程序要谨慎仔细。