Conversation with Merlin [email protected] · Mon Dec 11 2023

#include <stdio.h> #include <stdlib.h> #include <time.h>

#define MAX_TITLE_LENGTH 21 #define MAX_APPOINTMENTS 100

struct appointment { struct tm startDateTime; int duration; // in minutes char title[MAX_TITLE_LENGTH]; };

int addAppointment(struct appointment appointments[], int *countAppointments) { if (*countAppointments >= MAX_APPOINTMENTS) { printf("Es können keine weiteren Termine hinzugefügt werden. Maximale Anzahl erreicht.\n"); return -1; }

struct appointment newAppointment; printf("Geben Sie das Startdatum und die Uhrzeit (TT MM JJJJ hh mm ss) ein: "); scanf("%d %d %d %d %d %d", &newAppointment.startDateTime.tm_mday, &newAppointment.startDateTime.tm_mon, &newAppointment.startDateTime.tm_year, &newAppointment.startDateTime.tm_hour, &newAppointment.startDateTime.tm_min, &newAppointment.startDateTime.tm_sec); newAppointment.startDateTime.tm_year -= 1900; printf("Geben Sie die Dauer des Termins in Minuten ein (max. 8 Stunden): "); scanf("%d", &newAppointment.duration); printf("Geben Sie den Titel des Termins (max. 20 Zeichen) ein: "); scanf("%s", newAppointment.title); appointments[*countAppointments] = newAppointment; (*countAppointments)++; while (getchar() != '\n') ; return 0;

}

int terminSerie(struct appointment appointments[], int *countAppointments) { if (*countAppointments >= MAX_APPOINTMENTS) { printf("Es können keine weiteren Termine hinzugefügt werden. Maximale Anzahl erreicht.\n"); return -1; }

struct appointment newAppointment; printf("Datum eingeben (TT MM JJJJ hh mm ss): "); scanf("%d %d %d %d %d %d", &newAppointment.startDateTime.tm_mday, &newAppointment.startDateTime.tm_mon, &newAppointment.startDateTime.tm_year, &newAppointment.startDateTime.tm_hour, &newAppointment.startDateTime.tm_min, &newAppointment.startDateTime.tm_sec); // Adjust the year by subtracting 1900 newAppointment.startDateTime.tm_year -= 1900; printf("Geben Sie die Dauer des Termins in Minuten ein: "); scanf("%d", &newAppointment.duration); printf("Geben Sie den Titel des Termins (max. 20 Zeichen) ein: "); scanf("%s", newAppointment.title); printf("Wählen Sie den Intervalltyp: 1-täglich, 7- wöchentlich, 14- zweiwöchentlich: "); int frequency; scanf("%d", &frequency); printf("Geben Sie die Anzahl der Termine ein: "); int totalAppointments; scanf("%d", &totalAppointments); for (int i = 0; i < totalAppointments && *countAppointments < MAX_APPOINTMENTS; ++i) { appointments[*countAppointments] = newAppointment; (*countAppointments)++; // Calculate the time for the next appointment time_t nextTime = mktime(&newAppointment.startDateTime) + (frequency * 24 * 60 * 60); newAppointment.startDateTime = *localtime(&nextTime); } while (getchar() != '\n') ; return 0;

} void showAllAppointments(struct appointment appointments[], int countAppointments) { if (countAppointments == 0) { printf("Keine Termine vorhanden.\n"); return; }

printf("Alle Termine:\n"); for (int i = 0; i < countAppointments; ++i) { printf("Termin %d:\n", i + 1); printf("Startdatum und -zeit: %02d.%02d.%d %02d:%02d:%02d\n", appointments[i].startDateTime.tm_mday, appointments[i].startDateTime.tm_mon, appointments[i].startDateTime.tm_year + 1900, appointments[i].startDateTime.tm_hour, appointments[i].startDateTime.tm_min, appointments[i].startDateTime.tm_sec); printf("Dauer des Termins: %d Minuten\n", appointments[i].duration); printf("Titel des Termins: %s\n", appointments[i].title); printf("\n"); }

} void nextAppointment(struct appointment appointments[], int countAppointments) { time_t currentTime; struct tm *localTime;

currentTime = time(NULL); localTime = localtime(&currentTime); printf("Aktuelle Uhrzeit: %02d.%02d.%d %02d:%02d:%02d\n", localTime->tm_mday, localTime->tm_mon + 1, localTime->tm_year + 1900, localTime->tm_hour, localTime->tm_min, localTime->tm_sec); time_t minDifference = -1; // Initialize with a large value int nextIndex = -1; for (int i = 0; i < countAppointments; ++i) { struct tm appointmentTime = appointments[i].startDateTime; time_t appointmentTimestamp = mktime(&appointmentTime); if (difftime(appointmentTimestamp, currentTime) > 0) { time_t difference = difftime(appointmentTimestamp, currentTime); if (minDifference == -1 || difference < minDifference) { minDifference = difference; nextIndex = i; } } } if (nextIndex != -1) { printf("Next appointment:\n"); printf("Startdatum und -zeit: %02d.%02d.%d %02d:%02d:%02d\n", appointments[nextIndex].startDateTime.tm_mday, appointments[nextIndex].startDateTime.tm_mon + 1, appointments[nextIndex].startDateTime.tm_year + 1900, appointments[nextIndex].startDateTime.tm_hour, appointments[nextIndex].startDateTime.tm_min, appointments[nextIndex].startDateTime.tm_sec); printf("Dauer des Termins: %d Minuten\n", appointments[nextIndex].duration); printf("Titel des Termins: %s\n", appointments[nextIndex].title); } else { printf("Kein weiterer Termin gefunden.\n"); }

}

void displayCalendar(struct appointment appointments[], int countAppointments) { struct tm weekStart; printf("Geben Sie ein Datum ein, um die Termine für die Woche anzuzeigen (TT.MM.JJJJ): "); scanf("%d.%d.%d", &weekStart.tm_mday, &weekStart.tm_mon, &weekStart.tm_year); weekStart.tm_mon -= 1; // Adjust month to be zero-based weekStart.tm_year -= 1900; // Adjust year by subtracting 1900 weekStart.tm_sec = 0; weekStart.tm_min = 0; weekStart.tm_hour = 0;

// Calculate the start of the week (Monday) based on the provided date weekStart.tm_wday = 0; // Reset to Sunday mktime(&weekStart); // Normalize the time structure weekStart.tm_wday = (weekStart.tm_wday + 1) % 7; // Adjust to Monday // Calculate the end of the week (Sunday) struct tm weekEnd = weekStart; weekEnd.tm_mday += 6; mktime(&weekEnd); // Normalize the time structure printf("Termine für die Woche vom %02d.%02d.%d bis %02d.%02d.%d:\n", weekStart.tm_mday, weekStart.tm_mon + 1, weekStart.tm_year + 1900, weekEnd.tm_mday, weekEnd.tm_mon + 1, weekEnd.tm_year + 1900); // Create a calendar array to store appointments for each day of the week struct appointment calendar[7][MAX_APPOINTMENTS]; int countAppointmentsPerDay[7] = {0}; // Separate the appointments into each day of the week for (int i = 0; i < countAppointments; ++i) { struct tm appointmentTime = appointments[i].startDateTime; time_t appointmentTimestamp = mktime(&appointmentTime); if (difftime(appointmentTimestamp, mktime(&weekStart)) >= 0 && difftime(mktime(&weekEnd), appointmentTimestamp) >= 0) { int dayOfWeek = appointmentTime.tm_wday; calendar[dayOfWeek][countAppointmentsPerDay[dayOfWeek]] = appointments[i]; countAppointmentsPerDay[dayOfWeek]++; } } // Print the appointments for each day of the week for (int i = 0; i < 7; ++i) { struct tm currentDate = weekStart; currentDate.tm_mday += i; mktime(&currentDate); // Normalize the time structure printf("%02d.%02d.%d (%s):\n", currentDate.tm_mday, currentDate.tm_mon + 1, currentDate.tm_year + 1900, (i == 0) ? "Montag" : (i == 1) ? "Dienstag" : (i == 2) ? "Mittwoch" : (i == 3) ? "Donnerstag" : (i == 4) ? "Freitag" : (i == 5) ? "Samstag" : "Sonntag"); if (countAppointmentsPerDay[i] == 0) { printf("Keine Termine vorhanden.\n"); }

} }

