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;
}