版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、数据结构课程设计报告题目:栈的应用表达式求值专业计算机科学与技术学生姓名班级b计算机学号指导教师完成日期2012年1月11日一、简介 栈是一种运算受限的线性表,仅允许在表的一端进行插入和删除。对一个栈最基本的操作包括进栈和出栈,即对栈的插入和删除。表达式求值是程序设计语言编译中的一个基本问题,它的实现是栈应用的一个典型例子。本实验的目的是设计一个表达式求值的程序。该程序必须可以接受包括(,),+,-,*,/,%和(求幂运算符)的中缀表达式,并求出结果。如果表达式正确,则输出表达式的结果;如果表达式非法,则输出错误信息。输入要求:程序从“input.txt”文件中读取信息,在这个文件中如果有许多
2、个中缀表达式,则每个表达式独占一行,程序的读取操作在文件的结尾处停止。输出要求:对于每一个表达式,将结果放在“output.txt”文件中每一行中。这个结果可能是值,也可能是错误信息“error in infix notation”二、算法设计程序的整体算法分两步:1、 从“input。txt”文件中读取中缀表达式,并应用运算符栈opholder把中缀表达式转换成后缀表达式,将结果存放在一个temp文件中。2、 从temp文件中读取后缀表达式,并应用操作数栈operands计算后缀表达式结果将结果输出到“output.txt”文件中。为了实现算法,使用两个工作栈。一个称做opholder,用于
3、寄存运算符;另一个称做operands,用以寄存操作数或运算结果。算法的基本思想是:首先置操作数栈为空栈,表达式起始符“#”为运算符栈的栈底元素;依次读入表达式中每个字符,若是操作数则进opnd栈,若是运算符则和optr栈的栈顶运算符比较优先权后作相应操作,直至整个表达式求值完毕。具体算法如下:operandtype evaluateexpression()initstack(operands); push(operands,'#');initstack(operands);c=getchar();while(c!='#'|gettop(operands)!=&
4、#39;#')if(!in(c,op)push(operands,c);c=getchar();elseswitch(precede(gettop(operands),c)case'<':push(operands,c);c=getchar();breakcase'=':pop(operands,x); c=getchar();break;case'>':pop(operands,theta);pop(operands,b); pop(operands,a);push(operands),operate(a,theta,b);
5、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*
6、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
7、 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
8、infix notation2 2error in infix notation+-+error in infix notation四、分析与探讨算术表达式有中缀表示法和后缀表示法,本程序输入的表达式采用中缀表示法。在这种表达式中,二元运算符位于两个操作数之间。因为计算机只能一个字符一个字符的扫描,要想知道那个运算符优先,就必须对整个中缀表达式扫描一遍。而后缀表达式则很容易通过应用栈实现表达式计算,这为实现表达式求值程序提供了一种直接的计算机制。后缀表达式很容易应用栈进行计算,但要处理的是中缀表达式。同样,也可以应用栈将中缀表达式转换为后缀表达式。此时,栈里要保存的是运算符,而在后缀表达式中,
9、栈里保存的是操作数。应用栈将中缀表达式转换成后缀表达式处理过程的关键是不同运算符优先级的设置。在程序实现中,可以用一个数来代表运算符的优先级,优先级数值较大,它的优先级越高,这样优先级的比较就转换成两个数大小的比较。本程序的栈采用带头结点的链式存储结构,涉及两种类型:用于存储运算符号的char类型的链栈以及用于存储操作数的float类型的链栈。栈的操作在链式存储结构上的实现: linkstack *creatstack() /初始化栈,并将栈置空linkstack *s;s=(linkstack*)malloc(sizeof(linkstack);if(s=null)fatalerror(&q
10、uot;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)
11、/元素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_vo
12、id requires an explicit cast的错误,我通过查阅资料知道了这是由于,所有分配地址要加强制类型转换,因为分配内存后缺省内存是void *.这个错误改正后,程序可以正常运行。通过这次课程设计,我对栈的特性有了清楚的认识,我也深深体会到数据结构真的并不只是书本上的一些知识,上机实践对于这门科目来说真的很重要。平常简简单单的一个运算,也许自己通过加加减减就能得出结果,可是这些算法到底是怎么实现的?平常不会想到,通过实践才发现其中集合了很多我们平时忽略的知识,所以,通过这次实践,我也把书中很多知识巩固了下,加深印象!总之,通过这次课程设计,我确实收益颇多!附录 源代码:#inc
13、lude <stdio.h>#include <stdlib.h>#include <math.h>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;type
14、def 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
15、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 | !o
16、utputfile) 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(ou
17、tputfile,"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+&
18、quot;);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-
19、>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"
20、;);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)po
21、p(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(si
22、zeof(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)re
23、turn 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; /*指向新的堆栈*/voi
24、d 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')
25、&& (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);
26、/*进栈*/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(operat
27、orvalue(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
28、9;,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(top(ophol
29、der) && operatorvalue(top(opholder) = 3) /*幂运算情况*/putc(' ',temp);push('b',whereat); putc(top(opholder),temp);pop(opholder);while(isempty(opholder) != 1) && (operatorvalue(top(opholder) = 3)push('b',whereat);putc(' ',temp);putc(top(opholder),temp); pop(
30、opholder);push(holder,opholder);else if(operatorvalue(top(opholder) >= operatorvalue(holder)while(isempty(opholder) != 1) && (operatorvalue(top(opholder) >= operatorvalue(holder) && (operatorvalue(top(opholder)!=6)putc(' ',temp);putc(top(opholder),temp);push('b'
31、,whereat);pop(opholder);push(holder,opholder);else if(operatorvalue(top(opholder) < operatorvalue(holder) push(holder,opholder);holder=getc(in); /* while循环结束*/while(isempty(opholder)!=1) push('b',whereat);putc(' ',temp);putc(top(opholder),temp);pop(opholder);makeempty(opholder); f
32、ree(opholder);int isoperator(char tocompare)if (tocompare = '(' | tocompare = ')'| tocompare = '+' | tocompare = '-' | tocompare = '*'| tocompare = '/' | tocompare = ''| tocompare = '%')return 1;else if (tocompare = '1' | tocomp
33、are = '2'| tocompare = '3' | tocompare = '4' | tocompare = '5'| tocompare = '6' | tocompare = '7'| tocompare = '8' | tocompare = '9'| tocompare = '0'| tocompare = '.') return 0; elsereturn -1;int operatorvalue(char value
34、togive)if (valuetogive = '(')return 6;if (valuetogive = ')')return 5;if (valuetogive = '')return 3; if (valuetogive = '%')return 2;if (valuetogive = '*') return 2;if (valuetogive = '/')return 2;if (valuetogive = '+')return 1;if (valuetogive = &
35、#39;-')return 1;return 0;void calculate(file *change, stack whereat, file *temp)fstack operands;float looker;char op;char spacefinder;float answer = 0;float numa;float numb;operands = (ptr_fn)malloc(sizeof(struct fnode);operands->next= null;while (isempty(whereat) != 1) && printerror
36、!= 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) /* 判断是
37、什么运算符*/case(''): /*幂运算*/numb = ftop(operands);fpop(operands);if (fisempty(operands) /*错误处理*/printerror = 1;elsenuma = ftop(operands);fpop(operands);if (numa = 0 && numb < 0)|(numa<0) && (numb - (int)numb != 0)printerror = 1;elseanswer = pow(numa,numb);fpush(answer,opera
38、nds);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)printerr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026孝感教资面试题及答案
- 2026延时服务面试题及答案
- 2026英国审计面试题及答案
- 弘扬科学精神普及科学知识-小学科学主题班会课件
- 2026浙江宁波市教育局直属学校招聘事业编制教师76人笔试备考试题及答案详解
- 2026四川省国投资产托管有限责任公司资产管理岗、资金会计岗社会招聘2人笔试模拟试题及答案详解
- 招18人!2026年西宁市城西区面向社会公开招聘编外聘用教师笔试备考试题及答案详解
- 职业卫生技术服务专业技术人员考试(职业卫生检测)模拟题库及答案(2026年临夏回族)
- 2026年甘肃酒泉红西路军安西战役纪念馆招聘笔试参考题库及答案详解
- 2026山东聊城市第三人民医院招聘备案制工作人员8人笔试模拟试题及答案详解
- 2025年江苏省无锡市梁溪区侨谊教育集团小升初数学招生试卷(含答案解析)
- 贵州农商联合银行招聘笔试真题2025
- 煤矿安全生产标准化管理体系2024版与2026版对比分析报告
- 2025-2026学年-浙教版七年级下册数学期末质量检测模拟卷(含答案)
- 反恐怖防范安全风险评估工作指南(试行)
- 2026秋人教版九年级英语上册单词默写
- 体检科收费室工作制度
- 快递公司员工岗位职责及考核标准
- 无尘车间物料包装与拆包规范手册
- 2026广西南宁市青秀区文化广电体育和旅游局招聘1人笔试模拟试题及答案解析
- 期末综合达标测试卷(试卷)2025-2026学年三年级语文下册统编版(含答案)
评论
0/150
提交评论