// testComputeSplitTime3.cc
// P. Conrad 03/06/06  for CISC181
// write test cases for the computeSplitTime function

// this version uses a testing function
// this version also attempts to write correct code
// for the compute split time function

// THIS VERSION DOESN'T COMPILE!!! because of the const not being
// compatible with strtok... see version 4 for a fix

#include <iostream>
using namespace std;

// we use "const char * const" instead of just "char *"
// because we want to be able to pass in string literals such as "1:51.0"
// The keyword const means that we cannot modify either the pointer,
// or the data pointed to.

double computeSplitTime(const char * const splitTimePtr);

void testComputeSplitTime(const char * const splitTimePtr, 
			  double expectedResult);

int main(void)
{
  testComputeSplitTime("1:51.0",111.0);
  testComputeSplitTime("2:00.0",120.0);
  testComputeSplitTime("1:51.9",111.9);  
  testComputeSplitTime("0:00.0",0.0);  

  return 0;
}


void testComputeSplitTime(const char * const splitTimePtr, 
			  double expectedResult)
{

  double result = computeSplitTime(splitTimePtr);

  if (result == expectedResult)
    cout << "Passed" << endl;
  else
    cout << "Failed" << endl;

  return; 
}


double computeSplitTime(const char *  const splitTimePtr)
{
  
  double result;

  // stub function @@@ finish the stub later!!!

  // use strtok to divide the string into the first part
  // which is in minutes, and the second part which is in seconds.

  // for example, 1:51.9 will be divided into 1\051.9

  char *firstPart;
  char *secondPart;
  
  firstPart = strtok(splitTimePtr,":");
  secondPart = strtok(NULL,":");

  // compute the result by converting firstPart to a number
  // and multiplying by 60, and add to second part
  // use atof on the second part, becuase it is a double
  // (yeah, go figure... there is no atod... atof returns not
  //    a float, but rather, a double)

  result = atoi(firstPart) * 60 + atof(secondPart);

  return result;
}
