Grading Log for CS113 -- HW2 Name: McLaughlin, Matthew SCRIPT FILES ====== ===== calendar.scr =================================================== > Script started on Tue Feb 16 11:34:18 1999 > %cat calendar.c > > > /* > * File name: calendar.c > * Name: Matt McLaughlin > * Assignment: 2 > * Problem: 1 > * Date: February 15, 1999 > * > * This program is used to generate a calendar for a year > * entered by the user. > */ > > #include > > /* > * Constants: > * ---------- > * Days of the week are represented by the integers 0-6. > * Months of the year are identified by the integers 1-12; > * because this numeric representation for months is in > * common use, no special constants are defined. > */ > > #define MONDAY 0 > #define TUESDAY 1 > #define WEDNESDAY 2 > #define THURSDAY 3 > #define FRIDAY 4 > #define SATURDAY 5 > #define SUNDAY 6 > > /* Function prototypes */ > > void GiveInstructions(void); > int GetYearFromUser(void); > void PrintCalendar(int year); > void PrintCalendarMonth(int month, int year); > void IndentFirstLine(int weekday); > int PrintDays(int no_of_days, int weekday); > int MonthDays(int month, int year); > int FirstDayOfMonth(int month, int year); > char * MonthName(int month); > int IsLeapYear (int year); > > /* Main program */ > > int main(void) > { > /* The following is the year for which the calendar is printed out */ > int year; > > GiveInstructions(); > year = GetYearFromUser(); > PrintCalendar(year); > > return 0; > > } /* End of main */ > > /* > * Function: GiveInstructions > * Usage: GiveInstructions(); > * -------------------------- > * This procedure prints out instructions to the user. > */ > > void GiveInstructions(void) > { > > printf("This program displays a calendar for a full\n"); > printf("year. Any year after 1899 is okay.\n"); > > } /* End of function GiveInstructions */ > > /* > * Function: GetYearFromUser > * Usage: year = GetYearFromUser(); > * -------------------------------- > * This function reads in a year from the user and returns > * that value. If the user enters a year before 1900, the > * function gives the user another chance. > */ > > int GetYearFromUser(void) > { > > int year_input; /* The year inputed by the user */ > > printf("Enter a year for which you would like a calendar: "); > scanf("%d", &year_input); > > /* Check for invalid input. If it is invalid, prompt the user again */ > > for (printf("\n"); year_input < 1900; printf("\n")) { > printf("Invalid input. Year cannot be before 1900.\n"); > printf("Please enter a year: "); > scanf("%d", &year_input); > } /*End of while loop */ > > return year_input; > > } /* End of function GetYearFromUser */ > > /* > * Function: PrintCalendar > * Usage: PrintCalendar(year); > * --------------------------- > * This procedure prints a calendar for an entire year. > */ > > void PrintCalendar(int year) > { > > int month; > > /* Following loop calls PrintCalendarMonth once for each month */ > > for (month = 1; month <= 12; month++) { > PrintCalendarMonth(month, year); > printf("\n"); > } /* End of for loop */ > > } /* End of PrintCalendar */ > > /* > * Function: PrintCalendarMonth > * Usage: PrintCalendarMonth(month, year); > * --------------------------------------- > * This procedure prints a calendar for the given month > * and year. > */ > > void PrintCalendarMonth(int month, int year) > { > /* Value of day of week and number of days in a month, respectively */ > int weekday, no_of_days ; > > printf(" %s %d\n", MonthName(month), year); > printf(" Mo Tu We Th Fr Sa Su\n"); > no_of_days = MonthDays(month, year); /* Finds # of days in the month */ > weekday = FirstDayOfMonth(month, year); /* Finds day of the week */ > IndentFirstLine(weekday); /* Indents line according to day of the week */ > weekday = PrintDays (no_of_days, weekday); /* Prints out days of month */ > if (weekday != MONDAY) printf("\n"); /* Print blank line and end funtion */ > > } /* End of PrintCalendarMonth */ > > /* > * Function: IndentFirstLine > * Usage: IndentFirstLine(weekday); > * -------------------------------- > * This procedure indents the first line of the calendar > * by printing enough blank spaces to get to the position > * on the line corresponding to weekday. It does this simply > * by spacing over as many spaces are needed so that the numbers > * begin printing in the correct columns. > */ > > void IndentFirstLine(int weekday) > { > > while (weekday > 0) { > printf(" "); > --weekday; > } /* End of while loop */ > > } /* End of function */ > > /* > * Function: PrintDays > * Usage: PrintDays (no_of_days, weekday) > * -------------------------------- > * This procedure takes the number of days in a month and > * the first day in that month and prints out the dates in > * that month in the correct columns. > * It returns the value of the day after the last day of > * the month to the function PrintCalendarMonth. > */ > > int PrintDays (int no_of_days, int weekday) > { > > int day; > > for (day = 1; day <= no_of_days; day++) { > printf(" %2d", day); > if (weekday == SUNDAY) > printf("\n"); /* Go to the next line if it's a Sunday */ > weekday = (weekday + 1) % 7; > } /* End of for loop */ > > return (weekday); > > } /* End of PrintDays function */ > > /* > * Function: MonthDays > * Usage: ndays = MonthDays(month, year); > * -------------------------------------- > * MonthDays returns the number of days in the indicated > * month and year. The year is required to handle leap years. > */ > > int MonthDays(int month, int year) > { > /* > * Check to see if it's February. If not, check to see if it's a month > * with 30 days. If so, return 30, if not, return 31. > */ > > if (month != 2) { > if ((month == 4) || (month ==6) || (month == 9) || (month == 11)) > return 30; > else > return 31; > } /* End of first condition of outer if */ > /* The else statement is the case that it is February */ > else { > if (IsLeapYear(year)) > return 29; > else > return 28; > } /* End of else condition of outer if */ > > } /* End of MonthDays function */ > > /* > * Function: FirstDayOfMonth > * Usage: weekday = FirstDayOfMonth(month, year); > * ---------------------------------------------- > * This function returns the day of the week on which the > * indicated month begins. This program simply counts > * forward from January 1, 1900, which was a Monday. > */ > > int FirstDayOfMonth(int month, int year) > { > int days_from_monday = 0; /* Number of days from Monday */ > /* Below used as counters for months and years, respectively */ > int month_counter = 0, year_counter = 0; > > /* First count the days from Monday added on by each year from 1900 */ > for (year_counter = 1900; year_counter < year; ++year_counter) { > if (IsLeapYear(year_counter)) > days_from_monday += 2; /* 2 is equal to 366 mod 7 */ > else > days_from_monday += 1; /* 1 is equal to 365 mod 7 */ > } /* End of for loop */ > > /* Now count the days in the year entered up to the month we are at */ > for (month_counter = 1; month_counter < month; ++month_counter) > days_from_monday += (MonthDays(month_counter, year)) % 7; > > /* Now return the total number of days from monday mod 7 */ > return (days_from_monday % 7); > > } /* End of function FirstDayofMonth */ > > /* > * Function: MonthName > * Usage: name = MonthName(month); > * ------------------------------- > * MonthName converts a numeric month in the range 1-12 > * into the string name for that month. > */ > > char * MonthName(int month) > { > > switch (month) { > case 1: return ("January"); > case 2: return ("February"); > case 3: return ("March"); > case 4: return ("April"); > case 5: return ("May"); > case 6: return ("June"); > case 7: return ("July"); > case 8: return ("August"); > case 9: return ("September"); > case 10: return ("October"); > case 11: return ("November"); > case 12: return ("December"); > default: return ("Illegal month"); > } /* End of switch */ > > } /* End of MonthName */ > > /* > * Function: IsLeapYear > * Usage: if (IsLeapYear(year)) . . . > * ---------------------------------- > * This function returns 1 if year is a leap year, 0 if not. > * A year x is a leap year if x % 4 == 0 and it is not the case > * that (x % 100 == 0 and x % 400 != 0). > */ > > int IsLeapYear(int year) > { > > if ((year % 4 == 0) && (!((year % 100 == 0) && (year % 400 != 0)))) > return 1; > else > return 0; > > } /* End of IsLeapYear function */ > %gcc -ansi -pedantic -Wall calendar.c -o calprog > %calprog > This program displays a calendar for a full > year. Any year after 1899 is okay. > Enter a year for which you would like a calendar: 1999 > > January 1999 > Mo Tu We Th Fr Sa Su > 1 2 3 > 4 5 6 7 8 9 10 > 11 12 13 14 15 16 17 > 18 19 20 21 22 23 24 > 25 26 27 28 29 30 31 > > February 1999 > Mo Tu We Th Fr Sa Su > 1 2 3 4 5 6 7 > 8 9 10 11 12 13 14 > 15 16 17 18 19 20 21 > 22 23 24 25 26 27 28 > > March 1999 > Mo Tu We Th Fr Sa Su > 1 2 3 4 5 6 7 > 8 9 10 11 12 13 14 > 15 16 17 18 19 20 21 > 22 23 24 25 26 27 28 > 29 30 31 > > April 1999 > Mo Tu We Th Fr Sa Su > 1 2 3 4 > 5 6 7 8 9 10 11 > 12 13 14 15 16 17 18 > 19 20 21 22 23 24 25 > 26 27 28 29 30 > > May 1999 > Mo Tu We Th Fr Sa Su > 1 2 > 3 4 5 6 7 8 9 > 10 11 12 13 14 15 16 > 17 18 19 20 21 22 23 > 24 25 26 27 28 29 30 > 31 > > June 1999 > Mo Tu We Th Fr Sa Su > 1 2 3 4 5 6 > 7 8 9 10 11 12 13 > 14 15 16 17 18 19 20 > 21 22 23 24 25 26 27 > 28 29 30 > > July 1999 > Mo Tu We Th Fr Sa Su > 1 2 3 4 > 5 6 7 8 9 10 11 > 12 13 14 15 16 17 18 > 19 20 21 22 23 24 25 > 26 27 28 29 30 31 > > August 1999 > Mo Tu We Th Fr Sa Su > 1 > 2 3 4 5 6 7 8 > 9 10 11 12 13 14 15 > 16 17 18 19 20 21 22 > 23 24 25 26 27 28 29 > 30 31 > > September 1999 > Mo Tu We Th Fr Sa Su > 1 2 3 4 5 > 6 7 8 9 10 11 12 > 13 14 15 16 17 18 19 > 20 21 22 23 24 25 26 > 27 28 29 30 > > October 1999 > Mo Tu We Th Fr Sa Su > 1 2 3 > 4 5 6 7 8 9 10 > 11 12 13 14 15 16 17 > 18 19 20 21 22 23 24 > 25 26 27 28 29 30 31 > > November 1999 > Mo Tu We Th Fr Sa Su > 1 2 3 4 5 6 7 > 8 9 10 11 12 13 14 > 15 16 17 18 19 20 21 > 22 23 24 25 26 27 28 > 29 30 > > December 1999 > Mo Tu We Th Fr Sa Su > 1 2 3 4 5 > 6 7 8 9 10 11 12 > 13 14 15 16 17 18 19 > 20 21 22 23 24 25 26 > 27 28 29 30 31 > > %calprog > This program displays a calendar for a full > year. Any year after 1899 is okay. > Enter a year for which you would like a calendar: 1984 > > January 1984 > Mo Tu We Th Fr Sa Su > 1 > 2 3 4 5 6 7 8 > 9 10 11 12 13 14 15 > 16 17 18 19 20 21 22 > 23 24 25 26 27 28 29 > 30 31 > > February 1984 > Mo Tu We Th Fr Sa Su > 1 2 3 4 5 > 6 7 8 9 10 11 12 > 13 14 15 16 17 18 19 > 20 21 22 23 24 25 26 > 27 28 29 > > March 1984 > Mo Tu We Th Fr Sa Su > 1 2 3 4 > 5 6 7 8 9 10 11 > 12 13 14 15 16 17 18 > 19 20 21 22 23 24 25 > 26 27 28 29 30 31 > > April 1984 > Mo Tu We Th Fr Sa Su > 1 > 2 3 4 5 6 7 8 > 9 10 11 12 13 14 15 > 16 17 18 19 20 21 22 > 23 24 25 26 27 28 29 > 30 > > May 1984 > Mo Tu We Th Fr Sa Su > 1 2 3 4 5 6 > 7 8 9 10 11 12 13 > 14 15 16 17 18 19 20 > 21 22 23 24 25 26 27 > 28 29 30 31 > > June 1984 > Mo Tu We Th Fr Sa Su > 1 2 3 > 4 5 6 7 8 9 10 > 11 12 13 14 15 16 17 > 18 19 20 21 22 23 24 > 25 26 27 28 29 30 > > July 1984 > Mo Tu We Th Fr Sa Su > 1 > 2 3 4 5 6 7 8 > 9 10 11 12 13 14 15 > 16 17 18 19 20 21 22 > 23 24 25 26 27 28 29 > 30 31 > > August 1984 > Mo Tu We Th Fr Sa Su > 1 2 3 4 5 > 6 7 8 9 10 11 12 > 13 14 15 16 17 18 19 > 20 21 22 23 24 25 26 > 27 28 29 30 31 > > September 1984 > Mo Tu We Th Fr Sa Su > 1 2 > 3 4 5 6 7 8 9 > 10 11 12 13 14 15 16 > 17 18 19 20 21 22 23 > 24 25 26 27 28 29 30 > > October 1984 > Mo Tu We Th Fr Sa Su > 1 2 3 4 5 6 7 > 8 9 10 11 12 13 14 > 15 16 17 18 19 20 21 > 22 23 24 25 26 27 28 > 29 30 31 > > November 1984 > Mo Tu We Th Fr Sa Su > 1 2 3 4 > 5 6 7 8 9 10 11 > 12 13 14 15 16 17 18 > 19 20 21 22 23 24 25 > 26 27 28 29 30 > > December 1984 > Mo Tu We Th Fr Sa Su > 1 2 > 3 4 5 6 7 8 9 > 10 11 12 13 14 15 16 > 17 18 19 20 21 22 23 > 24 25 26 27 28 29 30 > 31 > > %exit > exit > > script done on Tue Feb 16 11:35:30 1999