Guest User

Untitled

a guest
Jan 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. void ArithmeticExpression::inputAndConvertToPostfix()
  2. {
  3. char inputChar; //declaring inputChar
  4. int i = 0; //inizalize i to 0
  5.  
  6. cout << "Enter the Arithmetic Expression(No Spaces): ";
  7.  
  8. while( ( inputChar = static_cast<char>( cin.get() ) ) != 'n' )
  9. {
  10. if (i >= MAXSIZE) break; //exits program if i is greater than or equal to 100
  11.  
  12. if(isdigit(inputChar) || isOperator(inputChar))
  13. {
  14. inFix[i] = inputChar; //copies each char to inFix array
  15. cout << inFix[i] << endl;
  16. }
  17. else
  18. cout << "You entered an invalid Arithmetic Expressionnn" ;
  19.  
  20. }
  21.  
  22. // increment i;
  23. i++;
  24. convertToPostfix(inFix, postFix);
  25.  
  26.  
  27. }
  28.  
  29.  
  30.  
  31.  
  32. bool ArithmeticExpression::isOperator(char currentChar)
  33. {
  34.  
  35. if(currentChar == '+')
  36. return true;
  37. else if(currentChar == '-')
  38. return true;
  39. else if(currentChar == '*')
  40. return true;
  41. else if(currentChar == '/')
  42. return true;
  43. else if(currentChar == '^')
  44. return true;
  45. else if(currentChar == '%')
  46. return true;
  47. else
  48. return false;
  49. }
  50.  
  51. bool ArithmeticExpression::precedence(char operator1, char operator2)
  52. {
  53. if ( operator1 == '^' )
  54. return true;
  55. else if ( operator2 == '^' )
  56. return false;
  57. else if ( operator1 == '*' || operator1 == '/' )
  58. return true;
  59. else if ( operator1 == '+' || operator1 == '-' )
  60. if ( operator2 == '*' || operator2 == '/' )
  61. return false;
  62. else
  63. return true;
  64.  
  65. return false;
  66. }
  67.  
  68. void ArithmeticExpression::convertToPostfix(char * const inFix, char * const postFix)
  69. {
  70. Stack2<char> stack;
  71.  
  72. const char lp = '(';
  73.  
  74. stack.push(lp); //Push a left parenthesis ‘(‘ onto the stack.
  75.  
  76. strcat(inFix,")");//Appends a right parenthesis ‘)’ to the end of infix.
  77.  
  78. // int i = 0;
  79. int j = 0;
  80.  
  81. if(!stack.isEmpty())
  82. {
  83.  
  84. for(int i = 0;i < 100;){
  85.  
  86. if(isdigit(inFix[i]))
  87. {
  88. postFix[j] = inFix[i];
  89. cout << "This is Post Fix for the first If: " << postFix[j] << endl;
  90. i++;
  91. j++;
  92. }
  93.  
  94. if(inFix[i] == '(')
  95. {
  96. stack.push(inFix[i]);
  97. cout << "The InFix was a (" << endl;
  98. i++;
  99. //j++;
  100. }
  101.  
  102. if(isOperator(inFix[i]))
  103. {
  104. char operator1 = inFix[i];
  105.  
  106. cout << "CUrrent inFix is a operator" << endl;
  107. if(isOperator(stack.getTopPtr()->getData()))
  108. {
  109. cout << "The stack top ptr is a operator1" << endl;
  110. char operator2 = stack.getTopPtr()->getData();
  111. if(precedence(operator1,operator2))
  112. {
  113. //if(isOperator(stack.getTopPtr()->getData())){
  114. cout << "The stack top ptr is a operato2" << endl;
  115. postFix[j] = stack.pop();
  116. cout << "this is post fix " << postFix[j] << endl;
  117. i++;
  118. j++;
  119. // }
  120.  
  121. }
  122.  
  123. }
  124. else
  125.  
  126. stack.push(inFix[i]);
  127. // cout << "Top Ptr is a: "<< stack.getTopPtr()->getData() << endl;
  128.  
  129.  
  130.  
  131. }
  132.  
  133. for(int r = 0;r != '';r++)
  134. cout << postFix[r] << " ";
  135.  
  136. if(inFix[i] == ')')
  137. {
  138. while(stack.stackTop()!= '(')
  139. {
  140. postFix[j] = stack.pop();
  141. i++;
  142. j++;
  143. }
  144. stack.pop();
  145.  
  146. }
  147. }
  148. }
  149.  
  150. }
Add Comment
Please, Sign In to add comment