main()
{ int num,i,t=1,a=0;
char ch[100];
FILE *fp;
printf(\"敬告:。为了信息
安全,我们采用验证身份!\\n\\n\\n\");
printf(\"\\t\\t\\t注:原密码为本学号(000),用户名为本姓名\\n\");
do
{
printf(\"\\n\\n\\t\\t请输入您的用户名:\");
scanf(\"%s\",ch);
printf(\"\\t\\t请输入您的密码:\");
scanf(\"%d\",&num);
fp=fopen(\"yy\",\"rb\");
for(i=0;i<4;i++)
{ fread(&stu[i],sizeof(struct student),1,fp);
if(strcmp(stu[i].name,ch)==0&&stu[i].num==num||num==000) {t=0;printf(\"\\n\\n\\n\\t\\t\\t%s同学,你好:)\\n\",ch);break;}
}
if(t==1) {printf(\"\\t\\t用户名或密码错误,请重新输入\\n\");a++;}
if(a==3) {printf(\"\\t\\t三次错误,任意键退出\");getch();exit(0);}
}
while(t);
KS: printf(\"\\n\\n\\n\\t1,精确查找 2,一般查找 3,退出程序\\n请选择:\");
scanf(\"%d\",&num);
switch(num)
{ case 1: {XZ: printf(\"\\n\\n\\n\\t1,按学号 2,按姓名 3,返回上一级\\n请选择:\");
break;}
case 2: {XY: printf(\"\\n\\n\\n\\t1, 按班级 2,按宿舍编号 3,返回上一级\\n请选择:\");
goto KC;}
case 3: exit(0);
default:{printf(\"无效输入!!\");goto KS;}
}
scanf(\"%d\",&num);
switch(num)
{ case 1: {printf(\"\\n\\t请输入你要查找的同学号码(8位,比如20036110)\\n请输入:\");
scanf(\"%d\",&num);
printf(\"\\n\\n\\t姓名\\t学号\\t年龄\\t寝室号码\\n\\n\");
fp=fopen(\"yy\",\"rb\");
for(i=0;i<4;i++)
{ fread(&stu[i],sizeof(struct student),1,fp);
if(stu[i].num==num) {printf(\"%12s %8d%5d %5s\",stu[i].name,stu[i].num,stu[i].age,stu[i].addr);break;}
}
if(stu[i].num!=num){printf(\"输入错误,请重新输入\");}
fclose(fp);goto XZ;
}
case 2: {printf(\"\\n\\t\\t请输入你要查询同学的姓名\\n请输入:\");
scanf(\"%s\",ch);
printf(\"\\n\\n\\t姓名\\t学号\\t年龄\\t寝室号码\\n\\n\");
fp=fopen(\"yy\",\"rb\");
for(i=0;i<4;i++)
{fread(&stu[i],sizeof(struct student),1,fp);
if(strcmp(stu[i].name,ch)==0) {printf(\"%12s %8d%5d %5s\",stu[i].name,stu[i].num,stu[i].age,stu[i].addr);break;}
}
if(strcmp(stu[i].name,ch)!=0) printf(\"输入错误,请重新输入\");
fclose(fp);goto XZ;
}
case 3: goto KS;
default:{printf(\"无效输入!!\");goto XZ;}
}
KC:scanf(\"%d\",&num);
switch(num)
{ case 1: {printf(\"\\n03级电子1班\\n\");printf(\"\\n\\t姓名\\t学号\\t年龄\\t寝室号码\\n\\n\");
fp=fopen(\"yy\",\"rb\");
for(i=0;i<4;i++)
{fread(&stu[i],sizeof(struct student),1,fp);
printf(\"%12s %8d%5d %5s\\n\",stu[i].name,stu[i].num,stu[i].age,stu[i].addr);
}
fclose(fp);goto XY;
}
case 2: {printf(\"\\n\\t\\t\\t请输入宿舍编号(比如4524)\\n请输入:\");
scanf(\"%s\",ch);
printf(\"\\n\\t姓名\\t学号\\t年龄\\t寝室号码\\n\\n\");
fp=fopen(\"yy\",\"rb\");
for(i=0;i<4;i++)
{fread(&stu[i],sizeof(struct student),1,fp);
if(strcmp(stu[i].addr,ch)==0) printf(\"%12s %8d%5d %5s\\n\",stu[i].name,stu[i].num,stu[i].age,stu[i].addr);
}
fclose(fp);goto XY;
}
case 3: goto KS;
default:{printf(\"无效输入!!\");goto XY;}
}
}
因为用到读写文件~所以下面这个是YY文件写操作~
#i nclude
struct student
{char name[10];
int num;
int age;
char addr[15];
}stu[4];
void save()
{ FILE *fp;
int i;
if((fp=fopen(\"yy\",\"wb\"))==NULL)
{printf(\"cannot open file\\n\");
return;
}
for(i=0;i<4;i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
printf(\"file write error\\n\");
fclose(fp);
}
main()
{int i;
for(i=0;i<4;i++)
scanf(\"%s %d %d %s\",stu[i].name,&stu[i].num,&stu[i].age,stu[i].addr);
save();