How can I make it so I dont need spaces to evaluate my postfix?

I don't want to have to include spaces when I cin my expression. Id like to just write 23+ not 2 3 +

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
30
31
32
33
34
35
36
37
38
39
40
41
42
double Expression::evaluate(string myExpression){
    stack<double> Stack;         //declaring stack of double, as we are only dealing with numbers

            for(int i = 0;i< myExpression.length();i++) {
                     //if theres a space or comma.. keep going.
                if(myExpression[i] == ' ' || myExpression[i] == ',') continue;
                else if(isOperator(myExpression[i])) {  //Expression is postfix so if we run into Operator pop two operands and perform operation
			// Pop two operands.
                int operand2 = Stack.top(); Stack.pop();
                int operand1 = Stack.top(); Stack.pop();
			// Perform operation
                int result = doOperation(myExpression[i], operand1, operand2);
			//Push back result of operation on stack.
                Stack.push(result);
            }

       else if(isANumber(myExpression[i])){
			// Extract the numeric operand from the string
			// Keep incrementing i as long as you are getting a numeric digit.
			int operand = 0;
			while(i<myExpression.length() && isANumber(myExpression[i])) {
				// For a number with more than one digits, as we are scanning from left to right.
				// Everytime , we get a digit towards right, we can multiply current total in operand by 10
				// and add the new digit.
				operand = (operand*10) + (myExpression[i] - '0');
				i++;
			}
			// Finally, you will come out of while loop with i set to a non-numeric character or end of string
			// decrement i because it will be incremented in increment section of loop once again.
			// We do not want to skip the non-numeric character by incrementing i twice.
			i--;

			// Push operand on stack.
			Stack.push(operand);
		}
	}
	// If expression is in correct format, Stack will finally have one element. This will be the output.
	return Stack.top();
}


Last edited on
Hello tit0n,

I find it difficult to figure out you code without knowing what "myExpression" contains and how you arrived to that information. An example of "myExpression" and if it is not to much to ask the code that collects the information for that variable.

Understanding what you have to work with helps understand how the code works.

Andy
No relevance to my question at all. I was saying when I enter 23+ my EVALUATION does not work properly. If I type 2 3 + I evaluates. No clue what you read. Anyways not gonna pointlessly argue. If anyone has any tips this one is bugging me. Please.
Hello tit0n,

What I was able to discern and the conclusion I came to is that the function is designed to work with every other element of the array. I believe a better approach would be to have the string look more like 23 + 21 and use a stringstream to extract each "operand" and the "function" into three variables instead of the for and while loops. It would save on a lot of confusion and work.

Another point I have a problem with is that you define "Stack" as a "double" and return a "double" from the function yet all the numeric variables in the function are "int"s. With a very possible problem with lines 9 and 10 in converting from a "double" to an "int" which could be a loss of information when you loose the decimal portion of the "double" if there should happen to be a decimal portion.

Lastly the function appears to be a member function of a class and I was wondering why you pass a string to the function that is outside of the class. I seams to me that "myExpression" should be a member variable of the class that the function would have direct access to.

Hope that helps,

Andy
Thanks for the tips. And yes I was messing with doubles/ints it wasnt originally like this. Thank you.
Topic archived. No new replies allowed.