1 #include<stdio.h>
2 #include<stdlib.h>
3 #define N 1010
4 //字符栈的操作
5 typedef struct
6 {
7 char *base;
8 char *top;
9 }SqStack;
10 int InitStack(SqStack &S)
11 {
12 S.base=(char *)malloc(N*sizeof(char));
13 if(!S.base) exit(1);
14 S.top=S.base;
15 return 1;
16 }
17 int StackEmpty(SqStack &S)
18 {
19 if(S.top==S.base)
20 return 1;
21 return 0;
22 }
23 int GetTop(SqStack S,char &e)
24 {
25 if(S.top==S.base) return 0;
26 e=*(S.top-1);
27 return 1;
28 }
29 int Push(SqStack &S,char e)
30 {
31 *S.top++=e;
32 return 1;
33 }
34 int Pop(SqStack &S,char &e)
35 {
36 if(S.top==S.base) return 0;
37 e=*--S.top;
38 return 1;
39 }
40 //转化的操作过程
41 static int i;
42 int Pass(char *s,char c)
43 {
44 s[i]=c;
45 i++;
46 return 1;
47 }
48 int level(char c,int k)
49 {
50 switch(c)
51 {
52 case '\n': return 1;
53 case ')': return k?2:5;
54 case '+':
55 case '-': return 3;
56 case '*':
57 case '/': return 4;
58 case '(': return k?5:2;
59 default : return 0;
60 }
61 }
62 int Precede(char c1,char c2)
63 {
64 if(level(c1,0)<level(c2,1)||c1=='\n'&&c2=='\n') return 0;
65 return 1;
66 }
67 int Converse(char *s)
68 {
69 SqStack OPTR;
70 char ch,x,c=getchar();
71 InitStack(OPTR); Push(OPTR,'\n');
72 while(!StackEmpty(OPTR))
73 {
74 if(!level(c,1))
75 Pass(s,c);
76 else
77 switch(c)
78 {
79 case '(': Push(OPTR,c);break;
80 case ')': Pop(OPTR,ch);while(ch!='('){Pass(s,ch);Pop(OPTR,ch);}break;
81 default :
82 while(GetTop(OPTR,ch)&&Precede(ch,c))
83 {
84 Pass(s,ch);Pop(OPTR,ch);
85 }
86 if(c!='\n')
87 Push(OPTR,c);
88 break;
89 }
90 if(c!='\n')
91 {
92 x=c;
93 c=getchar();
94 }
95 else
96 {
97 Pop(OPTR,ch);
98 Pass(s,ch);
99 }
100 }
101 s[i]='\0';
102 return 1;
103 }
104 //主函数
105 int main()
106 {
107 char s[1010];
108 int j,n;
109 scanf("%d%*c",&n);
110 while(n--){
111
112 i=0;
113 Converse(s);
114 for(j=0;s[j];++j)
115 putchar(s[j]);
116 }
117 return 0;
118 }