blackdove 0 Light Poster

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.

#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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.