Hello! I have a homework in which i should do this:
Input
x+-3129
Output:
3x1+2-9=-4
I wrote this code and my output is : 3-1+2x9
Can somebody help me to find the problem and a way to calculate the expression in code?
#include <iostream>
#include<stack>
using namespace std;
bool isOperand(char c)
{
if( (c >='0' && c<='9') )
return true;
else
return false;
}
string PrefixtoInfix(string prefix)
{
stack<string> s;
for(int i = prefix.length()-1;i>=0;i--)
{
if(isOperand(prefix[i]))
{
string op(1,prefix[i]);
s.push(op);
}
else
{
string op1=s.top();
s.pop();
string op2=s.top();
s.pop();
s.push(op1+prefix[i]+op2 );
}
}
return s.top();
}
int main() {
string prefix,infix;
cin>>prefix;
infix=PrefixtoInfix(prefix);
cout<<infix;
return 0;
}