0

I wrote this code to evaluate postfix expressions, but in this code I can only evaluate an expression with only single-digit numbers. I want to edit this code for it to evaluate multi-digit numbers. How can I do this?

#include <iostream>
#include <stack>
#include <string>

using namespace std;

float calc(float o1,float o2,char c)
{
    if(c=='+') return o1+o2;
    if(c=='-') return o1-o2;
    if(c=='*') return o1*o2;
    if(c=='/') return o1/o2;
    else return 0;
}

float evaluate(string exp)
{
    float result=0;
    stack<char>s;
    for(int i=0;i<exp.length();i++)
    {
         if(isdigit(exp[i]))
        {

             s.push(exp[i]-'0');
        }
        else
        {
            float o2=s.top();
            s.pop();
            float o1=s.top();
            s.pop();
             result = calc(o1,o2,exp[i]);
            s.push(result);

        }
    }
    return s.top();
}

int main()
{
    string exp="382/+5-";
    cout<<evaluate(exp);
    return 0;
}
Yun
  • 3,056
  • 6
  • 9
  • 28
  • 2
    You need to parse the entire number, not just the first digit. That means you have to keep going until you find the end of the number before you push it into your stack – UnholySheep Sep 13 '21 at 18:34
  • A pattern you could use is for a digit pop the value on the stack, multiply it by 10, and push the result back on the stack. That lets you build a number from multiple digits. But that doesn't get the process started. So somehow you are goingto have to tell if your digit input was a another digit, or the beginning of a new number altogether. Time for a flag. – infixed Sep 13 '21 at 18:41
  • @infixed you probably mean pop, multiply by 10, add new digit, then push back on the stack ;) Oh and it would be good to check if your close to max float before doing the multiplying too – Pepijn Kramer Sep 13 '21 at 18:51
  • Or use a proper parser library, Boost.Spirit . This is a similar calculator example https://www.boost.org/doc/libs/1_66_0/libs/spirit/example/qi/compiler_tutorial/calc4.cpp – alfC Sep 13 '21 at 20:09

1 Answers1

3

For multi-digit numbers, you need a separator symbol eg. space. All the tokens in postfix will be space separated. You need to read one token at a time. For example, expression "28 2 / 5 -" can be evaluated as follows:

#include <iostream>
#include <stack>
#include <string>

using namespace std;

float calc(float o1,float o2,char c)
{
    if(c=='+') return o1+o2;
    if(c=='-') return o1-o2;
    if(c=='*') return o1*o2;
    if(c=='/') return o1/o2;
    else return 0;
}

float evaluate(string exp)
{
    float result=0;
    stack<int> s ;
    int dig = 0;
    int i=0;
    while(i<exp.length())
    {
        char e = exp[i];
        if(isdigit(exp[i])) {
            dig = dig*10 + (exp[i]-'0');
        } else if (exp[i] == ' ') {
            s.push(dig);
            dig = 0; 
        } else {
            float o2=s.top();
            s.pop();
            float o1=s.top();
            s.pop();
            result = calc(o1,o2,e);
            s.push(result);
            i++;

        }i++;
    }
    return s.top();
}

int main()
{
    string exp="28 2 / 5 -";
    cout<<evaluate(exp);
    return 0;
}

additional ref link:Postfix evaluation for multidigit numbers

arp5
  • 169
  • 10