#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
struct Record
{
int ID;
char name[20];
char phoneNum[11];
int team;
};
struct Record Records[300];
int num=0;
void AddNew();
int IsLoop();
void Display();
void ShowMenu();
void Search_name();
void Search_phoneNum();
void Search_team();
void ChkData(char name[],char phoneNum[],int team);
void Record_Modify();
void DeleteMenu();
void DeleteAll();
void DeleteOne();
void ReadFromFile();
void MainMenu();
void WriteToFile();
void main()
{
ReadFromFile();
while (1)
{
MainMenu();
WriteToFile();
}
}
//返回下标
int Record_SearchByName(char name[])
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(Records[i].name,name)==0)
{
return i;
}
}
return -1;
}
//显示一条信息
void Record_DisplaySingle(int index)
{
printf("\n%s%s%s\n\n","姓名\t","号码\t","组名\t");
printf("--------------------------------------------------------------------------\n");
printf("%-6s%5s%7d\n",Records[index].name,Records[index].phoneNum,Records[index].team);
}
//主菜单
void MainMenu()
{
printf("------------------------主菜单-----------------------------\n");
int option;
printf("1-添加新记录\n");
printf("2-查看\n");
printf("3-修改\n");
printf("4-删除\n");
printf("5-退出程序\n");
printf("请选择:");
scanf("%d",&option);
getchar();
switch (option)
{
case 1:AddNew();break;
case 2:ShowMenu();break;
case 3:Record_Modify();break;
case 4:DeleteMenu();break;
case 5:exit(0);
}
}
//添加记录
void AddNew()
{
printf("------------------------添加新记录-----------------------------\n");
printf("输入姓名:");
scanf("%s",&Records[num].name);
getchar();
printf("输入电话号码:");
scanf("%s",&Records[num].phoneNum);
getchar();
printf("选择分组(1-家庭 2-朋友 3-其他):");
scanf("%d",&Records[num].team);
getchar();
ChkData(Records[num].name,Records[num].phoneNum,Records[num].team);
printf("你所输入的是:\n姓名:%s
电话号码:%s
",Records[num].name,Records[num].phoneNum);
printf("分组:");
if (Records[num].team==1)
printf("家庭");
if (Records[num].team==2)
printf("朋友");
if (Records[num].team==3)
printf("其他");
printf("\n是否存储?(Y/N):");
if (getchar()=='y' || getchar()=='Y')
{
num++;
getchar();
}
}
//查询菜单
void ShowMenu()
{
printf("------------------------查询菜单-----------------------------\n");
int option;
printf("1-查看所有\n2-按姓名查询\n3-按号码查询\n4-按分组查询\n5-返回主菜单\n请选择:");
scanf("%d",&option);
getchar();
switch (option)
{
case 1:
Display();ShowMenu();
case 2:
Search_name();ShowMenu();
case 3:
Search_phoneNum();ShowMenu();
case 4:
Search_team();ShowMenu();
case 5:
MainMenu();
default:
ShowMenu();
}
}
//显示所有信息
void Display()
{
int i;
printf("姓名
电话号码
分组\n--------------------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
printf("%-20s
%-11s",Records[i].name,Records[i].phoneNum);
if (Records[i].team==1)
printf("
%-5s\n","家庭");
if (Records[i].team==2)
printf("
%-5s\n","朋友");
if (Records[i].team==3)
printf("
%-5s\n","其他");
}
}
//按姓名查找
void Search_name()
{
printf("------------------------按姓名查找-----------------------------\n");
char name[20];
int i;
printf("请输入姓名:");
scanf("%s",name);
if (strlen(name)>20)
{
printf("输入的姓名不能超过20个字符!\n");
getchar();
ShowMenu();
}
printf("姓名
电话号码
分组\n--------------------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
if (strcmp(Records[i].name,name)==0)
{
printf("%-20s
%-11s
",Records[i].name,Records[i].phoneNum);
if (Records[i].team==1)
printf("
%-5s\n","家庭");
if (Records[i].team==2)
printf("
%-5s\n","朋友");
if (Records[i].team==3)
printf("
%-5s\n","其他");
}
}
}
//按电话号码查找
void Search_phoneNum()
{
char number[11];
int i;
printf("请输入电话号码:");
scanf("%s",number);
printf("姓名
电话号码
分组\n--------------------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
if (strcmp(Records[i].phoneNum,number)==0)
{
printf("%-20s
%-11s
",Records[i].name,Records[i].phoneNum);
if (Records[i].team==1)
printf("
%-5s\n","家庭");
if (Records[i].team==2)
printf("
%-5s\n","朋友");
if (Records[i].team==3)
printf("
%-5s\n","其他");
}
}
}
//按分组查找
void Search_team()
{
int op,i;
printf("选择分组(1-家庭 2-朋友 3-其他):");
scanf("%d",&op);
printf("姓名
电话号码
分组\n--------------------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
if (Records[i].team==op)
{
printf("%-20s
%-11s
",Records[i].name,Records[i].phoneNum);
if (Records[i].team==1)
printf("
%-5s\n","家庭");
if (Records[i].team==2)
printf("
%-5s\n","朋友");
if (Records[i].team==3)
printf("
%-5s\n","其他");
}
}
}
//修改记录
void Record_Modify()
{
char name[20];
int index;
printf("--------------------------修改信息----------------------------");
printf("\n请输入要修改的姓名:");
scanf("%s",&name);
if(strlen(name)>20)
{
printf("很抱歉!你输入的姓名长度太长了!\n");
if (IsLoop())
{
Record_Modify();
}
else
return;
}
getchar();
index=Record_SearchByName(name);
if (index==-1)
{
printf("\n该姓名不存在!\n");
}
else
{
printf("你要修改的信息为:\n");
Record_DisplaySingle(index);
printf("\n-----------------------------请输入新值--------------------------\n");
printf("\n请输入姓名:");
scanf("%s",&Records[index].name);
if(strlen(Records[index].name)>20)
{
printf("很抱歉!你输入的姓名长度太长了!\n");
if (IsLoop())
{
Record_Modify();
}
else
return;
}
getchar();
printf("\n请输入电话号码(7--11位):");
scanf("%s",&Records[index].phoneNum);
if(strlen(Records[index].phoneNum)>11||strlen(Records[index].phoneNum)<7)
{
printf("很抱歉!你输入的不是合法的电话号码!\n");
if (IsLoop())
{
Record_Modify();
}
else
return;
}
getchar();
printf("\n选择分组(1-家庭 2-朋友 3-其他):");
scanf("%d",&Records[index].team);
getchar();
}
if (IsLoop())
{
Record_Modify();
}
else
return;
}
//数据检测
void ChkData(char name[],char phoneNum[],int team)
{
if (strlen(name)>20)
{
printf("姓名的字符不能超过20个!\n");
MainMenu();
}
if (strlen(phoneNum)>11)
{
printf("电话号码不能超过11位!\n");
MainMenu();
}
else
{
double phoneNumLen;
phoneNumLen=strlen(phoneNum);
for (int i=0;i<phoneNumLen;i++)
{
if (isdigit(phoneNum[i])==0)
{
printf("电话号码应为数字!\n");
MainMenu();
}
}
}
if (team>3 || team<1)
{
printf("分组选择错误!\n");
MainMenu();
}
}
//删除菜单
void DeleteMenu()
{
printf("--------------------------删除菜单--------------------------\n");
printf("1-删除单条信息\n2-删除所有\n3-返回主菜单\n请输入:");
int op;
scanf("%d",&op);
switch (op)
{
case 1:
DeleteOne();break;
case 2:
DeleteAll();break;
case 3:
MainMenu();break;
default:
DeleteMenu();break;
}
}
//删除所有
void DeleteAll()
{
printf("是否删除?(Y/N)");
if (getchar()=='Y' || getchar()=='y')
{
printf("***********************删除所有******************\n");
system("copy none phone.txt");
printf("\r已经删除\n");
main();
}
else
return;
}
//删除一条信息
void DeleteOne()
{
int i;
char name[20];
int index;
printf("\n请输入要删除的姓名:");
scanf("%s",&name);
if(strlen(name)>20)
{
printf("很抱歉!你输入的姓名长度太长了!\n");
if (IsLoop())
{
DeleteMenu();
}
else
return;
}
getchar();
index=Record_SearchByName(name);
if (index==-1)
{
printf("\n该姓名不存在!\n");
}
else
{
printf("\n你要删除的记录为:\n");
Record_DisplaySingle(index);
printf("\n是否真的要删除?(y/n)\n");
char p;
scanf("\n%c",&p);
if (p=='Y'||p=='y')
{
for (i=index;i<num-1;i++)
{
Records[i]=Records[i+1];
}
num--;
}
getchar();
}
if (IsLoop())
{
DeleteMenu();
}
else
return;
}
//循环输入
int IsLoop()
{
printf("是否继续?(Y/N):");
if (getchar()=='y' || getchar()=='Y')
{
return 1;
}
else
return 0;
}
//从文件读取记录
void ReadFromFile()
{
FILE *fp;
int i;
if ((fp=fopen("phone.txt","rb"))==NULL)
{
printf("电话簿不存在,已重新建立!\n");
return;
}
if (fread(&num,sizeof(int),1,fp)!=1)
{
num=-1;
}
else
{
for(i=0;i<num;i++)
{
fread(&Records[i],sizeof(struct Record),1,fp);
}
}
fclose(fp);
}
//将记录写如文件
void WriteToFile()
{
FILE *fp;
int i;
if ((fp=fopen("phone.txt","wb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if (fwrite(&num,sizeof(int),1,fp)!=1)
{
printf("写入文件错误!\n");
}
for (i=0;i<num;i++)
{
if (fwrite(&Records[i],sizeof(struct Record),1,fp)!=1)
{
printf("写入文件错误!\n");
}
}
fclose(fp);
}
自己该 一 下