#include
#include
#include
#include
struct stuType
{
char name[9]; //姓名
char gender[3]; //性别
int age; //年龄
char phone[11]; //手机号
int stature; //身高
}stud[100]; //;不可少
/**********************************************/
void GotoXY(int x, int y);
void shuru();
void zhuye();
void chaxun();
void chaxunsx();
void chaxun_name();
void chaxunlb();
void chaxunxm1();
void chaxunxm2();
void displist();
void change();
void sort();
void sort_age();
void sort_stature();
void statistics();
/************************************************/
void main()//主函数
{
zhuye();//系统首页
}
/************************************************/
void GotoXY(int x, int y)//坐标定位函数
{
COORD c;
c.X = x - 1;
c.Y = y - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
/*******************控制输入的函数*****************************/
void shuru()
{
int gdok;
int nmOk;
int ageok;
int stok;
char keypress;
FILE *fp;
if((fp=fopen("stuType", "a"))==NULL)
{
printf("cannot open file\n");
return;
}
GotoXY(30,3);
printf("请顺序输入各项信息");
GotoXY(5,4);
printf("================================================================ ===");
GotoXY(5,6);
printf("姓名: 性别: 年龄: 手机号码: 身高(cm): ");
for(int i=0; i<100; i++)
{
nmOk=false;
while(!nmOk)
{
GotoXY(10,6);
scanf("%s",stud[i].name);
if(stud[i].name=='\0')
{
GotoXY(10,8);
printf("名字不能为空,按任意键继续...");
GotoXY(10,8);
printf(" ");
getche();
GotoXY(10,8);
printf(" ");
}
else
{
nmOk=true;
}
}
gdok=false;
while(!gdok)
{
GotoXY(25,6);
scanf("%s",stud[i].gender);
if(stud[i].gender=='\0')
{
GotoXY(10,9);
printf("性别不能为空,按任意键继续...");
GotoXY(10,9);
printf(" ");
getche();
GotoXY(10,9);
printf(" ");
}
else
{
gdok=true;
}
}
ageok=false;
while(!ageok)
{
GotoXY(34,6);
printf(" ");
GotoXY(34,6);
scanf("%d",&stud[i].age);
if(stud[i].age>100||stud[i].age<0)
{
GotoXY(10,9);
printf("年龄必须为0到100的整数,按任意键继续...");
getche();
GotoXY(10,9);
printf(" ");
}
else
{
ageok=true;
}
}
GotoXY(47,6);
scanf("%s",stud[i].phone);
stok=false;
while(!stok)
{
GotoXY(71,6);
printf(" ");
GotoXY(71,6);
scanf("%d",&stud[i].stature);
if(stud[i].stature>230||stud[i].stature<50)
{
GotoXY(10,9);
printf("身高必须为50到230的数,按任意键继续...");
getche();
GotoXY(10,9);
printf(" ");
}
else
{
stok=true;
}
}
if(fwrite(&stud[i], sizeof(struct stuType), 1, fp)!=1)
printf("file write error\n");
fclose(fp);
GotoXY(10,9);
printf("是否继续添加信息(Y/N)");
scanf("%s",&keypress);
if(keypress=='N'||keypress=='n')
{
system("cls");
zhuye();
}
else if(keypress=='Y'||keypress=='y')
{ system("cls");
shuru();
}
else
{
exit(0);
}
}
}
void zhuye()
{
int select;
GotoXY(30,3);
printf("同学通信录管理系统");
GotoXY(15,4);
printf("==============================================="); GotoXY(15,6);
printf("1:输入");
GotoXY(35,6);
printf("2:查询");
GotoXY(55,6);
printf("3:修改");
GotoXY(15,10);
printf("4:排序");
GotoXY(35,10);
printf("5:统计");
GotoXY(55,10);
printf("0:退出");
GotoXY(15,12);
printf("===============================================");
GotoXY(15,14);
printf("请输入要查询的选项:");
select=getch();
if(select<48||select>53){
printf("请输入0到5选项");
}
else
{
switch(select)
{
case 49://1:输入
system("cls");
shuru();
break;
case 50://2:查询
system("cls");
chaxun();
break;
case 51://3:修改
system("cls");
change();
break;
case 52://4:排序
system("cls");
sort();
break;
case 53://5:统计
system("cls"); statistics();
break;
case 48://0:退出
exit(0);
break;
}
}
}
void chaxun()
{
int selectcx;
int prX=5;
int prY=5;
GotoXY(prX,prY+2);
printf("1.顺序查询"); GotoXY(prX,prY+4);
printf("2.按姓名查询");
GotoXY(prX,prY+6);
printf("3.列表查询");
GotoXY(prX,prY+8);
printf("0.结束查询");
GotoXY(prX,prY+10);
printf("请选择您要查询的方式:"); selectcx=getch();
if(selectcx<48||selectcx>51){
printf("请输入0到3选项");
}
else
{
switch(selectcx)
{
case 48://0:结束查询
system("cls");
zhuye();
break;
case 49://1:顺序查询
system("cls");
chaxunsx();
break;
case 50://2:姓名查询
system("cls");
chaxun_name();
break;
case 51://3:列表查询
system("cls");
displist();
break;
}
}
}
void chaxunsx()
{
printf("以下为您查询的信息(顺序查询)");
int i=0;
char key1;
FILE *fp;
if((fp=fopen("stuType", "r"))==NULL)
{
printf("can not open file\n");
return;
}
while(!feof(fp))
{
fread(&stud[i], sizeof(struct stuType), 1, fp);
GotoXY(10,3);
printf("姓名性别年龄手机身高(CM)"); GotoXY(10,5);
printf("%s %s %s %s %d %s %s %s %d\n",stud[i].name,"
",stud[i].gender," ", stud[i].age," ", stud[i].phone,"
",stud[i].stature);
GotoXY(10,7);
printf("%s %d %s","当前是第",i+1,"条记录");
GotoXY(10,9);
printf("是否继续查询(Y/N):");
if(feof(fp))
{
system("cls");
printf("结束查询,输入N返回上一层,按任意键关闭窗口:");
}
scanf("%s",&key1);
if(key1=='y'||key1=='Y')
{
system("cls");
i=i+1;
}
if(key1=='N'||key1=='n')
{
system("cls");
chaxun();
}
}
fclose(fp);
}
void chaxun_name()
{
printf("\n\n%s"," 1:精确查询");
printf("\n\n%s"," 2:模糊查询");
printf("\n\n%s"," 0:返回");
printf("\n\n%s","请输入您要查询的方式:");
int selectxm;
selectxm=getch();
if(selectxm<48||selectxm>50){
printf("请输入0到2选项");
}
else
{
switch(selectxm)
{
case 48://0:结束查询
system("cls");
chaxun();
break;
case 49://1:全称查询
system("cls");
chaxunxm1();
break;
case 50://2:模糊查询
system("cls");
chaxunxm2();
break;
}
}
}
void chaxunxm1()
{
char search[9];
char key2;
FILE *fp;
printf("请输入您要查询的姓名:");
if((fp=fopen("stuType", "r"))==NULL)
{
printf("can not open file\n");
return;
}
scanf("%s",&search);
GotoXY(1,3);
printf("姓名性别年龄手机身高(CM)"); GotoXY(1,5);
printf("===================================================");
for(int i=0;i<100&&!feof(fp);i++)
{
fread(&stud[i], sizeof(struct stuType), 1, fp);
if(strcmp(search,stud[i].name)==0)
{
printf("\n\n%s %s %s %s %d %s %s %s %d\n",stud[i].name," ",stud[i].gender," ", stud[i].age," ", stud[i].phone,"
",stud[i].stature);
}
}
printf("\n%s","是否继续查询(Y/N):");
scanf("%s",&key2);
if(key2=='y'||key2=='Y')
{
system("cls");
chaxunxm1();
}
if(key2=='N'||key2=='n')
{
system("cls");
chaxun_name();
}
}
void chaxunxm2()
{
char search[9];
char key2;
FILE *fp;
printf("请输入您要查询的字段:");
if((fp=fopen("stuType", "r"))==NULL)
{
printf("can not open file\n");
return;
}
scanf("%s",&search);
GotoXY(1,3);
printf("姓名性别年龄手机身高(CM)");
GotoXY(1,5);
printf("===================================================");
for(int i=0;i<100&&!feof(fp);i++)
{
fread(&stud[i], sizeof(struct stuType), 1, fp);
char *str1 = stud[i].name, *str2 = search, *ptr;
if(ptr = strstr(str1, str2))
{
printf("\n\n%s %s %s %s %d %s %s %s %d\n",stud[i].name," ",stud[i].gender," ", stud[i].age," ", stud[i].phone,"
",stud[i].stature);
}
}
printf("\n%s","是否继续查询(Y/N):");
scanf("%s",&key2);
if(key2=='y'||key2=='Y')
{
system("cls");
chaxunxm2();
}
if(key2=='N'||key2=='n')
{
system("cls");
chaxun_name();
}
}
void displist()
{ int pagelen = 20; //一页显示的记录数
int cntInPage;
int count;
int i = 0;
FILE *fp;
if((fp=fopen("stuType", "r"))==NULL)
{
printf("can not open file\n");
return;
}
for(int k=0; k<100&& !feof(fp); k++)
{
fread(&stud[k], sizeof(struct stuType), 1, fp);
}
count=k-1;
fclose(fp);
while(i < count)
{
system("cls");
GotoXY(10, 1);
printf("共有 %d 位同学,每页显示 %d 个记录", count, pagelen);
GotoXY(10, 2);
printf("===============================================");
GotoXY(10, 3);
printf(" 姓名性别年龄手机身高");
cntInPage = 1;
while(i { fread(&stud[i], sizeof(struct stuType), 1, fp); GotoXY(10, 4+cntInPage); printf("%s", stud[i].name); GotoXY(25, 4+cntInPage); printf("%s %d %s %d", stud[i].gender,stud[i].age,stud[i].phone,stud[i].stature); cntInPage++; i++; } GotoXY(10, 24); printf("==============================================="); GotoXY(10, 25); if(i printf("按任意键继续显示下一页..."); else printf("查询结束,按任意键退出"); getch(); } } void change() { int count; FILE *fp; if((fp=fopen("stuType", "r"))==NULL) { printf("can not open file\n"); return; } for(int t=0; t<100&& !feof(fp); t++) { fread(&stud[t], sizeof(struct stuType), 1, fp); } count=t-1; fclose(fp); printf("先查询到您要修改的信息:"); char search[9]; printf("请输入您要查询的字段:"); if((fp=fopen("stuType", "r"))==NULL) { printf("can not open file\n"); return; } scanf("%s",&search); GotoXY(1,3); printf("姓名性别年龄手机身高(CM)"); GotoXY(1,5); printf("==================================================="); for(int i=0;i<100&&!feof(fp);i++) { fread(&stud[i], sizeof(struct stuType), 1, fp); char *str1 = stud[i].name, *str2 = search, *ptr; if(ptr = strstr(str1, str2)) { printf("\n\n%s %s %s %s %d %s %s %s %d 第%d个记录 ",stud[i].name," ",stud[i].gender," ", stud[i].age," ", stud[i].phone," ",stud[i].stature,i); } } printf("\n\n%s","选择要修改的记录的编号:"); scanf("%d",&i); fread(&stud[i], sizeof(struct stuType), 1, fp); system("cls"); GotoXY(5,3); printf("%s %s %s %s %d %s %s %s %d",stud[i].name," ",stud[i].gender," ", stud[i].age," ", stud[i].phone," ",stud[i].stature); GotoXY(10,5); printf("请输入您要修改为的信息,"); GotoXY(10,7); printf("================================"); GotoXY(10,9); printf("姓名:"); GotoXY(10,11); printf("性别:"); GotoXY(10,13); printf("年龄:"); GotoXY(10,15); printf("手机:"); GotoXY(10,17); printf("身高:"); GotoXY(10,19); printf("================================"); GotoXY(10,20); printf("以下为您修改后的信息"); GotoXY(15,9); scanf("%s",stud[i].name); GotoXY(15,11); scanf("%s",&stud[i].gender); GotoXY(15,13); scanf("%d",&stud[i].age); GotoXY(15,15); scanf("%s",stud[i].phone); GotoXY(15,17); scanf("%d",&stud[i].stature); if((fp=fopen("stuType", "w"))==NULL) { printf("can not open file\n"); return; } for(int k=0;k { if(fwrite(&stud[k], sizeof(struct stuType), 1, fp)!=1) printf("file write error\n"); } fclose(fp); GotoXY(5,21); printf("%s %s %s %s %d %s %s %s %d\n",stud[i].name," ",stud[i].gender," ", stud[i].age," ", stud[i].phone," ",stud[i].stature); char key2; GotoXY(5,23); printf("是否继续修改(N/Y)"); key2=getche(); if(key2=='y'||key2=='Y') { system("cls"); change(); } if(key2=='N'||key2=='n') { system("cls"); zhuye(); } } void sort() { int selectsort; GotoXY(10,7); printf("1:按年龄排列"); GotoXY(10,9); printf("2:按身高排列"); GotoXY(10,5); printf("请选择您要排列的方式(按0返回):"); selectsort=getch(); switch(selectsort) { case 49://1:年龄排序 system("cls"); sort_age(); break; case 50://2:身高排序 system("cls"); sort_stature(); break; case 48://0:返回 system("cls"); zhuye(); break; } } void sort_age() { int pagelen = 20; //一页显示的记录数 int cntInPage; int count; int i = 0; FILE *fp; if((fp=fopen("stuType", "r"))==NULL) { printf("can not open file\n"); return; } for(int k=0; k<100&& !feof(fp); k++) { fread(&stud[k], sizeof(struct stuType), 1, fp); } count=k-1; fclose(fp); while(i < count) { system("cls"); GotoXY(10, 1); printf("共有 %d 位同学,每页显示 %d 个记录", count, pagelen); GotoXY(10, 2); printf("==============================================="); GotoXY(10, 3); printf(" 姓名性别年龄手机身高"); cntInPage = 1; while(i { fread(&stud[i], sizeof(struct stuType), 1, fp); /****************maopao*****************************/ int count_age=count; while(count_age>=0) { for(int i=0;i<=count_age-2;i++) { stuType a; if(stud[i].age>stud[i+1].age) { a=stud[i+1]; stud[i+1]=stud[i]; stud[i]=a; } } count_age--; } /************************************************/ GotoXY(10, 4+cntInPage); printf("%s", stud[i].name); GotoXY(25, 4+cntInPage); printf("%s %d %s %d", stud[i].gender,stud[i].age,stud[i].phone,stud[i].stature); cntInPage++; i++; } GotoXY(10, 24); printf("==============================================="); GotoXY(10, 25); if(i printf("按任意键继续显示下一页..."); else printf("查询结束,按任意键退出"); getch(); } } void sort_stature() { int pagelen = 20; //一页显示的记录数 int cntInPage; int count; int i = 0; FILE *fp; if((fp=fopen("stuType", "r"))==NULL) { printf("can not open file\n"); return; } for(int k=0; k<100&& !feof(fp); k++) { fread(&stud[k], sizeof(struct stuType), 1, fp); } count=k-1; fclose(fp); while(i < count) { system("cls"); GotoXY(10, 1); printf("共有 %d 位同学,每页显示 %d 个记录", count, pagelen); GotoXY(10, 2); printf("==============================================="); GotoXY(10, 3); printf(" 姓名性别年龄手机身高"); cntInPage = 1; while(i { fread(&stud[i], sizeof(struct stuType), 1, fp); /****************maopao*****************************/ int count_stature=count; while(count_stature>=0) { for(int i=0;i<=count_stature-2;i++) { stuType a; if(stud[i].stature>stud[i+1].stature) { a=stud[i+1]; stud[i+1]=stud[i]; stud[i]=a; } } count_stature--; } /************************************************/ GotoXY(10, 4+cntInPage); printf("%s", stud[i].name); GotoXY(25, 4+cntInPage); printf("%s %d %s %d", stud[i].gender,stud[i].age,stud[i].phone,stud[i].stature); cntInPage++; i++; } GotoXY(10, 24); printf("==============================================="); GotoXY(10, 25); if(i printf("按任意键继续显示下一页..."); else printf("查询结束,按任意键退出"); getch(); } } void statistics() { float ave_age; float ave_stature; float adds_age=0; float adds_stature=0; char key3; FILE *fp; if((fp=fopen("stuType", "r"))==NULL) { printf("can not open file\n"); return; } for(int t=0; t<100&& !feof(fp); t++) { fread(&stud[t], sizeof(struct stuType), 1, fp); adds_age=adds_age+stud[t].age; adds_stature=adds_stature+stud[t].stature; } ave_age=adds_age/(t-1); ave_stature=adds_stature/(t-1); GotoXY(5,5); printf("1.所有人的平均年龄为:"); printf("%f",ave_age); GotoXY(5,7); printf("2.所有人的平均身高为:"); printf("%f",ave_stature); fclose(fp); GotoXY(5,9); printf("0.按Q返回,任意键退出:"); key3=getch(); if(key3=='q'||key3=='Q') { system("cls"); zhuye(); } }