Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stack>
  4. #include <string>
  5. #include <cctype>
  6. #include <cmath>
  7. using namespace std;
  8. stack <char> ParenthesisStack;
  9. stack <int> NumberStack;
  10. void printStack(stack <char> ParenthesisStack) {
  11. while (!ParenthesisStack.empty())
  12. {
  13. cout << " " << ParenthesisStack.top();
  14. ParenthesisStack.pop();
  15. }
  16. cout << endl;
  17. }
  18.  
  19. void evaluatePostFix(string n)
  20. {
  21. for (int i = 0; i<n.length(); i++) {
  22. if(n[i] == ' ' || n[i] == ',')
  23. continue;
  24. else if (isdigit(n[i]))
  25. {
  26. int operand = 0;
  27. while(isdigit(n[i]))
  28. {
  29. operand = (operand*10) + (n[i] - '0');
  30. i++;
  31. }
  32. i--;
  33. NumberStack.push(operand);
  34. }
  35. else
  36. {
  37. int val1 = NumberStack.top(); NumberStack.pop();
  38. int val2 = NumberStack.top(); NumberStack.pop();
  39. if(n[i] == '+')
  40. cout << val1 + val2 << endl;
  41. else if(n[i] == '-')
  42. cout << val1 - val2 << endl;
  43. else if(n[i] == '*')
  44. cout << val1 * val2 << endl;
  45. else if(n[i] == '/')
  46. cout << val1 / val2 << endl;
  47. else if(n[i] == '^')
  48. cout << pow(val1, val2) << endl;
  49.  
  50. else cout<<"Unexpected Error" << endl;
  51. }
  52. }
  53. }
  54.  
  55. int evaluateInFix(int num1, int num2)
  56. {
  57. return 0;
  58. }
  59.  
  60. void checkParenthesis(string n)
  61. {
  62. for (int i = 0; i<n.length(); i++) {
  63. if (n[i] == '(' || n[i] == '[' || n[i] == '{') {
  64. ParenthesisStack.push(n[i]);
  65. cout << "pushing" << endl;
  66. }
  67. else if (n[i] == ')' || n[i] == ']' || n[i] == '}') {
  68. if (ParenthesisStack.empty()) {
  69. cout << "Stack Empty" << endl;
  70. }
  71. else {
  72. ParenthesisStack.pop();
  73. cout << "popping" << endl;
  74. }
  75. }
  76. else {
  77. continue;
  78. }
  79. }
  80. printStack(ParenthesisStack);
  81. }
  82.  
  83. void checkString(string n)
  84. {
  85. bool isOperator = false;
  86.  
  87. for (int i = 0; i<n.length(); i++)
  88. {
  89. if (n[i] == ' ' || n[i] == ',')
  90. continue;
  91.  
  92. else if (n[i] == '+' || n[i] == '-' || n[i] == '/' || n[i] == '*' || n[i] == '^')
  93. {
  94. cout << "operator" << " ";
  95. }
  96. else if (isdigit(n[i]))
  97. {
  98.  
  99.  
  100. while(isdigit(n[i]))
  101. {
  102. i++;
  103. if(n[i] == '+' || n[i] == '-' || n[i] == '/' || n[i] == '*')
  104. {
  105. isOperator = true;
  106. }
  107. }
  108. cout << "integer" << " ";
  109. if (isOperator == true)
  110. {
  111. cout << "operator" << " ";
  112. isOperator = false;
  113. }
  114. }
  115.  
  116. }
  117. cout << endl;
  118. }
  119.  
  120. int main() {
  121. string n;
  122. cout << "Enter mathematical calculation string: ";
  123. getline(cin, n);
  124. checkParenthesis(n);
  125. checkString(n);
  126. evaluatePostFix(n);
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement