中缀转后缀&后缀求值
2013-03-08 22:02
204 查看
/* * Infix to Postfix, and get the value for Postfix. * Date: 2013/3/8 * NolanJian */ #include <stdio.h> #include <stdlib.h> #include <string.h> struct stacknode; typedef struct stacknode StackNode; typedef StackNode *StackPointer; int ValueOfPostfix(char *postfix); void InfixToPostfix(char *infix, char *postfix); char GetTop(StackPointer top); void Push(StackPointer *top, int ch); void Pop(StackPointer *top, char *postfix, int *i, int ch); void StringToInt(char *postfix); struct stacknode { int Num; StackPointer Next; }; int main() { char infix[100], postfix[100]; while(scanf("%s", infix)) { memset(postfix, '\0', 100); InfixToPostfix(infix, postfix); printf("Infix: %s\n", infix); printf("Postfix: %s\n", postfix); StringToInt(postfix); printf("Result: %d\n", ValueOfPostfix(postfix)); } return 0; } void StringToInt(char *postfix) { while(*postfix) { if(*postfix >= '0' && *postfix <= '9') *postfix -= '0'; postfix++; } } void Push(StackPointer *top, int ch) { StackPointer tmp; if(*top == NULL) { tmp = (StackPointer)malloc(sizeof(StackNode)); tmp->Next = NULL; tmp->Num = ch; (*top) = tmp; return ; } tmp = (StackPointer)malloc(sizeof(StackNode)); tmp->Next = (*top); tmp->Num = ch; (*top) = tmp; } void Pop(StackPointer *top, char *postfix, int *i, int ch) { StackPointer tmp; if(ch == '+' || ch == '-' || ch == '*' || ch == '/') { postfix[(*i)++] = (*top)->Num; tmp = *top; *top = (*top)->Next; free(tmp); return ; } if(ch == ')') { while((*top)->Num != '(') { postfix[(*i)++] = (*top)->Num; tmp = *top; *top = (*top)->Next; free(tmp); } tmp = *top; *top = (*top)->Next; free(tmp); return ; } if(ch == 3) { postfix[(*i)++] = (*top)->Num; tmp = *top; *top = (*top)->Next; free(tmp); return ; } tmp = *top; *top = (*top)->Next; free(tmp); } void InfixToPostfix(char *infix, char *postfix) { StackPointer top = NULL; int i = 0, flag; char ch; while(*infix) { if(*infix >= '0' && *infix <= '9') postfix[i++] = *infix; else if(*infix == ')') Pop(&top, postfix, &i, *infix); else if(*infix == '*' || *infix == '/' || *infix == '(') Push(&top, *infix); else if(*infix == '+' || *infix == '-') { ch = 0; if(!top) { Push(&top, *infix); infix++; continue; } while(top){ flag = 0; ch = GetTop(top); if(ch == '(') { Push(&top, *infix); flag = 1; break; } else Pop(&top, postfix, &i, *infix); } if(!flag) Push(&top, *infix); } infix++; } while(top) Pop(&top, postfix, &i, 3); postfix[i] = '\0'; } char GetTop(StackPointer top) { return top->Num; } int ValueOfPostfix(char *postfix) { StackPointer top = NULL; int a, b; while(*postfix) { if(*postfix >= 0 && *postfix <= 9) { Push(&top, *postfix); postfix++; continue; } if(*postfix == '+') { b = GetTop(top); Pop(&top, 0, 0, 0); a = GetTop(top); Pop(&top, 0, 0, 0); Push(&top, a + b); postfix++; continue; } if(*postfix == '-') { b = GetTop(top); Pop(&top, 0, 0, 0); a = GetTop(top); Pop(&top, 0, 0, 0); Push(&top, a - b); postfix++; continue; } if(*postfix == '*') { b = GetTop(top); Pop(&top, 0, 0, 0); a = GetTop(top); Pop(&top, 0, 0, 0); Push(&top, a * b); postfix++; continue; } if(*postfix == '/') { b = GetTop(top); Pop(&top, 0, 0, 0); a = GetTop(top); Pop(&top, 0, 0, 0); Push(&top, a / b); postfix++; continue; } } return top->Num; }
相关文章推荐
- http://acm.nyist.net/JudgeOnline/problem.php?pid=409&&中缀转化为前缀和后缀并求值
- 中缀转后缀 和 后缀表达式求值
- nyist 1272 表达式求值 (河南省第九届省赛) 中缀转后缀
- 中缀转后缀表达式并求值
- 多项式计算器-中缀变后缀并求值-C++版
- C/C++的中缀转后缀并求值的实现
- 表达式求值(中缀变后缀再求值)
- 栈和队列&前缀,中缀,后缀
- 线性表的实现与应用--表达式中缀转后缀并求值
- 中缀表达式求后缀,再由后缀表达式求值
- 前缀、中缀、后缀表达式及其求值
- 中缀转为后缀并求值_legend
- 中缀转后缀并求值(简单计算器的核心)
- 栈的应用--表达式计算&中缀转后缀
- 表达式求值,中缀后缀转换,表达式递归直接求值等相关算法的实现
- 中缀转后缀求值
- 中缀表达式转换为前缀及后缀表达式并求值【摘】
- 中缀转后缀及表达式求值
- 中缀转后缀,后缀求值,后缀构造表达式树
- 将中缀转换为后缀并求值