• Source
    1. #include<iostream>
    2. #include<stdio.h>
    3. #include<stack>
    4. #include<queue>
    5. #include<string.h>
    6.  
    7. using namespace std;
    8.  
    9. stack<char>st;
    10. queue<char>postfix;
    11.  
    12. char arr[10];
    13.  
    14. void Clear()
    15. {
    16. while(!st.empty())
    17. st.pop();
    18. }
    19.  
    20. int main()
    21. {
    22. int len,a,t,i;
    23.  
    24. string str1;
    25.  
    26. scanf("%d",&t);
    27.  
    28. getchar();
    29. getchar();
    30.  
    31. for(a=1; a<=t; a++)
    32. {
    33. str1+="(";
    34.  
    35. while(gets(arr))
    36. {
    37. if(arr[0]=='\0')
    38. {
    39. break;
    40. }
    41.  
    42. str1+=arr[0];
    43. }
    44.  
    45. str1+=')';
    46.  
    47. len = str1.length();
    48.  
    49. for(i=0; i<len; i++)
    50. {
    51. if(str1[i]=='(')
    52. {
    53. st.push(str1[i]);
    54. }
    55. else if(str1[i]>='0'&&str1[i]<='9')
    56. {
    57. postfix.push(str1[i]);
    58. }
    59. else if(str1[i]=='*'|str1[i]=='/')
    60. {
    61. while(st.size() && (st.top()=='*'|st.top()=='/'))
    62. {
    63.  
    64. postfix.push(st.top());
    65.  
    66. st.pop();
    67. }
    68.  
    69. st.push(str1[i]);
    70. }
    71. else if(str1[i]=='+'|str1[i]=='-')
    72. {
    73. while(st.size() && (st.top()=='+'|st.top()=='-'|st.top()=='*'|st.top()=='/'))
    74. {
    75.  
    76. postfix.push(st.top());
    77.  
    78. st.pop();
    79. }
    80.  
    81. st.push(str1[i]);
    82. }
    83. else if(str1[i]==')')
    84. {
    85. while(st.size() && st.top()!='(')
    86. {
    87. postfix.push(st.top());
    88.  
    89. st.pop();
    90. }
    91. st.pop();
    92. }
    93. }
    94.  
    95. while(!postfix.empty())
    96. {
    97. printf("%c",postfix.front());
    98. postfix.pop();
    99. }
    100.  
    101. printf("\n");
    102.  
    103. if(a<t)
    104. {
    105. printf("\n");
    106. }
    107.  
    108. Clear();
    109.  
    110. str1="";
    111.  
    112. }
    113.  
    114. return 0;
    115. }