Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include "Stack.h"
  4. #include <stdlib.h>
  5. using namespace std;
  6. void Stacker()
  7. {
  8. Stack s;
  9. s.push(1);
  10. s.push(2);
  11. s.push(3);
  12. s.push(4);
  13. s.push(5);
  14. s.push(6);
  15. s.push(7);
  16. cout << s.getTop()<<endl ;
  17. s.displayInOrder();
  18. s.displayInOrderOfInsertion();
  19. s.pop();
  20. s.displayInOrderOfInsertion();
  21. cout << endl << endl << endl << endl;
  22. }
  23. int Precedence(char c)
  24. {
  25. if (c == '^')
  26. {
  27. return 3;
  28. }
  29. else if (c == '*' || c == '/')
  30. {
  31. return 2;
  32. }
  33. else if(c =='+'||c=='-')
  34. {
  35. return 1;
  36. }
  37. else
  38. {
  39. return 0;
  40. }
  41. }
  42. string Infix()
  43. {
  44. string infix; //Input string
  45. string output = ""; //Output string
  46. Stack operators; //Store stack of operators
  47. string operate = "+-*/^";
  48. cout << "Please enter your infix expression : ";
  49. getline(cin, infix);
  50. for (int i = 0; i < infix.length(); i++)
  51. {
  52. if (infix[i] == ' ')
  53. {
  54.  
  55. }
  56. else
  57. {
  58. char c = infix[i];
  59. if (isalpha(c) || isdigit(c))
  60. {
  61. output += c;
  62. }
  63. else
  64. {
  65. if (c == ')')
  66. {
  67. //do something
  68. while (operators.getTop() != '(')// while top of stack is not '('
  69. {
  70. output += operators.getTop();
  71. operators.pop();
  72. }
  73. operators.pop();
  74. }
  75. else if (operators.isEmpty())
  76. {
  77. operators.push(c);
  78. }
  79. else
  80. {
  81. if (c == '(')
  82. {
  83. operators.push(c);
  84. }
  85. else
  86. {
  87. if (Precedence(operators.getTop()) >= Precedence(c))// If current stack top higher or equal precedence than c
  88. {
  89.  
  90. while (Precedence(operators.getTop()) >= Precedence(c))
  91. {
  92. output += operators.getTop();
  93. operators.pop();
  94. }
  95. operators.push(c);
  96. }
  97. else
  98. {
  99. operators.push(c);
  100. }
  101. }
  102. }
  103.  
  104. }
  105. cout << c << " read, Output string = (" << output << ") Stack operators = ("; operators.displayInOrder(); cout << ")";
  106. cout << endl;
  107. }
  108. }
  109. cout << "Leftover stack = "; operators.displayInOrder(); cout << endl;
  110. while (!operators.isEmpty())
  111. {
  112. output.append(1, operators.getTop());
  113. operators.pop();
  114. }
  115. cout << output << endl;
  116. return output;
  117. }
  118. int Cal(char c, int a, int b)
  119. {
  120. if (c == '/')
  121. {
  122. return a / b;
  123. }
  124. else if (c == '+')
  125. {
  126. return a + b;
  127. }
  128. else if (c == '*')
  129. {
  130. return a * b;
  131. }
  132. else if (c == '-')
  133. {
  134. return a - b;
  135. }
  136. else
  137. {
  138. return a ^ b;
  139. }
  140. }
  141. void Evaluate()
  142. {
  143. string infix = Infix();
  144. Stack s;// stack for numbers
  145. int first, second, result, num;
  146. for (int i = 0; i < infix.length(); i++)
  147. {
  148. char c = infix[i];
  149. if (isalnum(c))
  150. {
  151. //num is stored as int now;
  152. num = c - '0';
  153. s.push(num);
  154. }
  155. else
  156. {
  157. //imagine 21-
  158. first = s.getTop(); //1
  159. s.pop();
  160. second = s.getTop();//2
  161. s.pop();
  162. result = Cal(c, second, first);
  163. s.push(result);
  164. }
  165. }
  166. //COUT << static_cast <CHAR>(GETTOP())
  167. cout << "Result = " << static_cast <int>(s.getTop());
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement