one , Test logic
#define _CRT_SECURE_NO_WARNINGS #include "contact.h" void menu() {
printf("***********************************************\n"); printf("**********
1.Add 2.Del *********\n"); printf("********** 3.Sreach 4.Change *********\n");
printf("********** 5.Print 6.Clear *********\n"); printf("********** 7.Sort
0.Exit *********\n");
printf("***********************************************\n"); } int main() {
Contact con; InitContact(&con); int input = 0; menu(); do { scanf("%d",
&input); switch (input) { case Add: AddContact(&con); break; case Del:
DelContact(&con); break; case Sreach: SreachContact(&con); break; case Change:
ChangeContact(&con); break; case Print: PrintContact(&con); break; case Clear:
ClearContact(&con); break; case Sort: SortContact(&con); break; case Exit:
printf(" Exit address book \n"); break; default: printf(" Input error , Please re-enter :"); break; } } while
(input); return 0; }
two , Implementation of address book
#define _CRT_SECURE_NO_WARNINGS 1 #include "contact.h" // initialization - empty void
InitContact(Contact* pc) { assert(pc); pc->sz = 0; memset(pc->data, 0,
sizeof(pc->data)); } void ClearContact(Contact* pc) { assert(pc); pc->sz = 0;
memset(pc->data, 0, sizeof(pc->data)); printf(" The address book has been emptied \n"); } // add to void
AddContact(Contact* pc) { assert(pc); if (pc->sz == Max) { printf(" The address book is full \n");
return; } int n = 0; printf(" Please enter the number of people to add :"); scanf("%d", &n); while (n--) {
printf(" Please enter your 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 the phone number :"); scanf("%s", pc->data[pc->sz].tele);
printf(" Please enter your address :"); scanf("%s", pc->data[pc->sz].addr); printf("\n"); pc->sz++; }
printf(" Added successfully \n"); } int FindName(const Contact* pc, char name[]) { assert(pc);
int i = 0; for (i = 0; i < pc->sz; i++) { if (0 == strcmp(name,
pc->data[i].name)) { return i; } } return -1; } // delete void DelContact(Contact*
pc) { assert(pc); if (pc->sz == 0) { printf(" Address book is empty \n"); return; } char name[20]
= { 0 }; printf(" Please enter the name of the person to delete :"); scanf("%s", name); int pos = FindName(pc,
name); if (pos == -1) { printf(" The person to delete does not exist \n"); return; } int j = 0; for (j =
pos; j < pc->sz - 1; j++) { pc->data[j] = pc->data[j + 1]; } pc->sz--;
printf(" Deleted successfully \n"); } void PrintContact(const Contact* pc) { assert(pc);
printf("%-15s %-5s %-5s %-20s %-30s\n", " full name ", " Age ", " Gender "," Telephone ", " address "); int i =
0; for (i = 0; i < pc->sz; i++) { printf("%-15s %-5d %-5s %-20s %-30s\n",
pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele,
pc->data[i].addr); } } void SreachContact(Contact* pc) { assert(pc); if (pc->sz
== 0) { printf(" Address book is empty \n"); return; } char name[20] = { 0 };
printf(" Please enter the name you want to find :"); scanf("%s", name); int pos = FindName(pc, name); if (pos
== -1) { printf(" The person you are looking for does not exist \n"); return; } printf("%-15s %-5d %-5s %-20s
%-30s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex,
pc->data[pos].tele, pc->data[pos].addr); } void ChangeContact(Contact* pc) {
assert(pc); if (pc->sz == 0) { printf(" Address book is empty \n"); return; } char name[20] = { 0
}; printf(" Please enter the name of the person to modify :"); scanf("%s", name); int pos = FindName(pc, name); if
(pos == -1) { printf(" The person to modify the information does not exist \n"); return; } printf(" Please enter the age to modify :");
scanf("%d", &(pc->data[pos].age)); printf(" Please enter the gender to be modified :"); scanf("%s",
pc->data[pos].sex); printf(" Please enter the phone number to modify :"); scanf("%s", pc->data[pos].tele);
printf(" Please enter the address to be modified :"); scanf("%s", pc->data[pos].addr); printf("\n"); } int
cmp(const void* e1, const void* e2) { return strcmp((*(PeoInfo*)e1).name,
(*(PeoInfo*)e1).name); } void SortContact(Contact* pc) { assert(pc); if (pc->sz
== 0) { printf(" Address book is empty \n"); return; } qsort(pc->data, pc->sz, sizeof(PeoInfo),
cmp); printf(" Sorting succeeded \n"); }
three , Declaration of functions and definition of symbols
#pragma once #include <stdio.h> #include <string.h> #include <assert.h>
#include <stdlib.h> #define Max 1000 typedef struct PeoInfo { char name[20];
int age; char sex[10]; char tele[20]; char addr[20]; }PeoInfo; typedef struct
Contact { PeoInfo data[Max]; int sz; }Contact; enum Option { Exit, Add, Del,
Sreach, Change, Print, Clear, Sort }; // initialization - empty void InitContact(Contact* pc);
// add to void AddContact(Contact* pc); // delete void DelContact(Contact* pc); // Print void
PrintContact(Contact* pc); // lookup void SreachContact(Contact* pc); // modify void
ChangeContact(Contact* pc); // empty void ClearContact(Contact* pc); // Sort by name void
SortContact(Contact* pc);

Technology