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