C Language Address Book Management System complete edition
- 2020-06-03 07:37:49
- OfStack
C language realized the address book input information, save information, insert, delete, sort, search, single display and other functions.
The complete code is as follows:
#include <stdio.h>
#include <malloc.h> // I get a point of size zero Size Pointer to the first byte of the memory region //
#include <string.h>
#include <stdlib.h> // Standard library function //
#define NULL 0
#define LEN sizeof(struct address_list) // Calculation of bytes //
int n;
struct address_list
{
char name[30]; // The name
char work[30]; // professional
char handset[30]; // Mobile phone
char email[30]; // E-mail
char address[30]; // Correspondence address
struct address_list *next;
};
struct address_list *shifang(struct address_list *head); // Free memory function declaration
// Create a function that does not lead a linked list of nodes
struct address_list *creat(void)
{
struct address_list *head,*p1,*p2;
char name[20];
n=0;
p1=(struct address_list *)malloc(LEN);
p2=p1; // Forced memory conversion
printf(" Please enter the contents of the address book !\n Name input is 0 "Indicates completion of creation !\n");
printf(" Please enter your name :");
gets(name);
if(strcmp(name,"0")!=0)
{
strcpy(p1->name,name);
printf(" Please enter occupation :"); gets(p1->work);
printf(" Please enter your mobile phone :"); gets(p1->handset);
printf(" Please enter email :"); gets(p1->email);
printf(" Please enter your address :"); gets(p1->address);
head=NULL;
while(1)
{
n=n+1; // The number of people in the address book
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
printf(" Please enter your name :");
gets(name);
if(strcmp(name,"0")==0)
{
break;
}
else
{
p1=(struct address_list *)malloc(LEN);
strcpy(p1->name,name);
printf(" Please enter occupation :"); gets(p1->work);
printf(" Please enter your mobile phone :"); gets(p1->handset);
printf(" Please enter email :"); gets(p1->email);
printf(" Please enter your address :"); gets(p1->address);
}
}
p2->next=NULL;
return head;
}
else
return 0;
}
// Output function
void print(struct address_list *head)
{
struct address_list *p;
if(head!=NULL)
{
p=head;
printf(" This address book is now Shared %d people :\n",n);
printf("--- The name ------- professional -------- Mobile phone -------Email------- Correspondence address \n");
printf("==================================\n");
do
{
printf("== %s",p->name); printf(" ");
printf("%s",p->work); printf(" ");
printf("%s",p->handset); printf(" ");
printf("%s",p->email); printf(" ");
printf("%s",p->address); printf(" \n");
p=p->next;
}while(p!=NULL);
printf("==================================\n");
}
else
printf(" The address book is empty , Can't output !\n");
}
// Add function
struct address_list *insert(struct address_list *head)
{
struct address_list *p0,*p1,*p2;
char name[20];
p1=head;
printf(" Please enter the additions :\n");
printf(" Please enter your name :"); gets(name);
if(strcmp(name,"0")==0)
{
printf(" Name cannot be 0, Increase the failure !\n");
return(head);
}
else
{
p0=(struct address_list *)malloc(LEN);
strcpy(p0->name,name);
printf(" Please enter occupation :"); gets(p0->work);
printf(" Please enter your mobile phone :"); gets(p0->handset);
printf(" Please enter email :"); gets(p0->email);
printf(" Please enter your address :"); gets(p0->address);
n=n+1;
if(head==NULL)
{
head=p0;
p0->next=NULL;
return head;
}
else
{
while(strcmp(p0->name,p1->name)>0&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(strcmp(p0->name,p1->name)<0 || strcmp(p0->name,p1->name)==0)
{
if(head==p1)
{
head=p0;
}
else
{
p2->next=p0;
}
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
return head;
}
}
}
struct address_list* delete_txl(struct address_list *head)
{
struct address_list *p,*q;
char name[30];
if(head==NULL)
{
printf(" The address book is empty , Unable to display !\n");
return head;
}
p=head;
printf(" Please enter the name of the person to be deleted :");
gets(name);
if(strcmp(head->name,name)==0)
{
head=head->next;
free(p);
printf(" Delete operation successful! \n");
return head;
}
else
{
q=head,p=head->next;
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
q->next=p->next;
free(p);
printf(" Delete operation successful! \n");
return head;
}
p=p->next;
q=q->next;
}
}
}
// According to the function
struct address_list *display(struct address_list *head)
{
struct address_list *p1,*p2;
char name[30];
int m;
if(head==NULL)
{
printf(" The address book is empty , Unable to display !\n");
return head;
}
p1=head;
m=0;
printf(" Please enter the name of the person to be displayed :");
gets(name);
while(p1!=NULL)
{
while((strcmp(p1->name,name))!=0 && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
m++;
printf("%s The contents of the communication are as follows :\n",name);
printf("--- The name -------- professional -------- Mobile phone -------Email------ Correspondence address \n");
printf("==================================\n");
printf("== %s",p1->name);printf(" ");
printf("%s",p1->work);printf(" ");
printf("%s",p1->handset);printf(" ");
printf("%s",p1->email);printf(" ");
printf("%s",p1->address); printf(" \n");
printf("==================================\n");
}
p1=p1->next;
}
if(m==0)
{
printf(" The person is not in this address book !\n");
}
return(head);
}
// Sorting function
struct address_list *paixu(struct address_list *head)
{
struct address_list *p1,*p2;
int i,j;
struct address_list1
{
char name[30];
char work[30];
char handset[30];
char email[30];
char address[30];
};
struct address_list1 px[200];
struct address_list1 temp;
if(head==NULL)
{
printf(" The address book is empty , Unable to sort !\n");
return(head);
}
p1=head;
for(i=0;i<n,p1!=NULL;i++)
{
strcpy(px[i].name,p1->name);
strcpy(px[i].work,p1->work);
strcpy(px[i].handset,p1->handset);
strcpy(px[i].email,p1->email);
strcpy(px[i].address,p1->address);
p2=p1;
p1=p1->next;
}
head=shifang(head);
for(j=0;j<n-1;j++)
{
for(i=j+1;i<n;i++)
{
if(strcmp(px[i].name,px[j].name)<0)
{
temp=px[i];
px[i]=px[j];
px[j]=temp;
}
}
}
p1=(struct address_list *)malloc(LEN);
p2=p1;
strcpy(p1->name,px[0].name);
strcpy(p1->work,px[0].work);
strcpy(p1->handset,px[0].handset);
strcpy(p1->email,px[0].email);
strcpy(p1->address,px[0].address);
head=p1;
for(i=1;i<n;i++)
{
p1=(struct address_list *)malloc(LEN);
strcpy(p1->name,px[i].name);
strcpy(p1->work,px[i].work);
strcpy(p1->handset,px[i].handset);
strcpy(p1->email,px[i].email);
strcpy(p1->address,px[i].address);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
printf(" In order by name :\n");
print(head);
return(head);
}
// Name lookup function
struct address_list *search(struct address_list *head)
{
struct address_list *p1,*p2;
int m;
char name[30];
if(head==NULL)
{
printf(" The address book is empty , Unable to categorize search !\n");
return(head);
}
p1=head;
printf("********************\n");
printf("** Please enter the name you want to look up **\n");
printf("********************\n");
m=0;
gets(name);
while(p1!=NULL)
{
while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
m++;
printf(" What you're looking for is :\n");
printf("+++++++++++++++++++++++++++++++++++\n");
printf("++ %s %s %s %s %s\n",p1->name,p1->work,p1->handset,p1->email,p1->address);
printf("+++++++++++++++++++++++++++++++++++\n");
}
p1=p1->next;
if(m==0)
{
printf(" The person is not in this address book !\n");
}
break;
}
return(head);
}
// Memory free function
struct address_list *shifang(struct address_list *head)
{
struct address_list *p1;
while(head!=NULL)
{
p1=head;
head=head->next;
free(p1);
}
return(head);
}
// File write function
void save(struct address_list *head)
{
FILE *fp;
struct address_list *p1;
char tong[30];
if(head==NULL)
{
printf(" The address book is empty , Should not be stored !\n");
return;
}
printf(" Please enter the saved file name :");
gets(tong);
fp=fopen("(tong).txt","w");
if(fp==NULL)
{
printf("cannot open file\n");
return;
}
p1=head;
fprintf(fp," The name professional Mobile phone Email Correspondence address \n");
for(;p1!=NULL;)
{
fprintf(fp,"%s %s %s %s %s\n",p1->name,p1->work,p1->handset,p1->email,p1->address);
p1=p1->next;
}
printf(" Save the finished !\n");
fclose(fp);
}
// File readout function
struct address_list *load(struct address_list *head)
{
FILE *fp;
char tong[30];
struct address_list *p1,*p2;
printf(" Please enter the file name to output :");
gets(tong);
fp=fopen("(tong).txt","r");
if(fp==NULL)
{
printf(" This address book name does not exist , Can't output !\n");
return(head);
}
else
{
head=shifang(head);
}
p1=(struct address_list *)malloc(LEN);
fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address);
if(feof(fp)!=0)
{
printf(" The file is empty , Unable to open !\n");
return(head);
}
else
{
rewind(fp);
p2=p1;
head=p1;
n=0;
while(feof(fp)==0)
{
fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address);
if(feof(fp)!=0)
break;
p2->next=p1;
p2=p1;
p1=(struct address_list *)malloc(LEN);
n=n+1;
}
p2->next=NULL;
p1=head;
head=head->next;
n=n-1;
free(p1);
print(head);
printf(" Open the finished !\n");
return(head);
}
fclose(fp);
}
// Synthetic operation function
struct address_list *menu(struct address_list *head)
{
char num[10];
while(1)
{
printf("*********************\n");
printf("*** 1 Name lookup ****\n");
printf("*** 2 A single display ****\n");
printf("*** 3 increase ****\n");
printf("*** 4 exit ****\n");
printf("*********************\n");
printf(" Please enter the action of your choice :");
gets(num);
switch(*num)
{
case '1':
{
head=search(head); // Name lookup
print(head);
}
break;
case '2':
{
head=display(head); // According to
}
break;
case '3':
{
head=insert(head); // increase
print(head);
}
break;
case '4':
return head;
default:
printf(" Operation error, this item does not exist !\n");
break;
}
if(strcmp(num,"6")==0)
break;
}
return head;
}
// The main function
void main()
{
struct address_list *head=NULL;
char num[10];
printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
printf("*=* Programming instructions *=*\n");
printf("*=* Please save the address book in time ! *=*\n");
printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
while(1)
{
printf("************************\n");
printf("*** 1 Create address book ****\n");
printf("*** 2 Sort by name ****\n");
printf("*** 3 Comprehensive operation ****\n");
printf("*** 4 save ****\n");
printf("*** 5 Open the ****\n");
printf("*** 6 delete ****\n");
printf("*** 7 exit ****\n");
printf("************************\n");
printf(" Please enter the action of your choice :");
gets(num);
switch(*num)
{
case '1':
{
if(head==NULL)
{
head=creat(); // create
print(head);
}
else
{
head=shifang(head);
head=creat(); // To recreate the
print(head);
}
}
break;
case '2':
{
head=paixu(head); // The sorting
}
break;
case '3':
{
head=menu(head); // Comprehensive operation
}
break;
case '4':
{
save(head); // file
print(head);
}
break;
case '5':
{
head=load(head); // The output file
}
break;
case '6':
{
head=delete_txl(head); // delete
print(head);
}
break;
case '7':
head=shifang(head);
break;
default:
printf(" Operation error, this item does not exist !\n");
break;
}
if(strcmp(num,"7")==0)
break;
}
}
For more information, please pay attention to the topic management System Development.