/*********************************************
This program reads trial data from a roach
experiment, reads a time from the user, and
tells the user where the roach was going at
that point in time.
**********************************************/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

/*********************************************
** PROTOTYPES & STRUCT DEFINITIONS
*********************************************/
//--- POINT ---------------------------------//
struct point {
double x, y;
};
istream& operator>>(istream& IN, point& p);
ostream& operator<<(ostream& OUT, point p);

//--- TIME IN HH:MM:SS ----------------------//
struct hhmmss {
int h,m,s;
};
istream& operator>>(istream& IN, hhmmss &T);
bool operator<(hhmmss a, hhmmss b);

//--- A DATA READING FROM THE EXPERIMENT ----//
struct datum {
point position;
hhmmss time;
};
istream& operator>>(istream& IN, datum& D);

/*********************************************
** MAIN FUNCTION
*********************************************/
int main() {
// Open file and read heading info
int N;
string s;
ifstream IN("trial.txt");
IN >> N >> s >> s;

// Read and store data readings
datum *A = new datum[N];
for(int i = 0; i < N; i++)
IN >> A[i];

// Get the query time from the user
hhmmss T;
cout << "Enter a time: ";
cin >> T;

// Find the first sighting at or after given time
int k = 0;
while (k < N && A[k].time < T)
k++;

// Write result
if (k == 0) {
cout << "This was before the first sighting at ";
cout << A[0].position;
cout << endl;
}
else if (k == N) {
cout << "This was after the last sighting at ";
cout << A[N-1].position;
cout << endl;
}
else {
cout << "The roach was somewhere between ";
<< A[k-1].position
<< " and " << A[k].position << endl;
}

return 0;
}

/*********************************************
** FUNCTION DEFINITIONS
*********************************************/
istream& operator>>(istream& IN, point& p) {
char c;
IN >> c >> p.x >> c >> p.y >> c;
return IN;
}

ostream& operator<<(ostream& OUT, point p) {
OUT << '(' << p.x << ',' << p.y << ')';
return OUT;
}

istream& operator>>(istream& IN, hhmmss &T) {
char c;
IN >> c >> T.h >> c >> T.m >> c >> T.s >> c;
return IN;
}

bool operator<(hhmmss a, hhmmss b) {
if (a.h != b.h)
return a.h < b.h; // hours decide
if (a.m != b.m)
return a.m < b.m; // minutes decide
return a.s < b.s;   // down to seconds!
}

istream& operator>>(istream& IN, datum &d) {
char c;
IN >> c >> D.time >> c >> D.position >> c;
return IN;
}