数据结构课程设计表达式翻译本科论文.docx
《数据结构课程设计表达式翻译本科论文.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计表达式翻译本科论文.docx(14页珍藏版)》请在冰豆网上搜索。
![数据结构课程设计表达式翻译本科论文.docx](https://file1.bdocx.com/fileroot1/2022-10/24/d7e21c75-feb0-4ce9-a4fe-fb9396e2bbaf/d7e21c75-feb0-4ce9-a4fe-fb9396e2bbaf1.gif)
数据结构课程设计表达式翻译本科论文
数据结构课程考核报告
表达式的翻译
学号:
姓名:
专业:
软件工程专业
班级:
15级云计算1班
指导教师:
南阳理工学院软件学院
2016年12月
目录
1.需求分析-------------------------------------------------------------------3
1.1问题描述---------------------------------------------------------------3
1.2基本要求---------------------------------------------------------------3
2.系统设计-------------------------------------------------------------------3
3.程序流程图-----------------------------------------------------------------4
4.类关系图-------------------------------------------------------------------6
5.实现代码------------------------------------------------------------------7
6.个人总结-------------------------------------------------------------------7
7.参考书目-------------------------------------------------------------------7
一.需求分析
1.1问题描述
编写完整程序,将中缀表达式翻译成后缀表达式。
表达式由操作数(变量)、操作(运算符)以及小括弧“(”和“)”组成,其中:
(1)操作包括算术运算、关系运算和逻辑运算三类;
(2)操作数为单个字符或由字母和数字任意多个字符构成;(3)能够识别出简单的错误,如括弧不匹配。
1.2基本要求
输入:
中缀表达式,80个字符以内。
输出:
运算结果
功能:
将中缀表达式转化为后缀表达式
二.系统设计
根据题目要求,将中缀表达式转化为后缀表达式。
问题的解决方案有两种,分别是利用栈结构实现算数表达式的四则运算或者利用二叉树把中缀表达式转化为前缀表达式,我选择栈结构与树结构相结合来实现算数表达式的转化。
算法思想:
①:
运用栈和队列来将中缀转换为后缀,栈用来储存字符,队列用来存储后缀表达式。
②:
输入字符串c,若是数字的话直接进入队列,若是符号,为‘*’‘/’‘(’,则线检查栈中的栈顶元素,前提是栈不为空,若栈不为空的话,栈顶元素为‘*’||‘/’,则先将栈中的元素入队,如果栈顶是‘+’‘-’这些优先级小的,则直接将c入栈。
③:
如果c是优先级为“+”“-”的字符,先看一下栈是否为空,接着判断栈顶元素是否为“(”,若是,则继续将c进栈,否则的话,将栈中的元素全部出栈,直到栈空。
④:
若c为“)”则将栈中所有符号弹栈并进入队列。
⑤:
若c为“#”,则结束,并将栈中的剩余的符号全部入队列。
⑥:
打印队列中的,出队打印。
3.程序流程图
elseif
C!
=’#’
若为数
elseif
elseif
while栈空
elseif
elseif
elseif
while栈空
4.类关系图
(1).头文件:
#include
#include
(2)
宏定义:
#defineOK1
#defineERROR0
#defineSTACK_SIZE20
#defineSTACK_INCREMENT10
#defineQUEUE_SIZE20
(3)栈的定义结构体
typedefintStatus;
typedefcharStackElemtype;//栈的类型
typedefstructStack{
StackElemtype*base;
StackElemtype*top;
intstackSize;
}Stack;
(5)初始化栈:
StatusStackInit(Stack*s)
(6)出栈:
StatusPop(Stack*s,StackElemtype*value)
(7)进栈:
StatusPush(Stack*s,StackElemtypevalue)
(8)求栈的长度:
intStackLength(Stacks)
(9)定义队列的结构体:
typedefdoubleQueueElemtype;
typedefcharQueueOperatorValue;
typedefstructQueueNode{//定义队列结构体
QueueElemtypedata;
QueueOperatorValueoperator;
structQueueNode*next;
intflag;
}QueueNode,*QueueNodePtr;
typedefstructQueue{
QueueNodePtrfront;
QueueNodePtrrear;
}Queue;
(10)初始化队列
StatusQueueInit(Queue*q)
(11)队列的插入:
StatusQueueInsert(Queue*q,QueueElemtypevalue)
(12)出队列
StatusQueueInsert_operatorValue(Queue*q,QueueOperatorValuevalue)
(13)中缀转后缀函数
StatusInfix2Postfix(Queue*q)
(14)打印后缀表达式
StatusShowQueue(Queueq)
(15)主函数
intmain(){
Queueq;
QueueInit(&q);
Infix2Postfix(&q);
ShowQueue(q);
return0;
}
5.运行截图
六.实现代码
#include
#include
#defineOK1
#defineERROR0
#defineSTACK_SIZE20
#defineSTACK_INCREMENT10
#defineQUEUE_SIZE20
typedefintStatus;
typedefcharStackElemtype;//栈的类型
typedefstructStack{
StackElemtype*base;
StackElemtype*top;
intstackSize;
}Stack;
StatusStackInit(Stack*s){//初始化栈
s->base=(StackElemtype*)malloc(sizeof(StackElemtype)*STACK_SIZE);//申请栈的空间
if(!
s->base)
returnERROR;
s->top=s->base;
s->stackSize=STACK_SIZE;
returnOK;
}
StatusPop(Stack*s,StackElemtype*value){//出栈操作
if(s->base==s->top){
printf("\nstackempty\n");
returnERROR;
}
*value=*(--(s->top));
returnOK;
}
StatusPush(Stack*s,StackElemtypevalue){//进栈操作
if(s->top-s->base==s->stackSize){
s->base=(StackElemtype*)realloc(s->base,sizeof(StackElemtype)*(STACK_INCREMENT+STACK_SIZE));
if(!
s->base)
returnERROR;
s->top=s->base+STACK_SIZE;
s->stackSize=STACK_SIZE+STACK_INCREMENT;
}
*(s->top)=value;
s->top++;
returnOK;
}
intStackLength(Stacks){//求栈的长度
returns.top-s.base;
}
typedefdoubleQueueElemtype;
typedefcharQueueOperatorValue;
typedefstructQueueNode{//定义队列结构体
QueueElemtypedata;
QueueOperatorValueoperator;
structQueueNode*next;
intflag;
}QueueNode,*QueueNodePtr;
typedefstructQueue{
QueueNodePtrfront;
QueueNodePtrrear;
}Queue;
StatusQueueInit(Queue*q){//队列初始化
q->front=(QueueNodePtr)malloc(sizeof(QueueNode));
if(!
q->front)
returnERROR;
q->rear=q->front;
q->rear->next=NULL;
returnOK;
}
StatusQueueInsert(Queue*q,QueueElemtypevalue){//队列插入
QueueNodePtrnew;
new=(QueueNodePtr)malloc(sizeof(QueueNode));
if(!
new)
returnERROR;
new->data=value;
new->flag=1;
new->next=NULL;
q->rear->next=new;
q->rear=new;
returnOK;
}
StatusQueueInsert_operatorValue(Queue*q,QueueOperatorValuevalue){
QueueNodePtrnew;
new=(QueueNodePtr)malloc(sizeof(QueueNode));
if(!
new)
returnERROR;
new->operator=value;
new->flag=0;
new->next=NULL;
q->rear->next=new;
q->rear=new;
returnOK;
}
//利用栈将中缀表达式转化为后缀表达式:
Stat