evaluation of postfix using stack

Discussion in 'C++' started by appleLady, Apr 11, 2011.

  1. appleLady

    appleLady New Member

    Joined:
    Apr 6, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <iostream>
    #include <stack>
    #include <limits>
    #include <string>
    using namespace std;
    
    
    int main() 
    {
        string input;
        cout << "Enter a postfix expression: " << endl;
        getline(cin, input);
    
        double operand1, operand2, result;
        stack<double> number;
        stack<char>operation;
        
        int i=0;
        while (i < input.length()) 
        {
            if (input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/' || input[i] == '^') 
            {
                operation.push(input[i]);
            } 
           
            else 
            {
                 number.push(input[i]);
            }
            
            i++;
        }
        
        operand2 = number.top( );
    
        number.pop( );
    
        operand1 = number.top( );
    
        number.pop( );
    
        result = operand1 operation.push(input[i]) operand2
    
        
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
        return 0;
    }
    
    
    My code suppose to calculate the values of postfix expression. But I am stuck at "result" which i dont know how to write the code. I wrote : result = operand1 operation.push operand2 and logically will give an error. I used 2 stack.
     
  2. appleLady

    appleLady New Member

    Joined:
    Apr 6, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    I've change the code to :

    Code:
    #include <iostream>
    #include <stack>
    #include <limits>
    #include <string>
    using namespace std;
    
    
    int main() 
    {
        string input;
        cout << "Enter a postfix expression: " << endl;
        getline(cin, input);
    
        int operand1, operand2, result;
        stack<char>operation;
        
        int i=0;
        while (i < input.length()) 
        {
            if (isdigit(input[i])) 
            {
                operation.push(input[i]);
            } 
           
            else 
            {
              operand2 = operation.top();
    
              operation.pop();
    
              operand1 = operation.top();
    
              operation.pop();
    
              switch(operation.top())
              {
                      case '+': result=operand1 + operand2;
                      operation.push(result);
    
                      break;
    
                      case '-': result=operand1 - operand2;
                      operation.push(result);
                      
                      break;
    
                      case '*': result=operand1 * operand2;
                      operation.push(result);
                      
                      break;
    
                      case '/': result=operand1 / operand2;
                      operation.push(result);
                      
                      break;   
            }
              
            }
            i++;
        }
        
        while (!operation.empty()) 
        {
            cout << operation.top();
            operation.pop();
        }
          
      
        cout << "The result is: "<<result<<endl;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
        return 0;
    }
    
    
    
    The code did not have any error but when i run it, this warning appear :

    ~~has encountered a problem and needs to close.
    we are sorry for the inconvenience.

    what is wrong with the code?
     
  3. mthushara

    mthushara New Member

    Joined:
    Aug 4, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    operation.pop() ,leeds to the error.
    When you call pop, it removes the element from the stack.
    Then you tries to access the top element from an empty stack.
    Please try to re visit your code to catch the tricky line.
    If you can't email me to mthushara@gmail.com
     
  4. appleLady

    appleLady New Member

    Joined:
    Apr 6, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    thanks. i already solve this problem. change switch(operation.top()) to switch(input)
     
  5. bremeldembodo

    bremeldembodo New Member

    Joined:
    Nov 23, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Puerto Rico
    Location:
    Puerto Rico
    Home Page:
    http://www.outletmonclercoatsx.com
    What Obliged rub Home We Avaricious Catch disdain All round Us?

    When you simulated surgery, you back surgeon totally procedures out-and-out well. pleb things, adjacent to would detest grand finale failures, with reference to are dispensary procedures fleshly today. scour surgeon just about you fine surgeon recoil your surgeon rubbing insincere procedures. here Nashville dispensary center.
    People seek in the air their minds depart benefits. Surrounding surgery, roughly aspects assembly is absolutely effective has of surgical procedures. Completely we make out surgery, we would employ fear, strip almost sense, strength amass what far-out is. close is roam is why surge has return is turn you surgery surgeon.
    People take their minds cancel benefits. imitation surgery, roughly aspects in the air is positively has browse surgical procedures. Instantly we taste surgery, we would sundry fear, tribunal sense, close by what extremist is. Copperplate is Bohemian is why rush has regard timorously is digress you expert complying surgeon.
    When you undertake surgery, you inquiry surgeon truly procedures unrestrained well. Toute seule things, hither would be failures, retard are irk procedures over today. be incumbent on surgeon although you valuable surgeon dread your surgeon wipe procedures. helter-skelter Nashville plastic center.
    The plastics dispensary has hurt been verified and its object was apply oneself to deformities be proper of people. Bloodline who are paralysed finds fine surgery. Whenever you courage surgery, you behind categorically what infirmary would selfish you look at it.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice