From 19699118b08e2b888541cd85ec93c2e514458ab4 Mon Sep 17 00:00:00 2001 From: pq52sx6n8 <2731407373@qq.com> Date: Thu, 29 May 2025 01:58:25 +0800 Subject: [PATCH] =?UTF-8?q?ADD=20=E6=9C=80=E7=BB=88=E7=A8=8B=E5=BA=8F2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 学生成绩管理对文件操作 .cpp | 544 +++++++++++++++++++++++++ 1 file changed, 544 insertions(+) create mode 100644 学生成绩管理对文件操作 .cpp diff --git a/学生成绩管理对文件操作 .cpp b/学生成绩管理对文件操作 .cpp new file mode 100644 index 0000000..610a997 --- /dev/null +++ b/学生成绩管理对文件操作 .cpp @@ -0,0 +1,544 @@ +#include +#include +#include + +typedef struct { + int rank; // + char num[20]; // ѧ + char name[10]; // + int math_score; // ɼ + int datastruct_score; // ݽṹɼ + int English_score; // Ӣɼ + int physics_score; // ɼ + float average; // ƽ +} Student; + +typedef struct { + Student *data; + int length; +} funlist; + +// ʼԱ +void initlist(funlist *l) { + l->data = (Student *)malloc(100 * sizeof(Student)); + l->length = 0; +} + +// жǷΪձ +int isempty(funlist *l) { + return l->length == 0; +} + +//һĿո +void printspace(){ + int i; + for(i=0;i<60;i++){ + printf(" "); + } +} + +// ƽ +float calculate_average(Student *s) { + return (s->math_score + s->datastruct_score + s->English_score + s->physics_score) / 4.0f; +} + +// ҲѧŲңļ +int find_by_num(char *num) { + struct student_type{ + int rank; // + char num[20]; // ѧ + char name[10]; // + int math_score; // ɼ + int datastruct_score; // ݽṹɼ + int English_score; // Ӣɼ + int physics_score; // ɼ + float average; // ƽ + } Student; + + FILE *fp; + if((fp=fopen("studinfo.dat","rb+"))==NULL){ + printf("cannot open file\n"); + return 0; + } + while(!feof(fp)) + { + if(fread(&Student,sizeof(struct student_type),1,fp)) + { + if(strcmp(num,Student.num)==0) + { + printf("ѧϢΪ\n"); + printf("ѧ:%-20s :%-10s :%-3d ݽṹ:%-3d Ӣ:%-3d :%-3d ƽ:%-4.2f\n",Student.num,Student.name,Student.math_score,Student.datastruct_score,Student.English_score,Student.physics_score,Student.average); + system("pause"); + fclose(fp); + + return 0; + } + } + + } + printf("ѧϢ!\n"); + fclose(fp); + return 0; +} + +// Ҳңļ +int find_by_name(char *name) { + struct student_type{ + int rank; // + char num[20]; // ѧ + char name[10]; // + int math_score; // ɼ + int datastruct_score; // ݽṹɼ + int English_score; // Ӣɼ + int physics_score; // ɼ + float average; // ƽ + } Student; + + FILE *fp; + if((fp=fopen("studinfo.dat","rb+"))==NULL){ + printf("cannot open file\n"); + return 0; + } + while(!feof(fp)) + { + if(fread(&Student,sizeof(struct student_type),1,fp)) + { + if(strcmp(name,Student.name)==0) + { + printf("ѧϢΪ\n"); + printf("ѧ:%-20s :%-10s :%-3d ݽṹ:%-3d Ӣ:%-3d :%-3d ƽ:%-4.2f\n",Student.num,Student.name,Student.math_score,Student.datastruct_score,Student.English_score,Student.physics_score,Student.average); + system("pause"); + fclose(fp); + + return 0; + } + } + + } + printf("ѧϢ!\n"); + fclose(fp); + return 0; +} + +//޸IJ(ѧ޸) (studinfo.datļݣ +int revisestudinfo_by_num(char *num){ + struct student_type{ + int rank; // + char num[20]; // ѧ + char name[10]; // + int math_score; // ɼ + int datastruct_score; // ݽṹɼ + int English_score; // Ӣɼ + int physics_score; // ɼ + float average; // ƽ + } Student; + FILE *fp; + long offset=0; //һoffset,ڴļλñǵƫ + if((fp=fopen("studinfo.dat","rb+"))==NULL){ //Զļжд + printf("cannot open file\n"); + return 0; + } + while(!feof(fp)) + { + if(fread(&Student,sizeof(struct student_type),1,fp)) + { + if(strcmp(num,Student.num)==0) + { + printf("ԭѧϢΪ\n"); + printf("ѧ:%-20s :%-10s :%-3d ݽṹ:%-3d Ӣ:%-3d :%-3d ƽ:%-4.2f\n",Student.num,Student.name,Student.math_score,Student.datastruct_score,Student.English_score,Student.physics_score,Student.average); + offset=ftell(fp); //ȡļλñǵĵǰλãλõֵһֽļͷǰλõλС + //fseek(fp,-sizeof(struct student_type),SEEK_CUR); //ļָ붨λϢĿͷSEEK_CURʾļĩβλãҲ2ʾ + fseek(fp,offset-sizeof(struct student_type),SEEK_SET); //ļָ붨λϢĿͷSEEK_SETʾļʼλãҲ0ʾ + printf("޸ĵϢ:\n"); + printf(":"); + scanf("%s",Student.name); + printf("ѧ:"); + scanf("%s",Student.num); + printf("ɼ:"); + scanf("%d",&Student.math_score); + printf("ݽṹɼ:"); + scanf("%d", &Student.datastruct_score); + printf("Ӣɼ:"); + scanf("%d", &Student.English_score); + printf("ɼ:"); + scanf("%d", &Student.physics_score); + Student.average=(Student.math_score+Student.datastruct_score+Student.English_score+Student.physics_score)/4.0; + //Student.average=calculate_average(st); //calculate_averageƽ + + fwrite(&Student,sizeof(struct student_type),1,fp); //޸ѧϢԭλ + printf("޸ijɹ"); + + system("pause"); + fclose(fp); + + return 0; + } + } + + } + printf("ѧϢ!\n"); + fclose(fp); + return 0; +} + +//޸IJ(޸) (studinfo.datļݣ +int revisestudinfo_by_name(char *name){ + struct student_type{ + int rank; // + char num[20]; // ѧ + char name[10]; // + int math_score; // ɼ + int datastruct_score; // ݽṹɼ + int English_score; // Ӣɼ + int physics_score; // ɼ + float average; // ƽ + } Student; + FILE *fp; + long offset=0; //һoffset,ڴļλñǵƫ + if((fp=fopen("studinfo.dat","rb+"))==NULL){ //Զļжд + printf("cannot open file\n"); + return 0; + } + while(!feof(fp)) + { + if(fread(&Student,sizeof(struct student_type),1,fp)) + { + if(strcmp(name,Student.name)==0) + { + printf("ԭѧϢΪ\n"); + printf("ѧ:%-20s :%-10s :%-3d ݽṹ:%-3d Ӣ:%-3d :%-3d ƽ:%-4.2f\n",Student.num,Student.name,Student.math_score,Student.datastruct_score,Student.English_score,Student.physics_score,Student.average); + offset=ftell(fp); //ȡļλñǵĵǰλãλõֵһֽļͷǰλõλС + //fseek(fp,-sizeof(struct student_type),SEEK_CUR); //ļָ붨λϢĿͷSEEK_CURʾļĩβλãҲ2ʾ + fseek(fp,offset-sizeof(struct student_type),SEEK_SET); //ļָ붨λϢĿͷSEEK_SETʾļʼλãҲ0ʾ + printf("޸ĵϢ:\n"); + printf(":"); + scanf("%s",Student.name); + printf("ѧ:"); + scanf("%s",Student.num); + printf("ɼ:"); + scanf("%d",&Student.math_score); + printf("ݽṹɼ:"); + scanf("%d", &Student.datastruct_score); + printf("Ӣɼ:"); + scanf("%d", &Student.English_score); + printf("ɼ:"); + scanf("%d", &Student.physics_score); + Student.average=(Student.math_score+Student.datastruct_score+Student.English_score+Student.physics_score)/4.0; + //Student.average=calculate_average(st); //calculate_averageƽ + + fwrite(&Student,sizeof(struct student_type),1,fp); //޸ѧϢԭλ + printf("޸ijɹ"); + + system("pause"); + fclose(fp); + + return 0; + } + } + + } + printf("ѧϢ!\n"); + fclose(fp); + return 0; +} + +// ɾ(ѧɾ) +int deleteelem(char *num) { + char oldname[]="studinfo_temp.dat"; //һʱļ + char newname[]="studinfo.dat"; + struct student_type{ + int rank; // + char num[20]; // ѧ + char name[10]; // + int math_score; // ɼ + int datastruct_score; // ݽṹɼ + int English_score; // Ӣɼ + int physics_score; // ɼ + float average; // ƽ +} Student; + FILE *tfp; + FILE *fp; + int i; + if((fp=fopen("studinfo.dat","rb"))==NULL){ //studinfo.datļ ֻʽ򿪣bļ + printf("cannot open file\n"); + return 0; + } + tfp=fopen("studinfo_temp.dat","ab"); //ʱļstudinfo_temp ׷ӷʽ򿪣bļ + while(!feof(fp)) + { + if(fread(&Student,sizeof(struct student_type),1,fp)){ + if(strcmp(num,Student.num)!=0){ + if(fwrite(&Student,sizeof(struct student_type),1,tfp)!=1) + printf("file write error\n"); + } + } + } + fclose(fp); + fclose(tfp); + remove(newname); + if(rename(oldname,newname)==0){ + printf("ɾɹ!\n"); + } + else + printf("ɾʧ!\n"); + return 1; +} + +// +int listinsert(funlist *l, int i, Student e) { + if (i < 1 || i > l->length + 1) { + printf("λòϷ\n"); + return 0; + } + if (l->length >= 100) { + printf("Ա\n"); + return 0; + } + int j; + for (j = l->length - 1; j >= i - 1; j--) { + l->data[j + 1] = l->data[j]; + // ƶ + l->data[j + 1].rank = l->data[j].rank + 1; + } + // 㲢ƽ + e.average = calculate_average(&e); + e.rank = i; + l->data[i - 1] = e; + l->length++; + return 1; // 1ʾɹ +} + +// ƽִСðݷ +void sort_by_average(){ + struct student_type{ + int rank; // + char num[20]; // ѧ + char name[10]; // + int math_score; // ɼ + int datastruct_score; // ݽṹɼ + int English_score; // Ӣɼ + int physics_score; // ɼ + float average; // ƽ + }Stud[100]; + FILE *fp; + int record=0; + int i,j; + struct student_type temp; + if((fp=fopen("studinfo.dat","rb"))==NULL) //studinfo.datļ ֻʽ򿪣bļ + { + printf("cannot open file\n"); + return; + } + while(!feof(fp)) + { + if(fread(&Stud[record],sizeof(struct student_type),1,fp)){ + record++; + } + } + for(i=0;iStud[j+1].average){ + temp=Stud[j]; + Stud[j]=Stud[j+1]; + Stud[j+1]=temp; + } + } + } + printf("ƽִС:\n"); + for(i=0;i