Copying a vector to a string array

I am working on a homework problem and am stuck on something. My program is supposed to take an expression such as the one that follows:

5 + 3 * 2 + 4 / 1 + 1

The code is supposed to evaluate the expression properly obeying all of the rules of mathematics. So far I have my program to where it will strip the numbers and symbols apart and put them into a vector which gives me the following output:

5 3 2 4 1 1 + * + / +

I figure that I can use this to then evaluate the expression. The problem is that my teacher requires me to use the following function header to recursively perform the math:

double evaluateExpression(string exp)

I believe that I need to copy the vector into an array and send it to the evaluateExpression function, but I am not sure how to copy the vector into a string array. Any advice?

I excluded the code which reorganizes the expression as I feel like it was not necessary to include.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main()
{

	cout << "Enter an expression: ";

	string input;
	getline(cin, input);

	vector<string> splitVector;

	splitVector = split(input);

	for (unsigned int i = 0; i < splitVector.size(); i++)
		cout << splitVector.at(i) << " ";

	cout << endl;

	//Copy vector to array and then send it to method to be calculated
	double sum = 0; 

	sum = evaluateExpression(splitVector);

	return 0;
}

double evaluateExpression(string exp)
{

}
Last edited on
5 3 2 4 1 1 + * + / +
The output is not correct because it does not take the precedence into acount.

It should be:

3 2 5 ... * + ...

because the multiplication has do be done first.

The steps in evaluateExpression(...) is as follows:

- split the string
- find the next expression with the highes or same precedence (3 * 2)
- do the calculation -> 6
- form a string without the calculated expressin (5 + 4 / 1 + 1) and without the next operator (+)
- call evaluateExpression(...) with the reduced string (5 + 4 / 1 + 1)
- calculate the result according to the next operator (+)
I was going to put the vector output in a string and then have the evaluateExpression function determine precedence and perform the calculations appropriately. But perhaps this is the wrong way.
Topic archived. No new replies allowed.