/* This program tests the functionality of class template Stack.
...
-------------------------------------------------------------------*/

#include <iostream.h>

#include "Stack.h"

ostream& DisplayBinary(unsigned Num, ostream& Out = cout);

int main(void)
{
  cout << "\nEnter a string: ";        // PART I: characters

  char
    Ch;                                // input container
  Stack<char>
    CharStack;                         // a Stack of char values

  for (;;)                             // loop:
    {
      cin.get(Ch);                     //   read a char into Ch
      if (Ch == '\n')                  //   if it was a newline
        break;                         //     we're done

      CharStack.Push(Ch);              //   push it onto the stack
    }                                  // end loop
 
  cout << CharStack << "\n\n";         // display the stack

  cout << "Enter an integer: ";        // PART II: integers

  unsigned
    Number;                             // input container
  cin >> Number;                        // read a number into Number

  cout << "\nThe binary representation of " << Number << " is ";
  DisplayBinary(Number) << "\n\n";      // display its binary rep-
                                        //  resentation, using a stack
  return 0;
}

//--- Show the binary representation of an integer -----------------

ostream& DisplayBinary(unsigned Num, ostream& Out)
{
  Stack<int>
    IntStack;                           // build stack of integers

  while (Num != 0)                      // while Num not zero:
    {                                   //  integer-division Num/2,
      IntStack.Push(Num % 2);           //   push remainder on stack
      Num /= 2;                         //   replace Num with quotient 
    }

  while (!IntStack.Empty())             // while Stack not empty:
    Out << IntStack.Pop();              //   pop the stack and display
                                        //     the popped value
  return Out;
}

