/*********************************************
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;
};
point readpoint(istream* pIN);
void writepoint(point p, ostream* pOUT);
//--- TIME IN HH:MM:SS ----------------------//
struct hhmmss
{
int h,m,s;
};
hhmmss readtime(istream* pIN);
bool before(hhmmss a, hhmmss b);
//--- A DATA READING FROM THE EXPERIMENT ----//
struct datum
{
point position;
hhmmss time;
};
datum readdatum(istream* pIN);
/*********************************************
** 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++)
A[i] = readdatum(&IN);
// Get the query time from the user
cout << "Enter a time: ";
hhmmss T = readtime(&cin);
// Find the first sighting at or after given time
int k = 0;
while (k < N && before(A[k].time,T) )
k++;
// Write result
if (k == 0)
{
cout << "This was before the first sighting at ";
writepoint(A[0].position, &cout);
cout << endl;
}
else if (k == N)
{
cout << "This was after the last sighting at ";
writepoint(A[N-1].position, &cout);
cout << endl;
}
else
{
cout << "The roach was somewhere between ";
writepoint(A[k-1].position, &cout);
cout << " and ";
writepoint(A[k].position, &cout);
cout << endl;
}
return 0;
}
/*********************************************
** FUNCTION DEFINITIONS
*********************************************/
point readpoint(istream* pIN)
{
point p;
char c;
(*pIN) >> c >> p.x >> c >> p.y >> c;
return p;
}
void writepoint(point p, ostream* pOUT)
{
(*pOUT) << '(' << p.x << ',' << p.y << ')';
}
hhmmss readtime(istream* pIN)
{
hhmmss t;
char c;
(*pIN) >> c >> t.h >> c >> t.m >> c >> t.s >> c;
return t;
}
bool before(hhmmss a, hhmmss b)
{
if (a.h != b.h)
return a.h < b.h; // hours decide
else if (a.m != b.m)
return a.m < b.m; // minutes decide
else
return a.s < b.s; // down to seconds!
}
datum readdatum(istream *pIN)
{
datum d;
char c;
(*pIN) >> c;
d.time = readtime(pIN);
(*pIN) >> c;
d.position = readpoint(pIN);
(*pIN) >> c;
return d;
}