数据结构--表达式翻译_第1页
数据结构--表达式翻译_第2页
数据结构--表达式翻译_第3页
数据结构--表达式翻译_第4页
数据结构--表达式翻译_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

#include#include#define TRUE 1#define FALSE 0#define MAXNUM 100typedef int DataType;struct SeqStack DataType sMAXNUM;int t;typedef struct SeqStack *PSeqStack;PSeqStack createEmptyStack_seq() PSeqStack pastack;pastack = (PSeqStack)malloc(sizeof(struct SeqStack);if (pastack = NULL)printf(Out of space!n);elsepastack-t = -1;return pastack; int isEmptyStack_seq(PSeqStack pastack) return pastack-t = -1; void push_seq(PSeqStack pastack, DataType x) if (pastack-t = MAXNUM - 1)printf(Overflow!n);else pastack-t = pastack-t + 1;pastack-spastack-t = x; void pop_seq(PSeqStack pastack) if (pastack-t = -1)printf(Underflow!n);elsepastack-t = pastack-t - 1; DataType top_seq(PSeqStack pastack) return pastack-spastack-t; int infixtoSuffix(const char * infix, char * suffix) /*将中缀表达式转换为后缀表达式,顺利转换返回true,若转换过程中发现中缀表达式非法则返回false*/int state_int = FALSE; /*state_int记录状态,等于true表示刚读入的是数字字符,等于false表示刚读入的不是数字字符,设置这个变量是为了在每输出一个整数后输出一个空格,以免连续输出的两个整数混在一起。*/char c, c2;PSeqStack ps = createEmptyStack_seq(); /*运算符栈*/int i, j = 0;if (infix0 = 0) return FALSE; /*不允许出现空表达式*/for (i = 0; infixi != 0; i+)c = infixi;switch (c) case : case t: case n:if (state_int = TRUE) suffixj+ = ;/*状态从true转换为false时输出一个空格*/state_int = FALSE;break; /*遇到空格或制表符忽略*/case 0: case 1: case 2: case 3: case 4:case 5: case 6: case 7: case 8: case 9:state_int = TRUE;suffixj+ = c; /*遇到数字输出*/break;case (:if (state_int = TRUE)suffixj+ = ;/*状态从true转换为false时输出一个空格*/state_int = FALSE;push_seq(ps, c); /*遇到左括号,入栈*/break;case ):if (state_int = TRUE) suffixj+ = ;/*状态从true转换为false时输出一个空格*/state_int = FALSE;c2 = );while (!isEmptyStack_seq(ps) c2 = top_seq(ps);/*取栈顶*/pop_seq(ps); /*出栈*/if (c2 = () break;suffixj+ = c2;if (c2 != () free(ps);suffixj+ = 0;return FALSE; break;case +: case -:if (state_int = TRUE) suffixj+ = ;state_int = FALSE;while(!isEmptyStack_seq(ps) c2 = top_seq(ps); if (c2 = + | c2 = - | c2 = * | c2 = /) pop_seq(ps);suffixj+ = c2;else if(c2=() break;push_seq(ps, c); break;case *: case /:if (state_int = TRUE) suffixj+ = ;state_int = FALSE;while (!isEmptyStack_seq(ps) c2 = top_seq(ps); if (c2 = * | c2 = /) pop_seq(ps);suffixj+ = c2; else if(c2=+|c2=-|c2=() break;push_seq(ps, c); break;default:free(ps);suffixj+ = 0;return FALSE;if (state_int = TRUE) suffixj+ = ;while (!isEmptyStack_seq(ps) c2 = top_seq(ps);pop_seq(ps); if (c2 = () free(ps);suffixj+ = 0;return FALSE; suffixj+ = c2; free(ps);suffixj+ = 0;return TRUE; int calculateSuffix(const char * suffix, int * presult) int state_int = FALSE; PSeqStack ps = createEmptyStack_seq(); int num = 0, num1, num2;int i;char c;for (i = 0; suffixi != 0; i+) c = suffixi;switch (c) case 0:case 1: case 2: case 3: case 4:case 5: case 6: case 7: case 8: case 9:if (state_int = TRUE) num = num * 10 + c - 0;else num = c - 0;state_int = TRUE;break;case : caset: case n:if (state_int = TRUE) push_seq(ps, num); state_int = FALSE;break;case +: case -: case *: case /:if (state_int = TRUE) push_seq(ps, num); state_int = FALSE;if (isEmptyStack_seq(ps) free(ps);return FALSE; num2 = top_seq(ps);pop_seq(ps);if (isEmptyStack_seq(ps) free(ps);return FALSE; num1 = top_seq(ps);pop_seq(ps);if (c = +) push_seq(ps, num1 + num2);if (c = -) push_seq(ps, num1 - num2);if (c = *) push_seq(ps, num1 * num2);if (c = /) push_seq(ps, num1 / num2);break;default: free(ps);return FALSE;*presult = top_seq(ps); pop_seq(ps);if (!isEmptyStack_seq(ps) free(ps);return FALSE; free(ps);return TRUE; void getline(char * line, int limit) char c;int i = 0;while (i limit - 1 & (c = getchar() != EOF & c != n)linei+ = c;linei = 0; void main() char c, infixMAXNUM, suffixMAXNUM;int result;int flag = TRUE;while (flag = TRUE) printf(请输入表达式!nInput:);getline(infix, MAXNUM); if(infixtoSuffix(infix, suffix) = TRUE)printf(suffix:%sn, suffix); else printf(无效的输入!n);printf(nContinue? (y/n);scanf(%c, &c);if (c = n | c = N) flag = FALSE;while (getchar() != n);printf(n);continue;if

温馨提示

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

评论

0/150

提交评论