同時這個還有個bug,就是洗掉不能洗掉第一條錄入的資訊,不知道怎么解決
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct person //定義一個結構體
{
char name[10]; //姓名
char addr[30]; //地址
char offphnum[15]; //辦公電話
char hmphnum[15]; //家庭電話
char mbphum[15]; //移動電話
struct person * next;
}listnode,*listlink; //結構體的別名
struct add_person
{
char name[10]; //姓名
char addr[30]; //地址
char offphnum[15]; //辦公電話
char hmphnum[15]; //家庭電話
char mbphum[15]; //移動電話
};
listlink readfp() //將檔案的資訊讀出并轉存入鏈表中
{
FILE * fp ; //定義檔案指標
struct add_person persons;
listnode * s ;
listlink head = NULL , end = NULL ;
fp = fopen("people.txt" , "rb");
if(fp == NULL)
{
printf("cannot open file \n");
return head;
}
fread(&persons , sizeof(struct add_person) , 1 , fp);
while(!feof(fp)) //判斷檔案是否結束(檔案位置指示器是否到達了檔案結尾)
{
s=(listnode*)malloc(sizeof(listnode)); //存入鏈表中
strcpy(s->name , persons.name) ; //提取資料并復制
strcpy(s->addr , persons.addr) ;
strcpy(s->offphnum , persons.offphnum) ;
strcpy(s->hmphnum , persons.hmphnum) ;
strcpy(s->mbphum , persons.mbphum) ;
s->next = NULL;
if(head==NULL)
head=end=s ;
else
{
end->next=s ;
end=s;
}
fread(&persons , sizeof(struct add_person) , 1 , fp) ;
}
return head;
}
void Save(listlink head) //保存資訊
{
FILE * fp;
static struct add_person persons;
listlink p1;
fp=fopen("people.txt" , "wb");
for(p1=head ; p1!=NULL ; p1=p1->next);
{
strcpy(persons.name , p1->name);
strcpy(persons.addr , p1->addr);
strcpy(persons.hmphnum , p1->hmphnum);
strcpy(persons.mbphum , p1->mbphum);
strcpy(persons.offphnum , p1->offphnum);
fwrite(&persons , sizeof(struct add_person ) , 1 ,fp);
}
fclose(fp);
}
listlink create() //建立通訊錄函式
{
listlink s;
listlink head=NULL , end=NULL;
while(1)
{
s=(listlink)malloc(sizeof(listnode));
printf("\n\n\tcreate add_book's file \n");
printf("\n\n\t\tname:('#' is end)\n\t name:");
scanf("%s" , s->name);
if(strcmp(s->name , "#")==0)
break;
printf("\n\nAddr:\toffphnum: \thmphnum: \tmbphnum:\n");
scanf("%s %s %s %s", s->addr , s->offphnum , s->hmphnum , s->mbphum);
s->next=NULL;
if(head==NULL)
head=end=s;
else
{
end->next=s;
end=s;
}
}
return(head);
}
void Show(listlink head) //顯示所有資訊
{
listnode *p1;
p1=head;
while(p1!=NULL)
{
printf("%s\t", p1->name);
printf("%s\t", p1->addr);
printf("%s\t", p1->offphnum);
printf("%s\t", p1->hmphnum);
printf("%s\t\n", p1->mbphum);
p1=p1->next;
}
}
void Delete(listlink head) //定義一個洗掉的函式
{
listlink p1 , p2;
char name1[10];
p1=p2=head;
if(p1==NULL)
{
printf("no record\n");
return;
}
printf("\n\n\tDelete----please input the name");
scanf("%s" , name1); //輸入要洗掉人的姓名
while(strcmp(p1->name , name1)!=0 && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name , name1)==0) //根據判斷情況做相應處理
{
if(p1==head)
p1=p1->next;
else
p2->next=p1->next;
free(p1);
}
else
printf("It is not exist in the addr-book!\n"); //要洗掉的記錄不存在
}
void Find(listlink head) //查找指定記錄資訊
{
listlink p1;
char name1[10];
while(1)
{
p1=head;
printf("\n\n\tFind----please input the name: ('#' is end)\n\t name:");
scanf("%s", name1); //查找人的姓名
if(strcmp(name1,"#")==0)return;
while(strcmp(name1,p1->name)!=0 && p1->next!=NULL)
p1=p1->next;
if(strcmp(name1,p1->name)!=0)
printf("\n\n\tIt is not exist in the addr-book!");
else
{
printf("\tname:%s",p1->name);
printf("\taddr:%s",p1->addr);
printf("\tphnum:%s",p1->offphnum);
printf("\tphnum:%s",p1->hmphnum);
printf("\tphnum:%s",p1->mbphum);
}
}
}
void Input(listlink head) //向通訊錄追加一個人的資訊
{
listlink s , end;
s=(listlink)malloc(sizeof(listnode));
do
{
printf("\n\n\tInput----please input the sb's meg: \n\tname:('#' is end)\n\t name:");
scanf("%s",s->name);
if(strcmp(s->name,"#")==0)
return;
end=head;
if(end==NULL)break;
while((strcmp(end->name, s->name)!=0) && end->next!=NULL)
end=end->next;
}while(strcmp(end->name , s->name)==0);
printf("\n\nAddr:\toffphnum: \thmphnum: \tmbphnum:\n");
scanf("%s %s %s %s", s->addr , s->offphnum , s->hmphnum , s->mbphum);
s->next=NULL;
end->next=s;
}
void Alter(listlink head) //改變一個人的資訊
{
listlink p1;
char name1[10] , phnum1[15] , phnum2[15] , phnum3[15] , add1[30];
printf("\n\n\tAlter----Please input the sname:");
scanf("%s",name1); //輸入要修改人的姓名
p1=head;
while(strcmp(name1, p1->name)!=0 && p1->next!=NULL)
p1=p1->next;
if(strcmp(name1, p1->name)!=0)
{
printf("\n\n\tIt is not exist in the addr-book!");
return;
}
else
{
printf("\n\n\tPlease input the Alter meg!"); //輸入要修改人的新資訊
printf("\n\n addr: offphnum: hmphnum: mbphnum\n");
scanf("%s %s %s %s", add1 , phnum1 , phnum2 , phnum3);
strcpy(p1->name , name1);
strcpy(p1->addr, add1);
strcpy(p1->offphnum , phnum1);
strcpy(p1->hmphnum , phnum2);
strcpy(p1->mbphum , phnum3);
}
}
int main()
{
listlink head=NULL;
int sel;
head=readfp();
if(head==NULL)head=create();
do
{
printf("\n WELCOME TO USE Address book"); //顯示提示的資訊
printf("\n 1.Show all the meg 2.Delete a piece of meg");
printf("\n 3.Find a piece of meg 4.Insert a piece of meg");
printf("\n 5.Alter a piece of meg");
printf("\n 0.Save and Exit");
printf("\n Input Your Choice(0-5):");
scanf("%d",&sel);
switch(sel)
{
case 1: Show(head);
printf("press anykey to return \n");
getchar();
break;
case 2: Delete(head);
break;
case 3: Find(head);
printf("press anykey to return \n");
getchar();
break;
case 4: Input(head);
break;
case 5: Alter(head);
break;
case 0: Save(head);
break;
}
}while(1);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/179336.html
標籤:C語言
上一篇:如何進行已經溢位的資料運算
