|
|
#include<stdio.h>
|
|
|
#include<stdlib.h>
|
|
|
#include <string.h>
|
|
|
void caidan(); //主菜单界面函数
|
|
|
void xuanze(); //选择功能函数
|
|
|
void luru(); //录入功能函数:新增信息
|
|
|
void save(int t); //信息录入文本函数:将新增信息保存到文件中
|
|
|
void liulan(); //浏览功能函数:输出所有信息
|
|
|
void chaxun(); //查询功能函数
|
|
|
void chazhao1(); //按名字查找
|
|
|
void chazhao2(); //按电话号码查找
|
|
|
void chazhao3(); //按邮件地址查找
|
|
|
void change();
|
|
|
void xiugai(int n); //修改功能函数
|
|
|
void fanhui(); //返回函数:选择继续操作还是返回主菜单
|
|
|
struct student //定义结构体
|
|
|
{
|
|
|
char name[20]; //姓名
|
|
|
char area[20]; //籍贯
|
|
|
char tel[20]; //电话号码
|
|
|
char email[20]; //邮箱地址
|
|
|
};
|
|
|
struct student a[100]; //定义结构体类型变量
|
|
|
FILE *fp; //文件指针
|
|
|
int i; //全局变量
|
|
|
|
|
|
/*主函数*/
|
|
|
int main(void)
|
|
|
{
|
|
|
xuanze();
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
/*选择功能函数*/
|
|
|
void xuanze()
|
|
|
{
|
|
|
int i;
|
|
|
do
|
|
|
{
|
|
|
system("cls"); //清屏
|
|
|
caidan();
|
|
|
printf(" 请选择对应功能选项:");
|
|
|
scanf("%d",&i);
|
|
|
switch(i)
|
|
|
{
|
|
|
case 1: luru(); break;
|
|
|
case 2: liulan(); break;
|
|
|
case 3: chaxun(); break;
|
|
|
case 4: change(); break;
|
|
|
case 0: printf(" 请按回车键退出\n");exit(0);
|
|
|
default: printf(" 输入错误,请输入正确的选项数字!\n");
|
|
|
}
|
|
|
}while(1);
|
|
|
}
|
|
|
|
|
|
/*主菜单*/
|
|
|
void caidan()
|
|
|
{
|
|
|
printf(" \n");
|
|
|
printf(" 欢迎使用通讯录管理系统\n");
|
|
|
printf(" --------------------------------------\n");
|
|
|
printf(" ***录入信息请按 1***\n");
|
|
|
printf(" ***浏览信息请按 2***\n");
|
|
|
printf(" ***查询信息请按 3***\n");
|
|
|
printf(" ***修改信息请按 4***\n");
|
|
|
printf(" ***退出系统请按 0***\n");
|
|
|
printf(" --------------------------------------\n");
|
|
|
}
|
|
|
|
|
|
/*信息录入*/
|
|
|
void luru()
|
|
|
{
|
|
|
int i=0,n,t;
|
|
|
do
|
|
|
{
|
|
|
printf(" *----------------------------------*\n");
|
|
|
printf(" *----------------------------------*\n");
|
|
|
printf(" 请输入学生姓名:");
|
|
|
fflush(stdin); //清除缓存区
|
|
|
scanf("%s",a[i].name);
|
|
|
printf(" 请输入学生籍贯:");
|
|
|
scanf("%s",a[i].area);
|
|
|
printf(" 请输入学生电话号码:");
|
|
|
scanf("%s",a[i].tel);
|
|
|
printf(" 请输入学生邮箱地址:");
|
|
|
scanf("%s",a[i].email);
|
|
|
i++;
|
|
|
t=i;
|
|
|
printf(" 继续输入请按1,返回菜单请按2:");
|
|
|
scanf("%d",&n);
|
|
|
}while(n==1);
|
|
|
save(t); //将录入信息保存至文本
|
|
|
printf(" 信息录入完毕!\n");
|
|
|
xuanze(); //返回菜单
|
|
|
}
|
|
|
/*将录入的信息保存至文本*/
|
|
|
void save(int t)
|
|
|
{
|
|
|
int i;
|
|
|
if((fp=fopen("shuju.txt","a"))==NULL) //打开文本文件并判断是否正确打开
|
|
|
{
|
|
|
printf("File open error!\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
for(i=0;i<t;i++)
|
|
|
{
|
|
|
fprintf(fp,"%s\t%s\t%s\t%s\t",a[i].name,a[i].area,a[i].tel,a[i].email); //输出列表
|
|
|
}
|
|
|
if(fclose(fp)) //关闭文件
|
|
|
{
|
|
|
printf("关闭失败\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void liulan()
|
|
|
{
|
|
|
int n,j;
|
|
|
fp=fopen("shuju.txt","r"); //打开文件
|
|
|
for(i=0;feof(fp)==0;i++)
|
|
|
fscanf(fp,"%s %s %s %s\n",a[i].name,a[i].area,a[i].tel,a[i].email);
|
|
|
fclose(fp); //关闭文件
|
|
|
printf(" 姓名\t籍贯\t电话号码\t邮箱地址\t\t\n");
|
|
|
for(j=0;j<i;j++)
|
|
|
printf(" %s\t%s\t%s\t%s\t\t\n",a[j].name,a[j].area,a[j].tel,a[j].email); //输出所有信息以浏览
|
|
|
printf(" -----------------------------------------\n");
|
|
|
printf(" 若浏览完毕按任意键返回主菜单\n");
|
|
|
system("pause"); //暂停,由自己按任意键继续运行程序
|
|
|
xuanze();
|
|
|
}
|
|
|
|
|
|
void chaxun()
|
|
|
{
|
|
|
int n;
|
|
|
printf(" 1、按姓名查找 2、按电话号码查找 3、按邮箱地址查找\n");
|
|
|
do
|
|
|
{
|
|
|
printf(" 请选择选项:");
|
|
|
scanf("%d",&n);
|
|
|
switch(n)
|
|
|
{
|
|
|
case 1:chazhao1(); break;
|
|
|
case 2:chazhao2(); break;
|
|
|
case 3:chazhao3(); break;
|
|
|
default:printf(" 输入错误,请重新输入!\n");
|
|
|
}
|
|
|
}while(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
void chazhao1()
|
|
|
{
|
|
|
int flag=1,i; //flag状态量用来判断查找结果
|
|
|
char name[20];
|
|
|
printf(" 请输入要查找联系人的姓名:");
|
|
|
scanf("%s",name);
|
|
|
if((fp=fopen("shuju.txt","r"))==NULL) //打开文本
|
|
|
{
|
|
|
printf("File open error!\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
for(i=0;feof(fp)==0;i++)
|
|
|
{
|
|
|
fscanf(fp,"%s %s %s %s\n",a[i].name,a[i].area,a[i].tel,a[i].email);
|
|
|
if(strcmp(name,a[i].name)==0) //strcmp函数比较两个字符串,若相等则返回0
|
|
|
{
|
|
|
printf(" 姓名\t籍贯\t电话号码\t邮箱地址\t\t\n");
|
|
|
printf(" %s\t%s\t%s\t%s\t\t\n",a[i].name,a[i].area,a[i].tel,a[i].email); //输出查找内容
|
|
|
flag=0;
|
|
|
break;
|
|
|
}
|
|
|
else
|
|
|
flag=1;
|
|
|
}
|
|
|
fclose(fp); //关闭文本
|
|
|
if(flag==1)
|
|
|
printf(" 查无此人\n");
|
|
|
else
|
|
|
printf(" 查找完毕\n");
|
|
|
fanhui();
|
|
|
}
|
|
|
|
|
|
void chazhao2()
|
|
|
{
|
|
|
int flag=1,i; //flag状态量用来判断查找结果
|
|
|
char tel[20];
|
|
|
printf(" 请输入要查找联系人的电话号码:");
|
|
|
scanf("%s",tel);
|
|
|
if((fp=fopen("shuju.txt","r"))==NULL) //打开文本
|
|
|
{
|
|
|
printf("File open error!\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
for(i=0;feof(fp)==0;i++)
|
|
|
{
|
|
|
fscanf(fp,"%s %s %s %s\n",a[i].name,a[i].area,a[i].tel,a[i].email);
|
|
|
if(strcmp(tel,a[i].tel)==0) //strcmp函数比较两个字符串,若相等则返回0
|
|
|
{
|
|
|
printf(" 姓名\t籍贯\t电话号码\t邮箱地址\t\t\n");
|
|
|
printf(" %s\t%s\t%s\t%s\t\t\n",a[i].name,a[i].area,a[i].tel,a[i].email); //输出查找内容
|
|
|
flag=0;
|
|
|
break;
|
|
|
}
|
|
|
else
|
|
|
flag=1;
|
|
|
}
|
|
|
fclose(fp);
|
|
|
if(flag==1)
|
|
|
printf(" 查无此人\n");
|
|
|
else
|
|
|
printf(" 查找完毕\n");
|
|
|
fanhui();
|
|
|
}
|
|
|
|
|
|
void chazhao3()
|
|
|
{
|
|
|
int flag=1,i; //flag状态量用来判断查找结果
|
|
|
char email[20];
|
|
|
printf(" 请输入要查找联系人的邮箱地址:");
|
|
|
scanf("%s",email);
|
|
|
if((fp=fopen("shuju.txt","r"))==NULL) //打开文本
|
|
|
{
|
|
|
printf("File open error!\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
for(i=0;feof(fp)==0;i++)
|
|
|
{
|
|
|
fscanf(fp,"%s %s %s %s\n",a[i].name,a[i].area,a[i].tel,a[i].email);
|
|
|
if(strcmp(email,a[i].email)==0) //strcmp函数比较两个字符串,若相等则返回0
|
|
|
{
|
|
|
printf(" 姓名\t籍贯\t电话号码\t邮箱地址\t\t\n");
|
|
|
printf(" %s\t%s\t%s\t%s\t\t\n",a[i].name,a[i].area,a[i].tel,a[i].email); //输出查找内容
|
|
|
flag=0;
|
|
|
break;
|
|
|
}
|
|
|
else
|
|
|
flag=1;
|
|
|
}
|
|
|
fclose(fp);
|
|
|
if(flag==1)
|
|
|
printf(" 查无此人\n");
|
|
|
else
|
|
|
printf(" 查找完毕\n");
|
|
|
fanhui();
|
|
|
}
|
|
|
/*返回菜单函数*/
|
|
|
void fanhui()
|
|
|
{
|
|
|
int b;
|
|
|
printf(" ---------------------------------\n");
|
|
|
printf(" 按任意键返回主菜单\n");
|
|
|
system("pause"); //暂停
|
|
|
|
|
|
xuanze();
|
|
|
|
|
|
}
|
|
|
/*修改信息函数*/
|
|
|
void change()
|
|
|
{
|
|
|
int i,t,n=0,flag=1;
|
|
|
char name[20];
|
|
|
printf(" 请输入你需要修改信息的学生姓名:");
|
|
|
scanf("%s",name);
|
|
|
if((fp=fopen("shuju.txt","r+"))==NULL)
|
|
|
{
|
|
|
printf("File open error!\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
for(i=0;feof(fp)==0;i++)
|
|
|
{
|
|
|
fscanf(fp,"%s %s %s %s\n",a[i].name,a[i].area,a[i].tel,a[i].email);
|
|
|
if(strcmp(name,a[i].name)==0)
|
|
|
{ flag=0;
|
|
|
n=i;
|
|
|
|
|
|
}
|
|
|
}
|
|
|
if(flag==1)
|
|
|
{
|
|
|
printf(" 查无此人!\n");
|
|
|
fanhui();
|
|
|
}
|
|
|
printf(" 请确认学生信息\n");
|
|
|
printf(" 姓名\t\t籍贯\t\t电话号码\t\t邮箱地址\t\t\n");
|
|
|
printf(" %s\t\t%s\t\t%s\t\t%s\t\t\n",a[n].name,a[n].area,a[n].tel,a[n].email);
|
|
|
printf(" 确认修改按 1\t取消修改按 2:");
|
|
|
scanf("%d",&t);
|
|
|
if(t==1)
|
|
|
xiugai(n);
|
|
|
if(t==2)
|
|
|
fanhui();
|
|
|
if(fclose(fp))
|
|
|
{
|
|
|
printf("关闭失败\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
if((fp=fopen("shuju.txt","w"))==NULL)
|
|
|
{
|
|
|
printf("File open error!\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
for(n=0;n<i;n++)
|
|
|
fprintf(fp,"%s %s %s %s\n",a[n].name,a[n].area,a[n].tel,a[n].email);
|
|
|
if(fclose(fp))
|
|
|
{
|
|
|
printf("关闭失败\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
system("pause");
|
|
|
}
|
|
|
|
|
|
void xiugai(int n)
|
|
|
{
|
|
|
int k,z;
|
|
|
printf(" ************************************\n");
|
|
|
printf(" 1、姓名\n");
|
|
|
printf(" 2、籍贯\n");
|
|
|
printf(" 3、电话号码\n");
|
|
|
printf(" 4、邮箱地址\n");
|
|
|
printf(" ************************************\n");
|
|
|
printf(" 请选择您要修改的信息:");
|
|
|
scanf("%d",&k);
|
|
|
switch(k)
|
|
|
{
|
|
|
case 1:printf(" 请输入修改后的内容:"); scanf("%s",a[n].name); break;
|
|
|
case 2:printf(" 请输入修改后的内容:"); scanf("%s",a[n].area); break;
|
|
|
case 3:printf(" 请输入修改后的内容:"); scanf("%s",a[n].tel); break;
|
|
|
case 4:printf(" 请输入修改后的内容:"); scanf("%s",a[n].email); break;
|
|
|
}
|
|
|
printf(" 修改成功\n");
|
|
|
printf(" 按任意键返回主菜单\n");
|
|
|
}
|