commit 9549efc60b2d6cb18190625d767d81d8ecb765d5 Author: Sirin Puenggun Date: Fri Jul 14 21:35:32 2023 +0700 Add practice project diff --git a/contact.c b/contact.c new file mode 100644 index 0000000..a4d96cb --- /dev/null +++ b/contact.c @@ -0,0 +1,132 @@ +// File : contact.c +// Description : Implement functions defined in contact.h + +#include +#include +#include "contact.h" + +void addContact(Contact contacts[], int *numContacts) { + if (*numContacts >= MAX_CONTACTS) { + printf("Contact list is full.\n"); + return; + } + + Contact newContact; + printf("Enter name: "); + fgets(newContact.name, MAX_NAME_LENGTH, stdin); + newContact.name[strcspn(newContact.name, "\n")] = '\0'; + + printf("Enter phone number: "); + fgets(newContact.phone, MAX_PHONE_LENGTH, stdin); + newContact.phone[strcspn(newContact.phone, "\n")] = '\0'; + + printf("Enter email address: "); + fgets(newContact.email, MAX_EMAIL_LENGTH, stdin); + newContact.email[strcspn(newContact.email, "\n")] = '\0'; + + contacts[*numContacts] = newContact; + (*numContacts)++; + printf("Contact added successfully.\n"); +} + +void displayContacts(Contact contacts[], int numContacts) { + if (numContacts == 0) { + printf("Contact list is empty.\n"); + return; + } + + printf("Contacts:\n"); + for (int i = 0; i < numContacts; i++) { + printf("Name: %s\n", contacts[i].name); + printf("Phone: %s\n", contacts[i].phone); + printf("Email: %s\n", contacts[i].email); + printf("--------------------\n"); + } +} + +void searchContacts(Contact contacts[], int numContacts, char *searchTerm) { + if (numContacts == 0) { + printf("Contact list is empty.\n"); + return; + } + + printf("Search results for '%s':\n", searchTerm); + int found = 0; + for (int i = 0; i < numContacts; i++) { + if (strstr(contacts[i].name, searchTerm) != NULL || + strstr(contacts[i].phone, searchTerm) != NULL || + strstr(contacts[i].email, searchTerm) != NULL) { + printf("Name: %s\n", contacts[i].name); + printf("Phone: %s\n", contacts[i].phone); + printf("Email: %s\n", contacts[i].email); + printf("--------------------\n"); + found = 1; + } + } + + if (!found) { + printf("No matching contacts found.\n"); + } +} + +void updateContact(Contact contacts[], int numContacts, char *searchTerm) { + if (numContacts == 0) { + printf("Contact list is empty.\n"); + return; + } + + int found = 0; + for (int i = 0; i < numContacts; i++) { + if (strstr(contacts[i].name, searchTerm) != NULL || + strstr(contacts[i].phone, searchTerm) != NULL || + strstr(contacts[i].email, searchTerm) != NULL) { + printf("Contact found. Enter new details:\n"); + + printf("Enter name: "); + fgets(contacts[i].name, MAX_NAME_LENGTH, stdin); + contacts[i].name[strcspn(contacts[i].name, "\n")] = '\0'; + + printf("Enter phone number: "); + fgets(contacts[i].phone, MAX_PHONE_LENGTH, stdin); + contacts[i].phone[strcspn(contacts[i].phone, "\n")] = '\0'; + + printf("Enter email address: "); + fgets(contacts[i].email, MAX_EMAIL_LENGTH, stdin); + contacts[i].email[strcspn(contacts[i].email, "\n")] = '\0'; + + printf("Contact updated successfully.\n"); + found = 1; + break; + } + } + + if (!found) { + printf("No matching contacts found.\n"); + } +} + +void deleteContact(Contact contacts[], int *numContacts, char *searchTerm) { + if (numContacts == 0) { + printf("Contact list is empty.\n"); + return; + } + + int found = 0; + for (int i = 0; i < *numContacts; i++) { + if (strstr(contacts[i].name, searchTerm) != NULL || + strstr(contacts[i].phone, searchTerm) != NULL || + strstr(contacts[i].email, searchTerm) != NULL) { + for (int j = i; j < *numContacts - 1; j++) { + contacts[j] = contacts[j + 1]; + } + (*numContacts)--; + printf("Contact deleted successfully.\n"); + found = 1; + break; + } + } + + if (!found) { + printf("No matching contacts found.\n"); + } +} diff --git a/contact.h b/contact.h new file mode 100644 index 0000000..f14c798 --- /dev/null +++ b/contact.h @@ -0,0 +1,21 @@ +#ifndef CONTACT_H +#define CONTACT_H + +#define MAX_NAME_LENGTH 50 +#define MAX_PHONE_LENGTH 15 +#define MAX_EMAIL_LENGTH 50 +#define MAX_CONTACTS 100 + +typedef struct { + char name[MAX_NAME_LENGTH]; + char phone[MAX_PHONE_LENGTH]; + char email[MAX_EMAIL_LENGTH]; +} Contact; + +void addContact(Contact contacts[], int *numContacts); +void displayContacts(Contact contacts[], int numContacts); +void searchContacts(Contact contacts[], int numContacts, char *searchTerm); +void updateContact(Contact contacts[], int numContacts, char *searchTerm); +void deleteContact(Contact contacts[], int *numContacts, char *searchTerm); + +#endif diff --git a/contact_management_system.exe b/contact_management_system.exe new file mode 100644 index 0000000..a0f41b6 Binary files /dev/null and b/contact_management_system.exe differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..3c9887b --- /dev/null +++ b/main.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include "contact.h" +#include "utils.h" + +int main() { + Contact contacts[MAX_CONTACTS]; + int numContacts = 0; + int choice; + char searchTerm[MAX_NAME_LENGTH]; + + while (1) { + printMenu(); + printf("Enter your choice: "); + scanf("%d", &choice); + clearInputBuffer(); + + switch (choice) { + case 1: + addContact(contacts, &numContacts); + break; + case 2: + displayContacts(contacts, numContacts); + break; + case 3: + printf("Enter search term: "); + fgets(searchTerm, MAX_NAME_LENGTH, stdin); + searchTerm[strcspn(searchTerm, "\n")] = '\0'; + searchContacts(contacts, numContacts, searchTerm); + break; + case 4: + printf("Enter search term: "); + fgets(searchTerm, MAX_NAME_LENGTH, stdin); + searchTerm[strcspn(searchTerm, "\n")] = '\0'; + updateContact(contacts, numContacts, searchTerm); + break; + case 5: + printf("Enter search term: "); + fgets(searchTerm, MAX_NAME_LENGTH, stdin); + searchTerm[strcspn(searchTerm, "\n")] = '\0'; + deleteContact(contacts, &numContacts, searchTerm); + break; + case 6: + printf("Exiting program.\n"); + exit(0); + default: + printf("Invalid choice. Please try again.\n"); + break; + } + } + + return 0; +} diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..7713245 --- /dev/null +++ b/utils.c @@ -0,0 +1,17 @@ +#include +#include "utils.h" + +void clearInputBuffer() { + int c; + while ((c = getchar()) != '\n' && c != EOF) {} +} + +void printMenu() { + printf("\nContact Management System\n"); + printf("1. Add contact\n"); + printf("2. Display all contacts\n"); + printf("3. Search contact\n"); + printf("4. Update contact\n"); + printf("5. Delete contact\n"); + printf("6. Exit\n"); +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..b39a8b3 --- /dev/null +++ b/utils.h @@ -0,0 +1,9 @@ +#ifndef UTILS_H +#define UTILS_H + +#define MAX_CONTACTS 100 + +void clearInputBuffer(); +void printMenu(); + +#endif