
#ifdef GNU
#include <iostream.h>
#define std
#else
#include <iostream>
#endif
#include <string>

#include "heap.h"

bool
NextFloat( float& value )
{
  std::string str;
  do {
    std::cout << "Enter a float to put into the heap\n"
              << "(type -1 to end) ==> ";
    if ( ! (std::cin >> value) ) {
      if ( std::cin.eof() ) 
	     return false;
      else {
	     std::cin.clear();
	     std::cout << "Error in input.  Press <Enter> until you see the prompt again...\n";
	     std::getline( std::cin, str );
      }
    }
    else {
      return value != -1;
    }
  } while (true);
} 


template <class T>
void
hsort( T * arr, int count )
{
  Binary_Heap<T> heap( arr, count );
  for ( int i=0; i<count; i++ ) arr[i] = heap.Delete_Min();
}


int
main()
{
  Binary_Heap<float> heap1;

  do {
    float in_value;
    if ( NextFloat(in_value) ) {
      heap1.Insert( in_value );
    }
    else {
      break;
    }
  } while ( true );

  std::cout << "The heap has " << heap1.NumElements() << " values.\n"
	    << "Here they are in increasing order:" << std::endl;

  while ( ! heap1.Is_Empty() ) {
    float value = heap1.Delete_Min();
    std::cout << value << "\n";
  }

  const int count=10;
  double arr[] = { 75.0, -15.2, 90.0, 23, 67.1, 3.14, 
		  -121, 62, 101.01, 99.9 };
  hsort( arr, count );
  std::cout << "\nSorting results:\n";
  for ( int i=0; i<count; i++ ) 
	  std::cout << arr[i] << std::endl;
  return 0;
}
