The C language implements an address book

  • 2020-06-03 07:38:37
  • OfStack

1 address book can be used to store the information of 1000 people. Each person's information includes:

Name, sex, age, telephone, address

Ways to Provide:

1. Add contact information
2. Delete the specified contact information
3. Find the specified contact information
4. Modify specified contact information
5. Display all contact information
6. Empty all contacts
Sort all contacts by name

There is no way to open up dynamic memory:

Header file: ES21en. h


#ifndef __CONTACT 
#define __CONTACT 
#define _CRT_SECURE_NO_WARNINGS 1 
#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
#include<assert.h> 
#define N 1000 
typedef struct contact 
{ 
 char name[30]; 
 char gender[10]; 
 int age; 
 int telephone; 
 char address[100]; 
};// The structure does not define variables that belong to the declaration 1 Three structural types  
void meau(); 
void show(struct contact *p, int len);//struct contact *p : Struct pointer to this struct,  int len : The length of the structure array  
void Add_linkman(struct contact *p, int len,int flag); 
int Delete_linkman(struct contact *p, int d_number, int len);//int d_number Select which structure you want to delete  
void Find_member(struct contact *p, int d_number, int len);//int d_number Represents the contents of which structure to look for (that is, the contact information)  
void empty(struct contact *p); 
void Modify(struct contact *p, int M_member);//int M_member : Which contact to modify  
int cmp(const void *a, const void *b);//qsort The comparison function inside defines any type  
void sort(struct contact *p, int len);// Sort the contacts by name  
#endif// Conditional compilation  

contact c file


#include"test.h" 
void meau() 
{ 
 printf("    *******************************************\n"); 
 printf("    *******************************************\n"); 
 printf("    ##############My address book##############\n"); 
 printf("    *1-Add  2-Delete  3-Find  *\n"); 
 printf("    *4-Empty  5-Modify  6-Sort  *\n"); 
 printf("    *0-Exit      7-Show  *\n"); 
 printf("    *******************************************\n"); 
 printf("    *******************************************\n"); 
} 
void show(struct contact *p, int len) 
{ 
 assert(p); 
 int i = 0; 
 for (i = 0; i < len; i++) 
 { 
  printf("name:%s gender:%s age:%d telephone:%d address:%s", \ 
   p[i].name, p[i].gender, p[i].age, p[i].telephone, p[i].address); 
  printf("\n"); 
 } 
} 
void Add_linkman(struct contact *p, int len,int flag) 
{ 
 assert(p); 
 int i = 0; 
 for (i = flag; i < len; i++) 
 { 
  printf(" Please enter your name :"); 
  scanf("%s", &p[i].name); 
  printf(" Please enter last name :"); 
  scanf("%s", &p[i].gender); 
  printf(" Please enter your age :"); 
  scanf("%d", &p[i].age); 
  printf(" Please enter the telephone number :"); 
  scanf("%d", &p[i].telephone); 
  printf(" Please enter the address :"); 
  scanf("%s", &p[i].address); 
 } 
} 
int Delete_linkman(struct contact *p, int d_number, int len) 
{ 
  
 assert(p); 
 int i = 0; 
 for (i = d_number - 1; i < len - 1; i++) 
 { 
  p[i] = p[i + 1]; 
 
 } 
} 
void Find_member(struct contact *p, int d_number, int len) 
{ 
 assert(p); 
 if (d_number - 1 >= 0 || d_number - 1<len) 
 { 
  printf("name:%s gender:%s age:%d telephone:%d address:%s", \ 
   p[d_number].name, p[d_number].gender, p[d_number].age, p[d_number].telephone, p[d_number].address); 
  printf("\n"); 
 } 
 else 
 { 
  printf(" The contact does not exist :"); 
  return; 
 } 
 
} 
void empty(struct contact *p) 
{ 
 assert(p); 
 int i = 0; 
 for (i = 0; i <1000; i++) 
 { 
  memset(p+i, 0, sizeof(struct contact)); 
 } 
 
} 
void Modify(struct contact *p, int M_member) 
{ 
 assert(p); 
 printf(" Modify the previous contact information as: "); 
 printf("\n"); 
 printf("name:%s gender:%s age:%d telephone:%d address:%s", \ 
  p[M_member - 1].name, p[M_member - 1].gender, p[M_member - 1].age, p[M_member - 1].telephone, p[M_member - 1].address); 
 printf("\n"); 
 printf(" Please enter the information you want to modify: "); 
 printf(" Please enter your name :"); 
 scanf("%s", &p[M_member - 1].name); 
 printf(" Please enter last name :"); 
 scanf("%s", &p[M_member - 1].gender); 
 printf(" Please enter your age :"); 
 scanf("%d", &p[M_member - 1].age); 
 printf(" Please enter the telephone number :"); 
 scanf("%d", &p[M_member - 1].telephone); 
 printf(" Please enter the address :"); 
 scanf("%s", &p[M_member - 1].address); 
 
} 
int cmp(const void *a, const void *b) 
{ 
 
 struct contact *aa = (struct contact *)a; 
 struct contact *bb = (struct contact *)b; 
 if (aa->name != bb->name) 
  return(strcmp((aa->name), (bb->name))); 
 
} 
void sort(struct contact *p, int len) 
{ 
 assert(p); 
 qsort(p, len, sizeof(struct contact), cmp); 
 
} 

test. c file:


#include"test.h" 
int main() 
{ 
 int num = 0; 
 struct contact student[N]; 
 int len = 0; 
 int flag = 0;// define 1 The position of each structure in an array of structures  
 int total = N; 
 int delete_number = 0; 
 meau(); 
 while (1) 
 { 
  printf(" Please enter a number to select: "); 
  scanf("%d", &num); 
   switch (num) 
   { 
   case 1:{ 
      printf(" Please add len Student information :"); 
      scanf("%d", &len); 
      Add_linkman(student,len+flag,flag); 
      flag=flag+len; 
      
       
   }break; 
 
   case 2:{ 
      printf(" Please enter the number you want to delete i Student information :"); 
      scanf("%d", &delete_number); 
      Delete_linkman(student, delete_number, len+flag); 
      flag = flag - 1; 
   }break; 
 
   case 3:{ 
      int Find_number = 0; 
      printf(" Please enter the number you are looking for i Student information :"); 
      scanf("%d", &Find_number); 
      Find_member(student, delete_number, len+flag); 
 
   }break; 
 
   case 4:{ 
      printf(" Empty all contacts: "); 
      empty(student); 
 
   }break; 
 
   case 5:{ 
      printf(" Please enter the number you want to modify i Student information :"); 
      int M_member = 0; 
      scanf("%d", &M_member); 
      Modify(student, M_member); 
 
   }break; 
 
   case 6:{ 
      printf(" Sort all contacts by name :\n"); 
      sort(student, len+flag); 
 
   }break; 
 
   case 7:{ 
      printf(" Print the information for all contacts :\n"); 
      show(student, flag); 
 
   }break; 
   case 0:{ 
      exit(1); 
   } 
 
   default:printf("enter error data!!!"); 
 
  } 
 } 
 system("pause"); 
 return 0; 
 
} 

Related articles: