Depressing C Xiaojia (1)
Depressing C Xiaojia (1)Time Limit: 1000 MS | memory limit: 65535 KBDifficulty: 3Description
The expressions we are familiar with, such as a + B and a + B * (c + d), all belong to the infix expression. The infix expression is (for Binary operators) operator in the middle of two operands: num1 operand num2. Similarly, the suffix expression is the operator after two operands: num1 num2 operand. The ACM team's "C Xiaojia" is depressing how to convert an infix expression to a suffix expression. Now, please design a program to help C Xiaojia convert the infix expression to a suffix expression. To simplify the problem, the operands are single digits, and the operators are only +-*/and parentheses.
Input
Input T in the first line, indicating that T groups of test data exist (T <10 ).
Each group of test data has only one row. It is a string of no more than 1000 characters, indicating this expression. This expression only contains the +-*/and parentheses. Parentheses can be nested. Data ensures that no negative numbers are displayed in the input operations. And the input data will not be mismatched.
Output
Each group of outputs is a separate line, and the suffix expression of the output conversion is used.
Sample Input
21+2(1+2)*3+4*5
Sample output
12+12+3*45*+
Code
#include
#include
#define max 1000int main(void){int n,i,t,s;char a[max],ch[max];scanf("%d",&n);while(n--){s=-1;t=0;char str[max];scanf("%s",str);for(i=0;i
='0'&&str[i]<='9'){ch[t++]=str[i];}else if(str[i]=='('){a[++s]=str[i];}else if(str[i]==')'){while(s>=0&&a[s]!='('){ch[t++]=a[s--];}s--;}else if(str[i]=='+'||str[i]=='-'){while(s>=0&&a[s]!='('){ch[t++]=a[s--];}a[++s]=str[i];}else {while(a[s]=='*'||a[s]=='/'){ch[t++]=a[s--];}a[++s]=str[i];}}while(s>=0){ch[t++]=a[s--];}ch[t]='\0';printf("%s\n",ch);}return 0;}