stacks - push - pop

This is a discussion on stacks - push - pop within the C++ Programming forums, part of the General Programming Boards category; I have two classes (number and stack), I have to take a user input number and convert it into binary, ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    stacks - push - pop

    I have two classes (number and stack), I have to take a user input number and convert it into binary, hex, and octal. Im getting there but how do you 'push' the remainder on the stack? Do you do it through in the main program or in the classes.cpp in the function push? The same with 'pop'????
    Can someone please explain this too...."The program should show all 3 conversions each loop, you should be able to convert more than one vlue (overall loop for additional processing")?????

    I don't have much started, but this is what I got.....
    Code:
    class number
    {
    public:
    	int remainder;
    	
    	number* remainder;
    
    	number();
    	~number();
    };
    
    class stack
    {
    public:
    	int count;
    	int head;
    
    void getnumb(int);
    void display();
    bool pop();
    bool push();
    
    };
    
    
    #include <iostream>
    #include number.h
    
    using namespace std;
    
    number::number()
    {
    	cout << "Executing constructor..." << endl;
    	remainder = 0;
    }
    
    
    number::~number()
    {
    }
    
    stack::stack()
    {
    	count = 0;
    	head = NULL;
    }
    
    stack::~stack()
    {
    }
    
    
    bool stack::push(int remainder)
    {
    	bool success;
    	number* nptr = new number;	//new node
    	stack* top;
    
    	nptr->rem = remainder;		//store info
    
    	if (stack is full)
    	{
    		success = false;
    	}
    	else
    	{
    		if (head = = NULL)	//insert into empty stack
    		{
    			nptr->next = head;
    			head = nptr;
    		}
    		else			//insert into top of stack
    		{
    			nptr->next = top;
    			top = nptr;
    		}
    		count++;
    	}
    	success = true;
    }
    
    (Main program) ...
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int numb, digit;
        cout << "number: " << endl;
        cin >> numb;
    
        while (numb > 0)
        {
            digit = numb % 2;
            cout << digit;
            numb = numb/2;
        }
        system ("pause");
    return 0;
    }

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    guessing...

    >> Im getting there but how do you 'push' the remainder on the stack? Do you do it through in the main program or in the classes.cpp in the function push? The same with 'pop'????
    Do it in the main program. Instantiate an object of stack and push/pop the remainder

    >> ...."The program should show all 3 conversions each loop, you should be able to convert more than one vlue (overall loop for additional processing")?????
    Code:
    WHILE there's input number
        convert input number to binary and display it
        convert input number to hex and display it
        convert input number to octal and display it
    END WHILE
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. swapping pointers
    By csvraju in forum C Programming
    Replies: 17
    Last Post: 04-01-2009, 03:18 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 03:48 PM
  3. Inline asm
    By brietje698 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2007, 01:54 PM
  4. Getting position from game..
    By brietje698 in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2007, 12:15 PM
  5. Queues
    By ramayana in forum C Programming
    Replies: 22
    Last Post: 01-01-2006, 01:08 AM

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