fork download
  1. #include<iostream>
  2. #include<cstring>
  3. #include<stack>
  4. #include<vector>
  5. #include<cstdlib>
  6. #include<algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int getWeight(char ch) {
  11. switch (ch) {
  12. case '/':
  13. case '*': return 2;
  14. case '+':
  15. case '-': return 1;
  16. default : return 0;
  17. }
  18. }
  19. void infix_postfix(vector<char>& postfix)
  20. {
  21. int i=0;
  22. while(postfix.size()>1)
  23. {
  24. if(isdigit(postfix[i]))
  25. {
  26. i++;
  27. }
  28. else
  29. {
  30. switch(postfix[i])
  31. {
  32. case '+':
  33. {
  34. postfix[i-2]=(postfix[i-2]-48)+ (postfix[i-1]-48)+48;
  35. postfix.erase(postfix.begin()+ i);
  36. postfix.erase(postfix.begin()+ i-1);
  37. break;
  38. }
  39. case '-':
  40. {
  41. postfix[i-2]=(postfix[i-2]-48)- (postfix[i-1]-48)+48;
  42. postfix.erase(postfix.begin()+ i);
  43. postfix.erase(postfix.begin()+ i-1);
  44. break;
  45. }
  46. case '*':
  47. {
  48. postfix[i-2]=(postfix[i-2]-48)* (postfix[i-1]-48)+48;
  49. postfix.erase(postfix.begin()+ i);
  50. postfix.erase(postfix.begin()+ i-1);
  51. break;
  52. }
  53. case '/':
  54. {
  55. postfix[i-2]=(postfix[i-2]-48)/ (postfix[i-1]-48)+48;
  56. postfix.erase(postfix.begin()+ i);
  57. postfix.erase(postfix.begin()+ i-1);
  58. break;
  59. }
  60. }
  61. i=i-1;
  62. }
  63. }
  64. }
  65. void infix2postfix(char infix[], vector<char>& postfix, int size) {
  66. stack<char> s;
  67. int weight;
  68. int i = 0;
  69. int k = 0;
  70. char ch;
  71. while (i < size)
  72. {
  73. ch = infix[i];
  74. if (ch == '(')
  75. {
  76. s.push(ch);
  77. i++;
  78. continue;
  79. }
  80. if (ch == ')')
  81. {
  82. while (!s.empty() && s.top() != '(')
  83. {
  84. // postfix[k++] = s.top();
  85. postfix.push_back(s.top());
  86. s.pop();
  87. }
  88. if (!s.empty())
  89. {
  90. s.pop();
  91.  
  92. }
  93. i++;
  94. continue;
  95.  
  96. }
  97. weight = getWeight(ch);
  98. if (weight == 0)
  99. {
  100. // postfix[k++] = ch;
  101. postfix.push_back(ch);
  102.  
  103. }
  104. else
  105. {
  106. if (s.empty())
  107. {
  108. s.push(ch);
  109.  
  110. }
  111. else
  112. {
  113. while (!s.empty() && s.top() != '(' &&
  114. weight <= getWeight(s.top()))
  115. {
  116. //postfix[k++] = s.top();
  117. postfix.push_back(s.top());
  118. s.pop();
  119.  
  120. }
  121. s.push(ch);
  122. }
  123.  
  124. }
  125. i++;
  126.  
  127. }
  128. while (!s.empty()) {
  129. //postfix[k++] = s.top();
  130. postfix.push_back(s.top());
  131. s.pop();
  132. }
  133. // postfix[k] = 0;
  134. }
  135. int main() {
  136. char infix[] = "9*5*4-(8-4/2)";
  137. int size = strlen(infix);
  138. // char postfix[size];
  139. vector<char>postfix;
  140. infix2postfix(infix,postfix, size);
  141. cout<<"\nInfix Expression :: "<<infix;
  142. cout<<"\nPostfix Expression :: ";
  143. for(int i=0;i<postfix.size();i++)
  144. cout<<postfix[i];
  145. infix_postfix(postfix);
  146. cout<<"\nanswer="<<(int)postfix[0];
  147. //cout<<endl<<postfix.size()<<endl;
  148. return 0;
  149.  
  150. }
Success #stdin #stdout 0s 16056KB
stdin
Standard input is empty
stdout
Infix Expression :: 9*5*4-(8-4/2)
Postfix Expression :: 95*4*842/--
answer=-34