一、通讯录的制作
模块要求:
模块1——主函数main():根据选单的选项调用各函数,并完成相应的功能。
模块2——Menu()的功能是:显示英文提示选单。
模块3——Quit()的功能是:退出选单。
模块4——Create()的功能是:创建新的通讯录。
模块5——Add()的功能是:在通讯录的末尾,写入新的信息,并返回选单。
模块6——Find()的功能是:查询某人的信息,如果找到了,则显示该人的信息,如果未找到,则提示通讯录中没有此人的信息,并返回选单。
模块7——Alter()的功能是:修改某人的信息,如果未找到要修改的人,则提示通讯录中没有此人的信息,并返回选单。
模块8——Delete()的功能是:删除某人的信息,如果未找到要删除的人,则提示通讯录中没有此人的信息,并返回选单。
模块9——List()的功能是:显示通讯录中的所有记录。;
设计要求:
1) 每条信息至包含:姓名(NAME )、性别(GENDER)、电话(TEL)、城市(CITY)邮编(EIP)几项。
2) 作为一个完整的系统,应具有友好的界面和较强的容错能力
二、代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Menu(){
printf("----------Maillist---------------\n");
printf("\t0 Quit \n"); //退出选单
printf("\t1 Create \n"); //创建新的通讯录
printf("\t2 Add \n"); //在通讯录的末尾,写入新的信息,并返回选单
printf("\t3 Find \n"); //查询某人的信息,如果找到了,则显示该人的信息,如果未找到,则提示通讯录中没有此人的信息,并返回选单
printf("\t4 Alter \n"); //修改某人的信息,如果未找到要修改的人,则提示通讯录中没有此人的信息,并返回选单
printf("\t5 Delete \n"); //删除某人的信息,如果未找到要删除的人,则提示通讯录中没有此人的信息,并返回选单
printf("\t6 List \n"); //显示通讯录中的所有记录
printf("-------------------------------------------\n");
}
typedef struct mnode
{
char name[10];
char gender[10];
int tel[20];
char city[10];
int eip[10];
struct mnode *next;
}node;
node *head=NULL;
void Create()
{
node *r,*pt;
int flag;
pt=(node*)malloc(sizeof(node));
if(pt!=NULL)
{
head=pt;
while(1)
{
printf("please input name:");
scanf("%s",&pt->name);
printf("please input gender:");
scanf("%s",&pt->gender);
printf("please input telephone number:");
scanf("%s",&pt->tel);
printf("please input city:");
scanf("%s",&pt->city);
printf("please input eip:");
scanf("%s",&pt->eip);
/*----------------------------------------------*/
r=pt;//使r指向最后一个结点
pt=(node*)malloc(sizeof(node));//申请下一个要用的空间
if(pt!=NULL)
r->next=pt;//尾插法
printf("continue? 1、yes 2、no \n");
scanf("%d",&flag);
if(flag==1)//继续,给pt写入数据
continue;
else//退出
{
printf("createed successfully!\n");
r->next=NULL;
free(pt);//将申请的的无用内存释放
break;
}
}
}
}
void save()//保存数据
{
node *pt;
pt=head;
FILE *fp;
fp=fopen("E:\\Mymaillist.txt","w");
fprintf(fp,"name\tgender\tphone number\tcity\teip\t\n");
while(pt!=NULL)
{
fprintf(fp,"%s\t%s\t%s\t%s\t%s\t\n",pt->name,pt->gender,pt->tel,pt->city,pt->eip);
pt=pt->next;
}
fclose(fp);//关闭文件
}
void List()
{
node *pt;
pt=head;
printf("-------------------------------------------------\n");
printf("|name\t|gender\t|phone number\t|city\t|eip\t|\n");
printf("-------------------------------------------------\n");/*打印表格域*/
while (pt!=NULL)
{
printf("|%s\t|%s\t|%s\t|%s\t|%s\t|\n",pt->name,pt->gender,pt->tel,pt->city,pt->eip);
printf("-------------------------------------------------\n");/*打印表格域*/
pt=pt->next;
}
printf("listed successfully!\n");
}
void Add()
{
node *p, *pt;
pt=(node*)malloc(sizeof(node));
printf("please input name:");
scanf("%s",&pt->name);
printf("please input gender:");
scanf("%s",&pt->gender);
printf("please input telephone number:");
scanf("%s",&pt->tel);
printf("please input city:");
scanf("%s",&pt->city);
printf("please input eip:");
scanf("%s",&pt->eip);
pt->next=head->next;//头插法
head->next=pt;
pt=NULL;
printf("added successfully!\n");
}
void Find()
{
node *pt;
pt=head;
char name[10];
printf("please input the name of the person you want to find:");
scanf("%s",&name);
while(pt!=NULL)
{
if(!strcmp(pt->name, name))
{
printf("-------------------------------------------------\n");
printf("|name\t|gender\t|phone number\t|city\t|eip\t|\n");
printf("-------------------------------------------------\n");
printf("|%s\t|%s\t|%s\t|%s\t|%s\t|\n",pt->name,pt->gender,pt->tel,pt->city,pt->eip);
printf("-------------------------------------------------\n");
printf("finded successfully!\n");
break;
}
pt=pt->next;
}
if(pt==NULL)
{
printf("not exist!\n");
Menu();
}
}
void Alter()
{
node *pt;
pt=head;
char name[10];
printf("please input the name of the person you want to alter:");
scanf("%s",&name);
while(pt!=NULL)
{
if(!strcmp(pt->name, name))
{
int num;
printf("please select the information to be altered\n");
printf("\t0 Quit \n"); //退出选单
printf("\t1 name \n");
printf("\t2 gender \n");
printf("\t3 telephone numbe \n");
printf("\t4 city \n");
printf("\t5 eip \n");
printf("-------------------------------------------------\n");
printf("|name\t|gender\t|phone number\t|city\t|eip\t|\n");
printf("-------------------------------------------------\n");
printf("|%s\t|%s\t|%s\t|%s\t|%s\t|\n",pt->name,pt->gender,pt->tel,pt->city,pt->eip);
printf("-------------------------------------------------\n");
printf("choose(0-5):" );
scanf("%d",&num);
switch(num)
{
case 1:
printf("please input altered name:");
scanf("%s",&pt->name);
break;
case 2:
printf("please input altered gender:");
scanf("%s",&pt->gender);
break;
case 3:
printf("please input altered telephone number:");
scanf("%s",&pt->tel);
break;
case 4:
printf("please input altered city:");
scanf("%s",&pt->city);
break;
case 5:
printf("please input altered eip:");
scanf("%s",&pt->eip);
break;
case 0:
exit(0);
}
printf("altered successfully!\n");
break;
}
pt=pt->next;
}
if(pt==NULL)
{
printf("not exist!\n");
Menu();
}
}
void Delete()
{
node *pre, *pt;
pt=head;
char name[10];
printf("please input the name of the person you want to delete:");
scanf("%s",&name);
if(head==NULL)//通讯录已经没数据了
{
printf("not exist!\n");
Menu();
return;
}
if(!strcmp(name,pt->name))//头指针就是要删除的,为什么要单独拿出来,就是为了可以在删除后改变头指针
{
printf("-------------------------------------------------\n");
printf("|name\t|gender\t|phone number\t|city\t|eip\t|\n");
printf("-------------------------------------------------\n");
printf("|%s\t|%s\t|%s\t|%s\t|%s\t|\n",pt->name,pt->gender,pt->tel,pt->city,pt->eip);
printf("-------------------------------------------------\n");
head=pt->next;
printf("deleted successfully!\n");
return;
}
while(pt!=NULL&&strcmp(name,pt->name))
{
pre=pt;
pt=pt->next;
}
if(pt==NULL)//查找完了,没找到
{
printf("not exist!\n");
Menu();
return;
}
}
int main()
{
int n;
Menu();
while(1)
{
printf("choose(0-6):");
scanf("%d",&n);
if(n<0||n>6)
{
printf("Input error,please again!");
continue;
}
switch(n)
{
case 1: Create();break;
case 2: Add();Menu();break;
case 3: Find();break;
case 4: Alter();break;
case 5: Delete();break;
case 6: List();break;
case 0: save();exit(0);
}
}
}