int getUserInputMainMenu() { int input; printf("1. Termin anlegen\n" "2. Terminserie erstellen\n" "3. Alle Termine anzeigen\n" "4. Naechste Termin anzeigen\n" "5. Kalender Ausgeben\n" "9. Programm beenden\n"); scanf("%d", &input);

#if defined(_WIN32) || defined(_WIN64) fflush(stdin); // Clear input buffer on Windows #else while (getchar() != '\n') ; // Clear input buffer on Unix-like systems #endif

return input;

} int main(void) { struct appointment appointments[MAX_APPOINTMENTS]; int countAppointments = 0, userInputMainMenu;

while (1) { userInputMainMenu = getUserInputMainMenu(); switch (userInputMainMenu) { case 1: addAppointment(appointments, &countAppointments); break; case 2: terminSerie(appointments, &countAppointments); break; case 3: showAllAppointments(appointments, countAppointments); break; case 4: nextAppointment(appointments, countAppointments); break; case 5: displayCalendar(appointments, countAppointments); break; case 9: return 0; // Programm beenden default: puts("Ungueltige Eingabe!"); } } return 0;

} make displayCalendar show me the appointments for the week

It appears that the provided C code already contains a function to display appointments for the week, named displayCalendar(). To use this function, you need to compile and run the C program. Upon running the program, you should be prompted to input, then you can choose option 5 to display the appointments for the week.

Here's the option to be selected in the main menu:

5. Kalender Ausgeben

selecting this option, the program will prompt you to enter a date to display the appointments for the week. After providing the date, the program will output the appointments for the respective week.

If you encounter any issues while running the program or need further assistance, feel free to ask!

#include <stdio.h> #include <stdlib.h> #include <time.h>

#define MAX_TITLE_LENGTH 21 #define MAX_APPOINTMENTS 100

struct appointment { struct tm startDateTime; int duration; // in minutes char title[MAX_TITLE_LENGTH]; };

int addAppointment(struct appointment appointments[], int *countAppointments) { if (*countAppointments >= MAX_APPOINTMENTS) { printf("Es können keine weiteren Termine hinzugefügt werden. Maximale Anzahl erreicht.\n"); return -1; }

struct appointment newAppointment; printf("Geben Sie das Startdatum und die Uhrzeit (TT MM JJJJ hh mm ss) ein: "); scanf("%d %d %d %d %d %d", &newAppointment.startDateTime.tm_mday, &newAppointment.startDateTime.tm_mon, &newAppointment.startDateTime.tm_year, &newAppointment.startDateTime.tm_hour, &newAppointment.startDateTime.tm_min, &newAppointment.startDateTime.tm_sec); newAppointment.startDateTime.tm_year -= 1900; printf("Geben Sie die Dauer des Termins in Minuten ein (max. 8 Stunden): "); scanf("%d", &newAppointment.duration); printf("Geben Sie den Titel des Termins (max. 20 Zeichen) ein: "); scanf("%s", newAppointment.title); appointments[*countAppointments] = newAppointment; (*countAppointments)++; while (getchar() != '\n') ; return 0;

}

int terminSerie(struct appointment appointments[], int *countAppointments) { if (*countAppointments >= MAX_APPOINTMENTS) { printf("Es können keine weiteren Termine hinzugefügt werden. Maximale Anzahl erreicht.\n"); return -1; }

struct appointment newAppointment; printf("Datum eingeben (TT MM JJJJ hh mm ss): "); scanf("%d %d %d %d %d %d", &newAppointment.startDateTime.tm_mday, &newAppointment.startDateTime.tm_mon, &newAppointment.startDateTime.tm_year, &newAppointment.startDateTime.tm_hour, &newAppointment.startDateTime.tm_min, &newAppointment.startDateTime.tm_sec); // Adjust the year by subtracting 1900 newAppointment.startDateTime.tm_year -= 1900; printf("Geben Sie die Dauer des Termins in Minuten ein: "); scanf("%d", &newAppointment.duration); printf("Geben Sie den Titel des Termins (max. 20 Zeichen) ein: "); scanf("%s", newAppointment.title); printf("Wählen Sie den Intervalltyp: 1-täglich, 7- wöchentlich, 14- zweiwöchentlich: "); int frequency; scanf("%d", &frequency); printf("Geben Sie die Anzahl der Termine ein: "); int totalAppointments; scanf("%d", &totalAppointments); for (int i = 0; i < totalAppointments && *countAppointments < MAX_APPOINTMENTS; ++i) { appointments[*countAppointments] = newAppointment; (*countAppointments)++; // Calculate the time for the next appointment time_t nextTime = mktime(&newAppointment.startDateTime) + (frequency * 24 * 60 * 60); newAppointment.startDateTime = *localtime(&nextTime); } while (getchar() != '\n') ; return 0;

} void showAllAppointments(struct appointment appointments[], int countAppointments) { if (countAppointments == 0) { printf("Keine Termine vorhanden.\n"); return; }

printf("Alle Termine:\n"); for (int i = 0; i < countAppointments; ++i) { printf("Termin %d:\n", i + 1); printf("Startdatum und -zeit: %02d.%02d.%d %02d:%02d:%02d\n", appointments[i].startDateTime.tm_mday, appointments[i].startDateTime.tm_mon, appointments[i].startDateTime.tm_year + 1900, appointments[i].startDateTime.tm_hour, appointments[i].startDateTime.tm_min, appointments[i].startDateTime.tm_sec); printf("Dauer des Termins: %d Minuten\n", appointments[i].duration); printf("Titel des Termins: %s\n", appointments[i].title); printf("\n"); }

} void nextAppointment(struct appointment appointments[], int countAppointments) { time_t currentTime; struct tm *localTime;

currentTime = time(NULL); localTime = localtime(&currentTime); printf("Aktuelle Uhrzeit: %02d.%02d.%d %02d:%02d:%02d\n", localTime->tm_mday, localTime->tm_mon + 1, localTime->tm_year + 1900, localTime->tm_hour, localTime->tm_min, localTime->tm_sec); time_t minDifference = -1; // Initialize with a large value int nextIndex = -1; for (int i = 0; i < countAppointments; ++i) { struct tm appointmentTime = appointments[i].startDateTime; time_t appointmentTimestamp = mktime(&appointmentTime); if (difftime(appointmentTimestamp, currentTime) > 0) { time_t difference = difftime(appointmentTimestamp, currentTime); if (minDifference == -1 || difference < minDifference) { minDifference = difference; nextIndex = i; } } } if (nextIndex != -1) { printf("Next appointment:\n"); printf("Startdatum und -zeit: %02d.%02d.%d %02d:%02d:%02d\n", appointments[nextIndex].startDateTime.tm_mday, appointments[nextIndex].startDateTime.tm_mon + 1, appointments[nextIndex].startDateTime.tm_year + 1900, appointments[nextIndex].startDateTime.tm_hour, appointments[nextIndex].startDateTime.tm_min, appointments[nextIndex].startDateTime.tm_sec); printf("Dauer des Termins: %d Minuten\n", appointments[nextIndex].duration); printf("Titel des Termins: %s\n", appointments[nextIndex].title); } else { printf("Kein weiterer Termin gefunden.\n"); }

}

void displayCalendar(struct appointment appointments[], int countAppointments) { struct tm weekStart; printf("Geben Sie ein Datum ein, um die Termine für die Woche anzuzeigen (TT.MM.JJJJ): "); scanf("%d.%d.%d", &weekStart.tm_mday, &weekStart.tm_mon, &weekStart.tm_year); weekStart.tm_mon -= 1; // Adjust month to be zero-based weekStart.tm_year -= 1900; // Adjust year by subtracting 1900 weekStart.tm_sec = 0; weekStart.tm_min = 0; weekStart.tm_hour = 0;

// Calculate the start of the week (Monday) based on the provided date weekStart.tm_wday = 0; // Reset to Sunday mktime(&weekStart); // Normalize the time structure weekStart.tm_wday = (weekStart.tm_wday + 1) % 7; // Adjust to Monday // Calculate the end of the week (Sunday) struct tm weekEnd = weekStart; weekEnd.tm_mday += 6; mktime(&weekEnd); // Normalize the time structure printf("Termine für die Woche vom %02d.%02d.%d bis %02d.%02d.%d:\n", weekStart.tm_mday, weekStart.tm_mon + 1, weekStart.tm_year + 1900, weekEnd.tm_mday, weekEnd.tm_mon + 1, weekEnd.tm_year + 1900); // Create a calendar array to store appointments for each day of the week struct appointment calendar[7][MAX_APPOINTMENTS]; int countAppointmentsPerDay[7] = {0}; // Separate the appointments into each day of the week for (int i = 0; i < countAppointments; ++i) { struct tm appointmentTime = appointments[i].startDateTime; time_t appointmentTimestamp = mktime(&appointmentTime); if (difftime(appointmentTimestamp, mktime(&weekStart)) >= 0 && difftime(mktime(&weekEnd), appointmentTimestamp) >= 0) { int dayOfWeek = appointmentTime.tm_wday; calendar[dayOfWeek][countAppointmentsPerDay[dayOfWeek]] = appointments[i]; countAppointmentsPerDay[dayOfWeek]++; } } // Print the appointments for each day of the week for (int i = 0; i < 7; ++i) { struct tm currentDate = weekStart; currentDate.tm_mday += i; mktime(&currentDate); // Normalize the time structure printf("%02d.%02d.%d (%s):\n", currentDate.tm_mday, currentDate.tm_mon + 1, currentDate.tm_year + 1900, (i == 0) ? "Montag" : (i == 1) ? "Dienstag" : (i == 2) ? "Mittwoch" : (i == 3) ? "Donnerstag" : (i == 4) ? "Freitag" : (i == 5) ? "Samstag" : "Sonntag"); if (countAppointmentsPerDay[i] == 0) { printf("Keine Termine vorhanden.\n"); }

} }

int getUserInputMainMenu() { int input; printf("1. Termin anlegen\n" "2. Terminserie erstellen\n" "3. Alle Termine anzeigen\n" "4. Naechste Termin anzeigen\n" "5. Kalender Ausgeben\n" "9. Programm beenden\n"); scanf("%d", &input);

#if defined(_WIN32) || defined(_WIN64) fflush(stdin); // Clear input buffer on Windows #else while (getchar() != '\n') ; // Clear input buffer on Unix-like systems #endif

return input;

} int main(void) { struct appointment appointments[MAX_APPOINTMENTS]; int countAppointments = 0, userInputMainMenu;

while (1) { userInputMainMenu = getUserInputMainMenu(); switch (userInputMainMenu) { case 1: addAppointment(appointments, &countAppointments); break; case 2: terminSerie(appointments, &countAppointments); break; case 3: showAllAppointments(appointments, countAppointments); break; case 4: nextAppointment(appointments, countAppointments); break; case 5: displayCalendar(appointments, countAppointments); break; case 9: return 0; // Programm beenden default: puts("Ungueltige Eingabe!"); } } return 0;

} displayCalendar doesn't show me the appointments for the week

It seems that the displayCalendar function in the provided C code does not print the appointments for the week as intended. Upon reviewing the function, a logical error is identified in the output section. The function iterates over each day of the week and attempts to print an appropriate message for each day, but it is missing the code to print the appointments for each day.

To resolve this issue, the displayCalendar function could be enhanced to also display the appointments for each day of the week. Here is a possible implementation:

displayCalendar(struct appointment appointments[], int countAppointments) { // ... (existing code for (int i = 0; i < 7; ++i) { // ... (existing code) printf("%02d.%02d.%d (%s):\n", currentDate.tm_mday, currentDate.tm_mon + 1, currentDate.tm_year +1900, (i == 0) ? "Montag" : (i == 1) ? "Dienstag" : (i == 2) ? "Mittwoch" : (i == 3) ?Donnerstag" : (i == 4) ? "Freitag" :i == 5) ? "Samstag" : "Sonntag"); if (countAppointmentsPerDay[i] == 0) printf("Keine Termine vorhanden.\n"); } else { printf("Termine:\n"); for (int j = 0; j < countAppointmentsPerDay[i]; ++j) { printf("Start: %02d:%02d, Dauer: % Minuten, Titel: %s\n", calendar[i][j].startDateTime.tm_hour, calendar[i][].startDateTime.tm_min, calendar[i][j].duration, calendar[i][j].title); } } } }

In this enhanced implementation, the function now iterates through each day of the week and the appointments, if any, for that particular day.

You can incorporate this updated version of displayCalendar into your code to ensure it displays the appointments for the week correctly. If you need any further assistance, feel free to ask!