1

I am asking the user to input an expression which will be evaluated in postfix notation. The beginning of the expression is the variable name where the answer of the evaluated expression will be stored. Ex: A 4 5 * 6 + 2 * 1 – 6 / 4 2 + 3 * * = where A is the variable name and the equal sign means the answer to the expression will be stored in the variable A. The OUT A statement means that the number stored in the variable A will be printed out.

What I need help with is that when I input the second expression, I do not get the right answer. For example, my first expression A 4 5 * 6 + 2 * 1 – 6 / 4 2 + 3 * * = will evaluate to 153 and then when I input my second expression B A 10 * 35.50 + =, it has to evaluate to 1565.5, but it doesn't. It evaluates to 35.5. I cannot figure out why I am getting the wrong answer. Also, I need help with the OUT statement.

3
  • 1
    hint: use an array of 26 doubles to store the variable values, one for each letter (assuming your variables are all single uppercase letters) Commented May 13, 2015 at 4:48
  • The same way you handle numbers and operators, you have to handle the variables and evaluate or assign them. Now you are not handling them inside your loop but discarding them Commented May 13, 2015 at 4:52
  • Can I do something like this and add it to the loop? else if (isalpha(token) && assignOperator == '=') { if (token == 'A') { mapVars1[token] = stackIt.top(); } else if (token == 'B') { mapVars1[token] == stackIt.top(); } } Commented May 13, 2015 at 4:59

2 Answers 2

1
else if (isalpha(expr1[i]))
{
    stackIt.push(mapVars1[expr1[i]]);
}

Will place the variable, or zero if the variable has not been set, onto the stack.

else if (isalpha(expr1[i]))
{
    map<char, double>::iterator found = mapVars1.find(expr1[i]);
    if (found != mapVars1.end())
    {
        stackIt.push(found->second);
    }
    else
    {
        // error message and exit loop
    }
}

Is probably better.

Other suggestions:

Compilers are pretty sharp these days, but you may get a bit out of char cur = expr1[i]; and then using cur (or suitably descriptive variable name) in place of the remaining expr1[i]s in the loop.

Consider using isdigit instead of expr1[i] >= '0' && expr1[i] <= '9'

Test your code for expressions with multiple spaces in a row or a space after an operator. It looks like you will re-add the last number you parsed.

Test for input like 123a456. You might not like the result.

If spaces after each token in the expression are specified in the expression protocol, placing your input string into a stringstream will allow you to remove a great deal of your parsing code.

stringstream in(expr1);
string token;

while (in >> token)
{
    if (token == "+" || token == "-'" || ...)
    {
        // operator code
    }
    else if (token == "=")
    {
        // equals code
    }
    else if (mapVars1.find(token) != mapVars1.end())
    {
        // push variable
    }
    else if (token.length() > 0)
    {
        char * endp;
        double val = strtod(token.c_str(), &endp);
        if (*endp == '\0')
        {
            // push val
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

To use previous symbol names in subsequent expressions add this to the if statements in your parsing loop:

else if (expr1[i] >= 'A' && expr1[i] <= 'Z')
{
    stackIt.push(mapVars1[expr[i]]);
}

Also you need to pass mapVars by reference to accumulate its contents across Eval calls:

void Eval(string expr1, map<char, double> & mapVars1)

For the output (or any) other command I would recommend parsing the command token that's at the front of the string first. Then call different evaluators based on the command string. You are trying to check for OUT right now after you have already tried to evaluate the string as an arithmetic assignment command. You need to make that choice first.

4 Comments

@Kay I'd put the OUT check right at the top of the function. No point doing the rest of it's just an print of a variable unless OUT 45+= is a valid expression. Oh, and test what happens with 4K*= if K has not been set.
@ user4581301 Can I do if(expr1.substr(0, 2) == "OUT"){} in the loop? but how would I print out the value stored in the variable? I am confused on what will go inside those brackets.
@kay take too long to explain in comments. Get your variable code working, and then ask another question. As a rule of thumb, don't work on more than one section of code at a time. Do one, get it working, test it so you know it works, then worry about the next part. If you try and do tasks A and B at the same time the mistakes of task A pollute task B and B pollutes A. Not only do you have exponentially more potential problems, but the problems move as you fix different parts.
I updated my question with the code at the top. Thank you for your suggestions. It will surely help me right now and in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.