Stack Question

Hi,

I have to convert a number (integer) to binary.  If I use the following code...

**************************************************************************
#include <iostream.h>
#include <conio.h>
#include <windows.h>

void main()
{

      int number = 0;
      int digit = 0;
      int convert = 0;
      int choice = 0;
      bool exit = false;

      do {
            do {
                  cout << "Menu:" << endl;
                  cout << "1) Convert an integer to binary" << endl;
                  cout << "2) Exit" << endl;
                  cin >> choice;
                  system("cls");
            } while (choice != 1 && choice != 2);
      
            switch (choice) {

            case 1:
                  cout << "Please enter a number: ";
                  cin >> number;
                  cout << endl;

                  while (number > 0) {
                        digit = number % 2;
                        cout << digit;
                        number = number/2;
                  }

                  cout << endl << endl;
                  cout << "Press any key to continue..." << endl;
                  getch();
                  system("cls");
                  exit = false;
                  break;

            case 2:
                  exit = true;

            }
      } while (!exit);
   
}

**************************************************************************

This will produce a backwards binary value.  I need to use a stack to convert it.  I have the following header file...

**************************************************************************
#ifndef S__H__
#define S__H__

#include <list>
//necessary for specifying to use standard headers provided
using namespace std;



template <class bd>
class Stacks: protected list<bd>
{public:
                  Stacks ();
                  ~Stacks ();
                  void push(bd);
                  void pop();
                  bd top();
                  bool emptyS();
                  bool fullS();
};

template <class bd>
Stacks<bd>::Stacks(){}

template <class bd>
Stacks<bd>::~Stacks(){}

template <class bd>
void Stacks<bd>::push(bd item)
{
      if(!fullS())
            push_front(item);
}

template <class bd>
void Stacks<bd>::pop()
{
      if(!emptyS())
      {      iterator i = begin();
            erase(i);
      }
}

template <class bd>
bd Stacks<bd>::top()
{
      iterator i = begin();
      return *i;
}

template <class bd>
bool Stacks<bd>::emptyS()
{return empty();}

template <class bd>
bool Stacks<bd>::fullS()
{  
      //number of items in list
      //== maximum number
      return size() == max_size();
}
      
#endif
**************************************************************************

Does anyone have any ideas on how to do it (using the header file)?  Thanks.

NeoTek
LVL 1
NeoTekAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
HendrikTYRConnect With a Mentor Commented:
Hi, of course it is much easier to reverse your output, BUT if you HAVE to use the provided header file then change your main() to:

void main()
{

     int number = 0;
     int digit = 0;
     int convert = 0;
     int choice = 0;
     bool exit = false;
               Stacks<int> stack;

     do {
          do {
               cout << "Menu:" << endl;
               cout << "1) Convert an integer to binary" << endl;
               cout << "2) Exit" << endl;
               cin >> choice;
               system("cls");
          } while (choice != 1 && choice != 2);
     
          switch (choice) {

          case 1:
               cout << "Please enter a number: ";
               cin >> number;
               cout << endl;

               while (number > 0) {
                    digit = number % 2;
                    //cout << digit;
                    stack.push(digit);
                    number = number/2;
               }
               while(!stack.emptyS()) {
                   cout << stack.top();
                   stack.pop();
               }

               cout << endl << endl;
               cout << "Press any key to continue..." << endl;
               getch();
               system("cls");
               exit = false;
               break;

          case 2:
               exit = true;

          }
     } while (!exit);
   
}
0
 
Hamed ZaghaghiProgrammerCommented:
hi ,
you can use STL for stack,
using

#include "stack"
using namespace std;

and then, for example :

stack<int> st;
st.push(10);
st.push(12);
....

cout << st.top() << endl;

st.pop();

have a good programming day.


0
 
HendrikTYRCommented:
same thing...
remember to put :
>> cout << st.top() << endl;

>> st.pop();
in a while
0
 
Hamed ZaghaghiProgrammerCommented:
hi HendrikTYR,

the code above is only an example for using the satck class.

have good push and pop. ;)
0
All Courses

From novice to tech pro — start learning today.