C language implementation of telephone book management system

  • 2020-06-23 01:15:24
  • OfStack

This article shares the specific code of C language phone book management system for your reference, the specific content is as follows


/* big 2 In practice what Zhou did, 
 Time: 2017.9,11
 Telephone book management system 
*/
 
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0
typedef struct lianxiren{
 char job[30];
 char number[20];
 char name[10];
 char email[30];
 struct lianxiren*next;
}lianxiren;
#define LEN sizeof(lianxiren)
lianxiren* creat_list()
{
 lianxiren *head,*p1,*p2;
 char name[10];int n=0;
 head=NULL;
 p1=(lianxiren *)malloc(LEN);
 p2=p1;
 printf(" Please enter your name (name is 0 Stop creating when) :");
 gets(name);
 if(strcmp(name,"0")==0)return 0;
 else {
 strcpy(p1->name,name);
 printf(" Please enter the telephone number :");gets(p1->number);
 printf(" Please enter your employer :");gets(p1->job);
 printf(" Please enter the E-mail:");gets(p1->email);
 }
 while(1)
 {
 n++;
 if(n==1)
  head=p1;
 else
  p2->next=p1;
 p2=p1;
 printf(" Please enter your name (name is 0 Stop creating when) :");
 gets(name);
 if(strcmp(name,"0")==0)break;
 else{
  p1=(lianxiren*)malloc(LEN);
      strcpy(p1->name,name);
  printf(" Please enter the telephone number :");gets(p1->number);
  printf(" Please enter your employer :");gets(p1->job);
  printf(" Please enter the E-mail:");gets(p1->email);}
 }p2->next=NULL;
 return head;
}// List creation function 
lianxiren* shifang_list(lianxiren*head)
{
 lianxiren*p1;
 for(;head!=NULL;)
 {
 p1=head; 
 head=head->next; 
 free(p1);
 }
 return head;
}// Release the list function 
void print_list(lianxiren*head)
{
 void caidan();
 int n=0;
 printf(" The address book now has the following members :");
  while(head!=NULL)
 {
 printf("\n The name :");puts(head->name);
 printf("\n The phone number :");puts(head->number);
 printf("\n Work units :");puts(head->job);
 printf("\nE-mail:");puts(head->email);
 putchar('\n');
 head=head->next;n++;
 if(n%8==0){
 printf(" Press enter to display 1 page ");
 getchar();
 system("cls");
 caidan();
 } 
 }
 printf(" In total, %d A contact \n",n);
}// Print the list function 
int length(lianxiren*head) 
{ 
  int n = 0; 
  lianxiren *p; 
  p = head; 
  while(p != NULL) 
  { 
    p = p->next; 
    n++; 
  } 
  return n; 
} 
 
