西北工业大学数据结构实验报告02.docx
- 文档编号:23633946
- 上传时间:2023-05-19
- 格式:DOCX
- 页数:11
- 大小:17.27KB
西北工业大学数据结构实验报告02.docx
《西北工业大学数据结构实验报告02.docx》由会员分享,可在线阅读,更多相关《西北工业大学数据结构实验报告02.docx(11页珍藏版)》请在冰豆网上搜索。
西北工业大学数据结构实验报告02
数据结构实验报告
班级:
10011206姓名:
邹维韬学号:
2012302516E-mail:
396881328@日期:
10.30
◎实验题目:
使用键盘输入表达式,计算表达式的值并输出;将表达式转化成后缀表达式输出,利用后缀表达式求表达式的值并输出。
一、需求分析
1、本实验中,数据为整数,先建立一个链栈返回栈顶元素,输入表达式先判断表达式的正误,然后将中缀表达式变换为后缀表达式,继而计算求值。
2、表达式以后缀表达式形式输出,求值结果输出为整数。
3、程序所执行的命令有:
定义结构体,判断表达式的正误,建立链栈并初始化,字符串的输入,运算符高低的判断,表达式求值。
4、测试数据:
当输入表达式为3*(7-2)时,输出结果为后缀表达式为:
372-*计算结果:
15
当输入表达式为2*(6+2*(3+6*(6+6)))+(6+6)*3+2
时,输出结果为后缀表达式为:
2623666+*+*+*66+3*2++计算结果:
350
当输入表达式为8/(9-9)时,输出结果为后缀表达式为:
899-/ERROR:
除数为零!
二概要设计
1.基本操作
(1)、structnode
操作结果:
创建结构体
(2)、intSearchexpression(charstring1[])
初始条件:
表达式string1已经存在。
操作结果:
判断表达式是否非法
(3)、structnode*Initialization()
操作结果:
创建栈链。
(4)、structnode*assort(structnode*s)
初始条件:
string1、string2已存在。
操作结果:
将中缀表达式转换为后缀表达式并存在string2中。
(5)、structnode*calcolate(structnode*s)
操作结果:
求出表达式的值
三详细设计
1、每个模块:
(1)定义结构体
structnode
{
chardata;
intnum;
structnode*next;
};
(2)判断表达式是否非法
intSearchexpression(charstring1[])
{
inti1,b1,b2;
intm;
m=strlen(string1);
if(string1[0]<'0'||string1[0]>'9')
{
printf("ERROR:
表达式缺操作数!
\n");return(WRONG);
}
for(i1=0;i1<=m;i1++)
{
if(string1[i1]=='(')
b1++;
else
if(string1[i1]==')')
b2++;
}
if(b1!
=b2)
{printf("ERROR:
缺少括号\n");return(WRONG);}
for(i1=0;i1 if('0'<=string1[i1]&&string1[i1]<='9'&&'0'<=string1[i1+1]&&string1[i1+1]<='9') {printf("ERROR: 表达式缺操作符! \n");return(WRONG);} for(i1=0;i1<=m;i1++) if(string1[i1]=='+'&&(string1[i1+1]=='+'||string1[i1+1]=='-'||string1[i1+1]=='*'||string1[i1+1]=='/')) {printf("ERROR: 表达式缺操作数! \n");return(WRONG);} else if(string1[i1]=='-'&&(string1[i1+1]=='+'||string1[i1+1]=='-'||string1[i1+1]=='*'||string1[i1+1]=='/')) {printf("ERROR: 表达式缺操作数! \n");return(WRONG);} else if(string1[i1]=='*'&&(string1[i1+1]=='+'||string1[i1+1]=='-'||string1[i1+1]=='*'||string1[i1+1]=='/')) {printf("ERROR: 表达式缺操作数! \n");return(WRONG);} else if(string1[i1]=='/'&&(string1[i1+1]=='+'||string1[i1+1]=='-'||string1[i1+1]=='*'||string1[i1+1]=='/')) {printf("ERROR: 表达式缺操作数! \n");return(WRONG);} return(RIGHT); } (3)、将中缀表达式转换为后缀表达式 structnode*assort(structnode*s)//输入字符串 { structnode*p,*top; inti; top=s; intm; chara; m=strlen(string1); for(i=0;i<=m;i++) { a=string1[i]; if('0'<=string1[i]&&string1[i]<='9') { string2[j]=string1[i];j++; } else { switch(a) { case'(': { p=(structnode*)malloc(sizeof(structnode)); p->data=a;p->next=top; top=p; break; } case'*': case'/': string2[j]='';j++; if((top->data=='*')||(top->data=='/')) { string2[j]=top->data;j++;//比其高,现将栈顶运算符出栈,再进栈。 top->data=a; break; } else { p=(structnode*)malloc(sizeof(structnode));//否,直接进栈 p->data=a;p->next=top; top=p; break; } case'+': case'-': { string2[j]='';j++; if(top->data=='+'||top->data=='-'||top->data=='*'||top->data=='/') { string2[j]=top->data;j++; top->data=a; break; } else { p=(structnode*)malloc(sizeof(structnode)); p->data=a;p->next=top; top=p; break; } } case')': { string2[j]='';j++; if(top->data=='@'){printf("inputerror");break;} while(top->data! ='(') { string2[j]=top->data;j++; p=top; top=top->next; free(p); } p=top;top=top->next;free(p); break; } } } } while(top->data! ='@') { string2[j]=top->data;j++; p=top; top=top->next; free(p); } string2[j]='#'; printf("后缀表达式为: "); for(i=0;i if(string2[i]! ='') printf("%c",string2[i]); printf("\n"); returntop; } (4)表达式求值 structnode*calcolate(structnode*s) { structnode*top,*p; char*q; intx,y,a; inti,n; top=s;//指向栈顶的指针 for(i=0;i<=j;i++)//遍历字符串string2 { if(string2[i]>='0'&&string2[i]<='9') { q=&string2[i]; a=atoi(q); for(n=i;string2[n]>='0'&&string2[n]<='9';n++){} p=(structnode*)malloc(sizeof(structnode)); p->num=a;p->next=top;top=p; i=n-1; } else if(string2[i]=='#')//遇#号结束标志,输出栈中的最后计算结果 printf("计算结果为: %d\n",top->num); else { if(string2[i]==''){} else { y=top->num;p=top;top=top->next;free(p); x=top->num;p=top;top=top->next;free(p); switch(string2[i]) {case'+': {a=x+y; p=(structnode*)malloc(sizeof(structnode)); p->num=a;p->next=top;top=p; break;} case'-': {a=x-y; p=(structnode*)malloc(sizeof(structnode)); p->num=a;p->next=top;top=p; break;} case'*': {a=x*y; p=(structnode*)malloc(sizeof(structnode)); p->num=a;p->next=top;top=p; break;} case'/': {if(y==0) printf("ERROR: 除数为零! \n"); a=(float)x/y; p=(structnode*)malloc(sizeof(structnode)); p->num=a;p->next=top;top=p; break;} } } } } return0; } (5)、主函数 voidmain() { structnode*top,*head; top=Initialization();//建立一个链栈,并返回栈顶指针 printf("请输入表达式: \n"); gets(string1); if(Searchexpression(string1)) { head=assort(top);//中缀转化为后缀表达式 calcolate(head); } } 测试结果 当输入表达式为3*(7-2)时,输出结果为后缀表达式为: 372-*计算结果: 15 当输入表达式为2*(6+2*(3+6*(6+6)))+(6+6)*3+2 时,输出结果为后缀表达式为: 2623666+*+*+*66+3*2++计算结果: 350 当输入表达式为8/(9-9)时,输出结果为后缀表达式为: 899-/ERROR: 除数为零! 实验总结 这次数据结构实验的题目难度明显比上次的大,由于基础不是太好,所以比上次花了更多的时间和精力。 所幸最后在班里大神的帮忙以及浏览网上资源后终于完成了这次的编程。 通过这次作业,我觉得应该把更多的精力放在数据结构上,在打好基础的同时多多练习编程,在实验课的课堂上遇到不懂的就及时向老师请教,争取在最短的时间内做到自己独立完成编程作业,提升自己的编程能力。 教师评语: 实验成绩: 指导教师签名: 批阅日期:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 西北工业大学 数据结构 实验 报告 02