Skip to content

Instantly share code, notes, and snippets.

@royhowie
Last active October 16, 2015 21:41
Show Gist options
  • Save royhowie/ee1d1b4e81bd4441bcfe to your computer and use it in GitHub Desktop.
Save royhowie/ee1d1b4e81bd4441bcfe to your computer and use it in GitHub Desktop.
flight.c
// Roy Howie
// flight.c
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MIN_LOWER_BOUND 0
#define MAX_LOWER_BOUND 80
#define MAX_UPPER_BOUND 120
#define DELIM " "
typedef char String[256];
typedef enum { false, true } bool;
void getData (char *, char *, void *, int, int, char *);
bool isBetween (int, int, float);
void parseLine (char *, int *, float *);
int main (void) {
FILE * data;
String fileName, currentLine;
int lowest = 0, highest = 120;
// tracks for each month [ daysTotal, daysInRange ]
int tracker[][2] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
int greatestMonth = -1;
float greatestMonthValue = -1.0;
printf("Tell me about your dragon’s preferred temperature for flying:\n");
getData("What is the coldest temperature they can fly in?\n", "%d",
&lowest, MIN_LOWER_BOUND, MAX_LOWER_BOUND, "lower bound");
getData("What is the hottest temperature they can fly in?\n", "%d",
&highest, lowest, MAX_UPPER_BOUND, "upper bound");
printf("Please enter the name of the weather data file for Dragon Island.\n");
scanf("%s", fileName);
if ((data = fopen(fileName, "r")) == NULL) {
printf("File could not be opened. Exiting.\n");
return -1;
}
while (fgets(currentLine, 256, data) != NULL) {
int month;
float temp;
parseLine(currentLine, &month, &temp);
// if month is -1, break since we've reached EOF
if (month == -1) break;
// if the temperature for the current line is in the desired range
if (isBetween(lowest, highest, temp))
// incremenet the appropriate tracker by 1
tracker[month - 1][1]++;
// always increment day count by 1
tracker[month - 1][0]++;
}
for (int i = 0; i < 12; i++) {
float percent = 100.0 * tracker[i][1] / tracker[i][0];
printf("Month %d: %2.1f percent of days in range.\n", i + 1, percent);
// if this month was better than our current best, record it
if (percent > greatestMonthValue) {
greatestMonth = i + 1;
}
}
printf("I recommend month %d for the Celebration of the First Flight!\n", greatestMonth);
if (fclose(data) == EOF) {
perror("Unable to close file. Exiting program.");
return -1;
}
return 0;
}
void getData (char * msg, char * scanType, void * address, int lowest, int highest, char * boundType) {
String temp;
printf("%s", msg);
scanf(scanType, address);
while (!isBetween(lowest, highest, * (int *) address)) {
sprintf(temp, "Not a valid %s. Must be between %d and %d! Enter a new value:\n", boundType, lowest, highest);
printf("%s", temp);
scanf(scanType, address);
}
}
bool isBetween (int min, int max, float n) {
return min <= n && n <= max;
}
void parseLine (char * line, int * month, float * temp) {
char * token;
int step = 0;
while ((token = strsep(&line, DELIM)) != NULL) {
if (!isdigit(*token)) continue;
switch (step++) {
case 0:
*month = atoi(token);
if (*month == -1) return;
break;
case 3:
*temp = atof(token);
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment