last commit

master
parent d97ab49c3a
commit 936141d327

@ -1,19 +1,48 @@
#include <stdio.h> #include <stdio.h>
#include <process.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <windows.h>
//定义函数 //定义函数
FILE *fp; //文件指针 FILE *fp; //文件指针
void menu(); void menu();
void add(); void add();
void search(); void search();
void modify(); void modify();
void dele();
void look(); void look();
void quit(); void quit();
void enroll();
void login();
void reenroll();
void jiemian();
void ycmm();
void relogin();
int n;
int m;
int t;
int offset;
char mima_[10];
char MIMA[4]="5555";
struct manager
{
char name[10];
char password[10];
}manage[5];
//主函数 //主函数
int main() int main()
{ {
menu(); int a=0;
n=0;
do
{
jiemian();
getchar();
printf("\n按1继续回到主界面,其它任意键退出:");
a=getch();
}while(a==49);
return 0; return 0;
} }
//通讯录结构体 //通讯录结构体
@ -25,35 +54,192 @@ struct user
char tel2[15]; //电话2 char tel2[15]; //电话2
char email[20]; //电子邮箱 char email[20]; //电子邮箱
}user[50]; }user[50];
//菜单
void menu() void jiemian()
{ {
system("cls"); system("cls");
system("color f3"); system("color f3");
int n; int n;
printf("\n\n\n\n\n"); printf("\n\n\n\n\n");
printf("\t\t:---------通讯录---------:\n"); printf("\t\t:---------通讯录---------:\n");
printf("\t\t: 1:新建 :\n"); printf("\t\t: 1.注册 :\n");
printf("\t\t: 2:查询 :\n"); printf("\t\t: 2.登录 :\n");
printf("\t\t: 3:修改 :\n"); printf("\t\t:--输入数 <1-2>选择功能--:\n\n");
printf("\t\t: 4:浏览 :\n");
printf("\t\t: 5:退出 :\n");
printf("\t\t:--输入数 <1-5>选择功能--:\n\n");
scanf("%d",&n); scanf("%d",&n);
switch(n) switch(n)
{ {
case 1: add(); break; //添加 case 1: enroll(); break; //注册
case 2: search(); break; //查询 case 2: login(); break; //登录
case 3: modify(); break; //修改
case 4: look(); break; //浏览
case 5: return; break; //退出
default: return; default: return;
} }
} }
//添加用户信息 //注册
void enroll()
{
system("cls");
int anhao; //管理员密匙
char zhanghao[10];
char mima1[10];
char mima2[10];
//管理员注册
printf("\n请输入暗号:");
ycmm();
//暗号核对正确
if(!strcmp(mima_,MIMA)){
fp=fopen("manager.txt","ab");
struct manager one;
printf("\n\t\t\t请输入新管理员账号:");
scanf("%s",zhanghao);
printf("\n\t\t\t请输入新管理员密码:");
ycmm();
strcpy(mima1,mima_);
printf("\n\t\t\t请再次确认密码:");
ycmm();
strcpy(mima2,mima_);
if(!strcmp(mima1,mima2)){
strcpy(one.name,zhanghao);
strcpy(one.password,mima1);
fprintf(fp,"%-10s%-10s",one.name,one.password);
}
else{
fclose(fp);reenroll();return;
}
printf("\t\t\t注册成功!\n\n");
fclose(fp);
printf("3秒后进入管理员界面");
Sleep(3000);
menu();
return;
}
//暗号错误,再次注册
else
printf("暗号错误,注册失败!\n\n");reenroll();
return;
}
//继续注册
void reenroll()
{
printf("继续注册请按1");
if(getch()==49)
enroll();
else
jiemian();
}
//登录
void login()
{
system("cls");
char zhanghao[20];
char mima[20];
char password2[20];
if((fp=fopen("manager.txt","rb"))==NULL){
printf("未查询到管理员账号\n请先注册3秒后为您跳转到注册界面");
Sleep(3000);
enroll();
exit(0);
}
struct manager one;
printf("\n\t\t\t管理员账号:");
scanf("%s",zhanghao);
printf("\n\t\t\t管理员密码:");
ycmm();
while(!feof(fp)){
fscanf(fp,"%s%s",one.name,one.password);
if(!strcmp(zhanghao,one.name)&&!strcmp(mima_,one.password)){
printf("\n登录成功!\n");
n=1;
fclose(fp);
printf("3秒后进入管理员界面");
Sleep(3000);
menu();return;
}
}
printf("\t\t\t登录失败!\n\n");
fclose(fp);
relogin();
return;
}
//继续登录
void relogin()
{
printf("继续登录请按1按其它任意键返回主界面");
if(getch()==49)
login();
else
jiemian();
}
//隐藏密码
void ycmm()
{
int i=0;
for(;i<10;i++)
{
mima_[i]=getch();
if(mima_[i]=='\b')
{
if(i<=0)
printf(" "),i=1;
printf("\b \b");
i=i-2;
continue;
}
if(mima_[i]=='\r')
{
break;
}
printf("*");
}
mima_[i]='\0';
}
//菜单
void menu()
{
int b=0;
do{
system("cls");
system("color f3");
int a;
b=0;
printf("\n\n\n\n\n");
printf("\t\t:----通讯录管理员界面----:\n");
printf("\t\t: 1:新建 :\n");
printf("\t\t: 2:查询 :\n");
printf("\t\t: 3:修改 :\n");
printf("\t\t: 4:删除 :\n");
printf("\t\t: 5:浏览 :\n");
printf("\t\t: 6:返回 :\n");
printf("\t\t: 7:退出 :\n");
printf("\t\t:--输入数 <1-7>选择功能--:\n\n");
scanf("%d",&a);
switch(a)
{
case 1: add(); break; //添加
case 2: search(); break; //查询
case 3: modify(); break; //修改
case 4: dele(); break; //删除
case 5: look(); break; //浏览
case 6: jiemian(); break; //退出
case 7: exit(0);
default: return;
}
printf("按1返回到管理员界面");
b=getch();
}while(b==49);
}
//添加用户信息 1.新建
void add() void add()
{ {
struct user one; struct user one={'\0','\0','\0','\0','\0'};//初始化,防止文本里出现乱码
system("color e3"); system("color e3");
if((fp=fopen("user.txt","ab"))==NULL) if((fp=fopen("user.txt","ab"))==NULL)
{ {
@ -70,106 +256,99 @@ void add()
scanf("%s",one.tel2); scanf("%s",one.tel2);
printf("\n请输入用户电子邮箱:"); printf("\n请输入用户电子邮箱:");
scanf("%s",one.email); scanf("%s",one.email);
fwrite(&one,sizeof(struct user),1,fp); //把数据块录入user.txt中 fprintf(fp,"%-8s%-15s%-15s%-15s%-20s\n",one.name,one.place,one.tel1,one.tel2,one.email);
fclose(fp); fclose(fp);
printf("\n注册成功!\n"); printf("\n添加成功!\n");
system("pause"); }
menu(); //查询 2.查询
}; void search()
//修改
void modify()
{ {
struct user one;
char namekey[8]; char namekey[8];
printf("输入要查询的姓名:"); printf("输入要查询的姓名:");
scanf("%s",namekey); scanf("%s",namekey);
if((fp=fopen("user.txt","ab+"))==NULL) //打开文件 if((fp=fopen("user.txt","rb"))==NULL) //打开文件
{ {
printf("\n不能打开文件!\n"); printf("\n不能打开文件!\n");
exit(0); exit(0);
} }
int m=0,t=1; t=0;
m=0;
while(!feof(fp)) while(!feof(fp))
{ {
fread(&user[m],sizeof(struct user),1,fp)==1; //读取结构体中一个用户信息 offset=ftell(fp);
if(!strcmp(user[m].name,namekey)) //判断是否相同 fscanf(fp,"%s%s%s%s%s\n",one.name,one.place,one.tel1,one.tel2,one.email);
if(!strcmp(one.name,namekey)) //判断是否相同
{ {
t=0; //若相同则在后面if句中输出 t=1;
printf("%-8s%-15s%-15s%-15s%-20s\n",one.name,one.place,one.tel1,one.tel2,one.email); //若相同则在后面if句中输出
break; break;
} }
m++;
} }
if(t) if(!t)
printf("未找到\n"),fclose(fp),system("pause"),menu(); printf("未找到\n");
else
{
printf("找到\n");
printf("姓名\t\t籍贯\t\t电话1\t\t电话2\t\t邮箱\n");
printf("%-8s\t",user[m].name);
printf("%-15s\t",user[m].place);
printf("%-15s\t",user[m].tel1);
printf("%-15s\t",user[m].tel2);
printf("%-20s\t",user[m].email);
}
printf("\n您想要修改哪条信息?\n\n1.姓名\t2.籍贯\t3.电话1\t4.电话2\t5.邮箱\t其它任意键.不修改"); //修改信息提示
int a; //修改的信息序号
scanf("%d",&a);
char *newname;
char nweplace[15];
char newtel1[15];
char newtel2[15];
char newemail[20];
switch(a)
{
case 1:scanf("%s",newname),strcpy(user[m].name,newname),printf("%s",user[m].name);break;
default:break;
}
printf("修改成功\n");
fclose(fp); fclose(fp);
system("pause");
menu();
} }
//查询 //修改 3.修改
void search() void modify()
{ {
char namekey[8]; int a;
printf("输入要查询的姓名:"); char ch[10];
scanf("%s",namekey); struct user one;
if((fp=fopen("user.txt","r"))==NULL) //打开文件 search();
if(t==1)
{ {
printf("\n不能打开文件!\n"); printf("请输入你想修改的信息1.姓名 2.籍贯 3.电话1 4.电话2 5.邮箱\n"); //修改信息提示
exit(0); scanf("%d",&a);
fp=fopen("user.txt","r+");
printf("请输入新信息:");
switch(a)
{
case 1: scanf("%s",one.name);fprintf(fp,"%-8s",one.name);break;
case 2: fseek(fp,offset+8,SEEK_SET);
scanf("%s",one.place);fprintf(fp,"%-15s",one.place);break;
case 3: fseek(fp,offset+23,SEEK_SET);
scanf("%s",one.tel1);fprintf(fp,"%-15s",one.tel1);break;
case 4: fseek(fp,offset+38,SEEK_SET);
scanf("%s",one.tel2);fprintf(fp,"%-15s",one.tel2);break;
case 5: fseek(fp,offset+53,SEEK_SET);
scanf("%s",one.email);fprintf(fp,"%-20s",one.email);break;
default: break;
}
fclose(fp);
printf("修改成功\n");
} }
int m=0,t=0; }
while(!feof(fp)) //删除 4.删除
void dele()
{
char a;
for(;;)
{ {
fread(&user[m],sizeof(struct user),1,fp)==1; //读取结构体中一个用户信息 search();
if(!strcmp(user[m].name,namekey)) //判断是否相同 if(t==1)
{ break;
t=1; //若相同则在后面if句中输出 else
printf("退出请按N,按其它任意键继续:");
a=getchar();
if(a=='n'||a=='N')
break; break;
}
m++;
} }
if(t)
if(t==1)
{ {
printf("找到\n"); fp=fopen("user.txt","r+");
printf("姓名\t\t籍贯\t\t电话1\t\t电话2\t\t邮箱\n"); fseek(fp,offset,SEEK_SET);
printf("%-8s\t",user[m].name); fprintf(fp,"\n%-8s%-15s%-15s%-15s%-20s\n","","","","","");
printf("%-15s\t",user[m].place); fclose(fp);
printf("%-15s\t",user[m].tel1); printf("删除成功!\n");
printf("%-15s\t",user[m].tel2);
printf("%-20s\t",user[m].email);
} }
else }
printf("未找到\n"); //浏览 5.浏览
fclose(fp);
system("pause");
menu();
}
//浏览
void look() void look()
{ {
struct user one;
char c; char c;
if((fp=fopen("user.txt","r"))==NULL) //打开文件user.txt if((fp=fopen("user.txt","r"))==NULL) //打开文件user.txt
{ {
@ -177,36 +356,16 @@ void look()
exit(0); exit(0);
} }
int m=0; int m=0;
printf("%-8s%-15s%-15s%-15s%-20s\n","姓名","籍贯","电话1","电话2","邮箱");
while(!feof(fp)) while(!feof(fp))
{ {
if(fread(&user[m],sizeof(struct user),1,fp)==1) fscanf(fp,"%s%s%s%s%s\n",one.name,one.place,one.tel1,one.tel2,one.email);
printf("%-8s%-15s%-15s%-15s%-20s\n",one.name,one.place,one.tel1,one.tel2,one.email);
m++; m++;
} }
int i;
printf("姓名\t\t籍贯\t\t电话1\t\t电话2\t\t邮箱\n");
for(i=0;i<m;i++)
{
printf("%-8s\t",user[i].name);
printf("%-15s\t",user[i].place);
printf("%-15s\t",user[i].tel1);
printf("%-15s\t",user[i].tel2);
printf("%-20s\t",user[i].email);
printf("\n");
}
if(fclose(fp)) //关闭文件 if(fclose(fp)) //关闭文件
{ {
printf("关闭文件失败!\n"); printf("关闭文件失败!\n");
exit(0); exit(0);
} }
scanf("%c",&c);
printf("是否继续Y/N");
scanf("%c",&c);
if(c=='y'||c=='Y')
menu();
return;
}
//退出
void quit()
{
return;
} }

Binary file not shown.
Loading…
Cancel
Save