// Node .h
    
    
    #ifndef NODE_H_INCLUDED
    #define NODE_H_INCLUDED
    
    # include <iostream>
    
    using namespace std ;
    
    
    class Node
    {
        private:
            int object ;
            Node *nextNode ;
    
        public:
            Node() ;
    
            void setObject(int x) ;
            void setNext(Node *next) ;
    
            int getObject() ;
            Node *getNext() ;
    } ;
    
    #endif // NODE_H_INCLUDED
    
    //-----------------------------------------------------
    
    // Node.cpp
    
    # include "Node.h"
    
    
    Node::Node()
    {
        object = -1 ;
        nextNode = NULL ;
    }
    
    
    void Node::setObject(int x)
    {
        object = x ;
    }
    
    void Node::setNext(Node *next)
    {
        this -> nextNode = next ;
    }
    
    int Node::getObject()
    {
        return object ;
    }
    
    
    Node *Node::getNext()
    {
        return nextNode ;
    }
    
    // ----------------------------------------------------
    
    // Stack.h
    
    #ifndef STACK_H_INCLUDED
    #define STACK_H_INCLUDED
    
    # include "Node.cpp"
    
    class Stack
    {
        private:
            int topElement ;
            Node *headNode ;
    
        public:
            Stack() ;
            void push(int x) ;
            int pop() ;
            bool isEmpty() ;
            int top() ;
    } ;
    
    #endif // STACK_H_INCLUDED
    
    
    //------------------------------------------------------
    
    // Stack.cpp
    
    # include "Stack.h"
    
    
    Stack::Stack()
    {
        topElement = -1 ;
        headNode = NULL ;
    }
    
    void Stack::push(int x)
    {
        Node *newNode = new Node() ;
        newNode->setObject(x) ;
        newNode->setNext(headNode) ;
        headNode = newNode ;
    }
    
    int Stack::pop()
    {
        if (!isEmpty())
        {
            int e = headNode->getObject() ;
            Node *temp =  headNode ;
            headNode = headNode->getNext() ;
            delete temp ;
            return e ;
        }
        else
        {
            cout << "Stack Empty !!! " ;
        }
    }
    
    bool Stack::isEmpty()
    {
        if (headNode==NULL)
            return true ;
        else return false ;
    }
    
    int Stack::top()
    {
        if (headNode != NULL)
            return (headNode->getObject()) ;
        else cout << "Stack is Empty !" << endl ;
    }
    
    
    // -----------------------------------------------------------
    
    // inputPostFix.cpp
    
    # include <string.h>
    # include "Stack.cpp"
    
    void postFixEntring()
    {
        string userInput , str;
    
        cout << "Enter postfix expression to evaluate : " ;
        getline(cin,userInput) ;
    
     
        char *c , *p ;
        c = new char [userInput.size()+1] ;
        strcpy(c,userInput.c_str()) ;
    
        Stack myStack ;
    
        p = strtok(c," ") ;
    
        while(p !=NULL)
        {
           cout << p << endl ;
    
           if (p != "+" )
            {
                int y = atoi(p) ;
                myStack.push() ;
            }
            else if (p == "+")
            {
                int operend1 , operend2, result ;
                operend2 = myStack.pop() ;
                operend1 = myStack.pop() ;
    
                result = operend1 + operend2 ;
                myStack.push(result) ;
                cout << "in else if" << result ;
            }
    
            p = strtok(NULL," ") ;
        }
        delete c ;
    
        cout << endl << endl ;
    
        cout << myStack.pop() << endl ;
        cout << myStack.pop() << endl ;
    }
    
    //-----------------------------------------------
    
    // main() 
    
    # include "entringPostfixFunction.cpp"
    
    int main()
    {
        postFixEntring() ;
    
        char userDecission ;
        while(1)
        {
            cout << "Do you want to enter another postfix expression to evaluate? [Y/N] : "  ;
            cin >> userDecission ;
            if (userDecission == 'Y' || userDecission =='y')
                postFixEntring() ;
            else if (userDecission == 'n' || userDecission == 'N')
                break ;
            else
                cout << "Please Enter Correct option ! " << endl << endl ;
        }
    
    
    }

Recommended Answers

All 2 Replies

Is there a question in here?

1) this program gives me error when i press Y for again.
2) I want to give an error message to user when user enter invalid postfix expression . I dont know how to implement. plzzzz tell me.
thanks.

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.