/* Time planner program */ #include "cd103.h" char dummy ; /* Used to store the value of getc(stdin) to prevent */ /* the 'Code has no effect' compiler warning. */ void main () { /* Description ...... SECTION 1 MAKE COPY OF FILE AND UPDATE REPEATING RECORDS calculate current date as an integer Open input and output files Read first record initialise record counter WHILE not end of file calculate record date as an integer IF record current (i.e. not expired) set record's number equal to counter Write record to output file Add 1 to counter ELSE record has expired IF a Repeating record DO case Daily repeat : add 1 to days field (check month & year) case Weekly repeat : add 7 to days field (check month & year) case Monthly repeat : add 1 to month field (check year) WHILE record still expired set record's number equal to counter Write updated record to output file Add 1 to counter Read next record Write terminating record to output file Close the files Replace input file with output file SECTION 2 DISPLAY TODAY'S APPOINTMENTS Open input file DO Clear the screen Initialise counter to 1 Display headings for Today's Reminders Read first record from file WHILE not end of file and screen not full Calculate record's date as integer IF record's date = today Display record on screen Add 1 to counter Read next record IF not end of file Display Hit a key to see next screenful Accept key stroke WHILE not end of file Close input file Display Hit any key to continue Accept key stroke SECTION 3 DISPLAY MENU AND PROCESS RECORDS DO Display the main menu Accept user's choice case choice = 1 : Add a record to file case choice = 2 : Delete a record case choice = 3 : Amend a record case choice = 4 : Display today's appointments case choice = 5 : Display file contents case choice = 6 : Exit program otherwise : Display error message WHILE user not choose exit */ /* Identifiers as needed */ int user_choice ; int counter ; /* Prototypes for the functions */ int DaysInMonth (int, int) ; void DisplayFile (void) ; void DisplayMenu (void) ; void AddRecord (void) ; void DeleteRecord (void) ; void AmendRecord (void) ; void DisplayToday (void) ; /* End of Declarations section */ /* SECTION 1 */ system_date = GetDate () ; input_file = fopen (in_filename, "r") ; output_file = fopen (out_filename, "w"); ReadRecord () ; counter = 1 ; while (day != 99) /* while not at end of the data file */ { long_year = year ; long_month = month ; record_date = long_year * 10000 + long_month * 100 + day ; if (record_date >= system_date) /* record not expired */ { number = counter ; WriteRecord () ; ++ counter ; } else /* record expired, check to see if it needs to be repeated */ { if (type == 'R') /* repeating record */ { do { switch (frequency) { case 'D' : AddDays (frequency) ; day = day + 1; break ; case 'W' : AddDays (frequency) ; day = day + 7 ; break ; case 'M' : /* month ;*/ IncrementMonth (); month = month + 1 ; break ; } long_year = year ; long_month = month ; record_date = long_year * 10000 + long_month * 100 + day ; } while ( record_date < system_date ) ; number = counter ; WriteRecord () ; ++ counter ; } } ReadRecord () ; } /* END while loop */ WriteTerminator () ; fcloseall () ; remove (in_filename) ; rename (out_filename, in_filename) ; /* SECTION 2 */ input_file = fopen (in_filename, "r") ; do { clrscr () ; counter = 0 ; printf ("\n\n\n\n\n") ; printf ("%12c** Today's reminders **\n\n", ' ') ; printf ("%12cDate Start End Message\n", ' ') ; printf ("%12c============================================\n", ' ') ; ReadRecord () ; while ((day != 99) && (counter < 10)) { long_year = year ; long_month = month ; record_date = long_year * 10000 + long_month * 100 + day ; if (record_date == system_date) /* display appointment for today */ { printf ("%12c%d-%s %02d:%02d %02d:%02d %s\n", ' ', day, LookupMonth (month), start_hours, start_minutes, end_hours,end_minutes, message) ; ++ counter ; } ReadRecord () ; } if (day != 99) { printf ("\n%12cHit a key to see next screenful", ' ') ; getch () ; } } while (day != 99) ; fclose (input_file) ; printf ("\n%12cHit any key to continue", ' ') ; getch () ; /* SECTION 3 */ do { DisplayMenu (); user_response=getch () ; switch (user_response) { case '1' : AddRecord (); break ; case '2' : DeleteRecord () ; break ; case '3' : AmendRecord () ; break ; case '4' : DisplayToday () ; break ; case '5' : DisplayFile () ; break ; case '6' : WriteTerminator () ; exit() ; default : DisplayError (6); } } while ( user_response != 6 ) ; } /* END main */ /*Function definitions */ /* function DisplayFile */ /* This function displays the contents of the data file on the screen */ /* ten records at a time. */ void DisplayFile (void) { /* Description ...... Open input file DO Clear the screen Display headings Read first record from file Initialise counter to 0 WHILE screen not full (counter < 10) and not end of file Calculate record duration Display record on screen Read next record Add 1 to counter if not end of file Display Hit a key to see next screenful Accept key stroke WHILE not end of file Close input file Prompt user to continue */ int counter ; input_file = fopen ("in_filename", "r") ; do { clrscr () ; printf ("\n\n\n\n\n%12c** File Contents **\n\n", ' ') ; printf (" Date Start End Duration Type Freq. Message Number\n") ; printf ("=============================================================================\n") ; ReadRecord () ; counter = 0 ; while ((counter < 10) && (day !=99)) { start_time = start_hours * 60 + start_minutes ; end_time = end_hours * 60 + end_minutes ; duration = (end_time - start_time) ; printf ("%02d-%s-%d %02d:%02d %02d:%02d %02d:%02d %c %c %s %5d\n", day, LookupMonth (month), year, start_hours, start_minutes, end_hours, end_minutes, duration / 60, duration % 60, type, frequency, message, number) ; ++ counter ; ReadRecord () ; } if (day !=99) { printf ("\n%12cHit a key to see next screenful", ' ') ; getch () ; } } while (day !=99) ; fclose (input_file) ; printf ("\n%12cHit a key to continue", ' ') ; getch () ; } /* function DisplayMenu */ /* Function to display menu and prompt for user's choice */ void DisplayMenu (void) { /* Description ...... Clear the screen Display menu Display prompt for user's choice */ clrscr () ; printf ("\n\n\n\n\n%12c** Daily Appointments Reminder **\n\n", ' ') ; printf ("%12c1: ADD Item\n\n", ' ') ; printf ("%12c2: DELETE Item\n\n", ' ') ; printf ("%12c3: AMEND Item\n\n", ' ') ; printf ("%12c4: DISPLAY Today's Appointments\n\n", ' ') ; printf ("%12c5: DISPLAY File Contents\n\n", ' ') ; printf ("%12c6: EXIT\n\n", ' ') ; printf ("%12cPlease choose option (1-6) : ", ' ') ; } /* function AddRecord */ /* function to add a record to the file */ void AddRecord (void) { /* Description ...... Open input and output files Read first record from input file WHILE not end of file Copy record to output file Store record's number Read next record Close input file DO Clear the screen Display headings Prompt for and accept Day Prompt for and accept Month Prompt for and accept Year Prompt for and accept Time Prompt for and accept Type Convert type to upper case IF Repeating record Prompt for and accept frequency WHILE invalid frequency Display error message Prompt for and accept frequency ELSE non-repeating message Assign blank to frequency Prompt for and accept record message Prompt for saving record to file IF user wants to save record Assign stored record number to record number Write record to output file Add 1 to stored record number Prompt for and accept Add another record WHILE user wants to keep adding records Close files Replace input file with output file */ int last_record_number ; char filler, response ; input_file = fopen (in_filename, "r") ; output_file = fopen (out_filename, "w") ; ReadRecord () ; while (day !=99) { WriteRecord () ; last_record_number = number ; ReadRecord () ; } fclose (input_file) ; do { clrscr () ; printf ("\n\n\n\n\n ** Add an appointment **\n\n", ' ') ; printf (" Enter day (DD) ", ' ') ; scanf ("%d", &day) ; printf (" Enter month (MM) ", ' ') ; scanf ("%d", &month) ; printf ("%12cEnter year (YYYY) ", ' ') ; scanf ("%d", &year) ; printf (" Enter start time (HH MM) ", ' ') ; scanf ("%d%d", &start_hours, &start_minutes) ; printf (" Enter end time (HH MM) ") ; scanf ("%d%d", &end_hours, &end_minutes) ; printf ("%12cEnter record type (R/S) ", ' ') ; scanf ("\n%c", &type) ; type = toupper (type) ; /* toupper converts a character to its */ /* upper-case equivalent */ if (type == 'R') { printf ("%12cEnter repeat frequency (D/W/M) ",' ') ; scanf ("\n%c", &frequency) ; frequency = toupper (frequency) ; while ((frequency != 'D') && (frequency != 'W') && (frequency != 'M')) { clrscr() ; printf ("\n\n\n\n\n%12cFrequency must be one of D/W/M\n", ' ') ; printf ("%12cEnter the repeat frequency (D/W/M) ", ' ' ) ; scanf ("\n%c", &frequency) ; frequency = toupper (frequency); } } else { frequency = ' '; } printf ("%12cEnter message (max 25 characters) ", ' ') ; dummy = getc (stdin) ; gets (message) ; for (counter = strlen (message); counter < 25; counter ++) strcat (message, " ") ; printf ("%12cDo you want to save this message to the file? (Y/N) ", ' ') ; scanf ("\n%c", &response) ; if ((response == 'Y') || (response == 'y')) { number = last_record_number + 1 ; WriteRecord () ; ++ last_record_number ; } printf ("\n%12cDo you want to add another record? (Y/N) ", ' '); scanf ("\n%c", &response) ; } while ((response == 'Y') || (response == 'y')) ; WriteTerminator (); fclose (output_file) ; remove (in_filename) ; rename (out_filename, in_filename) ; } /* function DeleteRecord */ /* function to remove a record from the file */ void DeleteRecord (void) { /* Description ...... DO Display file contents Prompt for and accept record number to delete Open files Read first record from file Initialise counter to 1 WHILE not end of file IF this record not to be deleted Assign counter to record number Write record to output file Add 1 to counter Read next record Close input file Write terminating record to output file] Close output file Replace input file with output file Prompt for user to delete another record WHILE user wants to delete records */ int counter, delete_number ; char response ; do { DisplayFile () ; printf ("%12cPlease enter number of record to be deleted ", ' ') ; scanf ("%d", &delete_number) ; input_file = fopen (in_filename, "r" ) ; output_file = fopen (out_filename, "w") ; ReadRecord () ; counter = 1 ; while (day != 99) { if (number != delete_number) { counter = number; WriteRecord () ; ++ counter ; ReadRecord (); } fclose (input_file) ; WriteTerminator (); fclose (output_file) ; remove (in_filename) ; rename (out_filename, in_filename) ; printf ("\n%12cDo you want to delete another record ? (Y/N) ", ' ') ; scanf ("\n%c", &response) ; } } while ((response == 'Y') || (response == 'y')) ; } /* function AmendRecord */ /* function to alter the contents of a record */ void AmendRecord () { /* Description DO Display file contents Prompt for and accept record number to be amended Open files Read first record WHILE not end of file IF record number equals chosen number DO Clear the screen Display headings Display record on screen Display menu of fields to amend Prompt for and accept field choice case choice Date (1) : Clear screen Display record's date Prompt for and accept new date case Choice Time (2) : Clear screen Display record's time Prompt for and accept new time case choice Type (3) : Clear screen Prompt for and accept type IF repeating record Prompt for and accept frequency WHILE invalid frequency Display error message Prompt for and accept frequency ELSE Assign blank to frequency case choice Frequency (4) : IF a repeating record Clear screen Display current frequency Prompt for and accept frequency IF invalid frequency entered Display error, frequency unchanged ELSE Assign new frequency to frequency ELSE not a repeating record Display error message case choice Message (5) : Clear screen Prompt for and accept new message case choice Exit (6) : Exit default : Display error message, get new choice WHILE user wants to continue Write record to output file Read next record Close files Replace inputfile with output file Prompt for amending another WHILE user wants to amend records */ int amend_number, user_choice ; char new_frequency, response, filler; do { DisplayFile () ; printf ("\n\n%12cPlease enter number of record to be amended ", ' ') ; scanf ("%d", &amend_number) ; input_file = fopen (in_filename, "r" ) ; output_file = fopen (out_filename, "w") ; ReadRecord () ; while (day !=99) { if (number == amend_number) { do { clrscr () ; printf (" Date Start End Duration Type Freq. Message Number\n") ; printf ("=============================================================================\n") ; start_time = start_hours * 60 + start_minutes ; end_time = end_hours * 60 + end_minutes ; duration = (end_time - start_time) ; printf ("%02d-%s-%02d %02d:%02d %02d:%02d %02d:%02d %c %c %s %5d\n", day, LookupMonth (month), year, start_hours, start_minutes, end_hours, end_minutes, duration / 60, duration % 60, type,frequency, message, number) ; printf ("\n\n%12cUPDATE ITEM\n", ' ') ; printf ("%12c1. Date \n", ' ') ; printf ("%12c2. Time\n", ' ') ; printf ("%12c3. Record type\n", ' ') ; printf ("%12c4. Repeat frequency\n", ' ') ; printf ("%12c5. Message\n", ' ') ; printf ("%12c6. Exit\n", ' ') ; printf ("\n%12cPlease select a field to update : ", ' ') ; scanf ("%d%c", &user_choice, &filler) ; switch (user_choice) { case 1: clrscr () ; printf ("\n\n\n\n\n") ; printf ("%12cCurrent date is %02d:%02d:%d\n", ' ', day, month, year) ; printf ("%12cEnter new date (dd mm yyyy) ", ' ') ; scanf ("%d %d %d", &day, &month, &year) ; break ; case 2: clrscr () ; printf ("\n\n\n\n\n%12cCurrent start time is %02d:%02d\n", ' ', start_hours, start_minutes) ; printf ("%12cEnter new start time (hh mm) ", ' ') ; scanf ("%d%d", &start_hours, &start_minutes) ; printf ("\n\n Current end time is %02d:%02d\n", end_hours, end_minutes) ; printf (" Enter new end time (hh mm) ") ; scanf ("%d%d", &end_hours, &end_minutes) ; break ; case 3: clrscr () ; printf ("\n\n\n\n\n%12cCurrent type is %c\n", ' ', type) ; printf ("%12cEnter new record type (R/S) ", ' ') ; scanf ("\n%c", &type) ; type = toupper (type) ; if (type == 'R') { printf ( "\n%12cNow enter the repeat frequency (D/W/M) ",' ' ); scanf ("\n%c", &frequency) ; frequency = toupper (frequency) ; while ((frequency != 'D') && (frequency != 'W') && (frequency != 'M')) { clrscr() ; printf ("\n\n\n\n\n") ; printf ("%12cFrequency must be one of D/W/M\n", ' '); printf ("%12cEnter the repeat frequency (D/W/M) ", ' ' ) ; scanf ("\n%c", &frequency) ; frequency = toupper (frequency); } } else { frequency = ' ' ; } break ; case 4: clrscr () ; if (type == 'R') { clrscr () ; printf ("\n\n\n\n\n") ; printf ("%12cCurrent repeat frequency is %c\n", ' ',frequency) ; printf ("%12cEnter new frequency (D/W/M) ", ' ') ; scanf ("\n%c", &new_frequency) ; new_frequency = toupper (new_frequency) ; if ((new_frequency != 'D') && (new_frequency != 'W') && (new_frequency != 'M')) { printf ("%12cFrequency must be one of D/W/M.", ' ') ; printf (" Frequency is unchanged") ; printf ("\n%12cHit any key to continue",' '); getch () ; } else frequency = new_frequency ; } else { clrscr () ; printf ("\n\n\n\n\n") ; printf ("%12cRecord is not a repeating record,", ' '); printf (" cannot change frequency"); printf ("\n%12cHit any key to continue", ' '); getch () ; } break ; case 5: clrscr () ; printf ("\n\n\n\n\n") ; printf ("%12cCurrent message is :%s\n", ' ', message) ; printf ("%12cEnter new message : ", ' ') ; gets (message) ; for (counter = strlen (message); counter < 25; counter ++) strcat (message, " ") ; break ; case 6: break ; default : DisplayError (6) ; break ; } } while (user_choice !=6) ; } WriteRecord () ; ReadRecord () ; } fclose (input_file) ; WriteTerminator (); fclose (output_file) ; remove (in_filename) ; rename (out_filename, in_filename) ; printf ("\n%12cDo you want to amend another record ? (Y/N) ", ' ') ; scanf ("\n%c", &response) ; } while ((response == 'Y') || (response == 'y')) ; } /* function DisplayToday */ /* function to display today's appointments */ void DisplayToday () { /* Description ...... Open input file DO Clear screen Display headings Initialise counter to 0 Read first record from file WHILE screen not full and not end of file Calculate record date as integer IF record date = today Display record Add 1 to counter Read next record if not end of file Display Hit a key to see next screenful Accept key stroke WHILE not end of file Close file Display Hit a key to continue Accept key stroke */ int counter ; input_file = fopen (in_filename, "r") ; do { clrscr () ; printf ("\n\n\n\n\n%12c** Today's Appointments **\n\n", ' ') ; printf ("%12cStart End Message\n", ' ') ; printf ("%12c============================================\n", ' ') ; counter = 0 ; ReadRecord () ; while (( counter < 10) && (day !=99)) { long int GetDate () ; if (record_date == system_date ) { printf ("%12c%d-%s %02d:%02d %02d:%02d %s\n", ' ', day, LookupMonth (month), start_hours, start_minutes, end_hours,end_minutes, message) ; ++ counter ; } ReadRecord () ; } if (day != 99) { printf ("\n%12cHit a key to see next screenful", ' ') ; getch () ; } } while (day != 99) ; fclose (input_file) ; printf ("\n\n%12cHit a key to continue", ' ') ; getch () ; } /* function DaysInMonth */ /* function to return the number of days in a given month */ int DaysInMonth (int mm, int yy) { /* Description ...... case month = 4,6,9,11 (Apr, Jun, Sep, Nov) : number of days = 30 case month = 2 (Feb) : IF a leap year (century or normal) number of days = 29 ELSE number of days = 28 default : number of days = 31 */ /* Remember, a leap year is one in which the year is divisible by 4 */ /* except for century years (1900, 2000 etc) in which case the year must */ /* be divisible by 400. Thus 1900 was not a leap year (even though it's */ /* divisible by 4) whilst 2000 is a leap year (divisible by 400). */ int no_days, month, year; switch(mm) { case 4 : case 6 : case 9 : case 11 : no_days = 30 ; break ; case 2 : if (yy % 4 == 0) no_days = 29 ; else no_days = 28 ; break ; default : no_days = 31 ; } return no_days; }