| | |
Prefix expressions(recursion)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 46
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
#include <math.h> #include <iostream.h> #include <string.h> class Evaluate { /* does prefix evaluations of expressions holding single digit values & formatted correctly infix and postfix will be added next year */ public: // each of the following returns an integer representing the result from // evaluating the expression exp using the indicated type of notation int preFix(char exp[]); // prefix notation private: // recursively implements preFix int doPreFix (char exp[], int &left); // other private methods int doOp (int num1, char op, int num2); // returns num1 op num2 }; // Evaluate void main() { Evaluate it; char expres1[] = "+27"; cout <<"01234567890123456789\n" <<expres1 <<endl; cout <<endl <<expres1 <<" = " <<it.preFix (expres1)<<endl<<endl;//outputs 9 cout << "--------------------------------------------------------" << endl; char expres2[]= "+-235"; cout <<"01234567890123456789\n" <<expres2 <<endl; cout <<endl <<expres2 <<" = " <<it.preFix (expres2)<<endl<<endl;//outputs 4 cout << "--------------------------------------------------------" << endl; char expres3[]= "+1*2-41"; cout <<"01234567890123456789\n" <<expres3 <<endl; cout <<endl <<expres3 <<" = " <<it.preFix (expres3)<<endl<<endl;//should output 7(outputs a 2) cout << "--------------------------------------------------------" << endl; char expres[]= "*+-23/427"; cout <<"01234567890123456789\n" <<expres <<endl; cout <<endl <<expres <<" = " <<it.preFix (expres)<<endl<<endl;//should output 7 (outputs a 0) }//main int Evaluate::preFix(char exp[]) { int left= 0; return doPreFix (exp, left); } // Evaluate::inFix int Evaluate::doOp (int num1, char op, int num2) { switch (op) {//performs the correct operation accoding to the operator present case '+': return num1 + num2; case '-': return num1 - num2; case '*': return num1 * num2; case '/': if (num2==0) { cerr <<"Error: division by 0" <<endl; return 0; } // if return num1 / num2; default: cerr <<"Error in formula: +-*/ expected, found a " <<op <<endl; return 0; } // switch } // Evaluate::doOp int Evaluate::doPreFix (char exp[], int &left) {//assumes the first character is always an operator, //and that each number is a positive integer only one digit long int firstNum, secondNum, result; char op; if(exp[left] == '+' || exp[left] == '-' || exp[left] == '*' || exp[left] == '/') {//takes in first operator op = exp[left];//sets op to the position in the string cout << " first op: " << op << "\t" << "left: " << left << endl; left++;//increments left firstNum = doPreFix(exp, left); } else if(exp[left] != NULL)//if the character isn't an operator, then it must be a number { firstNum = int (exp[left]) - int ('0');//convert from a character to an integer cout << " firstNum: " << firstNum << "\t" << "left: " << left << endl; left++; return (firstNum); } if(exp[left] == '+' || exp[left] == '-' || exp[left] == '*' || exp[left] == '/') { op = exp[left]; cout << " second op: " << op << "\t"<< "left: " << left << endl; left++; secondNum = doPreFix(exp, left); } else if(exp[left] != NULL)//if the character isn't an operator, then it must be a number {//and if the character isnt null. secondNum = int (exp[left]) - int ('0'); cout << " second Num: " << secondNum << "\t" << "left: " << left<< endl; left++; doOp(firstNum, op, secondNum); // return secondNum; } cout << "First Number: " << firstNum << " Operator: " << op << " Second number: " << secondNum; result = doOp(firstNum, op, secondNum); return result; } // Evaluate::doPreFix
![]() |
Similar Threads
- Parenthesize following expressions (C++)
- need Recursion help (C)
- Recursion (C++)
- mod_rewrite: help with regular expressions (Linux Servers and Apache)
- powers of two, recursion. (C)
- Create menu from properties file by recursion (Java)
- matching regular expressions (Java)
- Recursion (C)
Other Threads in the C++ Forum
- Previous Thread: Anyway can help me to solve this problem???
- Next Thread: accessing database
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





