跪求一个C语言综合设计---编写一个通讯录管理程序,包含序号,姓名,电话,地址,
并且实现以下功能:新纪录的录入,删除和修改任何一条记录,可以根据姓名,电话或地址查询相关信息,系统的退出.设计必须含有主菜单,主控程序模块设计、并体现程序的交互性(必须有...
并且实现以下功能:新纪录的录入,删除和修改任何一条记录,可以根据姓名,电话或地址查询相关信息,系统的退出.设计必须含有主菜单,主控程序模块设计、并体现程序的交互性(必须有输入/输出)、模块化程序思想。拜托各位高手尽快替小弟解决哈,嘻嘻
展开
1个回答
展开全部
struct persons
{
char name[16];
char sex[6];
char age[3];
char bir[5];
char phnum[18];
char addr[20];
}persons[100];
/**********************************************************************************/
typedef struct lnode
{
char name[16]; /*姓名*/
char sex[6]; /*性别:以man代表男性,woman代表女性*/
char age[3]; /*年龄*/
char bir[5]; /*生日,其中前两位数字代表月份,后两位数字代表日期*/
char phnum[18]; /*电话*/
char addr[20]; /*地址*/
struct lnode *next;
}listnode,*linklist;
/*********************************************************************************/
linklist head=NULL,r=NULL;
listnode *s,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9;
int i;
char name1[16],phnum1[18],addr1[20],ch,a,b;
char s1[20];
FILE *fp;
//以上为stuct.h
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include"struct.h"
/************************************************************************/
void creat() /*将文件的信息读入结构体数组,再转存入链表中*/
{
int j;
long k;
fp=fopen("TXL.txt","r+"); /*如果这个文件存在,那么打开这个文件*/
if(fp!=NULL)
{
for(i=1;i<100;i++)
{
j=fgetc(fp);
if(j==EOF)
return;
k=i-1;
fseek(fp,k*sizeof(struct persons),0); /*读取一个人的信息*/
fread(&persons[i],sizeof(struct persons),1,fp);
s=(linklist)malloc(sizeof(listnode)); /*存到链表中*/
strcpy(s->name,persons[i].name);
strcpy(s->sex,persons[i].sex);
strcpy(s->age,persons[i].age);
strcpy(s->bir,persons[i].bir);
strcpy(s->phnum,persons[i].phnum);
strcpy(s->addr,persons[i].addr);
if(head==NULL)
head=s;
else
r->next=s;
r=s;}
}
else
{ fp=fopen("TXL.txt","w+");/*如果这个文件不存在,那么为读写创建一个默认文件TXL.txt*/
i=1;
}
}
/************************************************************************/
void Show()/*显示默认文件里面的所有信息*/
{ p1=head;
if(p1==NULL)
{ printf("\n\n\t\t There is nothing in the addr-book!");
printf("\n\n\t\t Please insert!");
}/*如果文件为空,则给出提示*/
while(p1!=NULL)
{
printf("\n\tName:%s ",p1->name);
printf("Sex:%s ",p1->sex);
printf("Age:%s ",p1->age);
printf("Bir:%s ",p1->bir);
printf("Phnum:%s ",p1->phnum);
printf("Addr:%s\n",p1->addr);
p1=p1->next;
}
}
/*****************************************************************/
void Delete_1() /*定义按姓名删除的函数*/
{
void menu();
printf("\n\n\t\tPlease input the name:");
gets(name1); /*输入要删除人的姓名*/
p4=head;
while(strcmp(name1,p4->name)!=0&&p4!=NULL)
p4=p4->next;
if(p4==NULL)
{
printf("\n\n\t\tIt is not found in the TXL!");
menu();
}/*如果找不到该人,则返回主菜单*/
else
{ printf("\t\tThe information have been deleted is that:");
printf("\n\t\tname:%s ",p4->name);
printf("sex:%s ",p4->sex);
printf("age:%s ",p4->age);
printf("bir:%s ",p4->bir);
printf("phnum:%s ",p4->phnum);
printf("addr:%s\n",p4->addr);
}
p4=head;
if(strcmp(p4->name,name1)==0)
{
p4=p4->next;
head=p4;
}
else
{
while(strcmp(p4->next->name,name1)!=0)
p4=p4->next;
p5=p4->next;
p4->next=p5->next;
free(p5);
}
}
/*****************************************************************/
void Delete_2() /*定义按电话号码删除的函数*/
{
void menu();
printf("\n\n\t\tPlease input the phnum:");
gets(phnum1); /*输入要删除的人的电话号码*/
p6=head;
while(strcmp(phnum1,p6->phnum)!=0&&p6!=NULL)
p6=p6->next;
if(p6==NULL)
{
printf("\n\n\t\tIt is not found in the TXL!");
menu();
}/*如果找不到该电话号码,则返回主菜单*/
else
{ printf("\t\tThe information have been deleted is that:");
printf("\n\t\tname:%s ",p6->name);
printf("sex:%s ",p6->sex);
printf("age:%s ",p6->age);
printf("bir:%s ",p6->bir);
printf("phnum:%s ",p6->phnum);
printf("addr:%s\n",p6->addr);
}
p6=head;
if(strcmp(p6->phnum,phnum1)==0)
{
p6=p6->next;
head=p6;
}
else
{
while(strcmp(p6->next->phnum,phnum1)!=0)
p6=p6->next;
p7=p6->next;
p6->next=p7->next;
free(p7);
}
}
/******************************************************************/
void Delete()/*在Delete()函数中按需要选择用不同的删除方式*/
{
printf("\n\t\tPlease make a choice below:\n");
printf("\t\t1.Delete by name.\n");
printf("\t\t2.Delete by phnum.\n");
printf("\t\tPlease insert your choice:");
a=getche();
switch(a)
{
case '1':Delete_1();
break;
case '2':Delete_2();
break;
default:
printf("\n\t\t**********************************\n");
printf("\n\t\t The number should 1-2!!! \n");
printf("\n\t\t**********************************");
break;
}
}
/*****************************************************************/
void Search_1() /*按姓名查找的函数定义*/
{
printf("\n\n\t\tPlease input the name:");
p8=head;
gets(name1); /*输入要查找的人的姓名*/
while(strcmp(name1,p8->name)!=0&&p8!=NULL)
p8=p8->next;
if(p8==NULL)
printf("\n\n\t\tIt is not found in the TXL!");
else
{
printf("\n\t\tname:%s ",p8->name);
printf("sex:%s ",p8->sex);
printf("age:%s ",p8->age);
printf("bir:%s ",p8->bir);
printf("phnum:%s ",p8->phnum);
printf("addr:%s\n",p8->addr);
}
}
/****************************************************************/
void Search_2()/*按电话号码查找的函数定义*/
{
printf("\n\n\t\tPlease input the phnum:");
p9=head;
gets(phnum1);/*输入要查找的人的电话号码*/
while(strcmp(phnum1,p9->phnum)!=0&&p9!=NULL)
p9=p9->next;
if(p9==NULL)
printf("\n\n\t\tIt is not found in the TXL!");
else
{
printf("\n\t\tname:%s ",p9->name);
printf("sex:%s ",p9->sex);
printf("age:%s ",p9->age);
printf("bir:%s ",p9->bir);
printf("phnum:%s ",p9->phnum);
printf("addr:%s\n",p9->addr);
}
}
/***************************************************************/
void Search_3()/*定义按宿舍(地址)查找的函数*/
{
printf("\n\n\t\tPlease input the addr:");
p0=head;
gets(addr1);/*输入要查到的人的宿舍(地址)*/
while(strcmp(addr1,p0->addr)!=0&&p0!=NULL)
p0=p0->next;
if(p0==NULL)
printf("\n\n\t\tIt is not found in the TXL!");
else
{
printf("\n\t\tname:%s ",p0->name);
printf("sex:%s ",p0->sex);
printf("age:%s ",p0->age);
printf("bir:%s ",p0->bir);
printf("phnum:%s ",p0->phnum);
printf("addr:%s\n",p0->addr);
}
}
/*************************************************************************/
void Search()/*在Search中按需要调用不同的查找方式*/
{
printf("\n\t\tPlease make a choice below:\n");
printf("\t\t1.Search by name.\n");
printf("\t\t2.Search by phnum.\n");
printf("\t\t3.Search by addr.\n");
printf("\t\tPlease insert your choice:");
b=getche();
switch(b)
{
case '1':Search_1();
break;
case '2':Search_2();
break;
case '3':Search_3();
break;
default:
printf("\n\t\t**********************************\n");
printf("\n\t\t The number should 1-3!!! \n");
printf("\n\t\t**********************************");
break;
}
}
/************************************************************************/
void Insert() /*向文件插入一组的信息*/
{
s=(linklist)malloc(sizeof(listnode));
printf("\n\t\tPlease insert the someone's information:");
printf("\n\t\tName:");
scanf("%s",s->name);
printf("\t\tSex:");
scanf("%s",s->sex);
printf("\t\tAge:");
scanf("%s",s->age);
printf("\t\tBir:");
scanf("%s",s->bir);
printf("\t\tPhnum:");
scanf("%s",s->phnum);
printf("\t\tAddr:");
scanf("%s",s->addr);
if(head==NULL)
head=s;
else
r->next=s;
r=s;
}
/***********************************************************************/
void Save() /*将信息保存在默认文件里面*/
{ int j;
fp=fopen("TXL.txt","w");
for(p2=head,j=0;p2!=NULL;j++,p2=p2->next)
{
strcpy(persons[j].name,p2->name);
strcpy(persons[j].sex,p2->sex);
strcpy(persons[j].age,p2->age);
strcpy(persons[j].bir,p2->bir);
strcpy(persons[j].phnum,p2->phnum);
strcpy(persons[j].addr,p2->addr);
fwrite(&persons[j],sizeof(struct persons),1,fp);
}
}
/************************************************************************/
void menu ()/*显示主菜单*/
{
do
{
printf("\n\n\n\n\n");
printf("\n\t\t ************************************");
printf("\n\t\t * Please make a choice below: *");
printf("\n\t\t * 1.Show all the information *");
printf("\n\t\t * 2.Delete a piece of information *");
printf("\n\t\t * 3.Search a piece of information *");
printf("\n\t\t * 4.Insert a piece of information *");
printf("\n\t\t * 5.Save and Exit *");
printf("\n\t\t * 6.Exit and Without Save *");
printf("\n\t\t * Input Your Choice: *");
printf("\n\t\t ************************************");
ch=getche();
switch(ch)
{
case '1': Show();
break;
case '2': Delete();
break;
case '3': Search();
break;
case '4': Insert();
break;
case '5': Save();fclose(fp);exit(0);
case '6':fclose(fp);exit(0);
break;
default:
printf("\n\t\t**********************************\n");
printf("\n\t\t The number should 1-6!!! \n");
printf("\n\t\t**********************************");
break;
}
}
while(1);
}
/**********************************************************************/
void main()
{
creat();
menu();
}
{
char name[16];
char sex[6];
char age[3];
char bir[5];
char phnum[18];
char addr[20];
}persons[100];
/**********************************************************************************/
typedef struct lnode
{
char name[16]; /*姓名*/
char sex[6]; /*性别:以man代表男性,woman代表女性*/
char age[3]; /*年龄*/
char bir[5]; /*生日,其中前两位数字代表月份,后两位数字代表日期*/
char phnum[18]; /*电话*/
char addr[20]; /*地址*/
struct lnode *next;
}listnode,*linklist;
/*********************************************************************************/
linklist head=NULL,r=NULL;
listnode *s,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9;
int i;
char name1[16],phnum1[18],addr1[20],ch,a,b;
char s1[20];
FILE *fp;
//以上为stuct.h
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include"struct.h"
/************************************************************************/
void creat() /*将文件的信息读入结构体数组,再转存入链表中*/
{
int j;
long k;
fp=fopen("TXL.txt","r+"); /*如果这个文件存在,那么打开这个文件*/
if(fp!=NULL)
{
for(i=1;i<100;i++)
{
j=fgetc(fp);
if(j==EOF)
return;
k=i-1;
fseek(fp,k*sizeof(struct persons),0); /*读取一个人的信息*/
fread(&persons[i],sizeof(struct persons),1,fp);
s=(linklist)malloc(sizeof(listnode)); /*存到链表中*/
strcpy(s->name,persons[i].name);
strcpy(s->sex,persons[i].sex);
strcpy(s->age,persons[i].age);
strcpy(s->bir,persons[i].bir);
strcpy(s->phnum,persons[i].phnum);
strcpy(s->addr,persons[i].addr);
if(head==NULL)
head=s;
else
r->next=s;
r=s;}
}
else
{ fp=fopen("TXL.txt","w+");/*如果这个文件不存在,那么为读写创建一个默认文件TXL.txt*/
i=1;
}
}
/************************************************************************/
void Show()/*显示默认文件里面的所有信息*/
{ p1=head;
if(p1==NULL)
{ printf("\n\n\t\t There is nothing in the addr-book!");
printf("\n\n\t\t Please insert!");
}/*如果文件为空,则给出提示*/
while(p1!=NULL)
{
printf("\n\tName:%s ",p1->name);
printf("Sex:%s ",p1->sex);
printf("Age:%s ",p1->age);
printf("Bir:%s ",p1->bir);
printf("Phnum:%s ",p1->phnum);
printf("Addr:%s\n",p1->addr);
p1=p1->next;
}
}
/*****************************************************************/
void Delete_1() /*定义按姓名删除的函数*/
{
void menu();
printf("\n\n\t\tPlease input the name:");
gets(name1); /*输入要删除人的姓名*/
p4=head;
while(strcmp(name1,p4->name)!=0&&p4!=NULL)
p4=p4->next;
if(p4==NULL)
{
printf("\n\n\t\tIt is not found in the TXL!");
menu();
}/*如果找不到该人,则返回主菜单*/
else
{ printf("\t\tThe information have been deleted is that:");
printf("\n\t\tname:%s ",p4->name);
printf("sex:%s ",p4->sex);
printf("age:%s ",p4->age);
printf("bir:%s ",p4->bir);
printf("phnum:%s ",p4->phnum);
printf("addr:%s\n",p4->addr);
}
p4=head;
if(strcmp(p4->name,name1)==0)
{
p4=p4->next;
head=p4;
}
else
{
while(strcmp(p4->next->name,name1)!=0)
p4=p4->next;
p5=p4->next;
p4->next=p5->next;
free(p5);
}
}
/*****************************************************************/
void Delete_2() /*定义按电话号码删除的函数*/
{
void menu();
printf("\n\n\t\tPlease input the phnum:");
gets(phnum1); /*输入要删除的人的电话号码*/
p6=head;
while(strcmp(phnum1,p6->phnum)!=0&&p6!=NULL)
p6=p6->next;
if(p6==NULL)
{
printf("\n\n\t\tIt is not found in the TXL!");
menu();
}/*如果找不到该电话号码,则返回主菜单*/
else
{ printf("\t\tThe information have been deleted is that:");
printf("\n\t\tname:%s ",p6->name);
printf("sex:%s ",p6->sex);
printf("age:%s ",p6->age);
printf("bir:%s ",p6->bir);
printf("phnum:%s ",p6->phnum);
printf("addr:%s\n",p6->addr);
}
p6=head;
if(strcmp(p6->phnum,phnum1)==0)
{
p6=p6->next;
head=p6;
}
else
{
while(strcmp(p6->next->phnum,phnum1)!=0)
p6=p6->next;
p7=p6->next;
p6->next=p7->next;
free(p7);
}
}
/******************************************************************/
void Delete()/*在Delete()函数中按需要选择用不同的删除方式*/
{
printf("\n\t\tPlease make a choice below:\n");
printf("\t\t1.Delete by name.\n");
printf("\t\t2.Delete by phnum.\n");
printf("\t\tPlease insert your choice:");
a=getche();
switch(a)
{
case '1':Delete_1();
break;
case '2':Delete_2();
break;
default:
printf("\n\t\t**********************************\n");
printf("\n\t\t The number should 1-2!!! \n");
printf("\n\t\t**********************************");
break;
}
}
/*****************************************************************/
void Search_1() /*按姓名查找的函数定义*/
{
printf("\n\n\t\tPlease input the name:");
p8=head;
gets(name1); /*输入要查找的人的姓名*/
while(strcmp(name1,p8->name)!=0&&p8!=NULL)
p8=p8->next;
if(p8==NULL)
printf("\n\n\t\tIt is not found in the TXL!");
else
{
printf("\n\t\tname:%s ",p8->name);
printf("sex:%s ",p8->sex);
printf("age:%s ",p8->age);
printf("bir:%s ",p8->bir);
printf("phnum:%s ",p8->phnum);
printf("addr:%s\n",p8->addr);
}
}
/****************************************************************/
void Search_2()/*按电话号码查找的函数定义*/
{
printf("\n\n\t\tPlease input the phnum:");
p9=head;
gets(phnum1);/*输入要查找的人的电话号码*/
while(strcmp(phnum1,p9->phnum)!=0&&p9!=NULL)
p9=p9->next;
if(p9==NULL)
printf("\n\n\t\tIt is not found in the TXL!");
else
{
printf("\n\t\tname:%s ",p9->name);
printf("sex:%s ",p9->sex);
printf("age:%s ",p9->age);
printf("bir:%s ",p9->bir);
printf("phnum:%s ",p9->phnum);
printf("addr:%s\n",p9->addr);
}
}
/***************************************************************/
void Search_3()/*定义按宿舍(地址)查找的函数*/
{
printf("\n\n\t\tPlease input the addr:");
p0=head;
gets(addr1);/*输入要查到的人的宿舍(地址)*/
while(strcmp(addr1,p0->addr)!=0&&p0!=NULL)
p0=p0->next;
if(p0==NULL)
printf("\n\n\t\tIt is not found in the TXL!");
else
{
printf("\n\t\tname:%s ",p0->name);
printf("sex:%s ",p0->sex);
printf("age:%s ",p0->age);
printf("bir:%s ",p0->bir);
printf("phnum:%s ",p0->phnum);
printf("addr:%s\n",p0->addr);
}
}
/*************************************************************************/
void Search()/*在Search中按需要调用不同的查找方式*/
{
printf("\n\t\tPlease make a choice below:\n");
printf("\t\t1.Search by name.\n");
printf("\t\t2.Search by phnum.\n");
printf("\t\t3.Search by addr.\n");
printf("\t\tPlease insert your choice:");
b=getche();
switch(b)
{
case '1':Search_1();
break;
case '2':Search_2();
break;
case '3':Search_3();
break;
default:
printf("\n\t\t**********************************\n");
printf("\n\t\t The number should 1-3!!! \n");
printf("\n\t\t**********************************");
break;
}
}
/************************************************************************/
void Insert() /*向文件插入一组的信息*/
{
s=(linklist)malloc(sizeof(listnode));
printf("\n\t\tPlease insert the someone's information:");
printf("\n\t\tName:");
scanf("%s",s->name);
printf("\t\tSex:");
scanf("%s",s->sex);
printf("\t\tAge:");
scanf("%s",s->age);
printf("\t\tBir:");
scanf("%s",s->bir);
printf("\t\tPhnum:");
scanf("%s",s->phnum);
printf("\t\tAddr:");
scanf("%s",s->addr);
if(head==NULL)
head=s;
else
r->next=s;
r=s;
}
/***********************************************************************/
void Save() /*将信息保存在默认文件里面*/
{ int j;
fp=fopen("TXL.txt","w");
for(p2=head,j=0;p2!=NULL;j++,p2=p2->next)
{
strcpy(persons[j].name,p2->name);
strcpy(persons[j].sex,p2->sex);
strcpy(persons[j].age,p2->age);
strcpy(persons[j].bir,p2->bir);
strcpy(persons[j].phnum,p2->phnum);
strcpy(persons[j].addr,p2->addr);
fwrite(&persons[j],sizeof(struct persons),1,fp);
}
}
/************************************************************************/
void menu ()/*显示主菜单*/
{
do
{
printf("\n\n\n\n\n");
printf("\n\t\t ************************************");
printf("\n\t\t * Please make a choice below: *");
printf("\n\t\t * 1.Show all the information *");
printf("\n\t\t * 2.Delete a piece of information *");
printf("\n\t\t * 3.Search a piece of information *");
printf("\n\t\t * 4.Insert a piece of information *");
printf("\n\t\t * 5.Save and Exit *");
printf("\n\t\t * 6.Exit and Without Save *");
printf("\n\t\t * Input Your Choice: *");
printf("\n\t\t ************************************");
ch=getche();
switch(ch)
{
case '1': Show();
break;
case '2': Delete();
break;
case '3': Search();
break;
case '4': Insert();
break;
case '5': Save();fclose(fp);exit(0);
case '6':fclose(fp);exit(0);
break;
default:
printf("\n\t\t**********************************\n");
printf("\n\t\t The number should 1-6!!! \n");
printf("\n\t\t**********************************");
break;
}
}
while(1);
}
/**********************************************************************/
void main()
{
creat();
menu();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询