中缀表达式转后缀表达式c语言实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了中缀表达式转后缀表达式c语言实现相关的知识,希望对你有一定的参考价值。
1.运行环境:VS2015/Win7
2.头文件:
stack.h
1 typedef char ElementType; 2 /* START: */ 3 #ifndef _Stack_h 4 #define _Stack_h 5 6 struct StkNode; 7 typedef struct StkNode *PtrStk; 8 typedef PtrStk Stack; 9 10 int IsEmpty(Stack S); 11 Stack CreateStack(void); 12 void DisposeStack(Stack S); 13 void MakeEmpty(Stack S); 14 void Push(ElementType X, Stack S); 15 ElementType Top(Stack S); 16 void Pop(Stack S); 17 ElementType GetTopAndPop(Stack S); 18 #endif /* _Stack_h */ 19 20 /* END */
postfix.h
1 #include<stdio.h> 2 #include"stack.h" 3 #define IS_OPERATOR 1 4 #define IS_OPEN_PARENTHESIS 2 5 #define IS_CLOSE_PARENTHESIS 3 6 7 int IsOperator(ElementType Elem); 8 void OutPut(ElementType Elem); 9 int IsLowerOrEqual(ElementType TopElem, ElementType Elem); 10 int Judge(ElementType Elem); 11 void OutPutPostfix(ElementType X,Stack S);
3.实现文件:
stack.c
1 #include "stack.h" 2 #include<stdio.h> 3 #include<stdlib.h> 4 5 struct StkNode 6 { 7 ElementType Element; 8 PtrStk Next; 9 }; 10 11 int IsEmpty(Stack S) 12 { 13 return S->Next == NULL; 14 } 15 16 Stack CreateStack(void) 17 { 18 Stack S; 19 20 S = malloc(sizeof(struct StkNode)); 21 if (S == NULL) 22 printf("Out of Memory"); 23 24 S->Next = NULL; 25 MakeEmpty(S); 26 return S; 27 } 28 void DisposeStack(Stack S) 29 { 30 MakeEmpty(S); 31 free(S); 32 } 33 void MakeEmpty(Stack S) 34 { 35 while (S->Next != NULL) 36 { 37 Pop(S); 38 } 39 S->Element = 0; 40 } 41 void Push(ElementType X, Stack S) 42 { 43 PtrStk tmpCell; 44 tmpCell = malloc(sizeof(struct StkNode)); 45 tmpCell->Element = X; 46 47 //表顶端插入实现Push; 48 tmpCell->Next = S->Next; 49 S->Next = tmpCell; 50 51 } 52 ElementType Top(Stack S) 53 { 54 if (!IsEmpty(S)) 55 { 56 return S->Next->Element; 57 } 58 else 59 { 60 return NULL; 61 } 62 63 64 } 65 void Pop(Stack S) 66 { 67 if (IsEmpty(S)) 68 { 69 printf("Empty Stack !"); 70 } 71 else 72 { 73 PtrStk tmpCell = S->Next;//标记栈顶结点 74 S->Next = tmpCell->Next;//指向新栈顶结点 75 free(tmpCell);//释放标记结点 76 } 77 }
postfix.c
1 #include<stdio.h> 2 #include"postfix.h" 3 4 5 int IsOperator(ElementType Elem) 6 { 7 return Elem == ‘+‘ || Elem == ‘-‘ || Elem == ‘*‘ || Elem == ‘/‘; 8 } 9 10 void OutPut(ElementType Elem) 11 { 12 printf("%c", Elem); 13 } 14 15 //设置运算符优先级 16 int IsLowerOrEqual(ElementType TopElem, ElementType Elem) 17 { 18 if (TopElem == ‘(‘) return 0; 19 if (TopElem == ‘+‘&& Elem == ‘-‘) return 1; 20 if (TopElem == ‘+‘&& Elem == ‘+‘) return 1; 21 if (TopElem == ‘-‘&& Elem == ‘+‘) return 1; 22 if (TopElem == ‘-‘&& Elem == ‘-‘) return 1; 23 if (TopElem == ‘*‘&& Elem == ‘*‘) return 1; 24 if (TopElem == ‘*‘&& Elem == ‘/‘) return 1; 25 if (TopElem == ‘*‘&& Elem == ‘+‘) return 1; 26 if (TopElem == ‘*‘&& Elem == ‘-‘) return 1; 27 if (TopElem == ‘/‘&& Elem == ‘*‘) return 1; 28 if (TopElem == ‘/‘&& Elem == ‘/‘) return 1; 29 if (TopElem == ‘/‘&& Elem == ‘+‘) return 1; 30 if (TopElem == ‘/‘&& Elem == ‘-‘) return 1; 31 return 0; 32 33 } 34 35 //返回元素类型 36 int Judge(ElementType Elem) 37 { 38 if (IsOperator(Elem)) return IS_OPERATOR;//运算符 39 else if (Elem == ‘(‘) return IS_OPEN_PARENTHESIS;//左圆括号 40 else if (Elem == ‘)‘) return IS_CLOSE_PARENTHESIS;//右圆括号 41 42 } 43 44 //输入:中缀表达式的字符串;栈 45 //输出:打印后缀表达式 46 void OutPutPostfix(ElementType *Str, Stack S) 47 { 48 for (int i = 0; Str[i] != ‘\\0‘; i++) 49 { 50 switch (Judge(Str[i])) 51 { 52 //检测到左括号则入栈 53 case IS_OPEN_PARENTHESIS: 54 Push(Str[i], S); break; 55 56 //检测到为右括号,则打印栈内运算符并出栈,直至栈顶元素匹配到左括号 57 case IS_CLOSE_PARENTHESIS: 58 do 59 { 60 OutPut(Top(S)); 61 Pop(S); 62 } while (Judge(Top(S)) != IS_OPEN_PARENTHESIS); 63 Pop(S);//弹出左圆括号; 64 break; 65 66 //检测到为运算符,若优先级比栈内运算符小则将栈内运算符打印并出栈,直至栈空或栈顶元素为左括号; 67 case IS_OPERATOR: 68 if (IsLowerOrEqual(Top(S), Str[i]) && !IsEmpty(S)) 69 { 70 do { 71 OutPut(Top(S)); 72 Pop(S); 73 } while (Judge(Top(S)) != IS_OPEN_PARENTHESIS && !IsEmpty(S)); 74 } 75 Push(Str[i], S);//将运算符压栈 76 break; 77 78 //检测到为操作数,则直接打印 79 default: OutPut(Str[i]); break; 80 } 81 82 } 83 84 //打印栈内剩下的运算符 85 while (!IsEmpty(S)) 86 { 87 OutPut(Top(S)); 88 Pop(S); 89 } 90 91 }
4.测试demo:
1 #include<stdio.h> 2 #include"stack.h" 3 #include "postfix.h" 4 5 int main() 6 { 7 8 printf("*****************************************************\\n"); 9 printf("此demo实现功能:中缀表达式转换为后缀表达式\\n"); 10 printf("注意:输入的表达式必须为合法的;\\n"); 11 printf("示例:\\n"); 12 printf("input: a+b*c+(d*e+f)*g\\n"); 13 printf("output: abc*+de*f+g*+\\n"); 14 printf("*****************************************************\\n"); 15 printf("请输入中缀表达式:"); 16 17 ElementType str[100]; 18 scanf_s("%s", str,100); 19 Stack S = CreateStack(); 20 OutPutPostfix(str, S); 21 DisposeStack(S); 22 getchar(); 23 24 return 0;
5.输出结果:
以上是关于中缀表达式转后缀表达式c语言实现的主要内容,如果未能解决你的问题,请参考以下文章