1

構造体とファイルを使用してカレンダーを実装するように求められました。構造体を使って実装することに成功しました。ただし、ファイルも処理するには、プログラムの上に構築する必要があります。難しいと思われる主なアイデアは、以前のデータを上書きせずにテキスト ファイルとバイナリ ファイルに書き込む方法です。ここに私が与えられたいくつかの仕様があります:

  1. カレンダーをテキストファイルに保存する
  2. カレンダーをバイナリファイルに保存する
  3. テキストからカレンダーを読み取る
  4. カレンダーをバイナリファイルに保存する

プログラムへのもう 1 つの追加は、カレンダーを最後に読み込んだ後に行った変更をテキスト ファイルに記録することです。ここでは、元のカレンダーを再読み込みし、ログに記録された変更を再取得できるファイル形式を使用する必要があります。プログラムがクラッシュした場合のカレンダーの状態により、失うものはほとんどありません。これは、次の 2 つの方法で実装する必要があります。

  1. 明示的なメニュー オプションとして、すべての変更を「eventlog_time.txt」というファイルに記録します。時刻は「time.h」ライブラリから取得されます。
  2. 自動ロギング アクションとして。このプログラムは、ユーザーがイベントを追加または削除するたびに自動的に更新される「eventlog_private.txt」というファイルを保持します。このファイルを空に初期化してから開き、追加し、各イベントをログに記録した後に閉じる必要があります。

ここに私が書いたコードがあります:

