[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Stack Question

Posted on 2004-10-23
4
Medium Priority
?
281 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
Comment
Question by:NeoTek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
HendrikTYR earned 1000 total points
ID: 12388411
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
ID: 12390338
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
ID: 12390646
same thing...
remember to put :
>> cout << st.top() << endl;

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

Expert Comment

by:zaghaghi
ID: 12390844
hi HendrikTYR,

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

have good push and pop. ;)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

650 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