编写调查学生户口薄信息的程序,大家给点意见。。
#include<conio.h>#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
typedef struct
{
char id[12];
char name[21];
char origin[101];
}student;
FILE *fp;
student stu,cache;
void Menu()
{
system("cls");
time_t t = time(0);
char tmp[64];
strftime(tmp, sizeof(tmp), "%Y/%m/%d %A",localtime(&t));
puts(tmp);
printf("\n");
printf("1 --输入籍贯信息\n");
printf("2 --显示所有信息\n");
printf("3 --按学号查询\n");
printf("4 --按姓名查询\n");
printf("5 --按籍贯查询\n");
printf("0 --退出\n\n\n请输入选项:");
}
void Creat_add()
{
system("cls");
fp=fopen("stu_origin.dat","rb+");
if(fp==NULL)
{
fp=fopen("stu_origin.dat","wb+");
if(fp==NULL)
{
printf("can't creat the file stu_origin.dat!\n");
printf("Press any key to exit...");getch();
}
}
fseek(fp,0,SEEK_END);
char next='y';
while(next=='y'||next=='Y')
{
printf("请输入学号:");scanf("%s",stu.id);
printf("请输入姓名:");scanf("%s",stu.name);
printf("请输入籍贯:");scanf("%s",stu.origin);
printf("\n\n确定写入数据?(y/n)");
if(getch()=='y'||getch()=='Y')
fwrite(&stu,sizeof(stu),1,fp);
printf("\n\n是否继续输入?(y/n)");
next=getch();
system("cls");
}
fclose(fp);
}
void Display_all()
{
system("cls");
fp=fopen("stu_origin.dat","rb");
if(fp==NULL)
{
printf("can't open the file stu_origin.dat!\nPress any key to get back...");
getch();
}
else
{
rewind(fp);printf("\n\n学号\t\t姓名\t\t籍贯\n");
while(fread(&stu,sizeof(stu),1,fp))
printf("%s\t%s\t%s\n",stu.id,stu.name,stu.origin);
fclose(fp);
printf("\n\nPress any key to get back...");getch();
}
}
int Check_Id(char *Id,char c)
{
rewind(fp);
while(fread(&cache,sizeof(stu),1,fp))
{
if(strcmp(Id,cache.id)==0)
{
printf("\n\n学号\t\t姓名\t\t籍贯\n");
printf("%s\t%s\t%s\n",cache.id,cache.name,cache.origin);
return 0;
}
}
printf("\n\n无此学生!\n");
return 1;
}
int Check_Name(char *Name,char c)
{
int n=0;
rewind(fp);
while(fread(&cache,sizeof(stu),1,fp))
{
if(strcmp(Name,cache.name)==0)
{
if(++n==1)
printf("\n\n学号\t\t姓名\t\t籍贯\n");
printf("%s\t%s\t%s\n",cache.id,cache.name,cache.origin);
}
}
if(!n)
{
printf("\n\n无此学生!\n");
return 1;
}
return 0;
}
int Check_Orgin(char *Origin,char c)
{
int n=0;
rewind(fp);
while(fread(&cache,sizeof(stu),1,fp))
{
if(strcmp(Origin,cache.origin)==0)
{
if(++n==1)printf("\n\n学号\t\t姓名\t\t籍贯\n");
printf("%s\t%s\t%s\n",cache.id,cache.name,cache.origin);
}
}
if(!n){printf("\n\n无此学生!\n");return 1;}
return 0;
}
void Search_id()
{
fp=fopen("stu_origin.dat","rb");
if(fp==NULL)
{
printf("can't open the file stu_origin.dat!\nPress any key to get back...");
getch();
}
char k;
do
{system("cls");
do{
printf("请输入学号:");
gets(stu.id);
}while(Check_Id(stu.id,'s'));
printf("\n\n继续查询?(y/n)");
k=getch();
}while(k=='y'||k=='Y');
fclose(fp);
printf("\nPress any key to get back...");
getch();
}
void Search_name()
{
fp=fopen("stu_origin.dat","rb");
if(fp==NULL) {printf("can't open the file stu_origin.dat!\nPress any key to get back...");getch();}
do
{system("cls");
do{
printf("请输入姓名:");
gets(stu.name);
}while(Check_Name(stu.name,'s'));
printf("\n\n继续查询?(y/n)");
}while(getch()=='y'||getch()=='Y');
fclose(fp);
printf("\nPress any key to get back...");getch();
}
void Search_origin()
{
fp=fopen("stu_origin.dat","rb");
if(fp==NULL) {printf("can't open the file stu_origin.dat!\nPress any key to get back...");getch();}
do
{system("cls");
do{
printf("请输入籍贯:");
gets(stu.origin);
}while(Check_Orgin(stu.origin,'s'));
printf("\n\n继续查询?(y/n)");
}while(getch()=='y'||getch()=='Y');
fclose(fp);
printf("\nPress any key to get back...");
getch();
}
void Call()
{
char key=getch();
if(key=='1') Creat_add();
if(key=='2') Display_all();
if(key=='3') Search_id();
if(key=='4') Search_name();
if(key=='5') Search_origin();
if(key=='0') exit(1);
}
void main()
{
while(1){Menu();Call();}
}