何をすべきかよくわからないので、仕様の最後の部分を説明してください。または、少なくとも問題を開始するためのヒントを教えてください。これが私のコードです:

        /************************************************************************************************
    Input file: None
    Output file: None
    Description: This calendar program is mainly a menu interactive program that prompts
                 the user for a date and its events, then the entry is stored in its appropriate
                 place inside an array of structures.
                 This program handles 5 main operations:
                    . Add an event to the calendar.
                    . Display events for a specific calendar date.
                    . Display the whole calendar.
                    . Delete the event from the calendar.

    ************************************************************************************************/


    #include <stdio.h>
    #include <string.h>
    #define MAXEVS 100   // Defining the maximum number of events as being 100
    typedef enum {FALSE = 0, TRUE = 1} BOOLEAN;
    typedef struct {
        int hours,
            minutes,
            seconds;
    } time_t;

    typedef struct {
        char month [20];
        int day,
            year; 
    } date_t;

    typedef struct {
        char ev_name [40];
        date_t dt;
        time_t tm;
    } event_t;

    typedef struct {
        BOOLEAN valid;
        event_t event_list [MAXEVS];
        int num_events;
    } calentry_t; 

        int counter = 0; //Global counter variable used to keep track of number of events
        void menu (); // This function displays the menu to the user 
        void InitializeCalender(calentry_t calendar[][31]  ); // Initializes the calendar for valid and invalid dates
        int ReadEvent ( calentry_t calendar[][31],char eventname[], char month[], int *day, int *year, int *h, int *min); // This function min role is to get the input from the user (the whole calendar entry)
        int MonthStr2Num ( char* month); // This function converts a string containing a month to its appropriate number
        void AddEvent (calentry_t calendar[][31], char eventname[], int* monthnum, int *day, int *year, int *h, int *min); // This function places the calendar entry places the event inside its appropriate place inside the 2D array
        char* MonthNum2Str (int* m); // Converts a number to its appropriate month
        void DisplayCalendar (calentry_t calendar[][31], int* monthnum, int* day, int* year); // Displays the calendar entry for a specific date
        void DisplayWholeCalendar ( calentry_t calendar[][31]); // This function displays the whole entries stored inside the calendar
        int DeleteEvent (calentry_t calendar [][31]); // This function deletes an event from the calendar
    main (){
        int choice;
        calentry_t calendar[12][31]; // Declaring a 2D array of structure calentry_t

        char eventname [100];
        char month[20];
        int day, year, h, min, monthnum;
        char temp;

        InitializeCalender(calendar);
        do{ // loop that continues to execute the program until the user decides to quit
        menu ();

        scanf ("%d",&choice);
        switch (choice)
        {
            case 1:
            printf("Calendar will now quit.\n");
            break;
            case 2:
            monthnum = ReadEvent(calendar ,eventname, month, &day, &year, &h, &min);
            AddEvent (calendar, eventname, &monthnum, &day, &year, &h, &min);
            break;
            case 3:
            printf ("What is the date that you want to display its events? input like ( 12 January 2012 ) ");
            scanf ("%d %s %d", &day, month, &year);
            monthnum = MonthStr2Num (month); 
            DisplayCalendar (calendar ,&monthnum, &day, &year);
            break;
            case 4:
            printf ("The whole calendar will be displayed immediately\n");
            DisplayWholeCalendar (calendar);
            break;
            case 5: 
            DeleteEvent (calendar);
            break;
        }
        }
        while (choice !=1);
    } //End of the main function

    void menu ()
    {
        printf("___________________________________________________________________________\n");
        printf("\tHere is the menu:\n");
        printf("\t1. Quit the Program.\n");
        printf("\t2. Add an event to the calendar.\n");
        printf("\t3. Display events for a specific calendar date.\n");
        printf("\t4. Display the whole calendar.\n");
        printf("\t5. Delete the event from the calendar.\n"); 
        printf("____________________________________________________________________________\n");
        printf("\tYour choice Please: ");
    }

    void InitializeCalender(calentry_t calendar[][31]){
    int x, y;
        for(x = 0; x < 12; x ++) {
            if (x==1){
                for(y = 0; y < 28; y ++){ 
                    (calendar[x][y]).valid = TRUE;
                    (calendar[x][y]).num_events = FALSE;
                }
                for (y=28; y< 31; y++){
                    (calendar[x][y]).valid = FALSE;
                }
            }
            else if  (x==3 || x==5 || x==8 || x==10){
                for(y = 0; y < 30; y ++){ 
                    (calendar[x][y]).valid = TRUE;
                    (calendar[x][y]).num_events = FALSE;

                }
                for (y=30; y< 31; y++){
                    (calendar[x][y]).valid = FALSE;
                }
            }
            else if (x==0 || x==2||x==4||x==6||x==7||x==9||x==11){
                for(y = 0; y < 31; y ++){ 
                    (calendar[x][y]).valid = TRUE;
                    (calendar[x][y]).num_events = FALSE;
                }
            }
        }
    }

    int ReadEvent (calentry_t calendar[][31] ,char eventname[], char month[], int *day, int *year, int *h, int *min){

        char temp, answer;
        int monthnum;

            printf ("\n\tOkay, for event number %d:\n",counter+1 );
            printf ("\n\tWhat is the name of the event? ");
            scanf ("%c",&temp);
            gets (eventname);

            printf("\n\twhat is the date of the event? input like ( 12 January 2012 ) ");   
            scanf ("%d %s %d", day, month, year);
            monthnum = MonthStr2Num (month);
                if ( calendar [(monthnum-1)][(*day-1)]. valid ==FALSE || *day>=32 ){
                    while ( calendar [(monthnum-1)][(*day-1)]. valid ==FALSE || *day>=32 ) { //Loop that checks invalid dates
                    printf ("\tSorry that'san invalid date!!!!!\n");
                    printf("\n\twhat is the date of the event? input like ( 12 January 2012 ) ");   
                    scanf ("%d %s %d", day, month, year);
                    monthnum = MonthStr2Num (month);
                    } 
                }


            printf ("\n\tWhat is the time of the event? input like (12:30) ");
            scanf ("%d:%d", h,min);
                if ( *h>=24 || *h<0 || *min>=60 || *min<0){ //Loop that checks invalid times
                    while ( *h>=24 || *h<0 || *min>=60 || *min<0){
                        printf ("\tInvalid Time !!!!! \n");
                        printf ("\n\tWhat is the time of the event? input like (12:30) ");
                        scanf ("%d:%d", h,min);
                    }
                }
            monthnum = MonthStr2Num (month);
            return monthnum;

    }
    int MonthStr2Num ( char* month){
            int i,
                m=0;
        // Converting all the month characters to uppercase in order to handle all the cases of how the user 
        // enters the month
        for(i=0;i<=strlen(month);i++)
        {
        if(month[i]>=97&&month[i]<=122)
        month[i]=month[i]-32;
        }

        if (strcmp((month), "JANUARY")==0){ 

            m = 1;}

        else if (strcmp((month), "FEBRUARY")==0)
        m = 2;
        else if (strcmp((month), "MARCH")==0)
        m = 3;
        else if (strcmp((month), "APRIL")==0)
        m = 4;
        else if (strcmp((month), "MAY")==0)
        m = 5;
        else if (strcmp((month), "JUNE")==0)
        m = 6;
        else if (strcmp((month), "JULY")==0)
        m = 7;
        else if (strcmp((month), "AUGUST")==0)
        m = 8;
        else if (strcmp((month), "SEPTEMBER")==0)
        m = 9;
        else if (strcmp((month), "OCTOBER")==0)
        m = 10;
        else if (strcmp((month), "NOVEMBER")==0)
        m = 11;
        else if (strcmp((month), "DECEMBER")==0)
        m = 12;
        return m;
    }

    void AddEvent (calentry_t calendar[][31], char eventname[], int* monthnum, int *day, int *year, int *h, int *min){
        char monthstr [20];
        strcpy(monthstr,MonthNum2Str(monthnum));
        strcpy ( calendar [(*monthnum-1)][(*day-1)]. event_list[calendar[(*monthnum-1)][(*day-1)].num_events].ev_name,eventname );
        strcpy ( calendar [(*monthnum-1)][(*day-1)]. event_list[calendar[(*monthnum-1)][(*day-1)].num_events].dt.month,monthstr );  
        calendar [(*monthnum-1)][(*day-1)].num_events++;
        calendar [(*monthnum-1)][(*day-1)]. event_list[calendar[(*monthnum-1)][(*day-1)].num_events].dt.day = (*day);
        calendar [(*monthnum-1)][(*day-1)]. event_list[calendar[(*monthnum-1)][(*day-1)].num_events].dt.year = (*year); 
        calendar [(*monthnum-1)][(*day-1)]. event_list[calendar[(*monthnum-1)][(*day-1)].num_events].tm.hours = (*h);
        calendar [(*monthnum-1)][(*day-1)]. event_list[calendar[(*monthnum-1)][(*day-1)].num_events].tm.minutes = (*min);           
        counter++;

    }

    char* MonthNum2Str (int* m){ 
        if (*m==1)
        return "January";
        else if (*m==2)
        return "February";
        else if (*m==3)
        return "March";
        else if (*m==4)
        return "April";
        else if (*m==5)
        return "May";
        else if (*m==6)
        return "June";
        else if (*m==7)
        return "July";
        else if (*m==8)
        return "August";
        else if (*m==9)
        return "September";
        else if (*m==10)
        return "October";
        else if (*m==11)
        return "November";
        else if (*m==12)
        return "December";
    }

    void DisplayCalendar (calentry_t calendar[][31], int* monthnum, int* day, int* year){
        int i;

        if ( calendar [(*monthnum-1)][(*day-1)].num_events==0)
            printf ("\n\tNo events on this date!!!!!!\n");
            else{
            printf ("\n\tOn %d-%s-%d You have %d event(s): \n", *day, calendar [(*monthnum-1)][(*day-1)]. event_list[(calendar[(*monthnum-1)][(*day-1)].num_events)-1].dt.month, *year, calendar[(*monthnum-1)][(*day-1)].num_events);
                for (i=0; i<calendar[(*monthnum-1)][(*day-1)].num_events; i++){
                    printf ("\t\n Event %d: ", i+1);
                    printf ("\t\n Name of the event: %s", calendar [(*monthnum-1)][(*day-1)]. event_list[i].ev_name );
                    printf ("\t\n Time of the event: %d:%d\n", calendar [(*monthnum-1)][(*day-1)]. event_list[i+1].tm.hours, calendar [(*monthnum-1)][(*day-1)]. event_list[i+1].tm.minutes);
                }
            }
    }

    void DisplayWholeCalendar ( calentry_t calendar[][31]){
        int x, y, i;

        printf ("You have a total of %d event(s)\n", counter);

        for(x = 0; x < 12; x ++) {
            for (y=0; y< 31; y++){
                if ((calendar[x][y]).valid == FALSE ||(calendar[x][y]).num_events == FALSE){
                }
                else {
                printf ("On %d-%s-%d You have %d event(s): \n", y+1, calendar [x][y]. event_list[(calendar[x][y].num_events)-1].dt.month, calendar [x][y]. event_list[calendar[x][y].num_events].dt.year, calendar[x][y].num_events);
                    for (i=0; i<calendar[x][y].num_events; i++){
                    printf ("\t\n Event %d: ", i+1);
                    printf ("\t\n Name of the event: %s", calendar [x][y]. event_list[i].ev_name );
                    printf ("\t\n Time of the event: %d:%d\n", calendar [x][y]. event_list[i+1].tm.hours, calendar [x][y]. event_list[i+1].tm.minutes);
                    }
                }
            }
        }
    }

    int DeleteEvent (calentry_t calendar [][31]){
        int day, year, monthnum, choice, c;
        char month [20];

        printf ("\n\tWhat is the date that you want to delete one of its events? input like ( 12 January 2012 )  ");
        scanf ("%d %s %d", &day, month, &year);
        monthnum = MonthStr2Num (month);
        if ( calendar [(monthnum-1)][(day-1)].num_events==0){
            printf ("No events on this date!!!!!!\n");
            return 0;
        }
        if ((calendar[(monthnum-1)][(day-1)]).valid == FALSE){
            printf ("Invalid date !!!!\n");
            return 0;
        }
        else {
        DisplayCalendar (calendar ,&monthnum, &day, &year);
        printf ("\n\nWhat is the event that you want to delete (press 1 for event 1 or 2 for event 2...) ");
        scanf ("%d", &choice);
            if ( choice >= (calendar [(monthnum-1)][(day-1)].num_events)+1 )
                 printf("\n\tDeletion not possible.\n");

            else {
                for ( c = choice - 1 ; c < (calendar [monthnum-1][day-1].num_events) ; c++ ){
              calendar [monthnum-1][day-1]. event_list[c] =calendar [monthnum-1][day-1]. event_list[c+1];   
                }         calendar [monthnum-1][day-1].num_events--;
                            printf ("\n\tEvent deleted \n");
                            counter--;
            }
        }


    }

誰でも助けてくれますか!前もって感謝します。

4

0 に答える 0