//
// program to time dynamicArray class.
//
// program input consists of a sequence of commands from standard
//     input, where a command is one of the following.
//   a N [ adds N elements to the end of the dynamic array ]
//   d N [ removes N elements from the end of the dynamic array ]
//   p   [ prints array size, operation counts, timing info ]
//   q   [ quits ]
//

#include <iostream>
#include <stdlib.h>		// has EXIT_SUCCESS
#include <sys/times.h>		// needed for timer()
#include <time.h>		// needed for timer()
#include "dynamicArray.h"

// ---- function to print program's execution time ----

// Postcondition:  total running time (CPU time) of the program
//                   printed to "out".
void timer(ostream & out) {
  struct tms t;
  times(&t);
  out << "total time in seconds = "
      << static_cast<double> (t.tms_utime + t.tms_stime + t.tms_cutime +
		   t.tms_cstime) / CLK_TCK 
      << endl;
}


// ---- main program ----

int main(void) {

  dynamicArray a;

  char prompt[] = 
    "Enter 'a N' to add N elements,\n"
    "      'd N' to remove N elements,\n"
    "      'p' to print size, operation counts, time,\n"
    "      'q' to quit:\n";

  char cmd;
  int n;

  cerr << prompt;
  while ((cin >> cmd) && cmd != 'q') {
    if (cmd == 'p') {
      cout << "size of a = " << a.size() << endl;
      a.show_op_counts(cout, "a");
      timer(cout);
    }
    else if (cmd == 'a') 
      for (cin >> n; n > 0; --n)
	a.push_back(n);
    else if (cmd == 'd') 
      for (cin >> n; n > 0; --n)
	a.pop_back();
    cerr << prompt;
  }

  return EXIT_SUCCESS;
}