数据结构之栈:算数表达式的转换 (sdut oj2484)
2016-08-02 19:21
330 查看
算术表达式的转换
Time Limit: 1000MS Memory limit: 65536K
题目描述
小明在学习了数据结构之后,突然想起了以前没有解决的算术表达式转化成后缀式的问题,今天他想解决一下。因为有了数据结构的基础小明很快就解出了这个问题,但是他突然想到怎么求出算术表达式的前缀式和中缀式呢?小明很困惑。聪明的你帮他解决吧。
输入
输入一算术表达式,以\'#\'字符作为结束标志。(数据保证无空格,只有一组输入)
输出
输出该表达式转换所得到的前缀式 中缀式 后缀式。分三行输出,顺序是前缀式 中缀式 后缀式。
示例输入
a*b+(c-d/e)*f#
示例输出
+*ab*-c/def a*b+c-d/e*f ab*cde/-f*+
提示
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; int cmp(char c) { if(c == '+'||c == '-') return 1 ; if(c == '*'||c == '/') return 2 ; if(c == '(') return 3 ; if(c == ')') return 4 ; return 0 ; } void Qianzhui(char a[]) { int top2 = -1, top3=-1; int l = strlen(a); char stack2[100], stack3[100]; for(int i = l-2; i >= 0; i--) { if(a[i] >= 'a'&&a[i]<= 'z') { stack3[++top3] = a[i]; } else { if(top2 == -1||stack2[top2] == ')'|| a[i] == ')') { stack2[++top2] = a[i]; } else if(a[i] == '(') { while(stack2[top2] != ')') { stack3[++top3] = stack2[top2--]; } top2--; } else { if(cmp(a[i]) < cmp(stack2[top2])) { stack3[++top3]= stack2[top2--]; i++; } else { stack2[++top2] = a[i]; } } } } while(top2 != -1) { stack3[++top3] = stack2[top2--]; } for(int i = top3; i >= 0; i--) printf("%c", stack3[i]); printf("\n"); } void Zhongzhui(char a[]) { for(int i = 0; a[i] != '#'; i++) { if(a[i] !='(' &&a[i] != ')') printf("%c", a[i]); } printf("\n"); } void Houzhui(char a[]) { int top1 = 0; char stack1[100] ; for(int i = 0; a[i] != '#'; i++) { if(a[i] >= 'a'&&a[i] <= 'z') { printf("%c", a[i]) ; } else { if(top1 == 0) { top1++ ; stack1[top1] = a[i] ; } else if(cmp(a[i]) > cmp(stack1[top1])) { if(cmp(a[i]) == 4) { while(stack1[top1] != '(') { printf("%c", stack1[top1--]); } top1-- ; } else { top1++ ; stack1[top1] = a[i]; } } else { if(stack1[top1] != '(') { printf("%c", stack1[top1]) ; stack1[top1] = a[i] ; } else { top1++ ; stack1[top1] = a[i] ; } } } } while(top1 != 0) { printf("%c", stack1[top1]) ; top1-- ; } printf("\n") ; } int main() { char a[1005]; scanf("%s",a); Qianzhui(a); Zhongzhui(a); Houzhui(a);
return 0; }
2:
#include<stdio.h> #include<stdlib.h> #include<string.h> int judge(char a,char b) { if((a=='*'||a=='/')&&(b=='+'||b=='-')) return 1; else if((a=='+'||a=='-')&&(b=='*'||b=='/')) return -1; else if((a=='+'||a=='-')&&(b=='+'||b=='-')) return 0; else if((a=='*'||a=='/')&&(b=='*'||b=='/')) return 0; } void houzhui(char a[]) { char c2[1005],c1[1005]; int t1,t2,i; t1=t2=-1; for(i=0; a[i]!='#'; i++) { if(a[i]!='*'&&a[i]!='-'&&a[i]!='+'&&a[i]!='/'&&a[i]!='('&&a[i]!=')') { c2[++t2]=a[i]; } else { if(t1==-1||c1[t1]=='('||a[i]=='(') { c1[++t1]=a[i]; // printf("%c",c1[t1]); } else if(a[i]==')') { //t1--; while(c1[t1]!='(') { c2[++t2]=c1[t1--]; } t1--; } else { if(judge(a[i],c1[t1])==1) c1[++t1]=a[i]; else { c2[++t2]=c1[t1--]; i--; } } } } while(t1!=-1) { c2[++t2]=c1[t1--]; } for(int i=0; i<=t2; i++) printf("%c",c2[i]); printf("\n"); } void zhongzhui(char a[]) { char c2[1005],c1[1005]; int t1,t2,i; t1=t2=-1; for(i=0; a[i]!='#'; i++) { if(a[i]!='('&&a[i]!=')') { c2[++t2]=a[i]; } } for(int i=0; i<=t2; i++) printf("%c",c2[i]); printf("\n"); } void qianzhui(char a[]) { char c2[1005],c1[1005];//c2Ã¥Â��c1Ã¥Â���符 int t1,t2,i; t1=t2=-1; int l=strlen(a); for(i=l-2; i>=0; i--) { if(a[i]!='*'&&a[i]!='-'&&a[i]!='+'&&a[i]!='/'&&a[i]!='('&&a[i]!=')') { c2[++t2]=a[i]; } else { if(t1==-1||c1[t1]==')'||a[i]==')') { c1[++t1]=a[i]; // printf("%c",c1[t1]); } else if(a[i]=='(') { //t1--; while(c1[t1]!=')') { c2[++t2]=c1[t1--]; } t1--; } else { if(judge(a[i],c1[t1])==-1) { c2[++t2]=c1[t1--]; i++; } else c1[++t1]=a[i]; } } } while(t1!=-1) { c2[++t2]=c1[t1--]; } for(int i=t2; i>=0; i--) printf("%c",c2[i]); printf("\n"); } int main() { char a[1005]; scanf("%s",a); qianzhui(a); zhongzhui(a); houzhui(a); }
相关文章推荐
- SDUT2484 算术表达式的转换(表达式树)
- SDUT 2484 算术表达式的转换
- 暑假集训 8.6 sdutoj2484 算术表达式的转换(模拟栈;前中后缀转换)
- SDUT2484算术表达式的转换
- Java语言基础{Java_se(02)}-字面量和常量-变量的基本定义和使用-Java表达式-数据类型和分类-基本数据类型的转换-算数运算符-赋值运算符-比较运算符-三元运算符-逻辑运算符
- 一般算数表达式转换为“后缀式”
- SDUT2484 算术表达式的转换
- SDUT2484 算术表达式的转换
- 表达式的转换(sdut-2484)
- SDUTOJ 2484 算术表达式的转换(表达式树)
- 算术表达式的转换 (算数表达的转换建树及遍历)
- SDUT 2484 算术表达式的转换
- sdutoj2484 算术表达式的转换 栈思想
- 一般算数表达式转换成后缀式问题
- [SPOJ]算数表达式转换成逆波兰式
- 数据结构实验之栈二:一般算术表达式转换成后缀式 (sdut oj 2132)
- 面试题77:前缀、中缀、后缀表达式的相互转换
- 中缀表达式转换为后缀表达式,计算后缀表达式
- QT算数表达式求值
- 中缀表达式转换为后缀表达式