Calculate an expression with +/ - using stack

This is a discussion on Calculate an expression with +/ - using stack within the C++ Programming forums, part of the General Programming Boards category; Hi , i need to calculate an expression with +/- only , it will get result once it see '=' ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Hell in heaven
    Posts
    9

    Exclamation Calculate an expression with +/ - using stack

    Hi , i need to calculate an expression with +/- only , it will get result once it see '=' sign using stack linked list (for exp: 200-100=) ,result 100. (multi digit needed)



    My code is not working , can someone enlighten me ? Thanks a lot


    Code:
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <iomanip>
    #include <ctime>
    using namespace std;
    
    
    const int MAX =100;
    int getResult(char *);
    
    
    class Stack
    {
        public:
            Stack();
            ~Stack();
            void push(int);
            int pop();
    		int top ();
    		
            bool isEmpty () const;
            
    
    
        private:
    
    
            struct Node;
            typedef Node* NodePtr ;
    
    
            struct Node
            {
                int data;
                NodePtr next;
    
    
            };
    		
    		NodePtr head;
    
    
    };
    
    
    Stack::Stack()
    {
    	head =NULL;	   
    }
    Stack::~Stack()
    {
    	//Let the compiler to do it
    }
    void Stack::push (int item)
    {
    	NodePtr pNew= new Node;
    	pNew -> data = item;
    	pNew -> next = head;
    	head = pNew;
    }
    
    
    int Stack::pop()
    {
    	int item = NULL;
    	try
    	{
    		if ( head == NULL)
    			throw exception ();
    		
    		
    		NodePtr temp = head;
    		head = head ->next;
    		
    		item = temp ->data;
    		
    		delete temp;
    	}
    	catch (exception e)
    	{
    		cout << " Empty exception caught : Remove head failed "<< endl;
    	}
    	return item;
    }
    	
    
    
    bool Stack::isEmpty () const
    {
    	return head == NULL;
    }
    
    
    int Stack::top ()
    {
    	if (!isEmpty())
    		return head-> data;
    }
    
    
    
    
    
    
    int main ()
    {
    	char * exp = new char[MAX];
    	cout <<"Enter an expression" <<endl;
       cin.getline(exp,MAX);
    	int result = getResult (exp);
    	
    	cout <<result <<endl;
    
    
    }
    
    
    
    
    int getResult(char *exp)
    {
    	
    	int i =0;
    	int k,k1,k2;
    	int result = 0;
    	char *p;
    	Stack s;
    	p = &exp[0];
    	
    	// Calculate the result if last element is '=' sign
    	while (*p!='=')
    	{
    		/* To remove spaces and tabs*/
    		while (*p ==' ' || *p =='\t')
    		{
    			p++;
    		}
    		
    		// If is operand
    		if (isdigit(*p))
    		{
    			s.push(*p);
    			
    		}
    		else
    		{
    		//If is operator +,-
    				k1 = s.pop();
    				k2 = s.pop();
    				
    			switch (*p)
    			{
    				case '+' : 	result =k1 + k2;
    							break;
    							
    				case '-' :  result =k1 - k2;
    							break
    				
    				default : return result;
    						  
    				
    			// push back to stack
    			s.push(result);
    			}
    					
    		
    		}
    		
    		p++; // Go to next element
    		
    	}
    	
    	
    	result= s.pop();
    	
    	
    
    
    	return result;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    What is your idea? How does your program not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializer expression list treated as compound expression
    By karthikeyanvisu in forum C Programming
    Replies: 7
    Last Post: 02-26-2011, 04:19 PM
  2. Replies: 2
    Last Post: 11-25-2009, 06:38 AM
  3. How to calculate the Stack Size?
    By shwetha_siddu in forum C Programming
    Replies: 32
    Last Post: 06-25-2008, 07:59 AM
  4. How to evaluate a postfix|prefix expression using stack?
    By Marrah_janine in forum C Programming
    Replies: 5
    Last Post: 08-04-2007, 04:12 AM
  5. Stack-Postfix Expression
    By kewell in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 12:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21