1、数据结构课程设计表达式翻译本科论文数据结构课程考核报告 表达式的翻译学号: 姓名: 专业: 软件工程专业 班级: 15级云计算1班 指导教师: 南 阳 理 工 学 院 软 件 学 院2016年12月 目录1.需求分析-3 1.1问题描述-3 1.2基本要求-32.系统设计-33.程序流程图-44.类关系图-65. 实现代码-76.个人总结-77.参考书目-7一需求分析 1.1问题描述 编写完整程序,将中缀表达式翻译成后缀表达式。表达式由操作数 ( 变量 ) 、操作 ( 运算符 ) 以及小括弧 “ ( ” 和 “ ) ” 组成,其中:(1)操作包括算术运算、关系运算和逻辑运算三类;(2)操作数为
2、单个字符或由字母和数字任意多个字符构成;(3)能够识别出简单的错误,如括弧不匹配。 1.2 基本要求 输入:中缀表达式,80个字符以内。 输出:运算结果 功能:将中缀表达式转化为后缀表达式 二系统设计 根据题目要求,将中缀表达式转化为后缀表达式。问题的解决方案有两种,分别是利用栈结构实现算数表达式的四则运算或者利用二叉树把中缀表达式转化为前缀表达式,我选择栈结构与树结构相结合来实现算数表达式的转化。算法思想:运用栈和队列来将中缀转换为后缀,栈用来储存字符,队列用来存储后缀表达式。:输入字符串c,若是数字的话直接进入队列,若是符号,为*/(,则线检查栈中的栈顶元素,前提是栈不为空,若栈不为空的话
3、,栈顶元素为*|/,则先将栈中的元素入队,如果栈顶是+-这些优先级小的,则直接将c入栈。:如果c是优先级为“+”“-”的字符,先看一下栈是否为空,接着判断栈顶元素是否为“(”,若是,则继续将c进栈,否则的话,将栈中的元素全部出栈,直到栈空。:若c为“)”则将栈中所有符号弹栈并进入队列。:若c为“#”,则结束,并将栈中的剩余的符号全部入队列。:打印队列中的,出队打印。 3程序流程图else ifC!=#若为数else ifelse ifwhile栈空else ifelse ifelse ifwhile栈空4类关系图(1).头文件:#include#include(2)宏定义:#define OK
4、 1#define ERROR 0#define STACK_SIZE 20#define STACK_INCREMENT 10#define QUEUE_SIZE 20(3)栈的定义结构体typedef int Status;typedef char StackElemtype; /栈的类型typedef struct Stack StackElemtype* base; StackElemtype* top; int stackSize;Stack;(5)初始化栈:Status StackInit(Stack* s)(6)出栈:Status Pop(Stack* s,StackElemty
5、pe* value)(7)进栈:Status Push(Stack* s,StackElemtype value)(8)求栈的长度:int StackLength(Stack s)(9)定义队列的结构体:typedef double QueueElemtype;typedef char QueueOperatorValue;typedef struct QueueNode /定义队列结构体 QueueElemtype data; QueueOperatorValue operator; struct QueueNode* next; int flag;QueueNode,*QueueNodeP
6、tr;typedef struct Queue QueueNodePtr front; QueueNodePtr rear;Queue;(10)初始化队列Status QueueInit(Queue* q)(11)队列的插入:Status QueueInsert(Queue* q,QueueElemtype value)(12)出队列Status QueueInsert_operatorValue(Queue* q,QueueOperatorValue value)(13)中缀转后缀函数Status Infix2Postfix(Queue* q)(14)打印后缀表达式Status ShowQu
7、eue(Queue q)(15)主函数int main() Queue q; QueueInit(&q); Infix2Postfix(&q); ShowQueue(q); return 0;5运行截图六实现代码#include#include#define OK 1#define ERROR 0#define STACK_SIZE 20#define STACK_INCREMENT 10#define QUEUE_SIZE 20typedef int Status;typedef char StackElemtype; /栈的类型typedef struct Stack StackElemt
8、ype* base; StackElemtype* top; int stackSize;Stack;Status StackInit(Stack* s) /初始化栈 s-base = (StackElemtype*)malloc(sizeof(StackElemtype) * STACK_SIZE); /申请栈的空间 if( !s-base ) return ERROR; s-top = s-base; s-stackSize = STACK_SIZE; return OK;Status Pop(Stack* s,StackElemtype* value) /出栈操作 if( s-base
9、= s-top ) printf(nstack emptyn); return ERROR; *value = *(-(s-top); return OK;Status Push(Stack* s,StackElemtype value) /进栈操作 if( s-top - s-base = s-stackSize) s-base = (StackElemtype*)realloc(s-base,sizeof(StackElemtype) * (STACK_INCREMENT + STACK_SIZE); if( !s-base ) return ERROR; s-top = s-base +
10、 STACK_SIZE; s-stackSize = STACK_SIZE + STACK_INCREMENT; *(s-top) = value; s-top+; return OK;int StackLength(Stack s) /求栈的长度 return s.top - s.base;typedef double QueueElemtype;typedef char QueueOperatorValue;typedef struct QueueNode /定义队列结构体 QueueElemtype data; QueueOperatorValue operator; struct Qu
11、eueNode* next; int flag;QueueNode,*QueueNodePtr;typedef struct Queue QueueNodePtr front; QueueNodePtr rear;Queue;Status QueueInit(Queue* q) /队列初始化 q-front = (QueueNodePtr)malloc(sizeof(QueueNode); if( !q-front ) return ERROR; q-rear = q-front; q-rear-next = NULL; return OK;Status QueueInsert(Queue*
12、q,QueueElemtype value) /队列插入 QueueNodePtr new; new = (QueueNodePtr)malloc(sizeof(QueueNode); if( !new ) return ERROR; new-data = value; new-flag = 1; new-next = NULL; q-rear-next = new; q-rear = new; return OK;Status QueueInsert_operatorValue(Queue* q,QueueOperatorValue value) QueueNodePtr new; new = (QueueNodePtr)malloc(sizeof(QueueNode); if( !new ) return ERROR; new-operator = value; new-flag = 0; new-next = NULL; q-rear-next = new; q-rear = new; return OK;/利用栈将中缀表达式转化为后缀表达式:Stat
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1