# Stack Postfix Calculator

• 03-08-2012
programminfool
Stack Postfix Calculator
I read a post about the same thing and didn't know how to comment on it so I just made a new thread. Here's the code and couldn't get it to run. If anyone could look at this and tell me what's wrong with it that'd be great. I get an answer, but its wrong, for example 23+ gives me 225.

Code:

```#include <iostream> #include <iomanip> using namespace std;   class node {   public:     float number;     node *next; };   node* push(node *stack, float data) {   node *utility;   utility = new node;   utility -> number = data;   utility -> next = stack;   return utility; }   node* pop(node *stack, float &data) {   node *temp;   if (stack != NULL)   {     temp = stack;     data = stack -> number;     stack = stack -> next;     delete temp;   }   else cout << "\nERROR: Empty stack.\n";   return stack; }   int main() {   float answer, operand1, operand2;   char ch = ' ';                                      node *utility, *top;     utility = new node;   utility -> number = 0;   utility -> next = NULL;   top = new node;   top -> number = 0;   top -> next = utility;   while(ch != 'q')   {  cout << "calc> ";  cin >> noskipws >> ch;     if(ch == 'q')     {         return 0;     }  while(ch != '\n')  {                       float operand = 0;         if((ch >= '0')&&(ch <= '9'))         {           while(ch != '\n')           {             operand = operand*10 + (ch-48);                           cin >> ch;           }           top = push(top, operand);         }         else         {             if((ch == '+')||(ch == '-')||(ch == '*')||(ch == '/')){           top = pop(top, operand1);           top = pop(top, operand2);           switch(ch){             case '+': answer = operand2 + operand1;                 break;             case '-': answer = operand2 - operand1;                 break;             case '*': answer = operand2 * operand1;                 break;             case '/': answer = operand2 / operand1;                 break;           }           top = push(top, answer);}             else {                 cout << "Error! Wrong Character Entered";             }         }         pop(top, answer);   cout << "\nAnswer: " << answer << endl;   }   } }```
• 03-10-2012
Neo1
I can't find the error at first glance, but let me just ask you, why are you implementing your own stack for this? This has been done a million times before, don't reinvent the wheel, use std::stack, then you can focus on getting the RPN and arithmetics correct.

Also, why on earth would you use a linked-list for a stack implementation? What's wrong with an array, seems much simpler! (Or even better, a std::vector!)
• 03-10-2012
Salem
```#include <iostream> #include <iomanip> using namespace std; class node { public:   float number;   node *next; }; node *push(node * stack, float data) {   node *utility;   utility = new node;   utility->number = data;   utility->next = stack;   return utility; } node *pop(node * stack, float &data) {   node *temp;   if (stack != NULL) {     temp = stack;     data = stack->number;     stack = stack->next;     delete temp;   } else     cout << "\nERROR: Empty stack.\n";   return stack; } int main() {   float answer, operand1, operand2;   char ch = ' ';   node *utility, *top;   utility = new node;   utility->number = 0;   utility->next = NULL;   top = new node;   top->number = 0;   top->next = utility;   while (ch != 'q') {     cout << "calc> ";     cin >> noskipws >> ch;     if (ch == 'q') {       return 0;     }     while (ch != '\n') {       float operand = 0;       if ((ch >= '0') && (ch <= '9')) {         while (ch != '\n') {           operand = operand * 10 + (ch - 48);           cin >> ch;         }         cout << "Debug: pushed " << operand << endl;         top = push(top, operand);       } else {         if ((ch == '+') || (ch == '-') || (ch == '*') || (ch == '/')) {           top = pop(top, operand1);           top = pop(top, operand2);           cout << "Debug: popped " << operand1 << " " << operand2 << endl;           switch (ch) {           case '+':             answer = operand2 + operand1;             break;           case '-':             answer = operand2 - operand1;             break;           case '*':             answer = operand2 * operand1;             break;           case '/':             answer = operand2 / operand1;             break;           }           top = push(top, answer);         } else {           cout << "Error! Wrong Character Entered";         }       }       pop(top, answer);       cout << "\nAnswer: " << answer << endl;     }   } }```