Prefix expressions(recursion)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 46
Reputation: blackdove is an unknown quantity at this point 
Solved Threads: 0
blackdove blackdove is offline Offline
Light Poster

Prefix expressions(recursion)

 
0
  #1
Oct 18th, 2005
I have been given a lab assignment that should take a prefix expression such as *+-23/427 and use recursion to solve its infix equivilant ((2-3)+(4/2))*7. So far, the code I have will work for an equation with 2 or 3 numbers. Any more and it starts returning weird results. Its probably a simple mistake on my part, but I'm not too great with recursion and I'm having a hard time figuring out whats going wrong. Any help would be greatly appreciated.

  1.  
  2. #include <math.h>
  3. #include <iostream.h>
  4. #include <string.h>
  5.  
  6. class Evaluate {
  7. /* does prefix evaluations of expressions holding single digit values & formatted correctly
  8. infix and postfix will be added next year
  9. */
  10. public:
  11. // each of the following returns an integer representing the result from
  12. // evaluating the expression exp using the indicated type of notation
  13. int preFix(char exp[]); // prefix notation
  14. private:
  15. // recursively implements preFix
  16. int doPreFix (char exp[], int &left);
  17.  
  18. // other private methods
  19. int doOp (int num1, char op, int num2); // returns num1 op num2
  20. }; // Evaluate
  21.  
  22. void main()
  23. {
  24. Evaluate it;
  25.  
  26. char expres1[] = "+27";
  27. cout <<"01234567890123456789\n" <<expres1 <<endl;
  28. cout <<endl <<expres1 <<" = " <<it.preFix (expres1)<<endl<<endl;//outputs 9
  29. cout << "--------------------------------------------------------" << endl;
  30. char expres2[]= "+-235";
  31. cout <<"01234567890123456789\n" <<expres2 <<endl;
  32. cout <<endl <<expres2 <<" = " <<it.preFix (expres2)<<endl<<endl;//outputs 4
  33. cout << "--------------------------------------------------------" << endl;
  34. char expres3[]= "+1*2-41";
  35. cout <<"01234567890123456789\n" <<expres3 <<endl;
  36. cout <<endl <<expres3 <<" = " <<it.preFix (expres3)<<endl<<endl;//should output 7(outputs a 2)
  37. cout << "--------------------------------------------------------" << endl;
  38. char expres[]= "*+-23/427";
  39. cout <<"01234567890123456789\n" <<expres <<endl;
  40. cout <<endl <<expres <<" = " <<it.preFix (expres)<<endl<<endl;//should output 7 (outputs a 0)
  41. }//main
  42.  
  43. int Evaluate::preFix(char exp[])
  44. {
  45. int left= 0;
  46. return doPreFix (exp, left);
  47. } // Evaluate::inFix
  48.  
  49. int Evaluate::doOp (int num1, char op, int num2)
  50. {
  51. switch (op) {//performs the correct operation accoding to the operator present
  52. case '+': return num1 + num2;
  53. case '-': return num1 - num2;
  54. case '*': return num1 * num2;
  55. case '/':
  56. if (num2==0) {
  57. cerr <<"Error: division by 0" <<endl;
  58. return 0;
  59. } // if
  60. return num1 / num2;
  61. default:
  62. cerr <<"Error in formula: +-*/ expected, found a " <<op <<endl;
  63. return 0;
  64. } // switch
  65. } // Evaluate::doOp
  66.  
  67. int Evaluate::doPreFix (char exp[], int &left)
  68. {//assumes the first character is always an operator,
  69. //and that each number is a positive integer only one digit long
  70. int firstNum, secondNum, result;
  71. char op;
  72.  
  73. if(exp[left] == '+' || exp[left] == '-' || exp[left] == '*' || exp[left] == '/')
  74. {//takes in first operator
  75. op = exp[left];//sets op to the position in the string
  76. cout << " first op: " << op << "\t" << "left: " << left << endl;
  77. left++;//increments left
  78. firstNum = doPreFix(exp, left);
  79. }
  80. else if(exp[left] != NULL)//if the character isn't an operator, then it must be a number
  81. {
  82. firstNum = int (exp[left]) - int ('0');//convert from a character to an integer
  83. cout << " firstNum: " << firstNum << "\t" << "left: " << left << endl;
  84. left++;
  85. return (firstNum);
  86. }
  87. if(exp[left] == '+' || exp[left] == '-' || exp[left] == '*' || exp[left] == '/')
  88. {
  89. op = exp[left];
  90. cout << " second op: " << op << "\t"<< "left: " << left << endl;
  91. left++;
  92. secondNum = doPreFix(exp, left);
  93. }
  94. else if(exp[left] != NULL)//if the character isn't an operator, then it must be a number
  95. {//and if the character isnt null.
  96. secondNum = int (exp[left]) - int ('0');
  97. cout << " second Num: " << secondNum << "\t" << "left: " << left<< endl;
  98. left++;
  99. doOp(firstNum, op, secondNum);
  100. // return secondNum;
  101. }
  102. cout << "First Number: " << firstNum << " Operator: " << op << " Second number: "
  103. << secondNum;
  104. result = doOp(firstNum, op, secondNum);
  105.  
  106. return result;
  107.  
  108. } // Evaluate::doPreFix
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC