<>C language program design : Address book system
<>【 subject 4】 Program design of address book
Design a practical small address book program , With add , Query and delete function . By name , Native place , Telephone number 1, Telephone number 2, Composition of e-mail , Names can be encoded by a mixture of characters and numbers . The telephone number may consist of characters and numbers .
requirement : The last address book is saved in the file to be written ;
Implementation function :
(1) The system works in menu mode
(2) Information input function
(3) Information browsing function
(4) Information query function
(5) Information modification function
(6) System exit function
Let's look at the code :
#include<stdio.h> #include<string.h> #include<malloc.h> #include<stdlib.h> #
include<windows.h> #define len sizeof(phone) typedef struct PHONE { char name[10
]; char home[5]; char phone1[20]; char phone2[20]; char email[20]; int sign; //
int classify; struct PHONE *next; }phone; phone *head; void mainmenuface() {
char menu[] = { " Welcome to our address book system !!!\n producer :201816404 He Pengfei \t\t\t201816406 Zhang Jinyuan \n" }; int i
; for(i = 0; menu[i] != '\0'; i++) { Sleep(30); printf("%c", menu[i]); } printf(
"★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆\n"); printf(
"☆********************************************************************★\n");
printf("★ Address book management system ☆\n"); printf("☆ ★\n"); printf("★ 1. New contact information 5. Save contact information ☆\n");
printf("☆ ★\n"); printf("★ 2. Browse all contacts 6. Add contact information ☆\n"); printf("☆ ★\n"); printf(
"★ 3. Modify contact information 7. Delete contact information ☆\n"); printf("☆ ★\n"); printf("★ 4. Find contact information 8. Return to main menu ☆\n")
; printf(
"☆********************************************************************★\n");
printf("★ input 9 Exit the system ☆\n"); printf("☆ -------------------------------------------
★\n"); } void show_one(phone *p) { printf(" full name :%s\t\t", p->name); printf(
" Serial number :%d\n", p->sign); printf(" Native place :%s\t", p->home); printf(" Telephone number 1:%s\t", p->phone1)
; printf(" Telephone number 2:%s\t", p->phone2); printf(" mail box :%s\n", p->email); } phone *
Lookdata(phone *p1) { phone *p2; p2 = head; printf("\n\t\t\t☆☆☆ Display data ☆☆☆\n");
printf(
"----------------------------------------------------------------------\n");
while(p2 != NULL) { printf(" full name :%s\t\t", p2->name); printf(" Serial number :%d\n", p2->sign);
printf(" Native place :%s\t", p2->home); printf(" Telephone number 1:%s\t", p2->phone1); printf(
" Telephone number 2:%s\t", p2->phone2); printf(" mail box :%s\t", p2->email); printf("\n\n");
printf(
"----------------------------------------------------------------------\n"); p2
= p2->next; } return p2; } phone * Inputdata() { phone*p1, *p2, *p3; int flag =
1; int k = 1, i = 1, max; p1 = p2 = (phone*)malloc(len); head = NULL; printf(
"\n\t\t\t☆☆☆ New contact information ☆☆☆\n"); if(p1 != NULL) { head = p1; while(flag) { printf(
"----------------------------------------------------------------------\n");
printf(" Please enter the name of the contact to be entered ( Name column over end )\n"); p3 = head; scanf("%s", p1->name); if(
strcmp(p1->name, "over") != 0) { p3 = head; while(i < k) { i++; if(strcmp(p1->
name, p3->name) == 0) { if(p3->sign > max)max = p3->sign; } p3 = p3->next; } p1
->sign=++max; max = 0; p1->sign = 1; printf(" Please enter the native place of the contact person to be entered \n"); scanf("%s", p1->
home); printf(" Please enter the phone number of the contact person to be entered 1\n"); scanf("%s", p1->phone1); printf(
" Please enter the phone number of the contact person to be entered 2\n"); scanf("%s", p1->phone2); printf(" Please enter the email address of the contact person to be entered \n"); scanf(
"%s", p1->email); p2->next = p1; p2 = p1; p1 = (phone*)malloc(len); p1->next =
NULL; k++; printf(
"----------------------------------------------------------------------\n"); }
else { flag = 0; p1->next = NULL; free(p1); printf(
"----------------- Input succeeded ! Don't forget to input when you exit 5 Save information !------------------\n"); printf(
"----------------------------------------------------------------------\n"); } }
} return (head); } void readfile() { FILE *fp; phone *p1, *p2, *p4, *p5; p1 = p2
= (phone *)malloc(len); int flag = 1; head = NULL; if((fp = fopen(" mail list ", "r"))
== NULL) { printf(" Address book not created , Please input 1 New contact !\n"); } else { if(p1 != NULL)head = p1;
while(1) { if(!feof(fp)) { fscanf(fp, "%s%d%s%s%s%s", p1->name, &p1->sign, p1->
home, p1->phone1, p1->phone2, p1->email); p2->next = p1; p5 = p2; p2 = p1; p1 =
(phone *)malloc(len); } else { p5->next = NULL; break; } } } fclose(fp); } Seek(
phone*p2) { char name[20]; char ch1[5], ch2[5]; memset(ch1, '\0', sizeof(ch1));
memset(ch2, '\0', sizeof(ch2)); int b; int n; printf("\n\t\t\t☆☆☆ Find data ☆☆☆\n");
printf(
"----------------------------------------------------------------------\n");
printf("--------------------------- By name, please enter 1:-----------------------------\n");
printf("--------------------------- By last name, please enter 2:-----------------------------\n");
printf(" Please enter your choice :"); scanf("%d", &n); getchar(); printf(" Please enter the name you want to find :"); switch(n)
{ case 1: b = 0; scanf("%s", name); while(p2 != NULL) { if(strcmp(name, p2->name
) == 0) { printf(" The data you need to find \n"); printf(" full name :%s\t\t", p2->name); printf(" Serial number :%d\n",
p2->sign); printf(" Native place :%s\t", p2->home); printf(" Telephone number 1:%s\t", p2->phone1); printf
(" Telephone number 2:%s\t", p2->phone2); printf(" mail box :%s\n", p2->email); printf(
"----------------------------------------------------------------------\n"); b =
1; } p2 = p2->next; } if(b == 0) { printf(
"\n-------------------- The person you are looking for does not exist !--------------------------------\n"); } break;
case 2: b = 0; printf(" Please enter the last name :"); scanf("%s", ch1); while(p2 != NULL) { strncpy(
ch2, p2->name, 2); if(strcmp(ch1, ch2) == 0) { printf(" The data you need to find \n"); printf(
" full name :%s\t\t", p2->name); printf(" Serial number :%d\n", p2->sign); printf(" Native place :%s\t", p2->home)
; printf(" Telephone number 1:%s\t", p2->phone1); printf(" Telephone number 2:%s\t", p2->phone2); printf(
" mail box :%s\n", p2->email); printf(
"----------------------------------------------------------------------\n"); b =
1; } p2 = p2->next; } if(b == 0) { printf(
"\n-------------------- The last name you are looking for does not exist !--------------------------------\n"); } } }
Keepdata(phone *p2) { FILE *fp; printf("\n\t\t\t☆☆☆ Save data ☆☆☆\n"); printf(
"----------------------------------------------------------------------\n"); if(
(fp = fopen(" mail list ", "w")) == NULL) { printf("cannot open this file\n"); exit(0);
} while(p2 != NULL) { fprintf(fp, "%s\t", p2->name); fprintf(fp, "%d\t", p2->
sign); fprintf(fp, "%s\t", p2->home); fprintf(fp, "%s\t", p2->phone1); fprintf(
fp, "%s\t", p2->phone2); fprintf(fp, "%s\t", p2->email); p2 = p2->next; fputc(
'\n', fp); } printf(
"\n------------------------------ Saved successfully !-----------------------------\n"); printf(
"---------------------------------------------------------------------\n");
fclose(fp); } Changedata(phone *p) { char name[20]; int k, flag = 1; printf(
"\n\t\t\t☆☆☆ Modify data ☆☆☆\n"); printf(
"----------------------------------------------------------------------\n");
printf("--------------------- Please enter the contact name to be modified :---------------------\n"); scanf("%s"
, name); while(p != NULL) { if(strcmp(p->name, name) == 0) { show_one(p); flag =
0; printf("\n------------------ Please select the information to modify :---------------------------\n"); printf
("+------------------------------------------------+\n"); printf("| |\n");
printf("| 1. full name 4. mobile phone 2 |\n"); printf("| |\n"); printf("| 2. Household register 5. mail box |\n"); printf
("| |\n"); printf("| 3. mobile phone 1 |\n"); printf("| |\n"); printf(
"+------------------------------------------------+\n"); scanf("%d", &k); switch
(k) { case 1: printf("------------------- Enter the modified name :------------------\n"); scanf(
"%s", p->name); break; case 2: printf(
"------------------- Enter registered residence :------------------\n"); scanf("%s", p->home); break
; case 3: printf("------------------- Input the modified mobile phone 1:-----------------\n"); scanf("%s"
, p->phone1); break; case 4: printf(
"------------------- Input the modified mobile phone 2:-----------------\n"); scanf("%s", p->phone2);
break; case 5: printf("-------------------- Enter the modified mailbox :------------------\n");
scanf("%s", p->email); break; } printf(
"----------------------- Modified successfully !-------------------------------------\n"); printf(
"----------------------------------------------------------------------\n");
break; } else p = p->next; } if(flag)printf(
"------------------------ There is no such person !------------------------------------\n"); printf(
"----------------------------------------------------------------------\n"); }
void deletedata() { phone *p1, *p2; phone *p3; char name[20]; int n, k = 1;
printf("\n\t\t\t☆☆☆ Delete data ☆☆☆\n"); printf(
"----------------------------------------------------------------------\n");
printf(" Enter the name of the contact you want to delete :\n"); scanf("%s", name); p1 = head; if(head == NULL) {
printf(" Contact information not established , Address book is empty \n"); return; } /*if(strcmp(p1->name,name)==0 ){
head=p1->next ;
printf("------------------------------ Successfully deleted !------------------------------\n");
} else{ */ printf(" Please select the person you want to delete :\n"); printf(
"----------------------------------------------------------------------\n"); p3
= head; while(p3 != NULL) { if(strcmp(name, p3->name) == 0) { printf(" full name :%s\t\t"
, p3->name); printf(" Serial number :%d\n", p3->sign); printf(" Native place :%s\t", p3->home); printf(
" Telephone number 1:%s\t", p3->phone1); printf(" Telephone number 2:%s\t", p3->phone2); printf(" mail box :%s\n",
p3->email); printf("\n\n"); k = 0; printf(
"----------------------------------------------------------------------\n"); }
p3= p3->next; } if(k) { printf(" There is no such person !\n"); return; } printf(" Please enter the serial number of the person you want to delete :\n");
scanf("%d", &n); while(p1 != NULL && (p1->sign != n || strcmp(p1->name, name) !=
0)) { p2 = p1; p1 = p1->next; } if(p1 == NULL) { printf(" There is no such person !\n"); return; } if
(p1 != NULL) { p1 = p1->next; p2->next = p1; printf(
"------------------------------ Successfully deleted !------------------------------\n"); return;
} printf(
"----------------------------------------------------------------------\n"); }
void input(phone *p1) { printf(" full name :"); scanf("%s", &p1->name); printf(" Household register :");
scanf("%s", &p1->home); printf(" cell-phone number 1:"); scanf("%s", &p1->phone1); printf(
" cell-phone number 2:"); scanf("%s", &p1->phone2); printf(" mailbox :"); scanf("%s", &p1->email); p1->
sign= 1; } void Insertdata() { char name2[20]; int flag, max; phone *p1, *p2, *
p3; phone *p4; p1 = head; p3 = (phone *)malloc(len); printf(
"\n\t\t\t☆☆☆ Add a Contact ☆☆☆\n"); printf(
"----------------------------------------------------------------------\n");
printf("------------------------- Please input relevant data :---------------------------\n"); input
(p3); p4 = head; max = 0; while(p4 != NULL) { if(strcmp(p3->name, p4->name) == 0
) { if(p4->sign > max)max = p4->sign; } p4 = p4->next; } p3->sign=++max; printf(
"------------------------- Please enter the location of the insert :---------------------------\n"); printf(
"+---------------------------------------------------------+\n"); printf("| |\n"
); printf("| 1. First position insertion |\n"); printf("| |\n"); printf("| 2. Tail position insertion |\n"); printf("|
|\n"); printf("| 3. Insert before specified name |\n"); printf(
"+---------------------------------------------------------+\n"); scanf("%d", &
flag); switch(flag) { loop: case 1: p3->next = p1; head = p3; break; case 2: p2
= head; while(p2->next != NULL) { p2 = p2->next; } p2->next = p3; p3->next =
NULL; break; case 3: printf(" Please enter the name you want to insert :\n"); scanf("%s", name2); while(p1->
next!= NULL && strcmp(p1->name, name2) != 0) { p2 = p1; p1 = p1->next; } if(p1
== head) { goto loop; } if(p1->next == NULL) { printf(
"------------------------------ There is no such person !---------------------------\n"); } else { p3
->next = p1; p2->next = p3; } break; } printf(
"------------------------------ Insert successfully !------------------------------\n"); printf(
"----------------------------------------------------------------------\n");
return; } int main() { int number; system("color e"); mainmenuface(); readfile()
; while(1) { loop: printf("------------------- Please select your action :----------------------\n")
; scanf("%d", &number); if(number >= 1 && number <= 9) { switch(number) { case 1
: Inputdata(); goto loop; break; case 2: Lookdata(head); break; case 3:
Changedata(head); goto loop; break; case 4: Seek(head); break; case 5: Keepdata(
head); break; case 6: Insertdata(); break; case 7: deletedata(); break; case 8:
mainmenuface(); break; case 9: exit(1); break; } } else { printf(
"--------------- Incorrect operation selected ! Please re-enter :------------------\n"); goto loop; } } return 0;
}
This is realized with linked list , It's convenient , The function of this address book system is quite complete
* Topic analysis :
Such a large application list can better deal with some problems , First, set up the main function , Secondly, we should cooperate to improve the sub function step by step , Realize the function modules one by one , Finally, the interface between sub function and main function is dealt with . The title is required to work in the form of menu , So we need to use it in the main function switch Statement for input selection function .
* Test run :
If there is no address book file in the computer , The following information will be prompted :
Prompt input 1 New contact
If there is a contact's address book file in the computer , It will be like this :
input 2 Browse all contacts :
The address book supports duplicate names ( It's reflected in the serial number )
input 3 Modify contact information :
You can see the phone number 1 It has been changed
input 5 Save the address book information in a file
You can see that the information has been saved in the file
input 6 Add contact information , It can be inserted at the beginning of the address book , It can also be inserted in the tail position , You can also specify a person , Insert in front of that person :
input 7 Delete contact data , Enter name , Encountered with the same name , Display contact information with the same name , Then select the serial number of the person to be deleted :
find contact , Search is divided into 2 species : One is to find the specified contact , Second, search by surname , That is to find the same surname , If there is no name or surname to search in the address book , The corresponding information will be displayed
input 8 Back to the main menu
input 9 Exit the system
The first time to do such a system , There will be shortcomings , We will make more corrections in the future , A lot of code , The function of this system is OK , A lot of situations were considered , Thank you for your support !!!
Technology