void paixu1(lianxiren*head)
{
 void caidan();
 int n=length(head);
 int i,j;
 lianxiren temp,*p;
 p=head;
 lianxiren a[100];
 for(i=1;i<=n;i++)
 {
 strcpy(a[i].name,p->name);
 strcpy(a[i].number,p->number);
 strcpy(a[i].job,p->job);
 strcpy(a[i].email,p->email);
 p=p->next;
 }
 for(i=1;i<=n-1;i++)
 {
 for(j=1;j<=n-i;j++)
 {if(strcmp(a[j].number,a[j+1].number)>0)
 {
  temp=a[j];
      a[j]=a[j+1];
  a[j+1]=temp;
 } 
 }
 }
 printf(" The address book now has the following members :");
 for(i=1;i<=n;i++)
 {
 printf("\n The name :");puts(a[i].name);
 printf("\n The phone number :");puts(a[i].number);
 printf("\n Work units :");puts(a[i].job);
 printf("\nE-mail:");puts(a[i].email);
 putchar('\n');
 if(i%8==0){
 printf(" Press enter to display 1 page ");
 getchar();
 system("cls");
 caidan();
 } 
 }
 printf(" In total, %d A contact \n",n);
}
void paixu2(lianxiren*head)
{
 void caidan();
 int n=length(head);int i,j;
 lianxiren temp,*p;
 p=head;
 lianxiren a[100];
 for(i=1;i<=n;i++)
 {
 strcpy(a[i].name,p->name);
 strcpy(a[i].number,p->number);
 strcpy(a[i].job,p->job);
 strcpy(a[i].email,p->email);
 p=p->next;
 }
for(i=1;i<=n-1;i++)
 {
 for(j=1;j<=n-i;j++)
 {if(strcmp(a[j].name,a[j+1].name)>0)
 {
  temp=a[j];
      a[j]=a[j+1];
  a[j+1]=temp;
 } 
 }
 }
 printf(" The address book now has the following members :");
 for(i=1;i<=n;i++)
 {
 printf("\n The name :");puts(a[i].name);
 printf("\n The phone number :");puts(a[i].number);
 printf("\n Work units :");puts(a[i].job);
 printf("\nE-mail:");puts(a[i].email);
 putchar('\n');
 if(i%8==0){
 printf(" Press enter to display 1 page ");
 getchar();
 system("cls");
 caidan();
 } 
 }
 printf(" In total, %d A contact \n",n);
}
void paixu_list(lianxiren*head)
{
 void caidan();
 if(head==NULL)
 {printf(" The phone book is empty, please re-create it !");
 return;
 }
 char a;
 printf("1, Sort by phone number in ascending order \n2, Sort by alphabetical ascending order of names \n");
 printf(" Please select a 1 Sorting method :");
 a=getchar();
 getchar();
 switch(a){
 case'1':paixu1(head);break;
 case'2':paixu2(head);break;
 default:printf(" Input is wrong !\n");break;
 }
}
void chazhao1(lianxiren*head)
{if(head==NULL)
{
 printf(" The phone book is empty, please re-create it ");
 return;
}
char name[10];
printf(" Please enter your name :");gets(name);
while(strcmp(name,head->name)!=0)
{head=head->next;
if(head==NULL){printf(" This contact is not in the phone book \n");
return;}
}
printf("%s The telephone number is :",name);
puts(head->number);
printf("\n Work units :");puts(head->job);
printf("\nE-mail:");puts(head->email);
}// Look up by name 
void chazhao2(lianxiren*head)
{if(head==NULL)
{
 printf(" The phone book is empty, please re-create it ");
 return;
}
char number[20];
printf(" Please enter the telephone number :");gets(number);
while(strcmp(number,head->number)!=0)
{head=head->next;
if(head==NULL){printf(" This contact is not in the phone book \n");
return;}
}
printf("%s The owner of the for :",number);
puts(head->name);
printf("\n Work units :");puts(head->job);
printf("\nE-mail:");puts(head->email);
}// Look it up by phone number 
void chazhao(lianxiren*head)
{
 int n;
 printf("  1, Look up by name \n  2, Look it up by phone number \n  3, exit \n Please select the service you need :");
  scanf("%d",&n);
 getchar();
 while(1)
 {
 switch(n){
 case 1:{chazhao1(head);printf(" Please select a service item :");scanf("%d",&n);getchar();}break;
 case 2:{chazhao2(head);printf(" Please select a service item :");scanf("%d",&n);getchar();}break;
 case 3:return;break;
 default:{printf(" Incorrect input !");printf(" Please select a service item :");scanf("%d",&n);getchar();}break;
 }}
}// Find contacts 
void add_list(lianxiren*head)
{
  lianxiren*p1,*p2,*h;
 char name[10];
 printf(" Please enter your name ( The name for 0 When to stop )");
 gets(name);
 if(strcmp(name,"0")!=0)
 {
 p1=(lianxiren*)malloc(LEN);
 strcpy(p1->name,name);
 printf(" Please enter the telephone number :");gets(p1->number);
 printf(" Please enter your employer :");gets(p1->job);
 printf(" Please enter the E-mail:");gets(p1->email);
 }
 else return;
 h=p1;
  while(1)
 {
 p2=p1;
 printf(" Please enter your name ( The name for 0 When to stop )");
 gets(name);
 if(strcmp(name,"0")==0)break;
 else{
  p1=(lianxiren*)malloc(LEN);
  strcpy(p1->name,name);
  printf(" Please enter the telephone number :");gets(p1->number);
  printf(" Please enter your employer :");gets(p1->job);
  printf(" Please enter the E-mail:");gets(p1->email);
  p2->next=p1;
 }
 }
  p1=head->next;
 head->next=h;
  p2->next=p1;
}// Add a linked list function 
lianxiren*xiugai_list(lianxiren*head)
{
 char a[20];
 printf(" Please enter the contact name or telephone number :");
 gets(a);
 if(head==NULL)
 {
 printf(" The phone book is empty, please re-create it ");
 return head;
 }
 lianxiren*p=head;
 while(strcmp(a,p->name)!=0&&strcmp(a,p->number)!=0)
 {
 p=p->next;
 if(p==NULL){printf(" This contact is not in the phone book \n");
 return head;}
 }
 printf(" To find the !");
 printf("\n The name :");puts(p->name);
 printf("\n The phone number :");puts(p->number);
 printf("\n Work units :");puts(p->job);
 printf("\nE-mail:");puts(p->email);
 putchar('\n');
 int n;
 while(1)
 {printf(" Please select the action item (1, The name 2, The phone number 3, Work units 4,E-mail 5, exit ):");
 scanf("%d",&n);
 getchar();
 switch(n)
 {
  case 1:{printf(" Please enter your name :");gets(p->name);}break;
 case 2:{printf(" Please enter the telephone number :");gets(p->number);}break;
 case 3:{printf(" Please enter your employer :");gets(p->job);}break;
 case 4:{printf(" Please enter the E-mail:");gets(p->email);}break;
  case 5:return head;break;
 default:printf(" Input error !");break;
 }
 }
}// Modify the list function 
lianxiren*delete_list(lianxiren*head)
{
 char a[20];
 printf(" Please enter the contact name or telephone number :");
 gets(a);
 if(head==NULL)
 {
 printf(" The phone book is empty, please re-create it ");
 return head;
 }
 lianxiren*p=head,*p1;
 while(strcmp(a,p->name)!=0&&strcmp(a,p->number)!=0)
 {
 p1=p;
 p=p->next;
 if(p==NULL){printf(" This contact is not in the phone book \n");
 return head;}
 }
 printf(" To find the !");
 printf("\n The name :");puts(p->name);
 printf("\n The phone number :");puts(p->number);
 printf("\n Work units :");puts(p->job);
 printf("\nE-mail:");puts(p->email);
 putchar('\n');
 char n;
 printf(" Whether to delete the contact (Y/N)");
 n=getchar();
 switch(n)
 {
  case 'y':
 case 'Y':{if(p==head){head=p->next;free(p);}else{p1->next=p->next;free(p);}printf(" Deleted successfully !");}break;
 case 'N':
 case 'n':printf(" delete !");break;
 default:printf(" Input error !");break;
 }
 return head;
}// Delete the list function 
void save_list(lianxiren*head)
{FILE *fp;
if((fp=fopen("dianhuabu.dat","wb"))==NULL) {
 printf("File cannot be opened\n");
 exit(0);}
if(head==NULL)
{
 printf(" The address book is empty \n");
 return;
}
lianxiren*p1=head;
while(p1!=NULL)
{
 if(fwrite(p1,LEN,1,fp)!=1){
 printf("cannot open file\n");
 return;}
 p1=p1->next;
}
printf(" Save the finished !\n");
fclose(fp);
}// File write function 
lianxiren*load_list(lianxiren*head)
{FILE *fp;
if((fp=fopen("dianhuabu.dat","rb"))==NULL) {
 printf(" The phone book is empty, please re-create it \n");
 exit(0);}
lianxiren*p1,*p2;
p1=(lianxiren*)malloc(LEN);
if(fread(p1,LEN,1,fp)==0)
{printf(" The phone book is empty, please re-create it ");
return head;
}
head=p1;
p2=p1;
p1=(lianxiren*)malloc(LEN);
while(fread(p1,LEN,1,fp))
{
 p2->next=p1;
 p2=p1;
 p1=(lianxiren*)malloc(LEN);
}
p2->next=NULL;
free(p1);
return(head);
fclose(fp);
}// File read function 
int kouling()
{
 char s[20];
 printf(" Please enter your password :");
 int n=0;
 while(n<3)
 {scanf("%s",s);
 getchar();
 if(strcmp(s,"20160390527")==0)
 return 0;
 else{n++;
 if(n==3){
 printf(" Sorry, typo \n");
 return 1;}
 printf(" Password error, please reenter \n");
 }
 }return 1;
}// Password verification, password is: 20160390527
void caidan()
{
 printf("       Welcome to the phone book system       \n");
 printf("********************************************\n");
  printf("    1 Create a phone book ( Will overwrite the original phone book )\n");
 printf("    2 , find contacts         \n");
 printf("    3 , adding contacts         \n");
 printf("    4 , modify the contact         \n");
 printf("    5 , delete contacts         \n");
  printf("    6 Check the phone book ( The sorting )     \n");
 printf("    7 , quit the system          \n");
 printf("********************************************\n");
}// Menu interface 
void main()
{
 if(kouling()) return;// Password authentication 
 system("cls");
 caidan();// Enter the menu interface 
 printf("      Please select the service you need :");
 int n;scanf("%d",&n);
 getchar();
 lianxiren*head;
 while(1){
 system("cls");
    caidan();
 switch(n){
 case 1:{
  head=creat_list();
  system("cls");
  caidan();
  print_list(head);
  save_list(head);
  shifang_list(head);
  printf("********************************************\n");
  printf("\n If additional services are required, please re-enter :");
  scanf("%d",&n);
  getchar();}break;// Create a phone book (create a linked list, write to a file , Release the list) 
 case 2:{
  head=load_list(head);
  chazhao(head);
  shifang_list(head);
  printf("********************************************\n");
  printf("\n If additional services are required, please re-enter :");
  scanf("%d",&n);
  getchar();}break;// Find contacts (read files, find functions, release lists) 
 case 3:{
  head=load_list(head);
  add_list(head);
  system("cls");
  caidan();
  print_list(head);
  save_list(head);
  shifang_list(head);
  printf("********************************************\n");
  printf("\n If additional services are required, please re-enter :");
  scanf("%d",&n);
  getchar();}break;// Add contacts (read files, add lists, write files, release lists) 
 case 4:{
  head=load_list(head);
  head=xiugai_list(head);
  save_list(head);
  shifang_list(head);
  printf("********************************************\n");
  printf("\n If additional services are required, please re-enter :");
  scanf("%d",&n);
  getchar();}break;// Modify contacts (read files, modify lists, write files, release lists) 
 case 5:{
  head=load_list(head);
  head=delete_list(head);
  save_list(head);
  shifang_list(head);
  printf("********************************************\n");
  printf("\n If additional services are required, please re-enter :");
  scanf("%d",&n);
  getchar();}break;// Delete contacts (read files, delete lists, write files, release lists) 
 case 6:{
  head=load_list(head);
      paixu_list(head);
  shifang_list(head);
  printf("********************************************\n");
  printf("\n If additional services are required, please re-enter :");
  scanf("%d",&n);
  getchar();}break;// View the phone book (read files, sort lists, release lists) 
 case 7:{
  system("cls");
  return;}break;// exit 
 default:{
  printf("\n Input is wrong , Please re-enter :");
  scanf("%d",&n);
  getchar();}break;
 }}
}

For more information, please pay attention to the topic management System Development.


Related articles: