You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

716 lines
15 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <windows.h>
#pragma comment(lib, "Winmm.lib")
#include<conio.h>
FILE *ptr1;
typedef struct
{
char name[20];
char phonenum[20];
int kind;
char email[50];
} lm;
lm linkman,lkm;
int *offset;
void add();
void view();
void search();
void sc();
void dial();
void amend();
void head();
void whattodo();
int passwd();
int addpw();
int changepw();
int passwd()
{
system("cls");
char passwd[20],pw[20];
ptr1=fopen("passwd.sto","rb+");
if(ptr1==NULL)
{
if((ptr1=fopen("passwd.sto","wb+"))==NULL)
{
printf("程序初始化错误\n");
system("pause");
exit(0);
}
}
fscanf(ptr1,"%s",passwd);
if(ftell(ptr1)==0)return 50;
printf("欢迎您使用手机通讯录小程序\n");
printf("本程序已被设置隐私保护,请输入密码:");
fflush(stdin);
gets(pw);
if(strcmp(pw,passwd)==0)
{
printf("验证成功~\n");
Sleep(2000);
return 100;
}
else
{
printf("验证失败~\n");
while(strcmp(pw,passwd)!=0)
{
printf("本程序已被设置隐私保护,请输入密码:");
fflush(stdin);
gets(pw);
if(strcmp(pw,passwd)==0)
{
printf("验证成功~\n");
Sleep(500);
return 100;
}
}
}
}
int main_menu(int flag)
{
char c;
do{
system("cls");
printf("\t\t ══════════════════════════════\n");
printf("\t\t 手机通讯录小程序 \n");
printf("\t\t ══════════════════════════════\n");
printf("\t\t ┌────────────────────────────────┐\n");
printf("\t\t │ 1. 新增联系人 2. 全部联系人 │\n");
printf("\t\t │ │\n");
printf("\t\t │ 3. 查找联系人 4. 修改联系人 │\n");
printf("\t\t │ │\n");
if(flag==50)
printf("\t\t │ 5. 设置密码保护 6. 拨号 │\n");
if(flag==100)
printf("\t\t │ 5. 修改密码保护 6. 拨号 │\n");
printf("\t\t │ │\n");
printf("\t\t │ 0. 退出程序 │\n");
printf("\t\t └────────────────────────────────┘\n");
printf("\t\t请您选择(0-6):");
c=getchar();
}while(c<'0'||c>'6');
return (c-'0');
}
main()
{
int flag;
flag=passwd();
while(1)
{
switch(main_menu(flag))
{
case 1:
printf("\t\t --------新增联系人--------\n");
add();
break;
case 2:
printf("\t\t --------全部联系人--------\n");
view();
break;
case 3:
printf("\t\t --------查找联系人--------\n");
search();
break;
case 4:
printf("\t\t --------修改联系人--------\n");
amend();
break;
case 5:
if(flag==100)
{
printf("\t\t --------修改密码保护---------\n");flag=changepw();
}
if(flag==50)
{
printf("\t\t --------设置密码保护---------\n");flag=addpw();
}
break;
case 6:
printf("\t\t -----------拨号------------\n");
dial();
break;
case 0:
printf("感谢使用,再见\n");
system("pause");
exit(0);
}
}
return 0;
}
void add()
{
int i=0,flag;
ptr1=fopen("storage.sto","rb+");
if(ptr1==NULL)
{
ptr1=fopen("storage.sto","wb+");
if(ptr1==NULL)
{
printf("无法建立文件\n");
exit(0);
}
}
rewind(ptr1);
while(!feof(ptr1))
{
fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email);
//printf("%s\n",linkman.name);
i++;
}
if(ftell(ptr1)==0)i=0;
fseek(ptr1,0,SEEK_END);
printf("您目前有%d条联系人数据您还可以新增%d条联系人数据(%d/15)\n",i,15-i,i);
if(i==15)
{
system("pause");
return;
}
char ch='y';
while(ch!='n'&&ch!='N')
{
fflush(stdin);
++i;
pos_1:
printf("姓名:");
gets(lkm.name);
rewind(ptr1);
flag=0;
while(!feof(ptr1))
{
fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email);
if(strcmp(linkman.name,lkm.name)==0)
{
printf("您输入的姓名和已有的记录相同,请重新输入\n");
goto pos_1;
}
}
pos_2:
printf("电话号码:");
gets(lkm.phonenum);
rewind(ptr1);
while(!feof(ptr1))
{
fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email);
if(strcmp(linkman.phonenum,lkm.phonenum)==0)
{
printf("您输入的电话号码和已有的记录相同,请重新输入\n");
goto pos_2;
}
}
printf("分类1 办公类 2 个人类 3 商务类):");
scanf("%d",&lkm.kind);
while(lkm.kind<0||lkm.kind>3)
{
printf("请输入1-3内的数字");
scanf("%d",&lkm.kind);
}
fflush(stdin);
printf("电子邮箱:");
gets(lkm.email);
fprintf(ptr1, "%20s%20s%5d%50s",lkm.name,lkm.phonenum,lkm.kind,lkm.email);
if(i<15)
printf("\n这是您的第%d条联系人您可以继续新增%d条联系人y/n",i,15-i);
else
{
printf("\n这是您的第%d条联系人您无法继续新增联系人了y/n",i,15-i);
system("pause");
return;
}
fflush(stdin);
ch=getchar();
}
fclose(ptr1);
}
void view()
{
int x,i=0;
ptr1=fopen("storage.sto","rb+");
if(ptr1==NULL)
{
whattodo();
return;
}
fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email);
if(ftell(ptr1)==0)
{
whattodo();
return;
}
rewind(ptr1);
fflush(stdin);
printf("请您选择查看联系人的范围:\n");
printf("【1】所有联系人\n");
printf("【2】办公联系人\n");
printf("【3】个人联系人\n");
printf("【4】商务联系人\n");
while(x<1||x>4)
{
printf("请输入1-4内的数字");
scanf("%d",&x);
}
head();//输出表头
if(x==1)
{
while(!feof(ptr1))
{
fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email);
//printf("%d %-10s %-10s %d %-10s",i,linkman.name,linkman.phonenum,linkman.kind,linkman.email);
if(linkman.kind==1)
printf("%2d %-10s %-20s 办公类 %-10s\n",++i,linkman.name,linkman.phonenum,linkman.email);
if(linkman.kind==2)
printf("%2d %-10s %-20s 个人类 %-10s\n",++i,linkman.name,linkman.phonenum,linkman.email);
if(linkman.kind==3)
printf("%2d %-10s %-20s 商务类 %-10s\n",++i,linkman.name,linkman.phonenum,linkman.email);
}
}
else
{
while(!feof(ptr1))
{
fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email);
switch(x)
{
case 2:if(linkman.kind==1)printf("%2d %-10s %-20s 办公类 %-10s\n",++i,linkman.name,linkman.phonenum,linkman.email);break;
case 3:if(linkman.kind==2)printf("%2d %-10s %-20s 个人类 %-10s\n",++i,linkman.name,linkman.phonenum,linkman.email);break;
case 4:if(linkman.kind==3)printf("%2d %-10s %-20s 商务类 %-10s\n",++i,linkman.name,linkman.phonenum,linkman.email);break;
}
}
}
printf("已经显示全部内容,共%d条\n",i);
system("pause");
fclose(ptr1);
return;
}
void search()
{
int x,i=0,u=0;
ptr1=fopen("storage.sto","rb+");
if(ptr1==NULL)
{
printf("您目前没有联系人的记录\n");
printf("【1】返回主目录\n");
printf("【2】新增联系人\n");
printf("【3】结束程序\n");
scanf("%d",&x);
while(x<1||x>3)
{
printf("请输入1-3内的数字");
scanf("%d",&x);
}
switch(x)
{
case 1:return;break;
case 2:void add();break;
case 3:exit(0);
}
}
fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email);
if(ftell(ptr1)==0)
{
whattodo();
return;
}
rewind(ptr1);
fflush(stdin);
printf("您想按照什么关键字搜索呢:\n");
printf("【1】模糊搜索\n");
printf("【2】姓名搜索\n");
printf("【3】手机号码搜索\n");
printf("【4】电子邮件地址搜索\n");
printf("【5】返回主菜单\n");
scanf("%d",&x);
while(x<1||x>5)
{
printf("请输入1-5内的数字");
scanf("%d",&x);
}
if(x==5)return;
char keywords[20];
fflush(stdin);
printf("请输入关键字:");
gets(keywords);
head();//输出表头
while(!feof(ptr1))
{
fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email);
if(x==1)
{
if(keywords[0]>=0)
{
if(keywords[0]==linkman.name[0])u=1;
}
else
if(keywords[0]==linkman.name[0]&&keywords[1]==linkman.name[1])u=1;
}
if(x==2)
{
if(strcmp(keywords,linkman.name)==0)u=1;
}
if(x==3)
{
if(strcmp(keywords,linkman.phonenum)==0)u=1;
}
if(x==4)
{
if(strcmp(keywords,linkman.email)==0)u=1;
}
if(u)
{
switch(linkman.kind)
{
case 1:printf("%2d %-10s %-20s 办公类 %-10s\n",++i,linkman.name,linkman.phonenum,linkman.email);break;
case 2:printf("%2d %-10s %-20s 个人类 %-10s\n",++i,linkman.name,linkman.phonenum,linkman.email);break;
case 3:printf("%2d %-10s %-20s 商务类 %-10s\n",++i,linkman.name,linkman.phonenum,linkman.email);break;
}
}
u=0;
}
printf("已经显示全部搜索结果,共%d条\n",i);
system("pause");
fclose(ptr1);
return;
}
void amend()
{
int x,i=0,flag,k,j,q;
lm array[15];
ptr1=fopen("storage.sto","rb+");
if(ptr1==NULL)
{
printf("您目前没有联系人的记录\n");
printf("【1】返回主目录\n");
printf("【2】新增联系人\n");
printf("【3】结束程序\n");
scanf("%d",&x);
while(x<1||x>3)
{
printf("请输入1-3内的数字");
scanf("%d",&x);
}
switch(x)
{
case 1:return;break;
case 2:add();return;break;
case 3:exit(0);
}
}
fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email);
if(ftell(ptr1)==0)
{
whattodo();
return;
}
fflush(stdin);
head();//输出表头
i=0;
j=0;
rewind(ptr1);
while(!feof(ptr1))
{
fscanf(ptr1, "%s%s%d%s",array[j].name,array[j].phonenum,&array[j].kind,array[j].email);
//printf("%2d %-10s %-20s 办公类 %-10s\n",++i,array[j].name,array[j].phonenum,array[j].email);
if(array[j].kind==1)
printf("%2d %-10s %-20s 办公类 %-10s\n",++i,array[j].name,array[j].phonenum,array[j].email);
if(array[j].kind==2)
printf("%2d %-10s %-20s 个人类 %-10s\n",++i,array[j].name,array[j].phonenum,array[j].email);
if(array[j].kind==3)
printf("%2d %-10s %-20s 商务类 %-10s\n",++i,array[j].name,array[j].phonenum,array[j].email);
j++;
}
printf("请从1-%d选择要修改/删除的序号:",i);
scanf("%d",&x);
while(x<0||x>i)
{
fflush(stdin);
printf("请从1-%d选择要修改/删除的序号:",i);
scanf("%d",&x);
}
q=i;//存储i的值;
printf("请问是要修改还是删除:\n【1】修改\n【2】删除\n");
scanf("%d",&flag);
while(flag<1||flag>2)
{
printf("请重新输入1/2");
scanf("%d",&flag);
}
if(flag==2)goto pos_3;
//写入待修改的联系人信息
char ch;
fflush(stdin);
printf("更改 %s 姓名(y/n)",array[x-1].name);
ch=getchar();
fflush(stdin);
if(ch=='y'||ch=='Y')
{
printf("姓名:");
fflush(stdin);
gets(array[x-1].name);
}
printf("更改 %s 的电话号码 %s (y/n)",array[x-1].name,array[x-1].phonenum);
fflush(stdin);
ch=getchar();
if(ch=='y'||ch=='Y')
{
printf("电话号码:");
fflush(stdin);
gets(array[x-1].phonenum);
}
fflush(stdin);
printf("更改现在分类 ");
switch(array[x-1].kind)
{
case 1:printf("办公类");break;
case 2:printf("个人类");break;
case 3:printf("商务类");break;
}
printf(" (y/n)");
fflush(stdin);
ch=getchar();
if(ch=='y'||ch=='Y')
{
printf("分类1 办公类 2 个人类 3 商务类):");
scanf("%d",&array[x-1].kind);
while(lkm.kind<0||lkm.kind>3)
{
printf("请输入1-3内的数字");
scanf("%d",&array[x-1].kind);
}
}
printf("更改 %s 的电子邮箱 %s (y/n)",array[x-1].name,array[x-1].email);
fflush(stdin);
ch=getchar();
if(ch=='y'||ch=='Y')
{
printf("电子邮箱:");
fflush(stdin);
gets(array[x-1].email);
}
pos_3:
i=0;
fclose(ptr1);
ptr1=fopen("storage.sto","wb");
i=0;
rewind(ptr1);
pos_4:
while(i<q)
{
if(x==(i+1)&&flag==2)
{
i++;continue;
}
fprintf(ptr1, "%20s%20s%5d%50s",array[i].name,array[i].phonenum,array[i].kind,array[i].email);
//printf("%20s%20s%5d%50s",array[i].name,array[i].phonenum,array[i].kind,array[i].email);
i++;
}
fclose(ptr1);
fflush(stdin);
printf("修改成功!\n");
system("pause");
return;
}
int addpw()
{
char pw[20];
ptr1=fopen("passwd.sto","wb");
if(ptr1==NULL)
{
printf("无法设置密码\n");
system("pause");
return 50;
}
printf("请输入20位以内的密码");
fflush(stdin);
gets(pw);
fputs(pw,ptr1);
//puts(pw);
printf("新建密码成功!");
fclose(ptr1);
system("pause");
passwd();
return 100;
}
int changepw()
{
char oldpw[20],newpw[20],newpw2[20],pw[20];
ptr1=fopen("passwd.sto","rb+");
if(ptr1==NULL)
{
printf("无法修改密码\n");
system("pause");
return 50;
}
fscanf(ptr1,"%s",oldpw);
pos_6:
printf("请输入您的旧密码:");
fflush(stdin);
gets(pw);
if(strcmp(pw,oldpw)!=0){
printf("密码错误!\n");goto pos_6;
}
pos_7:
printf("请输入您的新密码:");
fflush(stdin);
gets(newpw);
printf("请再次输入您的新密码:");
fflush(stdin);
gets(newpw2);
if(strcmp(newpw,newpw2)!=0)
{
printf("两次输入的密码不同,需要重新输入!\n");
goto pos_7;
}
fclose(ptr1);
ptr1=fopen("passwd.sto","wb");
if(ptr1==NULL)
{
printf("无法修改密码\n");
system("pause");
return 50;
}
fprintf(ptr1,"%s",newpw);
printf("密码修改成功\n");
Sleep(2000);
passwd();
return 100;
}
void dial()
{
int x,i=0,flag,k,j,q;
lm array[15];
ptr1=fopen("storage.sto","rb+");
if(ptr1==NULL)
{
printf("您目前没有联系人的记录\n");
printf("【1】返回主目录\n");
printf("【2】新增联系人\n");
printf("【3】结束程序\n");
scanf("%d",&x);
while(x<1||x>3)
{
printf("请输入1-3内的数字");
scanf("%d",&x);
}
switch(x)
{
case 1:return;break;
case 2:add();return;break;
case 3:exit(0);
}
}
fscanf(ptr1, "%s%s%d%s",linkman.name,linkman.phonenum,&linkman.kind,linkman.email);
if(ftell(ptr1)==0)
{
whattodo();
return;
}
fflush(stdin);
head();//输出表头
i=0;
j=0;
rewind(ptr1);
while(!feof(ptr1))
{
fscanf(ptr1, "%s%s%d%s",array[j].name,array[j].phonenum,&array[j].kind,array[j].email);
//printf("%2d %-10s %-20s 办公类 %-10s\n",++i,array[j].name,array[j].phonenum,array[j].email);
if(array[j].kind==1)
printf("%2d %-10s %-20s 办公类 %-10s\n",++i,array[j].name,array[j].phonenum,array[j].email);
if(array[j].kind==2)
printf("%2d %-10s %-20s 个人类 %-10s\n",++i,array[j].name,array[j].phonenum,array[j].email);
if(array[j].kind==3)
printf("%2d %-10s %-20s 商务类 %-10s\n",++i,array[j].name,array[j].phonenum,array[j].email);
j++;
}
printf("请从1-%d选择要拨打的序号",i);
scanf("%d",&x);
while(x<0||x>i)
{
fflush(stdin);
printf("请从1-%d选择要拨打的序号",i);
scanf("%d",&x);
}
for(q=0;q<strlen(array[x-1].phonenum);q++)
{
printf("%c",array[x-1].phonenum[q]);
Sleep(600);
}
system("pause");
return;
}
void head()
{
printf("序号 姓名 手机号码 分类 电子邮件\n");
printf("***************************************************************************\n");
}
void whattodo()
{
int x;
printf("您目前没有联系人的记录\n");
printf("【1】返回主目录\n");
printf("【2】新增联系人\n");
printf("【3】结束程序\n");
scanf("%d",&x);
while(x<1||x>3)
{
printf("请输入1-3内的数字");
scanf("%d",&x);
}
switch(x)
{
case 1:return;break;
case 2:add();return;break;
case 3:exit(0);
}
}