满意答案
arhrh596
2014.07.02
采纳率:50% 等级:9
已帮助:2665人
#include
#include
#include
#include
#include #define N 100
struct student
{char XUEHAO[20]; char XINGMING[10]; char XINGBIE[4]; char SHENGRI[10]; char JIGUAN[10]; char ZHUANYE[10]; char BANJI[6]; char XUEYUAN[20];}student;void CAIDAN();
void ZHUCE();
void XIUGAI();
void SHANCHU();
void del_XUEHAO(FILE *fp);
void del_XINGMING(FILE *fp);
void CHAZHAO();
void sch_XUEHAO(FILE *fp);
void sch_XINGMING(FILE *fp);
void TONGJI();
void XIANSHI();
void TIANJIA();
void save( struct student student[]);main()
{ char choose,yes_no;
do
{ CAIDAN();
printf("\n 请输入序号: (1~8) ");
choose=getch();
system("cls");
switch(choose)
{
case'1': printf(" %c",choose);ZHUCE();break;
case'2': printf(" %c",choose);XIUGAI();break;
case'3': printf(" %c",choose);SHANCHU();break;
case'4': printf(" %c",choose);TIANJIA();break;
case'5': printf(" %c",choose);CHAZHAO();break;
case'6': printf(" %c",choose);TONGJI();break;
case'7': printf(" %c",choose);XIANSHI();break;
case'8': break;
default:printf("\n %c 序号不对!\n",choose);
}
if(choose=='8')break;
printf("\n 要继续选择吗 ? (Y/N) \n");
do
{ yes_no=getch(); printf(" %c",yes_no);}
while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no=='Y'||yes_no=='y');
}
void CAIDAN()
{
system("cls");
printf(" **************************************** \n ");
printf(" * ——请选择相应的功能—— * \n ");
printf(" * 1-----注册 * \n ");
printf(" * 2-----修改学生信息 * \n ");
printf(" * 3-----删除学生信息 * \n ");
printf(" * 4-----添加学生信息 * \n ");
printf(" * 5-----查找学生信息 * \n ");
printf(" * 6-----统计学生信息 * \n ");
printf(" * 7-----显示学生信息 * \n ");
printf(" * 8-----退出系统 * \n ");
printf(" **************************************** \n ");
}/*定义学生信息注册的函数*/
void ZHUCE()
{ int i=1;
struct student temp;
FILE *fp;
fp=fopen("G://123.txt","w");
if(fp==NULL){ printf("\n 打开文件失败! \n");return; }
printf("\n 请输入第1个学生信息:\n");
printf(" 学号(用#结束输入): ");
do
{ gets(temp.XUEHAO);}
while(strcmp(temp.XUEHAO," ")==0);
printf(" 姓名(用#结束输入): ");gets(temp.XINGMING);
printf(" 性别(用#结束输入): ");gets(temp.XINGBIE);
printf(" 生日(用#结束输入): ");gets(temp.SHENGRI);
printf(" 籍贯(用#结束输入): ");gets(temp.JIGUAN);
printf(" 专业(用#结束输入): ");gets(temp.ZHUANYE);
printf(" 班级(用#结束输入): ");gets(temp.BANJI);
printf(" 学院(用#结束输入): ");gets(temp.XUEYUAN);
while(temp.XUEHAO[0]!='#'&&temp.XINGMING[0]!='#'&&temp.XINGBIE[0]!='#'&&temp.SHENGRI[0]!='#'&&temp.JIGUAN[0]!='#'&&temp.ZHUANYE[0]!='#'&&temp.BANJI[0]!='#'&&temp.XUEYUAN[0]!='#')
{ fprintf(fp,"%5s%10s%10s%10s%10s%10s%10s%10s\n",temp.XUEHAO,temp.XINGMING,temp.XINGBIE,temp.SHENGRI,temp.JIGUAN,temp.ZHUANYE,temp.BANJI,temp.XUEYUAN,temp);
i++;
printf("\n 请输入第 %d 个学生信息:\n ",i);
printf(" 学号(用#结束输入): ");
do
{ gets(temp.XUEHAO);}
while(strcmp(temp.XUEHAO," ")==0);
printf(" 姓名(用#结束输入): ");gets(temp.XINGMING);
printf(" 性别(用#结束输入): ");gets(temp.XINGBIE);
printf(" 生日(用#结束输入): ");gets(temp.SHENGRI);
printf(" 籍贯(用#结束输入): ");gets(temp.JIGUAN);
printf(" 专业(用#结束输入): ");gets(temp.ZHUANYE);
printf(" 班级(用#结束输入): ");gets(temp.BANJI);
printf(" 学院(用#结束输入): ");gets(temp.XUEYUAN);
}
student=temp;
save(&temp);
fclose(fp);
}/*定义保存函数*/
void save( struct student student[])
{
FILE *fp=NULL;
fp=fopen("G://123.txt","ab+");
fwrite(student,sizeof(struct student),1,fp);
fclose(fp);
}
/*定义查找学生信息的函数*/
void CHAZHAO()
{ char c;
FILE *fp;
fp=fopen("G://123.txt","r");
if(fp==NULL)
{ printf("\n 打开文件失败! \n");return; }
printf("\n 按学号查找( 1 ),还是按姓名查找( 2 )? ");
c=getch();
putch(c);
if(c=='1') sch_XUEHAO(fp);
else if(c=='2') sch_XINGMING(fp);
else printf("\n 非法字符!\n");
fclose(fp);
}
/*定义按学号查找学生信息的函数*/
void sch_XUEHAO(FILE *fp)
{ int flag=0,n=0;
char tempXUEHAO[20];
struct student temp;
printf("\n 请输入要查找学生信息的学号: ");
gets(tempXUEHAO);
while(feof(fp)==0)
{ fscanf(fp,"%5s%10s%10s%10s%10s%10s%10s%10s\n",&temp.XUEHAO,&temp.XINGMING,&temp.XINGBIE,&temp.SHENGRI,&temp.JIGUAN,&temp.ZHUANYE,&temp.BANJI,&temp.XUEYUAN);
if(strcmp(tempXUEHAO,temp.XUEHAO)==0)
{ if(flag==0)
printf(" 学号 姓名 性别 生日 籍贯 专业 班级 学院\n");
printf("%5s%10s%10s%10s%10s%10s%10s%10s\n",temp.XUEHAO,temp.XINGMING,temp.XINGBIE,temp.SHENGRI,temp.JIGUAN,temp.ZHUANYE,temp.BANJI,temp.XUEYUAN);
flag=1;
}
n++;
}
if(n==0) printf("\n 文件中无此信息!\n");
else if(flag==0) printf("\n 文件中无此人!\n");
}
/*定义按姓名查找学生信息的函数*/
void sch_XINGMING(FILE *fp)
{ int flag=0,n=0;
char tempXINGMING[10];
struct student temp;
printf("\n 请输入要查找学生信息的姓名: ");
gets(tempXINGMING);
while(feof(fp)==0)
{ fscanf(fp, "%5s%10s%10s%10s%10s%10s%10s%10s\n",&temp.XUEHAO,&temp.XINGMING,&temp.XINGBIE,&temp.SHENGRI,&temp.JIGUAN,&temp.ZHUANYE,&temp.BANJI,&temp.XUEYUAN);
if(strcmp(tempXINGMING,temp.XINGMING)==0)
{ if(flag==0) printf(" 学号 姓名 性别 生日 籍贯 专业 班级 学院\n");
printf("%5s%10s%10s%10s%10s%10s%10s%10s\n",temp.XUEHAO,temp.XINGMING,temp.XINGBIE,temp.SHENGRI,temp.JIGUAN,temp.ZHUANYE,temp.BANJI,temp.XUEYUAN);
flag=1;
}
n++;
}
if(n==0) printf("\n 文件中无此信息!\n");
else if(flag==0) printf("\n 文件中无此人!\n");
}
/*定义删除学生信息的函数*/
void SHANCHU()
{ char c;
FILE *fp;
fp=fopen("G://123.txt","r");
if(fp==NULL)
{ printf("\n 文件打开失败!\n ");return; }
printf("\n 按学号删除( 1 ),还是按姓名删除( 2 )? ");
c=getch();
putch(c);
if(c=='1') del_XUEHAO(fp);
else if(c=='2') del_XINGMING(fp);
else printf("\n 非法字符!\n");
}/*定义按学号删除学生信息的函数*/
void del_XUEHAO(FILE *fp)
{ char tempXUEHAO[20];
int n=0,i=0,k;
struct student stu[N];
while(feof(fp)==0)
{ fscanf(fp,"%5s%10s%10s%10s%10s%10s%10s%10s\n",stu[i].XUEHAO,stu[i]. XINGMING,stu[i].XINGBIE,stu[i].SHENGRI,stu[i].JIGUAN,stu[i].ZHUANYE,stu[i].BANJI,stu[i].XUEYUAN);i++;n++;}
fclose(fp);
printf("\n 请输入要删除的学生信息的学号:");
scanf("%s",tempXUEHAO);
printf(" 删除成功! ");
for(i=0;i
if(strcmp(tempXUEHAO,stu[i].XUEHAO)==0)break;
if(i
for(k=i;k
stu[k]=stu[k+1];
else printf("\n 无此人!\n");
fp=fopen("G://123.txt","w");
if(fp==NULL)
{ printf("\n 文件打开失败!\n");return; }
for(i=0;i
fprintf(fp,"%5s%10s%10s%10s%10s%10s%10s%10s\n",stu[i].XUEHAO,stu[i].XINGMING,stu[i].XINGBIE,stu[i].SHENGRI,stu[i].JIGUAN,stu[i].ZHUANYE,stu[i].BANJI,stu[i].XUEYUAN);
}
616分享举报