Skip to content

Instantly share code, notes, and snippets.

@timvisee
Created December 17, 2014 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timvisee/1c5c511ec67f4c16401a to your computer and use it in GitHub Desktop.
Save timvisee/1c5c511ec67f4c16401a to your computer and use it in GitHub Desktop.
/**
* Flight Times - A practical computer science project.
*
* @author Tim Visee
* @website http://timvisee.com/
* @copyright Copyright (c) Tim Visee 2014. All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define FLIGHT_TIME_COUNT 8
/**
* Print the arrival or departure time with proper formatting.
*
* @param int i The index of the arrival or departure time to print.
* @param bool departure True to print the departure time, false to print the arrival time.
*
* @return bool True on success, false on failure.
*/
bool printTime(int i, bool departure);
/**
* Print a time with proper formatting.
*
* @param int hour The hour of the time to print.
* @param int min The minute of the time to print.
*
* @return bool True on success, false on failure because the time was invalid.
*/
bool printTimeFormatted(int hour, int min);
/**
* Check whether a time is valid.
*
* @param int hour The hour.
* @param int min The minute.
*
* @param bool True if the time is valid, false otherwise.
*/
bool isValidTime(int hour, int min);
/**
* Get the closest flight to the preferred time.
*
* @pararm int prefHour The preferred hour.
* @param int prefMin The preferred minute.
*
* @return int The index of the closest flight.
*/
int getClosestFlight(int prefHour, int prefMin);
/**
* Get the difference between two times in minutes. This method works around the clock.
*
* @param int hour The hour of the first time.
* @param int min The minute of the first time.
* @param int otherHour The hour of the second time.
* @param int otherMin The minute of the other time.
*
* @param The time difference in minutes, or -1 on failure if one or both times are invalid.
*/
int getTimeDifference(int hour, int min, int otherHour, int otherMin);
/**
* Get the minimum value of two integers.
*
* @param int x The first value.
* @param int y The second value.
*
* @return The smallest of the two values.
*/
int minVal(int x, int y);
/**
* Get the maximum value of two integers.
*
* @param int x The first value.
* @param int y The second value.
*
* @return The biggest of the two values.
*/
int maxVal(int x, int y);
/**
* Get the absolute value of an integer.
*
* @param int x The value to get the absolute value from.
*
* @return int The absolute value.
*/
int absVal(int x);
/**
* @var The flight times in an array.
* The first dimension is used to define all the different time frames.
* The second dimension defines the departure and arrival time.
* The third dimension defines the hours and minutes.
*/
int flightTimes[FLIGHT_TIME_COUNT][2][2] = {
{{ 8, 0}, {10, 16}},
{{ 9, 43}, {11, 52}},
{{11, 19}, {13, 31}},
{{12, 47}, {15, 0}},
{{14, 0}, {16, 8}},
{{15, 45}, {17, 55}},
{{19, 0}, {21, 20}},
{{21, 45}, {23, 58}}};
/**
* Main function, called on initialization.
*
* @return int Application exit code.
*/
int main(void) {
// Define a variable to store the users hour and minute in
int prefHour = -1;
int prefMin = -1;
while(!isValidTime(prefHour, prefMin)) {
// Ask the user for the time
printf("Please enter the preferred time using the 24h format (XX:XX):\n");
scanf("%d:%d", &prefHour, &prefMin);
if(!isValidTime(prefHour, prefMin))
printf("The time you've entered is invalid!\n");
}
printf("\nPreferred Time: ");
printTimeFormatted(prefHour, prefMin);
printf("\n\n");
// Get the closest flight
int closest = getClosestFlight(prefHour, prefMin);
printf("Departure Time: ");
printTime(closest, true);
printf("\n");
printf("Arrival Time: ");
printTime(closest, false);
printf("\n\n");
// Pause and exit the program with the proper application exit code
printf("Done. Press a key to close the application.");
system("PAUSE > NUL");
return 0;
}
bool printTime(int i, bool departure) {
// Make sure the index is valid
if(i < 0 || i >= FLIGHT_TIME_COUNT)
return false;
// Print the time user proper formatting
printTimeFormatted(flightTimes[i][departure ? 0 : 1][0], flightTimes[i][departure ? 0 : 1][1]);
}
bool printTimeFormatted(int hour, int min) {
// Make sure the time is valid
if(!isValidTime(hour, min))
return false;
// Print the time with proper formatting, return the result
printf("%02d:%02d", hour, min);
return true;
}
bool isValidTime(int hour, int min) {
return !(hour < 0 || hour >= 24 || min < 0 || min >= 59);
}
int getClosestFlight(int prefHour, int prefMin) {
// Make sure the time is valid
if(!isValidTime(prefHour, prefMin))
return -1;
// Get the time before and after the preferred time
int before = FLIGHT_TIME_COUNT - 1;
int after = 0;
// Get the flight after the preferred time
int i;
for(i = 0; i < FLIGHT_TIME_COUNT; i++) {
// Make sure the current flight time is after the preferred time in hours
if(prefHour < flightTimes[i][0][0])
continue;
// Make sure the current flight time is after the preferred time in minutes
if(prefMin < flightTimes[i][0][1])
continue;
// Set the fight time after the preferred time
after = i;
break;
}
// Get the differences in time of both flights
int diffBefore = getTimeDifference(flightTimes[before][0][0], flightTimes[before][0][1], prefHour, prefMin);
int diffAfter = getTimeDifference(flightTimes[after][0][0], flightTimes[after][0][1], prefHour, prefMin);
// Check which one of the two times is closer to the preferred time
if(diffAfter > diffBefore)
return before;
return after;
}
int getTimeDifference(int hour, int min, int otherHour, int otherMin) {
// Make sure both times are valid
if(!isValidTime(hour, min) || !isValidTime(otherHour, otherMin))
return -1;
// Get both times in minutes
int time = hour * 60 + min;
int otherTime = otherHour * 60 + otherMin;
// Calculate the time difference
int diff = absVal(maxVal(time, otherTime) - minVal(time, otherTime));
int otherDiff = absVal(diff - (24 * 60));
// Return the smallest difference
if(diff < otherDiff)
return diff;
return otherDiff;
}
int minVal(int x, int y) {
return x < y ? x : y;
}
int maxVal(int x, int y) {
return x > y ? x : y;
}
int absVal(int x) {
return x >= 0 ? x : x * -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment