voidprint(){int i, m =0;FILE* fp;if((fp =fopen("stu.txt","rb"))==NULL){printf("文件打开失败\n");return;}while(!feof(fp)){if(fread(&stu[m], LEN,1, fp)==1){m++;}}fclose(fp);if(m ==0){printf("文件中没有内容\n");}printf("学号\t姓名\t班级\t性别\t英语成绩\tC语言成绩\t总成绩\t平均成绩\n");for(i =0; i < m; i++){PRINT_STUDENT_INFO(i);//宏打印}}
录入学生信息(一维数组)
//录入学生信息 voidin(){voidprint();//封装的打印全部学生的函数声明,避免重复书写FILE* fp;int i, m =0;char ch[2];//打开文本if((fp =fopen("stu.txt","rb"))==NULL){printf("文件打开失败\n");return;}//检查文件指针fp所指向的文件是否到达了文件末尾(EOF)while(!feof(fp)){//从文件中读取数据if(fread(&stu[m], LEN,1, fp)==1){m++;//记录人数}}fclose(fp);if(m ==0){printf("文件没有内容\n");}else{print();}//二进制追加模式if((fp =fopen("stu.txt","ab+"))==NULL){printf("文件打开失败\n");return;}printf("输入学生信息(y/n);\n");scanf("%s", ch);while(strcmp(ch,"y")==0||strcmp(ch,"Y")==0){printf("number:\n");scanf("%3d",&stu[m].num);//编号合法性检查for(i =0; i < m; i++){if(stu[i].num == stu[m].num){printf("您的信息已存在\n");fclose(fp);getchar();return;}}printf("name:\n");scanf("%s", stu[m].name);printf("class:\n");scanf("%s", stu[m].clas);printf("sex:\n");scanf("%s", stu[i].sex);printf("Eng_score:\n");scanf("%f",&stu[m].Eng_score);printf("C_score:\n");scanf("%f",&stu[m].C_score);stu[i].total = stu[i].C_score + stu[i].Eng_score;stu[i].aver =(stu[i].C_score + stu[i].Eng_score)/ SUBJECT_NUMBER;if(fwrite(&stu[m], LEN,1, fp)!=1){printf("不能保存\n");getchar();}else{printf("信息保存成功\n");m++;}printf("是否继续录入学生信息(y/n):");scanf("%s", ch);}fclose(fp);}
删除
//删除学生信息 voiddelet(){voidprint();FILE* fp;int num, m =0;int i, j;int flag =0;char ch[2];if((fp =fopen("stu.txt","rb"))==NULL){printf("文件打开失败\n");return;}while(!feof(fp)){if(fread(&stu[m], LEN,1, fp)==1){m++;}}fclose(fp);if(m ==0){printf("文件中没有学生信息\n");}else{print();printf("请输入需要删除学生信息的学号:");scanf("%d",&num);}for(i =0; i < m; i++){if(stu[i].num == num){printf("该学生已找到,是否删除(y/n):");scanf("%s", ch);if(strcmp(ch,"y")==0||strcmp(ch,"Y")==0){//更新数组下标for(j = i; j < m; j++){stu[j]= stu[j +1];}m--;flag =1;//变更标志if((fp =fopen("stu.txt","wb"))==NULL){printf("文件打开失败\n");return;}//写入文件for(j =0; j < m; j++){if(fwrite(&stu[j], LEN,1, fp)!=1){printf("不能保存\n");getchar();}}fclose(fp);printf("删除成功!\n");break;}else{printf("学生信息存在,不需要删除。");break;}}}if(m == i && flag ==0){printf("未找到该学生");}}
//查询学生信息 voidselect(){int num, m =0;int i;FILE* fp;if((fp =fopen("stu.txt","rb"))==NULL){printf("文件打开失败\n");return;}while(!feof(fp)){if(fread(&stu[m], LEN,1, fp)==1){m++;}}fclose(fp);if(m ==0){printf("文件没有内容\n");return;}printf("请输入学生学号进行查询:");scanf("%d",&num);for(i =0; i < m; i++){if(num == stu[i].num){printf("\n");printf("该学生信息已被查到;\n");printf("学号\t姓名\t班级\t性别\t英语成绩\tC语言成绩\t总成绩\t平均成绩\n");PRINT_STUDENT_INFO(i);break;}}if(m == i){printf("该学生信息不存在\n");return;}}
排序(冒泡)
//排序 voidsort(){int i, j, m =0;int ch;FILE* fp;structstudent temp;if((fp =fopen("stu.txt","rb"))==NULL){printf("文件打开失败\n");return;}while(!feof(fp)){if(fread(&stu[m], LEN,1, fp)==1){m++;}}fclose(fp);if(m ==0){printf("文件中没有内容\n");}printf("1:学号升序,2:英语成绩升序,3:C语言升序,4:总成绩升序,5:平均成绩升序\n");printf("请选择显示形式:");scanf("%d",&ch);switch(ch){case1:for(i =0; i < m; i++){for(j = i +1; j < m; j++){if(stu[i].num > stu[j].num){temp = stu[i];stu[i]= stu[j];stu[j]= temp;}}}break;case2:for(i =0; i < m; i++){for(j = i +1; j < m; j++){if(stu[i].Eng_score > stu[j].Eng_score){temp = stu[i];stu[i]= stu[j];stu[j]= temp;}}}break;case3:for(i =0; i < m; i++){for(j = i +1; j < m; j++){if(stu[i].C_score > stu[j].C_score){temp = stu[i];stu[i]= stu[j];stu[j]= temp;}}}break;case4:for(i =0; i < m; i++){for(j = i +1; j < m; j++){if(stu[i].total > stu[j].total){temp = stu[i];stu[i]= stu[j];stu[j]= temp;}}}break;case5:for(i =0; i < m; i++){for(j = i +1; j < m; j++){if(stu[i].aver > stu[j].aver){temp = stu[i];stu[i]= stu[j];stu[j]= temp;}}}break;}printf("学号\t姓名\t班级\t性别\t英语成绩\tC语言成绩\t总成绩\t平均成绩\n");for(i =0; i < m; i++){PRINT_STUDENT_INFO(i);}}
最大值
//最大值 void_max(){int i, j =0;FILE* fp;int m =0;if((fp =fopen("stu.txt","rb"))==NULL){printf("文件打开失败\n");return;}while(!feof(fp)){if(fread(&stu[m], LEN,1, fp)==1){m++;}}fclose(fp);if(m ==0){printf("文件中没有内容\n");}for(i =0; i < m; i++){if(stu[i].total > stu[j].total)j = i;}printf("总成绩成绩最高的同学是:\n");printf("学号\t姓名\t班级\t性别\t英语成绩\tC语言成绩\t总成绩\t平均成绩\n");PRINT_STUDENT_INFO(j)}
分组统计
//分组统计voidcut(){int i, j;float n;FILE* fp;int m =0;if((fp =fopen("stu.txt","rb"))==NULL){printf("文件打开失败\n");return;}while(!feof(fp)){if(fread(&stu[m], LEN,1, fp)==1){m++;}}fclose(fp);if(m ==0){printf("文件中没有内容\n");}printf("请输入按总成绩分组的值:");scanf("%f",&n);printf("总成绩大于%.2f的同学有:\n", n);printf("学号\t姓名\t班级\t性别\t英语成绩\tC语言成绩\t总成绩\t平均成绩\n");for(i =0; i < m; i++){if(stu[i].total >= n)PRINT_STUDENT_INFO(i);}printf("总成绩小于%.2f的同学有:\n", n);printf("学号\t姓名\t班级\t性别\t英语成绩\tC语言成绩\t总成绩\t平均成绩\n");for(j =0; j < m; j++){if(stu[j].total < n)PRINT_STUDENT_INFO(j);}