From 3e002e33218a80cdd0a2e363f2938737f7692bc5 Mon Sep 17 00:00:00 2001 From: pq52sx6n8 <2731407373@qq.com> Date: Wed, 28 May 2025 12:58:32 +0800 Subject: [PATCH] =?UTF-8?q?ADD=20=E5=AE=9E=E7=8E=B0=E4=BA=86=E5=AF=B9?= =?UTF-8?q?=E4=BA=8C=E8=BF=9B=E5=88=B6=E6=96=87=E4=BB=B6=E7=9A=84=E5=86=99?= =?UTF-8?q?=E5=85=A5=E3=80=81=E6=9F=A5=E6=89=BE=E3=80=81=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E3=80=81=E6=98=BE=E7=A4=BA=E5=AD=A6=E7=94=9F=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E7=AD=89=E6=93=8D=E4=BD=9C=EF=BC=8C=E4=BD=86=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E8=BF=98=E5=B0=9A=E6=9C=AA=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 学生成绩管理对文件操作.cpp | 579 ++++++++++++++++++++++++++ 1 file changed, 579 insertions(+) create mode 100644 学生成绩管理对文件操作.cpp diff --git a/学生成绩管理对文件操作.cpp b/学生成绩管理对文件操作.cpp new file mode 100644 index 0000000..637d425 --- /dev/null +++ b/学生成绩管理对文件操作.cpp @@ -0,0 +1,579 @@ +#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; +} + +// ȡֵ +int getelem(funlist l, int i, Student *e) { + if (i < 1 || i > l.length) { + printf("λòϷ\n"); + return 0; + } + *e = l.data[i - 1]; + return 1; +} + +//һĿո +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("Z:\\C\\ݽṹ\\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("Z:\\C\\ݽṹ\\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; +} + + +//ʾϢ +void display() +{ + 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; + int i; + if((fp=fopen("E:\\dev-c++\\ѧɼϢ.txt","r+"))==NULL){ + printf("cannot open file\n"); + return; + } + + while(!feof(fp)) + { + fscanf(fp,"%d%s%s%d%d%d%d%f\n",&Student.rank,Student.num,Student.name,&Student.math_score,&Student.datastruct_score,&Student.English_score,&Student.physics_score,&Student.average); + printf(":%d ѧ:%s :%s :%d ݽṹ:%d Ӣ:%d :%d ƽ:%.2f\n",Student.rank,Student.num,Student.name,Student.math_score,Student.datastruct_score,Student.English_score,Student.physics_score,Student.average); + } + fclose(fp); +} + +//޸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("Z:\\C\\ݽṹ\\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("Z:\\C\\ݽṹ\\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(int i) { + int j; + if (i < 1 || i > l->length) { + return 0; + } + for (j = i; j < l->length; j++) { + l->data[j - 1] = l->data[j]; + } + l->length--; + // µ + for (j = i - 1; j < l->length; j++) { + l->data[j].rank = j + 1; + } + 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("Z:\\C\\ݽṹ\\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