/* Filename: datecmp.c Author: Br. David Carlson Date: February 18, 1999 This program compares 2 dates given in the command line parameters. There must be 6 such parameters in the form shown in this example: Jun 2, 1999 Sep 17, 2000 The program writes an integer to standard output to indicate the result of the comparison: 0 if the dates are equal, -1 if the first is less than the second, +1 if the first is greater than the second. A 2 is written out if an error occurs, such as an unknown date. */ #include #include /* Given: str A 3 letter string indicating a month. Task: To convert the str info into a number, using 1 for Jan, 2 for Feb, and so on. Return: This number in the function name. */ int GetMonth(char * str) { if (strcmp(str, "Jan") == 0) return 1; if (strcmp(str, "Feb") == 0) return 2; if (strcmp(str, "Mar") == 0) return 3; if (strcmp(str, "Apr") == 0) return 4; if (strcmp(str, "May") == 0) return 5; if (strcmp(str, "Jun") == 0) return 6; if (strcmp(str, "Jul") == 0) return 7; if (strcmp(str, "Aug") == 0) return 8; if (strcmp(str, "Sep") == 0) return 9; if (strcmp(str, "Oct") == 0) return 10; if (strcmp(str, "Nov") == 0) return 11; if (strcmp(str, "Dec") == 0) return 12; return 0; // unknown month } int main(int argc, char * argv[]) { int MonthA, MonthB, DayA, DayB, YearA, YearB; if (argc != 7) { printf("%d", 2); exit(1); } MonthA = GetMonth(argv[1]); MonthB = GetMonth(argv[4]); if ((MonthA == 0) || (MonthB == 0)) { printf("%d", 2); exit(1); } if (sscanf(argv[2], "%d", &DayA) != 1) { printf("%d", 2); exit(1); } if (sscanf(argv[5], "%d", &DayB) != 1) { printf("%d", 2); exit(1); } if (sscanf(argv[3], "%d", &YearA) != 1) { printf("%d", 2); exit(1); } if (sscanf(argv[6], "%d", &YearB) != 1) { printf("%d", 2); exit(1); } if (YearA < YearB) { printf("%d", -1); exit(0); } if (YearA > YearB) { printf("%d", 1); exit(0); } if (MonthA < MonthB) { printf("%d", -1); exit(0); } if (MonthA > MonthB) { printf("%d", 1); exit(0); } if (DayA < DayB) { printf("%d", -1); exit(0); } if (DayA > DayB) { printf("%d", 1); exit(0); } printf("%d", 0); // dates must be equal if reach this point exit(0); }