Design requirements:
It can store the information of 1000 people. Each person's information includes name, age, gender, telephone and address
Address book functions include:
1. Add contacts
2. Delete the specified contact
3. Find the designated contact
4. Modify the designated contact
5. Show all contacts
6. Exit address book
Train of thought analysis:
First, we need to create three files: source file (test.c), address book file (contact.c) and address book header file (contact.h). The header file is used to contain header file, name information, structure, create global variables and macros.
We complete this procedure step by step:
1. Write a menu function menu () and union Option in the source program. Of course, there is no header file at this time and it cannot be run. The menu function menu () clearly indicates the functions corresponding to each number, which are 1. Add contacts 2. Delete the specified contact 3. Find the designated contact 4. Modify the designated contact 5. Show all contacts 0. Exit the address book. The number of corresponding members in the consortium is 0 ~ 5.
void menu() { printf("\n"); printf("**************************************\n"); printf("****** 1. add 2. del ******\n"); printf("****** 3. search 4.modify ******\n"); printf("****** 5. show 0. exit ******\n"); printf("**************************************\n"); } enum Option { EXIT, //0 ADD, //1 DEL, //2 SEARCH, //3 MODIFY, //4 SHOW //5 };
2. Write the main function main(), define that input represents the number to be input. In the do while loop, first call the menu function menu(), and then ask you to enter a number, and then use the switch statement to correspond to the corresponding function. We said earlier that the members of the consortium represent the numbers 0 ~ 5, so case ADD == case case 1, and so on. Select the corresponding function corresponding function, if you choose 1, then enter case ADD, then call AddContact (), pass &con, of course, this function should be created at the back. What con is will be explained later.
int main() { int input = 0; struct Contact con;//Create an address book InitContact(&con);//Initialize address book do { menu(); printf("Please select:"); scanf("%d", &input); switch (input) { case ADD: AddContact(&con); break; case DEL: DelContact(&con); break; case SEARCH: SearchContact(&con); break; case MODIFY: ModifyContact(&con); break; case SHOW: ShowContact(&con); break; case EXIT: printf("Exit address book\n"); break; default: printf("Selection error\n"); break; } } while (input); return 0; }
3. Create the header file contact.h and define the structure types struct PeoInfo and struct Contact. The former is a person's information, and the structure type variable that created him represents the information that created a person; The latter is the data of an address book. Create a variable of struct Contact type, including a struct PeoInfo type array data [] storing 1000 data, and then define a sz to calculate the number of people stored. Of course, we see that the symbols in the array are #define defined macros that are used to replace those numbers. The con seen earlier is to create a struct Contact type variable and initialize it.
#define NAME_MAX 30 #define SEX_MAX 5 #define TELE_MAX 12 #define ADDR_MAX 30 #define MAX 1000 struct PeoInfo { char name[NAME_MAX]; //full name int age; //Age char sex[SEX_MAX]; //Gender char tele[TELE_MAX]; //Telephone char addr[ADDR_MAX]; //address }; struct Contact { struct PeoInfo data[MAX];//The data of 1000 people is stored in the data array int sz;//Number of currently valid elements in the address book };
4. Reference the header file and declare the function. So far, contact.h has been written. Because the header file has referenced #include < stdio. H > and #include < string. H > that need to be used next, you can directly reference #include "contact.h" in test.c.
#define NAME_MAX 30 #define SEX_MAX 5 #define TELE_MAX 12 #define ADDR_MAX 30 #define MAX 1000 #include <stdio.h> #include <string.h> struct PeoInfo { char name[NAME_MAX]; int age; char sex[SEX_MAX]; char tele[TELE_MAX]; char addr[ADDR_MAX]; }; struct Contact { struct PeoInfo data[MAX];//The data of 1000 people is stored in the data array int sz;//Number of currently valid elements in the address book }; void AddContact(struct Contact* pc);//Add a contact void DelContact(struct Contact* pc);//Delete specified contact void SearchContact(struct Contact* pc);//Find the specified contact void ModifyContact(struct Contact* pc);//Modify specified contact void ShowContact(struct Contact* pc);//Show all contacts
5. Write contact.c, that is, the specific implementation of the address book. First introduce the header file #include "contact.h", and then write the initialization function InitContact(). Accept with a structure pointer (1000 capacities). Assign sz to 0 and all members of the array to 0. Use the memset function. The header file #include < string > has been quoted before.
#include "contact.h" void InitContact(struct Contact* pc) { pc->sz = 0;//No information by default memset(pc->data, 0, sizeof(pc->data)); }
6. Next, write the functions of each function:
AddContact() adds a contact and inputs information to the corresponding member of the sz bit of the array pointed to by the pointer. sz is initially 0, which just corresponds to bit 0 of the array.
void AddContact(struct Contact* pc) { if (pc->sz == MAX) { printf("The address book is full and cannot be added\n"); } else { printf("Please enter your first name:"); scanf("%s", pc->data[pc->sz].name); printf("Please enter age:"); scanf("%d", &(pc->data[pc->sz].age)); printf("Please enter gender:"); scanf("%s", pc->data[pc->sz].sex); printf("Please enter phone number:"); scanf("%s", pc->data[pc->sz].tele); printf("Please enter address:"); scanf("%s", pc->data[pc->sz].addr); printf("Added successfully\n"); pc->sz++; } }
DelContact() deletes the specified contact. If SZ is not 0, it indicates that there is information in the address book and can be deleted. After you enter a name, you need to find it. Here, you can find it by writing a traversal search name function FindContactByName(), in which the library function strcmp (string comparison function) is used. If it is equal, the array subscript pos is returned, and then the cycle starts from the pos bit in DelContact(), and the value of the latter bit is assigned to the previous bit, indicating that the deletion is successful, and then sz--.
int FindContactByName(const struct Contact* pc, const char* name) { int i = 0; for (i = 0; i < pc->sz; i++) { if (strcmp(pc->data[i].name, name) == 0) { return i; } } return -1; } void DelContact(struct Contact* pc) { if (pc->sz == 0) { printf("The address book is empty and cannot be deleted\n"); return; } char name[NAME_MAX] = { 0 }; printf("Please enter the name of the person to delete:"); scanf("%s", name); //lookup int pos = FindContactByName(pc, name); if (pos == -1) { printf("The specified contact does not exist\n"); } else { //delete int j = 0; for (j = pos; j < pc->sz-1; j++) { pc->data[j] = pc->data[j + 1]; } pc->sz--; printf("Delete succeeded\n"); } }
SearchContact() finds the specified contact and directly calls FindContactByName() to print the information after finding it.
void SearchContact(const struct Contact* pc) { char name[NAME_MAX] = { 0 }; printf("Please enter the name of the person you want to find:"); scanf("%s", name); int pos = FindContactByName(pc, name); if (pos == -1) { printf("No one was found\n"); } else { printf("%15s\t%5s\t%8s\t%15s\t%30s\n", "name", "age", "sex", "tele", "addr"); printf("%15s\t%5d\t%8s\t%15s\t%30s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr); } }
Modifycontact() modifies the specified contact, uses FindContactByName() to find it, returns the array subscript pos if it exists, and then enters new information to overwrite the corresponding member of the array data whose pointer points to the subscript pos.
void ModifyContact(struct Contact* pc) { char name[NAME_MAX] = { 0 }; printf("Please enter the name of the person to modify:"); scanf("%s", name); int pos = FindContactByName(pc, name); if (pos == -1) { printf("The person to modify does not exist\n"); } else { printf("Please enter a new name:"); scanf("%s", pc->data[pos].name); printf("Please enter a new age:"); scanf("%d", &(pc->data[pos].age)); printf("Please enter a new gender:"); scanf("%s", pc->data[pos].sex); printf("Please enter a new phone number:"); scanf("%s", pc->data[pos].tele); printf("Please enter a new address:"); scanf("%s", pc->data[pos].addr); printf("Modified successfully"); } }
ShowContact() displays all contacts. After entering the function, first print a row header separated by "\ t", and then use the for loop to print everyone's data until the sz person stops.
void ShowContact(struct Contact* pc) { int i = 0; printf("%15s\t%5s\t%8s\t%15s\t%30s\n","name","age","sex","tele","addr"); for (i = 0; i < pc->sz; i++) { printf("%15s\t%5d\t%8s\t%15s\t%30s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr); } }
After writing here, the production of the address book program has been completed. All the codes are shown below:
test.c
#include "contact.h" void menu() { printf("\n"); printf("**************************************\n"); printf("****** 1. add 2. del ******\n"); printf("****** 3. search 4.modify ******\n"); printf("****** 5. show 0. exit ******\n"); printf("**************************************\n"); } enum Option { EXIT, ADD, DEL, SEARCH, MODIFY, SHOW }; int main() { int input = 0; struct Contact con;//Create an address book InitContact(&con);//Initialize address book do { menu(); printf("Please select:"); scanf("%d", &input); switch (input) { case ADD: AddContact(&con); break; case DEL: DelContact(&con); break; case SEARCH: SearchContact(&con); break; case MODIFY: ModifyContact(&con); break; case SHOW: ShowContact(&con); break; case EXIT: printf("Exit address book\n"); break; default: printf("Selection error\n"); break; } } while (input); return 0; }
contact.c
#include "contact.h" void InitContact(struct Contact* pc) { pc->sz = 0;//No information by default memset(pc->data, 0, sizeof(pc->data)); } void AddContact(struct Contact* pc) { if (pc->sz == MAX) { printf("The address book is full and cannot be added\n"); } else { printf("Please enter your first name:"); scanf("%s", pc->data[pc->sz].name); printf("Please enter age:"); scanf("%d", &(pc->data[pc->sz].age)); printf("Please enter gender:"); scanf("%s", pc->data[pc->sz].sex); printf("Please enter phone number:"); scanf("%s", pc->data[pc->sz].tele); printf("Please enter address:"); scanf("%s", pc->data[pc->sz].addr); printf("Added successfully\n"); pc->sz++; } } int FindContactByName(const struct Contact* pc, const char* name) { int i = 0; for (i = 0; i < pc->sz; i++) { if (strcmp(pc->data[i].name, name) == 0) { return i; } } return -1; } void DelContact(struct Contact* pc) { if (pc->sz == 0) { printf("The address book is empty and cannot be deleted\n"); return; } char name[NAME_MAX] = { 0 }; printf("Please enter the name of the person to delete:"); scanf("%s", name); //lookup int pos = FindContactByName(pc, name); if (pos == -1) { printf("The specified contact does not exist\n"); } else { //delete int j = 0; for (j = pos; j < pc->sz-1; j++) { pc->data[j] = pc->data[j + 1]; } pc->sz--; printf("Delete succeeded\n"); } } void SearchContact(const struct Contact* pc) { char name[NAME_MAX] = { 0 }; printf("Please enter the name of the person you want to find:"); scanf("%s", name); int pos = FindContactByName(pc, name); if (pos == -1) { printf("No one was found\n"); } else { printf("%15s\t%5s\t%8s\t%15s\t%30s\n", "name", "age", "sex", "tele", "addr"); printf("%15s\t%5d\t%8s\t%15s\t%30s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr); } } void ModifyContact(struct Contact* pc) { char name[NAME_MAX] = { 0 }; printf("Please enter the name of the person to modify:"); scanf("%s", name); int pos = FindContactByName(pc, name); if (pos == -1) { printf("The person to modify does not exist\n"); } else { printf("Please enter a new name:"); scanf("%s", pc->data[pos].name); printf("Please enter a new age:"); scanf("%d", &(pc->data[pos].age)); printf("Please enter a new gender:"); scanf("%s", pc->data[pos].sex); printf("Please enter a new phone number:"); scanf("%s", pc->data[pos].tele); printf("Please enter a new address:"); scanf("%s", pc->data[pos].addr); printf("Modified successfully"); } } void ShowContact(struct Contact* pc) { int i = 0; printf("%15s\t%5s\t%8s\t%15s\t%30s\n","name","age","sex","tele","addr"); for (i = 0; i < pc->sz; i++) { printf("%15s\t%5d\t%8s\t%15s\t%30s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr); } }
contact.h
#define NAME_MAX 30 #define SEX_MAX 5 #define TELE_MAX 12 #define ADDR_MAX 30 #define MAX 1000 #include <stdio.h> #include <string.h> struct PeoInfo { char name[NAME_MAX]; int age; char sex[SEX_MAX]; char tele[TELE_MAX]; char addr[ADDR_MAX]; }; struct Contact { struct PeoInfo data[MAX];//The data of 1000 people is stored in the data array int sz;//Number of currently valid elements in the address book }; void AddContact(struct Contact* pc);//Add a contact void DelContact(struct Contact* pc);//Delete specified contact void SearchContact(struct Contact* pc);//Find the specified contact void ModifyContact(struct Contact* pc);//Modify specified contact void ShowContact(struct Contact* pc);//Show all contacts