Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. //check if char
  6. bool isChar(char a) {
  7. a = int(a);
  8. if (((a >= 65) && (a <= 90)) || ((a >= 97) && (a <= 122)))
  9. return true;
  10. else return false;
  11. }
  12.  
  13. //check if operator
  14. bool isOperator(char a) {
  15. switch (a) {
  16. case '+': return true;
  17. case '-': return true;
  18. case '*': return true;
  19. case '/': return true;
  20. case '(': return true;
  21. case ')': return true;
  22. case '^': return true;
  23. default: return false;
  24. }
  25. }
  26.  
  27. int order(char op) {
  28. switch (op){
  29. case '+': return 1;
  30. case '-': return 1;
  31. case '*': return 2;
  32. case '/': return 2;
  33. case '^': return 3;
  34.  
  35. default: return 0;
  36. }
  37. }
  38.  
  39. bool isHigher(char a, char b) {
  40. if (order(a) >= order(b)) return true;
  41. else return false;
  42. }
  43.  
  44. //function pops string
  45. string pop(string s) {
  46. return s.substr(0, s.size() - 1);
  47. }
  48.  
  49. //function turns infix string into postfix
  50. string toPostfix(string infix) {
  51. string postfix, stack;
  52.  
  53. for (int i = 0; i<infix.length(); ++i) {
  54.  
  55. //if operand
  56. if (isChar(infix[i])) {
  57. postfix.push_back(infix[i]);
  58. }
  59.  
  60. //if operator
  61. else if (isOperator(infix[i])) {
  62.  
  63. //if stack is empty
  64. if (stack.empty()) {
  65. stack.push_back(infix[i]);
  66. }
  67.  
  68. //if stack not empty
  69. else if (!stack.empty()) {
  70.  
  71. //if (
  72. if (infix[i] == '(') {
  73. stack.push_back(infix[i]);
  74. }
  75.  
  76. //if ) is encountered pop till ( to postfix
  77. else if (infix[i] == ')') {
  78.  
  79. while (*(stack.end() - 1) != '(') {
  80.  
  81. postfix.push_back(*(stack.end() - 1));
  82. stack = pop(stack);
  83. }
  84. stack = pop(stack);
  85. }
  86.  
  87. else {
  88. //pop until tos has lesser precedence or tos is null.
  89. while (isHigher(*(stack.end() - 1), infix[i])) {
  90. postfix.push_back(*(stack.end() - 1));
  91. stack = pop(stack);
  92. }
  93.  
  94. if (!(isHigher(*(stack.end() - 1), infix[i])))
  95. stack.push_back(infix[i]);
  96. }
  97. }
  98. }
  99. }
  100.  
  101. while (!stack.empty()) {
  102. string::iterator tos = stack.end() - 1;
  103. postfix.push_back(*tos);
  104. stack = pop(stack);
  105. }
  106.  
  107. return postfix;
  108. }
  109.  
  110. int main() {
  111.  
  112. int t;
  113. cin >> t;
  114.  
  115. string infix[20], postfix[20]; //problems here, can't put array as size t, needs to be "defined" amount
  116.  
  117. for (int i = 0; i < t; ++i) {
  118. cin >> infix[i];
  119. }
  120.  
  121. for (int i = 0; i < t; ++i) {
  122. postfix[i] = toPostfix(infix[i]);
  123. cout << "\n" << postfix[i];
  124. }
  125.  
  126. return 0;
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement