Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Postfix Notation using std::deque. Help me find the bug.

BUGGY
//-----------------------------------------------------------
for(int i = 0; i < line.length(); i+=2){
// char ch = line.at(i);

    if(line.at(i) == '*'){
        stack.push_back(stack.pop_back() * stack.pop_back());
    }

    else if(line.at(i) == '+'){
        stack.push_back(stack.pop_back() + stack.pop_back());
    }

    else if(line.at(i) == '-'){
        stack.push_back(-stack.pop_back() + stack.pop_back());
    }

    else stack.push_back(line.at(i)- '0');

}

//-----------------------------------------------------------
A LITTLE FIX. BUT STILL BUGGY
//-----------------------------------------------------------
for(int i = 0; line[i]; ++i){
// char ch = line.at(i);
if(line.at(i) == ''){
p = stack.back();
stack.pop_back();
q = stack.back();
stack.pop_back();
stack.push_front(p
q);
}

        else if(line.at(i) == '+'){
            p = stack.back();
            stack.pop_back();
            q = stack.back();
            stack.pop_back();
            stack.push_front(p + q);
        }

        else if(line.at(i) == '-'){
            p = stack.back();
            stack.pop_back();
            q = stack.back();
            stack.pop_back();
            stack.push_front(-p + q);
        }else stack.push_front(line[i]- '0');
    }
Sign In or Register to comment.