Depressing C Xiaojia (1)

Source: Internet
Author: User

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;}
   
  
 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.