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

https://www.google.com/search?&q=3x1%2B2-9 says it's -4 but to me it wasn't clear what was expected versus what happened.

Also, what was the assignment? The assignment appears to be missing here so it's guesswork on my part and I can't tell what it was really supposed to do. Maybe it's some sort of odd calculator but no calculator I know takes in that Input and gives up either answer you shared.

Tell more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.