数据结构栈的应用—表达式求值课程设计.doc_第1页
数据结构栈的应用—表达式求值课程设计.doc_第2页
数据结构栈的应用—表达式求值课程设计.doc_第3页
数据结构栈的应用—表达式求值课程设计.doc_第4页
数据结构栈的应用—表达式求值课程设计.doc_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

数据结构课程设计报告题目:栈的应用表达式求值专业计算机科学与技术学生姓名班级b计算机学号指导教师完成日期2012年1月11日一、简介 栈是一种运算受限的线性表,仅允许在表的一端进行插入和删除。对一个栈最基本的操作包括进栈和出栈,即对栈的插入和删除。表达式求值是程序设计语言编译中的一个基本问题,它的实现是栈应用的一个典型例子。本实验的目的是设计一个表达式求值的程序。该程序必须可以接受包括(,),+,-,*,/,%和(求幂运算符)的中缀表达式,并求出结果。如果表达式正确,则输出表达式的结果;如果表达式非法,则输出错误信息。输入要求:程序从“input.txt”文件中读取信息,在这个文件中如果有许多个中缀表达式,则每个表达式独占一行,程序的读取操作在文件的结尾处停止。输出要求:对于每一个表达式,将结果放在“output.txt”文件中每一行中。这个结果可能是值,也可能是错误信息“error in infix notation”二、算法设计程序的整体算法分两步:1、 从“input。txt”文件中读取中缀表达式,并应用运算符栈opholder把中缀表达式转换成后缀表达式,将结果存放在一个temp文件中。2、 从temp文件中读取后缀表达式,并应用操作数栈operands计算后缀表达式结果将结果输出到“output.txt”文件中。为了实现算法,使用两个工作栈。一个称做opholder,用于寄存运算符;另一个称做operands,用以寄存操作数或运算结果。算法的基本思想是:首先置操作数栈为空栈,表达式起始符“#”为运算符栈的栈底元素;依次读入表达式中每个字符,若是操作数则进opnd栈,若是运算符则和optr栈的栈顶运算符比较优先权后作相应操作,直至整个表达式求值完毕。具体算法如下:operandtype evaluateexpression()initstack(operands); push(operands,#);initstack(operands);c=getchar();while(c!=#|gettop(operands)!=#)if(!in(c,op)push(operands,c);c=getchar();elseswitch(precede(gettop(operands),c)case:pop(operands,theta);pop(operands,b); pop(operands,a);push(operands),operate(a,theta,b);berakreturn gettop(opnd);三、测试与结果设计针对程序的input.txt文件,并将运行结果与期望测试结果进行比较 输入数据: 输出数据:测试项目测试实例期望测试结果基本测试3.00 3.00 1+2+3-42.00 1 + 23.00 4.99+5.99+6.99*1.0618.39 (3*5*(4+8)%2)0.00 223256.00 22.5350535.16 (2-4)3-8.00 扩展测试2.5-3.4/2+1*22.80 (2.5)*(3-2)+5.6-190%32(1+1)-19.90 1+2+36.00 1*2*36.00 (1+2)*3+4/(5+1*4)+3.2612.70 3+4-6.7+88.30 2.9*1.2+0.5-4%3/2+4(5-5)4.48 23-(5+2)/7*9-1.00 50-322+4%2-7*(3)-52.00 (2)-3)-1.00 2.526.25 2(4.4-2.4)4.00 2(4.4-3.1)2.46 (2-4)*3-8.00 2-4)(5-8)-0.13 容错测试1+2(error in infix notation2/0error in infix notation2%0error in infix notation(2-4)3.1error in infix notation2.5%2error in infix notation2%2.5error in infix notation2+3)(-5error in infix notation(2)-3)error in infix notation(2)-3)error in infix notation3.5/(1-1)error in infix notation(5.6-2)%3error in infix notation5%(3.2-2.1)error in infix notation3.0.2+1error in infix notation1+1error in infix notation1#1error in infix notation2 2error in infix notation+-+error in infix notation四、分析与探讨算术表达式有中缀表示法和后缀表示法,本程序输入的表达式采用中缀表示法。在这种表达式中,二元运算符位于两个操作数之间。因为计算机只能一个字符一个字符的扫描,要想知道那个运算符优先,就必须对整个中缀表达式扫描一遍。而后缀表达式则很容易通过应用栈实现表达式计算,这为实现表达式求值程序提供了一种直接的计算机制。后缀表达式很容易应用栈进行计算,但要处理的是中缀表达式。同样,也可以应用栈将中缀表达式转换为后缀表达式。此时,栈里要保存的是运算符,而在后缀表达式中,栈里保存的是操作数。应用栈将中缀表达式转换成后缀表达式处理过程的关键是不同运算符优先级的设置。在程序实现中,可以用一个数来代表运算符的优先级,优先级数值较大,它的优先级越高,这样优先级的比较就转换成两个数大小的比较。本程序的栈采用带头结点的链式存储结构,涉及两种类型:用于存储运算符号的char类型的链栈以及用于存储操作数的float类型的链栈。栈的操作在链式存储结构上的实现: linkstack *creatstack() /初始化栈,并将栈置空linkstack *s;s=(linkstack*)malloc(sizeof(linkstack);if(s=null)fatalerror(out of space!);s-next=null;return s;int pop(linkstack *s) /删除栈顶元素,并返回之linkstack *firscell;if(s-next=null)error(empty stack!);elseint tempfirscell=s-next;s-next=s-next-next;temp=firstcell-data;free(firstcell);return temp;void push(int x,linkstack *s) /元素x进栈linkstack *tmpcell;tmpcell=(linkstack *)malloc(sizeof(linkstack);if(tmpcell=null)fatalerror(out of space!);elsetmpcell-data=x;tmpcell-next=s-next;s-next=tmpcell;五:总结 在设计过程中出现了一个cannot convert from “void *”to“struct node *”conversion from “void *”to pointer to non_void requires an explicit cast的错误,我通过查阅资料知道了这是由于,所有分配地址要加强制类型转换,因为分配内存后缺省内存是void *.这个错误改正后,程序可以正常运行。通过这次课程设计,我对栈的特性有了清楚的认识,我也深深体会到数据结构真的并不只是书本上的一些知识,上机实践对于这门科目来说真的很重要。平常简简单单的一个运算,也许自己通过加加减减就能得出结果,可是这些算法到底是怎么实现的?平常不会想到,通过实践才发现其中集合了很多我们平时忽略的知识,所以,通过这次实践,我也把书中很多知识巩固了下,加深印象!总之,通过这次课程设计,我确实收益颇多!附录 源代码:#include #include #include int printerror = 0;typedef struct node *ptrtonode;typedef ptrtonode stack;int isempty(stack s);void makeempty(stack s);void push(char x,stack s);char top(stack s);void pop(stack s);typedef struct nodechar element;ptrtonode next;typedef struct fnode *ptr_fn;typedef ptr_fn fstack;int fisempty(fstack s);void fpush(float x,fstack s);float ftop(fstack s);void fpop(fstack s);typedef struct fnodefloat element;ptr_fn next;void converttopost(file *in, stack whereat,file *temp);void reverse(stack rev);void calculate(file *change, stack whereat,file *temp);int main()file *inputfile, *outputfile,*temp;/*初始化变量*/stack whereat;char sample;inputfile = fopen(input.txt,r);/*打开文件*/outputfile = fopen(output.txt,w);whereat = malloc(sizeof(struct node);whereat-next = null;if (!inputfile | !outputfile) printf(intput or output file(s) do not exist.n);return(1);sample = getc(inputfile); while ( sample != eof)temp = fopen(temp.txt,w+); /*生成temp文件*/ungetc(sample,inputfile); converttopost(inputfile,whereat,temp); /*中缀变后缀*/if (printerror) /*错误处理*/fprintf(outputfile,error in infix notation.); fscanf(inputfile,n,&sample);printerror = 0;else if (isempty(whereat) = 1) else if (isempty(whereat) != 1)reverse(whereat);if (top(whereat) = b) /*错误处理,*/ printerror = 1; fclose(temp);temp = fopen(temp.txt,r+);calculate(outputfile, whereat,temp);/*计算结果*/fclose(temp);makeempty(whereat);putc(n,outputfile);/* 在输出文件中换行*/sample = getc(inputfile); /* while循环结束*/free(whereat); fclose(inputfile);fclose(outputfile);remove(temp.txt); /* 删除temp.txt*/return 1;int isempty(stack s)return(s-next=null);int fisempty(fstack s)return(s-next=null);void pop(stack s)ptrtonode firstcell;if (isempty(s)perror(empty stack);elsefirstcell = s-next;s-next = s-next-next;free(firstcell);void fpop(fstack s)ptr_fn firstcell;if (fisempty(s)perror(empty stack);elsefirstcell = s-next;s-next = s-next-next;free(firstcell);void makeempty(stack s)if (s = null)perror(must use createstack first);elsewhile (!isempty(s)pop(s);void fmakeempty(fstack s)if (s = null)perror(must use createstack first);elsewhile (!isempty(s)pop(s);void push(char x, stack s)ptrtonode tmpcell;tmpcell = (ptrtonode)malloc(sizeof(struct node);if (tmpcell = null)perror(out of space!);elsetmpcell-element = x;tmpcell-next = s-next;s-next = tmpcell;void fpush(float x, fstack s)ptr_fn tmpcell;tmpcell = (ptr_fn)malloc(sizeof(struct fnode);if (tmpcell = null)perror(out of space!);elsetmpcell-element = x;tmpcell-next = s-next;s-next = tmpcell;char top(stack s)if (!isempty(s)return s-next-element;perror(empty stack);exit(1);return 0;float ftop(fstack s)if (!fisempty(s)return s-next-element;perror(empty stack);exit(1);return 0;void reverse(stack rev)stack tempstack;tempstack = malloc(sizeof(struct node);tempstack-next = null;while (!isempty(rev)push(top(rev),tempstack); /*将元素压栈到一个临时堆栈*/pop(rev);rev-next = tempstack-next; /*指向新的堆栈*/void converttopost(file *in, stack whereat, file *temp)stack opholder;char holder;char lastseen;int digitcounter = 0; /*操作数的计数器*/opholder = malloc(sizeof(struct node); /*初始化*/opholder-next = null;holder=getc(in);lastseen = ; /*用来防止输入格式错误putc( ,temp); while (holder !=n) & (holder != eof)if (holder = )digitcounter = 0;else if ( isoperator(holder) = -1) printerror = 1;else if (isoperator(holder)=0)if (lastseen = holder) & (lastseen = .) /*错误处理*/printerror = 1;elselastseen = holder;if (digitcounter = 0)push(a,whereat); /*进栈*/digitcounter+;/*计数器加一*/putc( ,temp);putc(holder,temp);elsedigitcounter = 0;if (lastseen = holder) & (lastseen != () & (lastseen != ) printerror = 1;elselastseen = holder;if(isempty(opholder)=1) /*当opholder为空*/push(holder,opholder);else if(operatorvalue(top(opholder) = 6) if(operatorvalue(holder)=5)pop(opholder);elsepush(holder,opholder);else if(operatorvalue(holder) = 6) push(holder,opholder);else if(operatorvalue(holder) = 5) while (isempty(opholder) != 1) & (operatorvalue(top(opholder) != 6) putc( ,temp);push(b,whereat);putc(top(opholder),temp);pop(opholder);if (isempty(opholder) = 1) printerror = 1;else pop(opholder);else if(operatorvalue(holder) = operatorvalue(top(opholder) & (operatorvalue(holder) = 3) /*幂运算情况*/push(holder,opholder);else if(operatorvalue(holder) = operatorvalue(holder)while(isempty(opholder) != 1) & (operatorvalue(top(opholder) = operatorvalue(holder) & (operatorvalue(top(opholder)!=6)putc( ,temp);putc(top(opholder),temp);push(b,whereat);pop(opholder);push(holder,opholder);else if(operatorvalue(top(opholder) next= null;while (isempty(whereat) != 1) & printerror != 1) if (top(whereat) = a)fscanf(temp, ,&spacefinder);fscanf(temp,%f,&looker); /*如果是a,则是操作数*/fpush(looker,operands);pop(whereat);else if (top(whereat) = b)fscanf(temp, ,&spacefinder); /*如果是b,则是运算符*/op = getc(temp);switch(op) /* 判断是什么运算符*/case(): /*幂运算*/numb = ftop(operands);fpop(operands);if (fisempty(operands) /*错误处理*/printerror = 1;elsenuma = ftop(operands);fpop(operands);if (numa = 0 & numb 0)|(numa0) & (numb - (int)numb != 0)printerror = 1;elseanswer = pow(numa,numb);fpush(answer,operands);break;case %: /*取模运算*/numb = ftop(operands);fpop(operands);if (fisempty(operands)/*错误处理*/printerror = 1;elsenuma = ftop(operands);fpop(operands);if (numa - (int)numa != 0) | (numb - (int)numb != 0) | (numb = 0)printerror = 1;elseanswer = (int)numa % (int)numb; fpush(answer,operands);break;case *: numb = ftop(operands);fpop(operands);if (fisempty(operands)printerror = 1;elsenuma = ftop(operands);fpop(operands);answer = numa * numb; fpush(answer,operands);break;case /: numb = ftop(operands);fpop(operands);if (fisempty(operands)printerror = 1;elsenuma = ftop(operands);fpop(oper

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论