#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct record
{
char name[20];
char phone[20];
char address[40];
char number[30];
char sex[20];
}telebook[500];
int n=0;
FILE *fp;
void mainmenu();
void addrecord();
void showallmenu();
void searchmenu();
void searchbyname();
void searchbyphone();
void modifymenu();
void deletemenu();
void deleteall();
void deletesingle();
void deletebyname();
void deletebyphone();
void readfile();
void savefile();
void main()
{printf("\n\t\t :)欢迎使用通讯录管理系统");
system("cls");
readfile();
while (1)
{
mainmenu();
}
}
void mainmenu()
{
char choice;
system("cls");
printf("\n\t\t*******************主菜单****************");
printf("\n\t\t*****************1-新建联系人*************"); printf("\n\t\t*****************2-显示菜单***************"); printf("\n\t\t*****************3-查找菜单***************"); printf("\n\t\t*****************4-修改菜单 **************"); printf("\n\t\t*****************5-删除菜单 **************"); printf("\n\t\t*****************6-退出 *****************");
printf("\n\t\t*****************************************"); printf("\n\t\t请选择你的操作:");
choice=getchar();
switch (choice)
{
case '1':addrecord();break;
case '2':browsemenu();break;
case '3':searchmenu();;break;
case '4':modifymenu();break;
case '5':deletemenu();break;
case '6':exit(0);
default:mainmenu();
}
}
void addrecord()
{
printf("\n\t\t**************** 请输入联系人的信息***********\n"); printf("\n\t\t 请输入联系人姓名:");
scanf("%s",&telebook[n].name);
printf("\n\t\t请输入联系人电话:");
scanf("%s",&telebook[n].phone);
printf("\n\t\t请输入联系人地址:");
scanf("%s",&telebook[n].address);
printf("\n\t\t请输入联系人编号:");
scanf("%s",&telebook[n].number);
Printf("\n\t\t请输入联系人性别:");
scanf("%s",&telebook[n].sex);
n++;
savefile();
printf("\n\t\t是否继续添加?(y/n):");
if (getchar()=='y')
addrecord();
return;
}
void browsemenu()
{
int i;
system("cls");
if(n!=0)
{
printf("\n\t\t*************** 以下为通讯录所有信息************"); for (i=0;i<n;i++)
{
printf("\n\t\t姓名: %s",telebook[i].name);
printf("\n\t\t电话: %s",telebook[i].phone);
printf("\n\t\t地址: %s",telebook[i].address);
printf("\n\t\t 编号: %s",telebook[i].number);
printf("\n\t\t 性别: %s",telebook[i].sex);
if (i<n-1)
{
printf("\n\t\t__________________________");
system("pause");
}
}
printf("\n\t\t************************************************"); }
else
printf("\n\t\t通讯录中无任何纪录");
printf("\n\t\t按任意键返回主菜单:");
getchar();
return;
}
void searchmenu()
{
char choice;
system("cls");
printf("\n\t\t*****************查找菜单 *******************"); printf("\n\t\t***************** 1-按姓名查找 *****************"); printf("\n\t\t***************** 2-按电话查找 *****************"); printf("\n\t\t***************** 3-返回主菜单 *****************"); printf("\n\t\t************************************************"); printf("\n\t\t请选择你的操作:");
choice=getchar();
switch (choice)
{
case '1':searchbyname();break;
case '2':searchbyphone();break;
case '3':mainmenu();break;
}
}
void searchname()
{
int mark=0;
int i;
char name[20];
printf("\n\t\t***************** 按姓名查找 *******************"); printf("\n\t\t请输入姓名:");
scanf("%s",name);
for(i=0;i<n;i++)
{
if (strcmp(telebook[i].name,name)==0)
{
printf("\n\t\t************ 以下是您查找的联系人信息 **********");
printf ("\n\t\t姓名:%s",telebook[i].name);
printf("\n\t\t 电话:%s",telebook[i].phone);
printf("\n\t\t 地址:%s",telebook[i].address);
printf("\n\t\t 编号:%s",telebook[i].number);
printf(“\n\t\t 性别:%s”,telebook[i].sex);
printf("\n\t\t************************************************");
mark++;
printf("\n\t\t按任意键返回主菜单");
getchar();
return;
}
}
if(mark==0)
{printf("\n\t\t没有该姓名的联系人纪录");
printf("\n\t\t按任意键返回主菜单");
getchar();
return;
}
}
void searchbyphone()
{
int mark=0;
int i;
char phone[10];
printf("\n\t\t****************** 按电话查找 ******************"); printf("\n\t\t请输入电话:");
scanf("%s",phone);
for(i=0;i<n;i++)
{
if (strcmp(telebook[i].phone,phone)==0)
{
printf("\n\t\t************* 以下是您查找的联系人信息 *********");
printf ("\n\t\t姓名: %s",telebook[i].name);
printf("\n\t\t电话: %s",telebook[i].phone);
printf("\n\t\t地址: %s",telebook[i].address);
printf("\n\t\t 编号:%s",telebook[i].number);
printf("\n\t\t 性别:%s",telebook[i].sex);
printf("\n\t\t************************************************");
mark++;
printf ("\n\t\t按任意键返回主菜单");
getchar();
return;
}
}
if (mark==0)
{
printf("\n\t\t没有该联系人信息");
printf("\n\t\t按任意键返回主菜单");
getchar();
return;
}
}
void modifymenu()
{ int mark=0;
int i,m;
int t=0;
char choice;
char name[20];
struct record modify;
system("cls");
printf("\n\t\t请输入您要修改的姓名:");
scanf("%s",name);
for(i=0;i<n;i++)
{
if (strcmp(telebook[i].name,name)==0)
{ m=i;mark=1;
modify=telebook[i];
printf("\n\t\t********** 以下是您将修改的联系人信息 **********");
printf ("\n\t\t姓名: %s",telebook[i].name);
printf("\n\t\t电话: %s",telebook[i].phone);
printf("\n\t\t地址: %s",telebook[i].address);
printf("\n\t\t 编号: %s",telebook[i].number);
printf("\n\t\t性别: %s",telebook[i].sex);
printf("\n\t\t是否继续?y/n");
if(getchar()=='n')
modifymenu();
else
{printf("\n\t\t选择您要修改的选项:");
printf("\n\t\t\t1.姓名");
printf("\n\t\t\t2.电话");
printf("\n\t\t\t3.地址");
printf("\n\t\t\t4.编号");
printf("\n\t\t\t5.性别");
printf("\n\t\t\t6.返回");
printf("\n\t\t请选择你的操作: ");
choice=getchar();
switch(choice)
{ case '1': printf("姓名:"); scanf("%s",telebook[i].name); break;
case '2': printf("电话:"); scanf("%s",telebook[i].phone); break;
case '3': printf("地址:"); scanf("%s",telebook[i].address); break;
case '4': printf("编号:"); scanf("%s",telebook[i].number); break;
case '5': printf("性别:"); scanf("%s",telebook[i]sex); break;
case '6': mainmenu(); break;
default: modifymenu();
}
printf("\n\t\t您的修改结果为:");
printf("\n\t\t姓名: %s",telebook[i].name);
printf("\n\t\t电话: %s",telebook[i].phone);
printf("\n\t\t地址: %s",telebook[i].address);
printf("\n\t\t 编号: %s",telebook[i].number);
printf("\n\t\t 性别: %s",telebook[i].sex);
printf("\n\t\t确认修改?y/n");
if(getchar()=='n')
{telebook[i]=modify;
printf("\n\t\t取消修改!");
}
else printf("\n\t\t修改成功!");
savefile();
mark=0;
printf("\n\t\t是否继续修改?(y/n)");
if (getchar()=='y')
modifymenu();
}
}
}
if (mark==0)
{
printf("\n\t\t没有该联系人的纪录");
printf("\n\t\t是否继续修改?(y/n)");
if (getchar()=='y')
modifymenu();
}
return;
}
void deletemenu()
{
char choice;
if(n==0)
{
printf("\n\t\t对不起,文件中无任何纪录");
printf("\n\t\t按任意键返回主菜单");
getchar();
return;
}
system("cls");
printf("\n\t\t******************* 删除菜单 *******************");
printf ("\n\t\t****************** 1-删除所有 ******************");
printf ("\n\t\t****************** 2-删除单个 ******************");
printf("\n\t\t***************** 3-返回主菜单 *****************");
printf ("\n\t\t************************************************")
printf ("\n\t\t请选择你的操作:");
choice=getchar();
switch(choice)
{
case '1':deleteall();break;
case '2':deletesingle();break;
case '3':mainmenu();break;
}
}
void deleteall()
{
printf("\n\t\t确认删除?(y/n)");
if (getchar()=='y')
{
if ((fp=fopen("telebook","w"))==0)
{
printf("\n\t\t不能打开文件,删除失败");
readfile();
}
n=0;
printf("\n\t\t已删除全部联系人,按任意键返回主菜单");
getchar();
fclose(fp);
return;
}
else
return;
}
void deletesingle()
{
char choice;
printf("\n\t\t\t1.按姓名删除");
printf("\n\t\t\t2.按号码删除");
printf("\n\t\t\t3.返回删除菜单");
printf("\n\t\t请选择你的操作:");
choice=getchar();
switch (choice)
{
case '1':deletebyname();break;
case '2':deletebyphone();break;
case '3':deletemenu();break;
}
}
void deletebyname()
{
int i,j;
int mark=0;
char name[20];
printf("\n\t\t请输入要删除联系人姓名:");
scanf("%s",name);
if(n==0)
{
printf("\n\t\t对不起,文件中无联系人");
printf("\n\t\t按任意键返回主菜单");
getchar();
return;
}
for (i=0;i<n;i++)
{
if (strcmp(telebook[i].name,name)==0)
{
printf("\n\t\t以下是您要删除的联系人记录:");
printf("\n\t\t姓名: %s",telebook[i].name);
printf("\n\t\t电话: %s",telebook[i].phone);
printf("\n\t\t地址: %s",telebook[i].address);
printf("\n\t\t 编号: %s",telebook[i].number);
printf("\n\t\t 性别: %s",telebook[i].sex);
printf("\n\t\t是否删除?(y/n)");
if (getchar()=='y')
{
for (j=i;j<n-1;j++)
telebook[j]=telebook[j+1];
n--;
mark++;
printf("\n\t\t删除成功");
savefile();
printf("\n\t\t是否继续删除?(y/n)");
if (getchar()=='y')
deletesingle();
return;
}
else
return;
}
if (mark==0)
{
printf("\n\t\t+_+没有该联系人的纪录");
printf("\n\t\t是否继续删除?(y/n)");
if (getchar()=='y')
deletesingle();
return;
}
}
void deletebyphone()
Int i,j
if(n==0)
{
printf("\n\t\t对不起,文件中无任何纪录");
printf("\n\t\t按任意键返回主菜单");
getchar();
return;
}
for (i=0;i<n;i++)
{
if (strcmp(telebook[i].phone,phone)==0)
{
printf("\n\t\t以下是您要删除的联系人纪录:");
printf("\n\t\t姓名: %s",telebook[i].name);
printf("\n\t\t电话: %s",telebook[i].phone);
printf("\n\t\t地址: %s",telebook[i].address);
printf("\n\t\t 编号: %s",telebook[i].number);
printf("\n\t\t 性别: %s",telebook[i].sex);
printf("\n\t\t是否删除?(y/n)");
if (getchar()=='y')
{
for (j=i;j<n-1;j++)
telebook[j]=telebook[j+1];
n--;
mark++;
printf("\n\t\t删除成功");
savefile();
printf("\n\t\t是否继续删除?(y/n)");
if (getchar()=='y')
deletesingle();
return;
}
else
return;
}
continue;
}
if (mark==0)
{
printf("\n\t\t+_+没有该联系人的纪录");
printf("\n\t\t是否继续删除?(y/n)");
if (getchar()=='y')
deletesingle();
return;
}
}
void readfile()
{
int n=0;
FILE *fp;
if((fp=fopen("telebook.txt","rb"))==0)
{
printf("\n\n\n\n\n\t\t\电话簿文件不存在");
if ((fp=fopen("telebook.txt","wb"))==0)
{
printf("\n\t\t\t\t建立失败");
exit(0);
}
else
{
printf("\n\t\t\t电话簿已建立");
printf("\n\t\t\t 按任意键进入主菜单");
getchar();
return();
}
exit(0);
}
fseek(fp,0,2);
if (ftell(fp)>0)
{
rewind(fp);
for (n=0;!feof(fp) && fread(&telebook[n],sizeof(struct record),1,fp);n++); printf("\n\n\n\n\t\t\t\t :)文件导入成功");
printf("\n\t\t\t\t 按任意键返回主菜单");
getchar();
return(n);
}
printf("\n\n\n\n\t\t\t :)文件导入成功");
printf("\n\t\t\t电话簿文件中无任何纪录");
printf("\n\t\t\t 按任意键返回主菜单");
getchar();
return();
}
void savefile()
{
int i;
if ((fp=fopen("student.txt","wb"))==0
{
printf("\n\t\t文件打开失败!\n");
}
for (i=0;i<n;i++)
{
if (fwrite(&telebook[i],sizeof(struct record),1,fp)!=1)
{
printf("\n\t\t写入文件错误!\n");
}
}
printf("\n\t\t^_^已成功保存至文件!\n");
fclose(fp);
}