Solved

Stack Question

Posted on 2004-10-23
274 Views
Last Modified: 2010-04-01
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
0
Question by:NeoTek
    4 Comments
     
    LVL 3

    Accepted Solution

    by:
    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
     
    LVL 9

    Expert Comment

    by:zaghaghi
    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
     
    LVL 3

    Expert Comment

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

    >> st.pop();
    in a while
    0
     
    LVL 9

    Expert Comment

    by:zaghaghi
    hi HendrikTYR,

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

    have good push and pop. ;)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: JavaScript Coding - Massive 12-Part Bundle

    Regardless of your programming skill level, you'll go from basics to advanced concepts in a vast array of JavaScript subjects including Sammy.js, Agility.js, Ember.js, Node.js, jQuery, AJAX, Extjs, AngularJS, Knockout.js, and JSON.

      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now