您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 数据结构算术表达式求值实验报告
北京理工大学珠海学院《数据结构》课程设计报告题目:____________算术表达式求值_________________所在学院:专业班级:学生姓名:指导教师:2010年05月26日目录1.前言·································································································12.概要设计·································································································12.1数据结构设计···············································································································12.2算法设计····················································································································12.3ADT描述···················································································································22.4功能模块分析··············································································································23.详细设计·································································································33.1数据存储结构设计·········································································································33.2主要算法流程图(或算法伪代码)·····················································································34.软件测试·································································································65.心得体会·································································································8参考文献·····································································································8附录··········································································································8北京理工大学珠海学院计算机科学技术学院第1页1.前言在计算机中,算术表达式由常量、变量、运算符和括号组成。由于不同的运算符具有不同的优先级,又要考虑括号,因此,算术表达式的求值不可能严格地从左到右进行。因而在程序设计时,借助栈实现。算法输入:一个算术表达式,由常量、变量、运算符和括号组成(以字符串形式输入)。为简化,规定操作数只能为正整数,操作符为+、-*、/,用#表示结束。算法输出:表达式运算结果。算法要点:设置运算符栈和运算数栈辅助分析算符优先关系。在读入表达式的字符序列的同时,完成运算符和运算数的识别处理,以及相应运算。2.概要设计2.1数据结构设计任何一个表达式都是由操作符,运算符和界限符组成的。我们分别用顺序栈来寄存表达式的操作数和运算符。栈是限定于紧仅在表尾进行插入或删除操作的线性表。顺序栈的存储结构是利用一组连续的存储单元依次存放自栈底到栈顶的数据元素,同时附设指针top指示栈顶元素在顺序栈中的位置,base为栈底指针,在顺序栈中,它始终指向栈底,即top=base可作为栈空的标记,每当插入新的栈顶元素时,指针top增1,删除栈顶元素时,指针top减1。2.2算法设计为了实现算符优先算法。可以使用两个工作栈。一个称为OPTR,用以寄存运算符,另一个称做OPND,用以寄存操作数或运算结果。1.首先置操作数栈为空栈,表达式起始符”#”为运算符栈的栈底元素;2.依次读入表达式,若是操作符即进OPND栈,若是运算符则和OPTR栈的栈顶运算符比较优先权后作相应的操作,直至整个表达式求值完毕(即OPTR栈的栈顶元素和当前读入的字符均为”#”)。数据结构课程设计第2页2.3ADT描述ADTStack{数据对象:D={ia|ia∈ElemSet,i=1,2,…,n,n≧0}数据对象:R1={1,iiaa|1ia,Dai,i=2,…,n}约定na端为栈顶,ia端为栈底。基本操作:InitStack(&S)操作结果:构造一个空栈S。GetTop(S)初始条件:栈S已存在。操作结果:用P返回S的栈顶元素。Push(&S,ch)初始条件:栈S已存在。操作结果:插入元素ch为新的栈顶元素。Pop(&S)初始条件:栈S已存在。操作结果:删除S的栈顶元素。In(ch)操作结果:判断字符是否是运算符,运算符即返回1。Precede(c1,c2)初始条件:c1,c2为运算符。操作结果:判断运算符优先权,返回优先权高的。Operate(a,op,b)初始条件:a,b为整数,op为运算符。操作结果:a与b进行运算,op为运算符,返回其值。num(n)操作结果:返回操作数的长度。EvalExpr()初始条件:输入表达式合法。操作结果:返回表达式的最终结果。}ADTStack2.4功能模块分析1.栈的基本功能。InitStack(Stack*s)和InitStack2(Stack2*s)分别构造运算符栈与构造操作数栈,Push(Stack*s,charch)运算符栈插入ch为新的栈顶元素,Push2(Stack2*s,intch)操作数栈插入ch为新的栈顶元素,Pop(Stack*s)删除运算符栈s的栈顶元素,用p返回其值,Pop2(Stack2*s)删除操作数栈s的栈顶元素,北京理工大学珠海学院计算机科学技术学院第3页用p返回其值,GetTop(Stacks)用p返回运算符栈s的栈顶元素,GetTop2(Stack2s)用p返回操作数栈s的栈顶元素。2.其它功能分析。(1)In(charch)判断字符是否是运算符功能,运算符即返回1,该功能只需简单的一条语句即可实现,return(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')'||ch=='#')。(2)Precede(charc1,charc2)判断运算符优先权功能,该函数判断运算符c1,c2的优先权,具体优先关系参照表1。(3)Operate(inta,charop,intb)操作数用对应的运算符进行运算功能。运算结果直接返回。(4)num(intn)求操作数的长度功能,需要用itoa函数把int型转换成字符串型,strlen函数可求字符长度。(5)EvalExpr()主要操作函数运算功能。分析详细见“3.详细设计…3.2”。3.详细设计3.1数据存储结构设计因为表达式是由操作符,运算符和界限符组成的。如果只用一个char类型栈,不能满足2位以上的整数,所以还需要定义一个int类型的栈用来寄存操作数。/*定义字符类型栈*/typedefstruct{intstacksize;char*base;char*top;}Stack;/*定义整型栈*/typedefstruct{intstacksize;int*base;int*top;}Stack2;3.2主要算法流程图(或算法伪代码)1.Precede(charc1,charc2)判断运算符优先权,返回优先权高的。算符间的优先关系如下:数据结构课程设计第4页+-*/()#+-*/(=)#=表1算法伪代码如下:charPrecede(charc1,charc2){staticchararray[49]={'','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','=','!','','','','','!','','','','','','','','!','='};//用一维数组存储49种情况switch(c1){/*i为下面array的横标*/case'+':i=0;break;case'-':i=1;break;case'*':i=2;break;case'/':i=3;break;case'(':i=4;break;case')':i=5;break;case'#':i=6;break;}switch(c2){/*j为下面array的纵标*/case'+':j=0;break;case'-':j=1;break;case'*':j=2;break;case'/':j=3;break;case'(':j=4;break;case')':j=5;break;case'#':j=6;break;}return(array[7*i+j]);/*返回运算符array[7*i+j]为对应的c1,c2优先关系*/}北京理工大学珠海学院计算机科学技术学院第5页2.intEvalExpr()主要操作函数。算法概要流程图:利用该算法对算术表达式3*(7-2)求值操作过程如下:步骤OPTR栈OPND栈输入字符主要操作1#3*(7-2)#Push(OPND,’3’)2#3*(7-2)#Push(OPTR,’*’)3#*3(7-2)#Push(OPNR,’(’)4#*(37-2)#Push(OPND,’7’)5#*(37-2)#Push(OPNR,’-’)6#*(-372)#Push(OPND,’2’)7#*(-372)#Operate(‘7’,’-’,’2’)8#*(35)#Pop(OPTR)9#*35#Operate(‘3’,’*’,5’)10#15#Return(GetTop2(OPND))表2算法伪代码如下:intEvalExpr()//主要操作函数{c=*ptr++;while(c!='#'||GetTop(OPTR)!='#'){if(!In(c))//不是运算符即进栈{if(!In(*(ptr-1)))ptr=ptr-1;数据结构课程设计第6页m=atoi(ptr);//取字符串前面的数字段n=num(m);Push2(&OPND,m);ptr=ptr+n;c=*ptr++;}elseswitch(Precede(GetTop(OPTR),c)){case''://栈顶元素优先权底Push(&OPTR,c);c=*ptr++;break;case'='://脱括号并接收下一字符x=Pop(&OPTR);c=*ptr++;break;case'':
本文标题:数据结构算术表达式求值实验报告
链接地址:https://www.777doc.com/doc-6371081 .html