Search code examples
c++stackrpn

RPN Calculator (issues with applying operands to the stack)


I'm currently working on building on RPN calculator for a class that has to function through a stack of ints, and various functions. It also has to have input taken through cin statements and then sorted into integers or operands and either be pushed onto the stack or initiate the appropriate functions from the class to calculate.

Most of it I have figured out and working, however I'm having a really weird problem that I can't figure out.

It will take my first set of numbers and the first operand fine (for example, I enter 1 2 3 and the stack will show 3, 2, 1 as the contents) but after I apply a second operand I'm getting zeros thrown in before each answer.

Example:

Input: 1 2 + 2 *

Expected Output: 6

What I Get: 0, 2, 0, 3

I'm not sure if this is an error in my push() function for the stack, or in main, or elsewhere. I haven't been able to find it. Any help would be appreciated, even just a point in the right direction!

Here's the parts of the code I'm assuming are causing the problem somewhere:

Main Function:

int main(){

  Stack mystack;

  std::string getIt; // taken as input
  int pushIt;  // converted to int and pushed if nessecary

  do{
    // get user input
    std::cin >> getIt;
    if(getIt == "@"){};
    if(getIt == "!") mystack.negate();
    if(getIt == "+") mystack.add();
    if(getIt == "-") mystack.subt();
    if(getIt == "/") mystack.div();
    if(getIt == "%") mystack.mod();
    if(getIt == "SUM") mystack.sumof();
    if(getIt == "R") mystack.reverse();
    if(getIt == "#") mystack.print();
    if(getIt == "$") mystack.clear();
    else {
      pushIt = atoi(getIt.c_str()); // I have no idea if this was
                                    //utilized correctly, feel free
     mystack.push(pushIt);          // to correct me here if not..
    }
   }
  while(getIt!="@"); // breaks on @
  return 0;
 }

Push, Pop, and Top Operators:

void Stack::push(const int& val){
  Node* newNode = new Node;
  newNode->data = val;
  if(!head){
    head = newNode;
    return;
  }
  newNode->next = head;
  head = newNode;
}


void Stack::pop(){
  if(!head)
    return;
  if(head->next == NULL){
    delete head;
    head = NULL;
    return;
  }
  Node* deleteIt = head;
  head = head->next;
  delete deleteIt;
  return;
}


const int& Stack::top() const throw(Oops) { //Oops returns
  if(head == NULL){                     // an error variable
    std::cout<<"ERROR!! : No values in the stack.";
  }      
  return head->data;
}
// I also don't know if I used the throw right here.. 

And, just incase I'm actually doing something wrong here instead... Here's one of my operation functions (+), the others are all coded similarly.

void Stack::add(){
  int num1 = top();
  pop();
  int num2 = top();
  pop();
  int sum = num2+num1;
  push(sum);
  return;
}

Thanks!


Solution

  • Your flow control is incorrect. Consider what happens when the user inputs a +:

    if(getIt == "+") mystack.add();   // <== this happens
    if(getIt == "-") mystack.subt();  // nope
    if(getIt == "/") mystack.div();   // nope
    // ... snip ... 
    if(getIt == "$") mystack.clear(); // nope
    else {                            // this else is associated ONLY with the 
                                      // previous if. As a result...
      pushIt = atoi(getIt.c_str());   // <== this happens too!
      mystack.push(pushIt);
    }
    

    You process a + as both an addition operand and a number - and atoi("+") == 0. The problem is that all your ifs are independent, and they shouldn't be:

    if (getIt == "+") ...
    else if (getIt == "-") ...
    else if (getIt == "/") ...
    ...
    else {
        // handle int here
    }