/* Adapted from Program 3-16 in the textbook */
/* This program reads an integer from the keyboard
   and prints its binary equivalent. It uses a stack
   to reverse the order of 0s and 1s produced.
  Written by:
  Date:*/ 
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"

int main (void)
{
  int num;
  int* digit;
  STACK stack;

  /* Create Stack */
  stack = createStack ();

  /* prompt and read a number */
  printf ("Enter an integer: ");
  scanf ("%d", &num);

  /* create 0s and 1s and push them into the stack */
  while (num > 0) {
    digit  = (int*) malloc (sizeof(int));
    *digit = num % 2;
    push(stack, digit);
    num = num /2;  
  }

  /* Binary number created. Now print it */
  printf ("The binary number is: ");
  while (!isEmpty(stack)) {
    digit = (int*)pop(stack);
    printf ("%1d", *digit);
  }
  printf ("\n");

  /* Destroying Stack */
  destroyStack (stack);
  return 0;
}

/*	Results
  Enter an integer:      45
  The binary number is : 101101
*/
