NYOJ 257 郁闷的C小加(一)
2012-08-12 16:53
218 查看
同中缀式转化到后缀式
地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=257
地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=257
#include<stdio.h> #include<stdlib.h> #define N 1010 //字符栈的操作 typedef struct { char *base; char *top; }SqStack; int InitStack(SqStack &S) { S.base=(char *)malloc(N*sizeof(char)); if(!S.base) exit(1); S.top=S.base; return 1; } int StackEmpty(SqStack &S) { if(S.top==S.base) return 1; return 0; } int GetTop(SqStack S,char &e) { if(S.top==S.base) return 0; e=*(S.top-1); return 1; } int Push(SqStack &S,char e) { *S.top++=e; return 1; } int Pop(SqStack &S,char &e) { if(S.top==S.base) return 0; e=*--S.top; return 1; } //转化的操作过程 static int i; int Pass(char *s,char c) { s[i]=c; i++; return 1; } int level(char c,int k) { switch(c) { case '\n': return 1; case ')': return k?2:5; case '+': case '-': return 3; case '*': case '/': return 4; case '(': return k?5:2; default : return 0; } } int Precede(char c1,char c2) { if(level(c1,0)<level(c2,1)||c1=='\n'&&c2=='\n') return 0; return 1; } int Converse(char *s) { SqStack OPTR; char ch,x,c=getchar(); InitStack(OPTR); Push(OPTR,'\n'); while(!StackEmpty(OPTR)) { if(!level(c,1)) Pass(s,c); else switch(c) { case '(': Push(OPTR,c);break; case ')': Pop(OPTR,ch);while(ch!='('){Pass(s,ch);Pop(OPTR,ch);}break; default : while(GetTop(OPTR,ch)&&Precede(ch,c)) { Pass(s,ch);Pop(OPTR,ch); } if(c!='\n') Push(OPTR,c); break; } if(c!='\n') { x=c; c=getchar(); } else { Pop(OPTR,ch); Pass(s,ch); } } s[i]='\0'; return 1; } //主函数 int main() { char s[1010]; int j,n; scanf("%d%*c",&n); while(n--){ i=0; Converse(s); for(j=0;s[j];++j) putchar(s[j]); } return 0; }
相关文章推荐
- nyoj_257郁闷的c小加(一)
- NYOJ 257 郁闷的C小加(一)(中缀式变后缀式)
- nyoj-257-郁闷的C小加(一 )中缀式变后缀式
- NYOJ 257 郁闷的C小加(一)
- nyoj 257 郁闷的C小加(一)
- nyoj-257 郁闷的C小加(一) 前缀表达式变后缀
- nyoj 467 中缀式变后缀式 nyoj 257 郁闷的C小加(一)
- nyoj-257 郁闷的C小加(一)
- NYOJ 257 郁闷的C小加(一)
- nyoj_257 郁闷的c小加
- NYOJ 257 郁闷的C小加(一)
- NYOJ 257 郁闷的C小加(一)
- NYOJ 257 郁闷的C小加(一)
- NYOJ 257 郁闷的C小加(一)
- NYOJ 257 郁闷的C小加(一)(栈和队列)(计算器)
- NYOJ 257 郁闷的C小加(一)
- nyoj257 郁闷的C小加(一)
- nyoj-257 郁闷的C小加(一)【表达式求值】
- NYOJ 257 郁闷的C小加(一) (栈 、中缀转后缀)
- NYOJ 267 郁闷的C小加(二)