//**************************************************************
//
//  CS 240B   Winter Quarter 2003 Ohio University Travis Dillon
//  Proj2   : Array Workshop
//  file    : menu_print.cc  
//  started : 01-16-03
//
//**************************************************************

#include "search_sort.h"
#include "prog2.h"
#include "menu_print.h"
#include "fill.h"

void main_menu()
{
   int a[MAX_ARRAY_SIZE] = {0};
   int cur_array_size = MAX_ARRAY_SIZE / 2;
   bool done = false;
   char ans;
   while (!done)
   {
      cout<<"\nArray Workshop.";
      space(14);
      cout<<"Main Menu";
      space(13);
      cout<<"Travis Dillon"<<endl;
      cout<<"\ncurrent array size = " <<cur_array_size;
      space(18);
      cout<<"maximum array size = " <<MAX_ARRAY_SIZE <<endl;
      cout<<"\n R       Resize the array.";
      cout<<"\n F       Fill the array.";
      cout<<"\n S       Sort the array.";
      cout<<"\n L       search array using Linear search.";
      cout<<"\n B       search array using Binary search.";
      cout<<"\n M       find the Maximum element in the array.";
      cout<<"\n P       Print the array to the console.";
      cout<<"\n Q       Quit.";
      cout<<"\n\n   Enter choice of tasks in < R, F, S, L, B, M, P, Q > :  ";
      cin >> ans;
      cout<<endl;
      
      switch(ans)
      {
         case'Q':  //quit the program
         case'q':  done = true;
            break;
            
         case'F':  //go to fill_menu
         case'f':  fill_menu(a, cur_array_size);
            break;
            
         case'R':  //change the size of the active portion of array
         case'r':  set_size(a, cur_array_size);
            break;
            
         case'S':  //sort array by selection sort
         case's':  sort(a, cur_array_size);
            break;
            
         case'L':  //linear search for array element
         case'l':  {int tar;
                   cout<<"Enter value to search for:  ";
                   cin >>tar;
                   int loc = lin_search(a,cur_array_size,tar);
                   if(loc < cur_array_size)
                   {
                      cout <<"\nThe first occurrence of "<<tar
                           <<" is at location " << loc <<endl;
                   }else{
                      cout<<"\nSorry, the number "<<tar<<" was not found.\n";
                   }}
            break;
            
         case'B':  //binary search for array element
         case'b':  {int tar;
                   cout<<"Enter value to search for:  ";
                   cin >>tar;
                   int loc = bin_search(a,cur_array_size,tar);
                   if(loc < cur_array_size)
                   {
                      cout <<"\nAn occurrence of "<<tar
                           <<" is at location " << loc <<endl;
                   }else{
                      cout<<"\nSorry, the number "<<tar<<" was not found.\n";
                   }}
            break;
         
         case'M':  //find location of biggest element in array
         case'm':  cout <<"\nThe biggest element is "
                        <<a[find_max(a,cur_array_size)] <<" at location "
                        <<find_max(a,cur_array_size) <<endl;
            break;
            
         case'P':  //print out the array in formatted output
         case'p':  print_out(a, cur_array_size);
            break;
            
         default:  cout<<"\a" <<"\nInvalid choice.  Choose again.\n";
            break;
      }
      
   }
}

void fill_menu(int x[], int array_sz)
{
   char ans;
   bool done = false;
   
   while (!done)
   {
      cout<<"\nArray Workshop.";
      space(17);
      cout<<"Array Fill Menu";
      space(14);
      cout<<"array size = " <<array_sz <<endl;
      cout<<"\nchoose a fill method\n";
      cout<<"\n C       fill array with one Constant value.";
      cout<<"\n H       fill array with values entered by hand.";
      cout<<"\n R       fill array with Random numbers.";
      cout<<"\n A       fill array with an Arithmetic progression.";
      cout<<"\n F       fill array with a Fibonacci sequence.";
      cout<<"\n X       return to previous menu without filling array.";
      cout<<"\n\n   Enter choice of tasks in < C, H, R, A, F, X > :  ";
      cin >>ans;
      cout<<endl;

      switch(ans)
      {
         case'X':  //exit to main menu
         case'x':  done = true;
            break;
            
         case'C':  //fill array with some constant
         case'c':  constant(x, array_sz);
                   done = true;
            break;
            
         case'H':  //fill array by keyboard input
         case'h':  by_hand(x, array_sz);
                   done = true;
            break;
            
         case'R':  //fill array with random numbers
         case'r':  random(x, array_sz);
                   done = true;
            break;
            
         case'A':  //fill array by arithmatic progression
         case'a':  arith_progr(x, array_sz);
                   done = true;
            break;
            
         case'F':  //fill array with fibonacci sequence
         case'f':  fibonacci(x, array_sz);
                   done = true;
            break;
      
         default:  cout<<"\a" <<"\nInvalid choice.  Choose again.\n";
            break;
      }
   }
}

void constant(int x[], int array_sz)
{
   static int const_val = 0;
   char ans;
   bool done = false;
   
 while(!done)
 {
   cout<<"\nArray Workshop.";
   space(4);
   cout<<"Constant Fill";
   space(4);
   cout<<"array size = " <<array_sz;
   space(4);
   cout<<"Current Constant = " <<const_val;
   cout<<"\n choose:    task:\n";
   cout<<"\n    F       Fill array using current constant value .";
   cout<<"\n    C       Change the current constant value.";
   cout<<"\n    X       return to fill array menu without filling array.";
   cout<<"\n\n   Enter choice of tasks in < F, C, X > :  ";
   cin >> ans;
   cout<<endl;   
   
   switch(ans)
   {
      case'X':  //exit to fill menu
      case'x':  done = true;
         break;
         
      case'F':  //fill the array with const_val
      case'f':  for(int j(0); j < array_sz; j++)
                {
                   x[j] = const_val;
                }
                done = true;
         break;
         
      case'C':  //change the const_val
      case'c':  cout<<"enter new value for constant value:  ";
                cin >>const_val;
         break;
         
      default:  cout<<"\a" <<"\nInvalid choice.  Choose again.\n";
         break;
   }
 }
}

void print_out(int x[], int array_sz)
{
   cout<<endl<<endl<< "The array is.";
   for(int i(0); i < array_sz; i++)
   {  //formatting
      if(i%50 == 0)cout<< endl;
      if(i%100 == 0)cout<< endl;
      if(i%10 == 0)cout<<endl <<setw(4) <<i/10 <<":  ";
      if(i%10 == 5)cout<<"  ";
      cout<<setw(5) <<x[i];
   }
   cout<<endl<<endl;
}

void space(int num_space)
{  //space formatting
   for(int i = 1; i <= num_space; i++)
   {
      cout<<' ';
   }
}