Grading Log for CS113 -- HW2 Name: Yaar, Abraham SCRIPT FILES ====== ===== calendar.scr =================================================== > Script started on Mon Feb 15 13:56:59 1999 > %cat calendar.c > /* > * File name: calendar.c > * Name: Avi Yaar > * Assignment: 2 > * Problem: 1 > * Date: February 16, 1999 > * > * This is a calendar program that will print out the calendar of > * any year after 1899. It prints the calendar in 4 rows by 3 columns. > */ > > /* > * Inclusions > */ > #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 PrintThreeMonths(int month, int year); > void PrintMonthTitles(int month, int year); > void IndentFirstLine( int weekday ); > void PrintCalendar(int year); > void GiveInstructions(void); > int PrintWeek( int startday, int days_in_month, int daynum ); > int FirstDayOfMonth(int month, int year); > int MonthDays( int month, int year); > int FirstDayOfYear( int year ); > int GetYearFromUser(void); > int IsLeapYear(int year); > char *MonthName(int month); > > /* > * Main program: Simply calls instruction, input and output functions. > */ > int main(void) > { > int year; > > GiveInstructions(); > year = GetYearFromUser(); > PrintCalendar(year); > > return 0; > } > > > /* > * 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"); > } > > > /* > * 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. Using a while loop here > * is a good idea. The loop should only end when a good year is entered. > */ > int GetYearFromUser(void) > { > int year = 0; > > /*scanf function returns the number of arguments that were properly entered * > * (i.e. if scaning for int and user enters char, scanf returns 0.) The loop* > * continues until the user enters an integer (!scanf) and that integer is * > * greater than 1900. */ > while ( !scanf("%d", &year) || year < 1900 ) { > printf("That is not a valid year. Please try again.\n"); > fflush(stdin); /* DON'T FLUSH INPUT FILES -- TF */ > } /* of WHILE */ > > return(year); > } > > > /* > * Function: PrintCalendar > * Usage: PrintCalendar(year); > * --------------------------- > * This procedure prints a calendar for the year specified. > */ > void PrintCalendar(int year) > { > int month; > > for (month = 1; month <= 12; month += 3) { /* Loop increments by three */ > PrintThreeMonths(month, year); /* because 3 months are */ > printf("\n"); /* printed at once. */ > } /* of FOR */ > } > > > /* > * Function: PrintThreeMonths > * Usage: PrintThreeMonths(month, year); > * --------------------------------------- > * This function prints out the calendar for the month > * specified and the next two months as well. Each variable > * is declard in triplicate for its respective month. > */ > void PrintThreeMonths(int month, int year) > { > int daynum1, daynum2, daynum3, > weekday1, weekday2, weekday3, > days_in_month1, days_in_month2, days_in_month3, > i; > > PrintMonthTitles( month, year ); > > days_in_month1 = MonthDays(month, year); /* Get the num of days */ > days_in_month2 = MonthDays(month + 1, year); /* in each month. */ > days_in_month3 = MonthDays(month + 2, year ); > > weekday1 = FirstDayOfMonth( month, year ); /* Get the day of the week */ > weekday2 = FirstDayOfMonth( month+1, year ); /* for the first day of */ > weekday3 = FirstDayOfMonth( month+2, year ); /* the month. */ > > daynum1 = 1; /*The first day of the month is the '1st'*/ > IndentFirstLine( weekday1 ); > daynum1 = PrintWeek( weekday1, days_in_month1, daynum1 ); > > printf(" "); /* add spacing between months */ > daynum2 = 1; > IndentFirstLine( weekday2 ); > daynum2 = PrintWeek( weekday2, days_in_month2, daynum2 ); > > printf(" "); /* add spacing between months */ > daynum3 = 1; > IndentFirstLine( weekday3 ); > daynum3 = PrintWeek( weekday3, days_in_month3, daynum3 ); > > printf("\n"); > > /* After the first week in each month is printed, each succesive week easy*/ > /* to loop through. At most, 5 weeks still need to be printed. */ > for ( i = 0; i < 5; i++ ) { > daynum1 = PrintWeek( MONDAY, days_in_month1, daynum1 ); > > printf(" "); /* add spacing between months */ > daynum2 = PrintWeek( MONDAY, days_in_month2, daynum2 ); > > printf(" "); /* add spacing between months */ > daynum3 = PrintWeek( MONDAY, days_in_month3, daynum3 ); > > printf("\n"); > } /* of FOR */ > } ^^ good commenting in this function > > > /* Function: PrintWeek > * Usage: PrintWeek( weekday, days in the month, number of day to start at ); > * -------------------------------------------------------------------------- > * This function prints out one week of the month passed to it. It starts > * on the weekday specified and stops either at the end of the week > * or at the end of the month. If the end of the month is reached it > * moves the cursor to the end of the week. Likewise, if there is no > * valid week to print, it will simply print a blank week. The function > * returns the number of the next day to be printed. > */ > int PrintWeek( int startday, int days_in_month, int daynum ) > { > int day = startday; > > if ( daynum <= days_in_month ) { /*if there are still valid weeks to print*/ > do > { > printf(" %2d", daynum ); > daynum++; > day = (day+1)%7; > } while ( (daynum <= days_in_month) && (day != MONDAY) ); > > for ( ; (day%7) != MONDAY; day++ ) /* Move cursor to end of week, */ > printf(" "); /* without any output, for alignment*/ > /* of other months. */ > } /* of IF */ > else /* If there are no more weeks to */ > printf(" "); /* print, simply print a blank week.*/ > > return(daynum); > } > > > /* > * Function: PrintMonthTitles > * Usage: PrintMonthTitles( month, year ); > * --------------------------------------- > * This procedure simply prints out name of the month passed to > * it and the next two month names as well. It also prints > * out the days of the week for each of the three months > */ > void PrintMonthTitles(int month, int year ) > { > int i; > /* print month names in a */ > for (i = month; i < month + 3; i++ ) /* field of at least size 8.*/ > printf(" %8s %d\t", MonthName(i), year); /* This helps center the */ > /* names above the list. */ > printf("\n"); > for ( i = 0; i < 3; i++) /* Print the days of the week x3 */ > printf(" Mo Tu We Th Fr Sa Su\t"); > > printf("\n"); > } > > > /* > * 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) > { > for( ; weekday != 0; weekday--) /* move 3 spaces for every 1 day until */ > printf(" "); /* the proper day is reached. */ > } > > > /* > * 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. > * You may want to use the IsLeapYear function in this function. > */ > int MonthDays(int month, int year) > { > switch( month ) { > case 4: /* April, June, September and */ > case 6: /* November all have 30 days. */ > case 9: > case 11: return(30); > > case 1: /* January, March, May, July, August */ > case 3: /* October, and December all have 30 days. */ > case 5: > case 7: > case 8: > case 10: > case 12: return(31); > > case 2: if (IsLeapYear(year)) /* Febuary has 29 days on leap */ > return(29); /* year, 28 otherwise. */ > else > return(28); > default: return(0); > } /* of switch */ > } > > > /* > * 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. > * It may be useful to use the IsLeapYear function. > */ > int FirstDayOfMonth(int month, int year) > { > int day = FirstDayOfYear( year ); /*initialize day to first day of year*/ > int i; > > for ( i = 1; i < month; i++ ) /* go through months, start with Jan */ > day += MonthDays( i, year ); /* and keep a running sum of days. */ > > day = day % 7; /* wrap days to 0-6 */ > > return( day ); > } > > > /* > * Function: FirstDayOfYear > * Usage: weekday = FirstDayOfYear( year ); > * ---------------------------------------- > * This function returns the day of the week on which the > * indicated year begins. I ADDED THIS FUNCTION AS A CONVENIENCE > * TO SIMPLIFY THE FirstDayOfMonth function. This function does > * the counting from Monday 1/1/1900. > */ > int FirstDayOfYear( int year ) > { > int i=1900, day = MONDAY; /*start day of week Monday and count from 1900*/ > > while ( i != year ) /* In any given year the first day of the */ > { /* week is one day ahead of the first day */ > if ( IsLeapYear(i) ) /* of the year before it. If the previous */ > day += 2; /* year was a leap year, the first day is */ > else /* 2 days ahead of the previous year. */ > day++; > > day = day % 7; /* Use of modulus keeps day looping between 0-6 */ > i++; > } /* of WHILE */ > > return(day); > } > > > /* > * 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"); > } /* of SWITCH */ > } > > > /* > * Function: IsLeapYear > * Usage: if (IsLeapYear(year)) . . . > * ---------------------------------- > * This function returns 1 if year is a leap year, 0 if not. > */ > int IsLeapYear(int year) > { > /* If y is evenly divisible by x then !(x%y) is true and (x%y) is false.*/ > if ( !(year % 4 ) ) /*If year is divisible by four then test century.*/ > { > if ( !(year % 100) && (year % 400) ) /*If year is century but not */ > return(0); /*divisible by 400 then no leap */ > else /*year, otherwise, it is */ > return(1); > } /* of IF */ > else /*If year not divisible by 4 then not leap year*/ > return(0); > } > %gcc -ansi -pedantic -Wall calendar.c > %a.out > This program displays a calendar for a full > year. Any year after 1899 is okay. > 1999 > January 1999 February 1999 March 1999 > Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su > 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7 > 4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14 > 11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21 > 18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28 > 25 26 27 28 29 30 31 29 30 31 > > > April 1999 May 1999 June 1999 > Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su > 1 2 3 4 1 2 1 2 3 4 5 6 > 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13 > 12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20 > 19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27 > 26 27 28 29 30 24 25 26 27 28 29 30 28 29 30 > 31 > > July 1999 August 1999 September 1999 > Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su > 1 2 3 4 1 1 2 3 4 5 > 5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12 > 12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19 > 19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26 > 26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30 > 30 31 > > October 1999 November 1999 December 1999 > Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su > 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 > 4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12 > 11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19 > 18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26 > 25 26 27 28 29 30 31 29 30 27 28 29 30 31 > > > %a.out > This program displays a calendar for a full > year. Any year after 1899 is okay. > 1984 > January 1984 February 1984 March 1984 > Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su > 1 1 2 3 4 5 1 2 3 4 > 2 3 4 5 6 7 8 6 7 8 9 10 11 12 5 6 7 8 9 10 11 > 9 10 11 12 13 14 15 13 14 15 16 17 18 19 12 13 14 15 16 17 18 > 16 17 18 19 20 21 22 20 21 22 23 24 25 26 19 20 21 22 23 24 25 > 23 24 25 26 27 28 29 27 28 29 26 27 28 29 30 31 > 30 31 > > April 1984 May 1984 June 1984 > Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su > 1 1 2 3 4 5 6 1 2 3 > 2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10 > 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17 > 16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24 > 23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30 > 30 > > July 1984 August 1984 September 1984 > Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su > 1 1 2 3 4 5 1 2 > 2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9 > 9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16 > 16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23 > 23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30 > 30 31 > > October 1984 November 1984 December 1984 > Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su > 1 2 3 4 5 6 7 1 2 3 4 1 2 > 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 > 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 > 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 > 29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 > 31 > > %exit > exit > > script done on Mon Feb 15 13:58:03 1999