标签:nyoj467
2 1.000+2/4= ((1+2)*5+1)/4=
1.000 2 4 / + = 1 2 + 5 * 1 + 4 / =
#include <stdio.h> #include <string.h> #include <ctype.h> #define maxn 1010 char buf[maxn], out[maxn << 1]; char sta[maxn]; // 符号栈 int id, id2; int getLevel(char ch) { switch(ch) { case '(': return 0; case '+': case '-': return 1; case '*': case '/': return 2; } } void check(char ch) { int level; if(ch == '(') sta[id2++] = ch; else if(ch == ')') { while(sta[id2-1] != '(') { out[id++] = sta[--id2]; out[id++] = ' '; } --id2; } else { while(id2 && getLevel(sta[id2-1]) >= getLevel(ch)) { out[id++] = sta[--id2]; out[id++] = ' '; } sta[id2++] = ch; } } void solve() { int i, sign; id = id2 = 0; for(i = sign = 0; buf[i] != '='; ++i) { if(isdigit(buf[i]) || buf[i] == '.') { out[id++] = buf[i]; sign = 1; } else { if(sign) { out[id++] = ' '; sign = 0; } check(buf[i]); } } while(id2) { if(sign) { out[id++] = ' '; sign = 0; } out[id++] = sta[--id2]; out[id++] = ' '; } out[id] = '\0'; printf("%s=\n", out); } int main() { // freopen("stdin.txt", "r", stdin); int t; scanf("%d", &t); while(t--) { scanf("%s", buf); solve(); } return 0; }
标签:nyoj467
原文地址:http://blog.csdn.net/chang_mu/article/details/40157149