当前位置:网站首页>Final training simple address book c language
Final training simple address book c language
2022-06-29 21:49:00 【Eighty eight old】
#include "stdio.h"
#include "stdlib.h"
#include <string.h>
struct address
{
char city[10];
char street[20];
};
struct linklist
{
char name[10];
char phone[15];
struct address add;
char zip[7];
char email[20];
struct linklist *next;
};
struct linklist *create();// Create a linked list 8
void writelisttofile(FILE *fp, struct linklist *head);// Write the linked list to the file 7
void showmenu();// The main menu 1
void listshow(struct linklist *head);// Show one , No files , Changed 5
void listdelete(struct linklist *head);// Delete 9
struct linklist * listdeletebyname(struct linklist *head);// Delete by name 10
void showdeletemenu();//2
void showsearchmenu();// Find the menu 3
void listsearch(struct linklist *head);// lookup 11
struct linklist * listsearchbyname(struct linklist *head, char []);// Look up by name 12
void itemlistshow(struct linklist *ptr);// Find out if there are contacts and output 13
void showemodifiermenu();// Modify the menu 4
struct linklist *listmodifierbyname(struct linklist *head);//15
void modifier(struct linklist *head);//14
struct linklist *paixu(struct linklist *head);//16
struct linklist *listadd(struct linklist *);// add to 6
struct linklist *head;
int main()
{
int choice;
head=create();
while(1)
{
showmenu();
scanf("%d",&choice);
switch(choice)
{
case 1: listshow(head);
break;
case 2: listadd(head);
break;
case 3: listdelete(head);
break;
case 4: modifier(head);
break;
case 5: listsearch(head);
break;
case 6: system("cls");
break;
default:paixu(head);
listshow(head);
printf(" Input error , Please re-enter !\n") ;
exit(0);
}
}
return 0;
}
// The main menu
void showmenu()
{
printf("**********************************\n");
printf("1. Show \n");
printf("2. add to \n");
printf("3. Delete \n");
printf("4. modify \n");
printf("5. Inquire about \n");
printf("6. clear screen \n");
printf("7. Exit the system \n");
printf("**********************************\n");
printf(" Please enter your options :\n");
}
// Create a linked list success
struct linklist * create()
{
struct linklist *head, *ptr1,*ptr2;
int i,n,flag=0;
char name[10],phone[15],city[10],street[20],zip[7],email[20];
FILE *fp;
fp = fopen("lianxiren.txt","r");
if (fp == NULL)
{
printf(" Please enter the number of contacts :\n");
scanf("%d",&n);
if (n == 0)
{
head = NULL;
return head;
}
else
{
printf(" Please enter %d Contact information :\n",n);
for(i=1;i<=n;i++)
{
printf(" Please enter the name of the contact person :");
scanf("%s",name);
printf(" Please enter the contact's phone number :");
scanf("%s",phone);
printf(" Please enter the contact's city :");
scanf("%s",city);
printf(" Please enter the street of the contact :");
scanf("%s",street);
printf(" Please enter the postal code of the contact :");
scanf("%s",zip);
printf(" Please enter the email of the contact :");
scanf("%s",email);
if (!flag)
{
head = ptr2 = ptr1 = (struct linklist *)malloc(sizeof(struct linklist));
flag = 1;
}
else
ptr1 = (struct linklist *)malloc(sizeof(struct linklist));
strcpy(ptr1->name,name);
strcpy(ptr1->phone,phone);
strcpy(ptr1->add.city,city);
strcpy(ptr1->add.street,street);
strcpy(ptr1->zip,zip);
strcpy(ptr1->email,email);
ptr2 ->next = ptr1;
ptr2 = ptr1;
}
ptr1->next=NULL;
}
}
else
{
head = NULL;
while(!feof(fp))
{
if(!flag)
{
head = ptr2 = ptr1 = (struct linklist *)malloc(sizeof(struct linklist));
flag = 1;
}
else
ptr1 = (struct linklist *)malloc(sizeof(struct linklist));
fscanf(fp,"%s %s %s %s %s %s",ptr1->name,ptr1->phone,ptr1->add.city,ptr1->add.street,ptr1->zip,ptr1->email);
ptr2 ->next = ptr1;
ptr2 = ptr1;
}
ptr1->next = NULL;
fclose(fp);
}
if ((fp = fopen("lianxiren.txt","w")) == NULL)
{
printf(" fail to open file !\n");
exit(0);
}
writelisttofile(fp, head);
fclose(fp);
return head;
}
// Write the linked list into the function
void writelisttofile(FILE *fp, struct linklist *head)
{
struct linklist *ptr;
ptr = head;
while(ptr)
{
if (ptr->next!=NULL)
{
fprintf(fp,"%s %s %s %s %s %s\n",ptr->name,ptr->phone,ptr->add.city,ptr->add.street,ptr->zip,ptr->email);
}
else
fprintf(fp,"%s %s %s %s %s %s",ptr->name,ptr->phone,ptr->add.city,ptr->add.street,ptr->zip,ptr->email);
ptr = ptr->next;
}
}
// Show
void listshow(struct linklist *head)
{
struct linklist *temp;
FILE *fp;
temp = head;
while(temp)
{
printf("%s %s %s %s %s %s\n",temp->name,temp->phone,temp->add.city,temp->add.street,temp->zip,temp->email);
temp = temp->next;
}
if ((fp = fopen("lianxiren.txt","w")) == NULL)// Here is what I added
{
printf(" fail to open file !\n");
exit(0);
}
writelisttofile(fp, head);
fclose(fp);
}
// add to
struct linklist *listadd(struct linklist *head)
{
FILE *fp;
char name[10],phone[15],city[10],street[20],zip[7],email[20];
struct linklist *ptr;
ptr = head;
while(ptr->next)
{
ptr = ptr->next;
}
printf(" Please enter the information of the contact you want to add :\n");
printf(" Please enter the name of the contact person :");
scanf("%s",name);
printf(" Please enter the contact's phone number :");
scanf("%s",phone);
printf(" Please enter the contact's city :");
scanf("%s",city);
printf(" Please enter the street of the contact :");
scanf("%s",street);
printf(" Please enter the postal code of the contact :");
scanf("%s",zip);
printf(" Please enter the email of the contact :");
scanf("%s",email);
ptr->next = (struct linklist *)malloc(sizeof(struct linklist));
ptr = ptr->next;
strcpy(ptr->name,name);
strcpy(ptr->phone,phone);
strcpy(ptr->add.city,city);
strcpy(ptr->add.street,street);
strcpy(ptr->zip,zip);
strcpy(ptr->email,email);
ptr->next = NULL;
if ((fp = fopen("lianxiren.txt","w")) == NULL)
{
printf(" fail to open file !\n");
exit(0);
}
writelisttofile(fp, head);
fclose(fp);
return head;
}
// Delete menu
void showdeletemenu()
{
printf("*************** Delete except ***************\n");
printf("1. Delete by name \n");
printf("2. Go back to the previous level \n");
printf("3. Exit the system \n");
printf("**********************************\n");
}
// Delete
void listdelete(struct linklist *head)
{
int choice;
while(1)
{
showdeletemenu();
scanf("%d",&choice);
switch(choice)
{
case 1:
head = listdeletebyname(head);
break;
case 2: break;
default: paixu(head);
listshow(head);
exit(0);
}
if (choice == 2)
break;
}
}
// Delete the name
struct linklist * listdeletebyname(struct linklist *head)
{
FILE *fp;
char name[10];
struct linklist *temp,*ptr,*ptr1;
printf(" Please enter the name to delete :\n");
scanf("%s",name);
temp = listsearchbyname(head,name);
itemlistshow(temp);
if (temp == NULL)
{
head=head;
}
else
{
if (temp == head)
{
head = head->next;
free(temp);
}
else
{
ptr = head;
ptr1 = head->next;
while(ptr1!=temp)
{
ptr=ptr->next;
ptr1 = ptr1->next;
}
ptr->next = ptr1->next;
free(temp);
}
head=head;
}
if ((fp = fopen("lianxiren.txt","w")) == NULL)
{
printf(" fail to open file !\n");
exit(0);
}
writelisttofile(fp, head);
fclose(fp);
return head;
}
// lookup , Whether to find the contact and output
void itemlistshow(struct linklist *ptr)
{
if (ptr == NULL)
{
printf(" Can't find !\n");
}
else
{
printf("%s %s %s %s %s %s\n",ptr->name,ptr->phone,ptr->add.city,ptr->add.street,ptr->zip,ptr->email);
}
}
void showmodifiermenu()
{
printf("*************** repair Change ***************\n");
printf("1. Modify by name \n");
printf("2. Go back to the previous level \n");
printf("3. Exit the system \n");
printf("**********************************\n");
}
// modify
void modifier(struct linklist *head)
{
int choice;
while(1)
{
showmodifiermenu();
scanf("%d",&choice);
switch(choice)
{
case 1:
head = listmodifierbyname(head);
break;
case 2: break;
default: paixu(head);
listshow(head);
exit(0);
}
if(choice==2)
break;
}
}
// Modify by name
struct linklist * listmodifierbyname(struct linklist *head)
{
FILE *fp;
char name[10],phone[15],city[10],street[20],zip[7],email[20];
struct linklist *temp;
printf(" Please enter the contact name to modify :\n");
scanf("%s",name);
temp= listsearchbyname(head,name);
itemlistshow(temp);
if(temp!=NULL)
{
printf(" Please enter all the information of this contact after modification :\n");
scanf("%s%s%s%s%s%s",name,phone,city,street,zip,email);
strcpy(temp->name,name);
strcpy(temp->phone,phone);
strcpy(temp->add.city,city);
strcpy(temp->add.street,street);
strcpy(temp->zip,zip);
strcpy(temp->email,email);
}
if ((fp = fopen("lianxiren.txt","w")) == NULL)
{
printf(" fail to open file !\n");
exit(0);
}
writelisttofile(fp, head);
fclose(fp);
return head;
}
// Find the menu
void showsearchmenu()
{
printf("*************** check look for ***************\n");
printf("1. Search by name \n");
printf("2. Go back to the previous level \n");
printf("3. Exit the system \n");
printf("**********************************\n");
}
// lookup
void listsearch(struct linklist *head)
{
int choice;
char name[10];
struct linklist *temp;
while(1)
{
showsearchmenu();
scanf("%d",&choice);
switch(choice)
{
case 1: printf(" Please enter the contact to query :\n");
scanf("%s",name);
temp = listsearchbyname(head,name);
itemlistshow(temp);
break;
case 2: break;
default: paixu(head);
listshow(head);
exit(0);
}
if (choice == 2)
{
break;
}
}
}
// Look up by name
struct linklist * listsearchbyname(struct linklist *head,char name[])
{
struct linklist *ptr;
ptr = head;
while(ptr)
{
if (strcmp(ptr->name,name) == 0)
{
break;
}
ptr = ptr->next;
}
return ptr;
}
struct linklist *paixu(struct linklist *head)
{
FILE *fp;
struct linklist *ptr,*ptr1;
int i,j,listlength=0;
char temp[100];
ptr = head;
while(ptr)
{
listlength++;
ptr=ptr->next;
}
ptr = head;
for(i=1;i<listlength;i++)
{
ptr = head;
for(j=1;j<=listlength-i;j++,ptr=ptr->next)
{
ptr1 = ptr->next;
if(strcmp(ptr->name,ptr1->name)>=0)
{
strcpy(temp,ptr->name);
strcpy(ptr->name,ptr1->name);
strcpy(ptr1->name,temp);
strcpy(temp,ptr1->phone);
strcpy(ptr->phone,ptr1->phone);
strcpy(ptr1->phone,temp);
strcpy(temp,ptr->add.city);
strcpy(ptr->add.city,ptr1->add.city);
strcpy(ptr1->add.city,temp);
strcpy(temp,ptr->add.street);
strcpy(ptr->add.street,ptr1->add.street);
strcpy(ptr1->add.street,temp);
strcpy(temp,ptr->zip);
strcpy(ptr->zip,ptr1->zip);
strcpy(ptr1->zip,temp);
strcpy(temp,ptr->email);
strcpy(ptr->email,ptr1->email);
strcpy(ptr1->email,temp);
}
}
}
if ((fp = fopen("lianxiren.txt","w")) == NULL)
{
printf(" fail to open file !\n");
exit(0);
}
writelisttofile(fp, head);
fclose(fp);
}
边栏推荐
- 状态管理 利用Session限制页面访问 只有通过登录验证SessionLogin.aspx才能访问Session.aspx
- 知识蒸馏(Knowledge Distilling)学习笔记
- Detailed explanation of key points in implementing MES system in Enterprises
- Shell implementation of Memcache cache cache hit rate monitoring script
- leetcode:238. 除自身以外数组的乘积
- 【ROS进阶篇】第二讲 自定义头、源文件封装
- After inventing anti-virus software, he chose to be a top-notch gangster
- Summary of document level symbols under different systems
- LeetCode 1. 两数之和
- Topic39——78. subset
猜你喜欢
verilog实现串口通信发送到数码管
verilog实现DDS波形发生器模块,可实现频率、相位可调,三种波形
About Effect Size
Redis (I) -- getting started with redis (1) -- redis introduction, installation and startup, and common configurations
Flame retardant test of aluminum sheet as/nzs 1530.1 non combustible materials
Hardware development notes (VIII): basic process of hardware development, making a USB to RS232 module (VII): creating a basic dip component (crystal oscillator) package and associating the principle
Huawei cloud AOM version 2.0 release
Visual analysis and display effect of summer data
Design of VHDL telephone billing system
Implementation and Simulation of ads131a04 ADC Verilog
随机推荐
Résumé du projet de petite bibliothèque
How do new shareholders open accounts online? Is it safe to open an account online?
Add the applet "lazycodeloading": "requiredcomponents" in taro,
Realization of graduation project topic selection system based on JSP
Rsync method of establishing multi directory module
Change detection and batch update
美国隧道法ASTM E84 表面阻燃测试
About Effect Size
leetcode:307. Area and retrieval - array modifiable
Cloud native database query optimization - statistics and row count estimation
Realize inotify and Rsync real-time backup
阿里巴巴店铺的所有商品API接口(item_search_shop-获得店铺的所有商品接口),阿里巴巴API接口
What is a SYN Flood attack? How to protect?
Verilog implements DDS waveform generator module, which can realize adjustable frequency and phase, three waveforms
Bs-gx-018 student examination system based on SSM
Implementing LDAP proxy service with haproxy + keepalive
Viewing technological changes through Huawei Corps (V): smart Park
Verilog realizes serial communication and sends it to the nixie tube
A. Print a Pedestal (Codeforces logo?)
Motianlun "high availability architecture" dry goods document sharing (including 124 Oracle, MySQL and PG materials)