1, Experimental purpose
- Understanding the development environment
- Master the definition and use of structural variables;
- Master the definition and use of structure array;
- Master the basic methods of file input and output
- Master function usage
2, Experimental content
[item 1] complete the design of the following functions.
This student achievement management can create, add and delete student achievement and basic information, query, sort, count and save the existing student information to a file.
Student information includes: name, student number, gender, English, computer, advanced mathematics and average score. Except for the average score, other scores are known. The initial grades are stored in the text file data Dat. Input student data from the file and print out the records of each student (except the average score).
Please take care of the data, such as:
Data in text form should be stored in data In DAT
Each sub function should be called as a function.
Design description
1. Menu function: make a selective structure with if else or switch, so as to input corresponding numbers to achieve the corresponding function; Repeat until you exit by entering 0.
2. Create: define the corresponding structure, which can be implemented by array or chain structure. You can choose freely without limitation.
3. New student: insert a new student information in the appropriate position. How to insert and where to insert are free to choose. I feel that where to operate data is efficient and convenient.
4. Display student information: displays the basic information of all students.
5. Search student information: enter the name of the student you want to view. If there is a match, output it. If not, output "no student information".
6. Delete student information:
Traverse the storage table and enter the location where you want to delete student information. If the entered location is false, continue to traverse to the next node until it matches the entered location.
Traverse the storage table, enter the student number of the student you want to delete, and delete the student if it exists.
7. Statistics of students' gender information: make statistics and output through students' gender.
8. Sorting by English scores: using the bubble sorting method, the sorting results are output according to the descending order of English scores.
9. Exit program: enter the number matching the exit program.
10. The information added or deleted should be rewritten to the data Dat file
Structure definition tips:
#define NAME_NUMBER 10 #define ID_NUMBER 9 #define SEX_NUMBER 3 #define SCORE_NUMBER 3 #define MAX_STUDENT_NUMBER 100 struct student { char name[NAME_NUMBER]; char id[ID_NUMBER]; char sex[SEX_NUMBER]; float score[SCORE_NUMBER]; float averageSCore; }; typedef struct student STUDENT;
Source code:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct student { char name[100]; int id; char sex[100]; float egl; float mat; float com; float ave; }; typedef struct student STUDENT; STUDENT stu[100]; int getlong(STUDENT stu_[]);//Get the current number of students int print(STUDENT stu_[]);//Display all student information int add(int n);//Add student information int reserch();//Query student information int delete1(int n);//Method 1 of deleting student information int delete2();//Delete student information method 2 int sexprint();//Statistical student gender int yingyusort();//Sort by English score int main() { FILE *fp = fopen("datas.dat", "r+"); int i=0; if(fp == NULL) { printf("can't read data from the datas.dat!\n"); exit(0); } while(fscanf(fp,"%s%d%s%f%f%f",stu[i].name,&stu[i].id,stu[i].sex,&stu[i].egl, &stu[i].mat,&stu[i].com)!=EOF) { i++; } printf("Import complete,The number of students is:%d\n",getlong(stu)); fclose(fp); int choice,n,x; while (1) { printf("\n\n Student information management system\n\n"); printf("[0] Save information and exit\n\n"); printf("[1] Display student information "); printf("[2] Add student information\n\n"); printf("[3] Search student information "); printf("[4] Delete student information\n\n"); printf("[5] Statistics of student gender information "); printf("[6] Sort by English score\n\n"); printf("Please enter an option (0) - 6):\n"); scanf("%d",&choice); switch (choice) { case 1://Display; print(stu); break; case 2://newly added; printf("Please enter the location of the new student:\n"); scanf("%d",&n); add(n); FILE *fp1 = fopen("datas.dat", "w"); for(i=0;i<getlong(stu);i++) fprintf(fp1,"%s\t%d\t%s\t%f\t%f\t%f\n",stu[i].name,stu[i].id,stu[i].sex,stu[i].egl, stu[i].mat,stu[i].com); fclose(fp1); break; case 3://Search; reserch(); break; case 4://delete printf("[1] Delete the student by the student's location "); printf("[2] Delete the student by Student ID\n"); scanf("%d",&x); switch(x) { case 1: printf("Please enter the location where you want to delete students:"); scanf("%d",&n); delete1(n); FILE *fp2 = fopen("datas.dat", "w"); for(i=0;i<getlong(stu);i++) fprintf(fp2,"%s\t%d\t%s\t%f\t%f\t%f\n",stu[i].name,stu[i].id,stu[i].sex,stu[i].egl, stu[i].mat,stu[i].com); fclose(fp2); break; case 2: delete2(); FILE *fp3 = fopen("datas.dat", "w"); for(i=0;i<getlong(stu);i++) fprintf(fp3,"%s\t%d\t%s\t%f\t%f\t%f\n",stu[i].name,stu[i].id,stu[i].sex,stu[i].egl, stu[i].mat,stu[i].com); fclose(fp3); break; default: printf("The menu you entered is incorrect. Please re-enter!\n"); } break; case 5://Statistical student gender sexprint(); break; case 6://Sort by English score yingyusort(); break; case 0://Exit program printf("Exit program\n"); printf("The program is over, thank you for using!\n"); exit(0); default: printf("The menu you entered is incorrect. Please re-enter!\n"); } } return 0; } int getlong(STUDENT stu_[]) { int i,k=0; for(i=0; ;i++) { if(stu_[i].id!=0) k++; else break; } return k; } int print(STUDENT stu_[]) { int i; for( i=0; i<getlong(stu); i++) { if(stu[i].id != 0) printf("full name:%s\t Student No.:%d\t Gender:%s\t English:%f\t High number:%f\t computer:%f\t\n", stu[i].name, stu[i].id, stu[i].sex, stu[i].egl, stu[i].mat, stu[i].com); } return 0; } int add(int n) { int i,k=getlong(stu); for(i=k;i>=n;i--) { stu[i]=stu[i-1]; } printf("Please enter the student's name:\n"); scanf("%s",stu[n-1].name); printf("Please enter the student number:\n"); scanf("%d",&stu[n-1].id); printf("Please enter the gender of the student:\n"); scanf("%s",stu[n-1].sex); printf("Please enter the student's English score:\n"); scanf("%f",&stu[n-1].egl); printf("Please enter the student's high score:\n"); scanf("%f",&stu[n-1].mat); printf("Please enter the student's computer grade:\n"); scanf("%f",&stu[n-1].com); printf("Entry completed,At this time, the number of students is:%d\n",getlong(stu)); return 0; } int reserch() { int i,k=getlong(stu); char name1[100]; printf("Please enter the name of the student you want to view:"); scanf("%s",name1); for(i=0;i<k;i++) { if(strcmp(name1,stu[i].name)==0) { printf("The student's student number is:%d,Gender:%s,English score:%f,High scores:%f,Computer performance:%f\n",stu[i].id, stu[i].sex, stu[i].egl, stu[i].mat, stu[i].com); break; } } if(i==k) printf("Sorry, there is no such person\n"); return 0; } int delete1(int n) { int i,k=getlong(stu); if( n>0 && n<=k ) { for(i=n;i<=k;i++) stu[i-1]=stu[i]; stu[i].id=0; printf("Delete complete,At this time, the number of students is:%d\n",getlong(stu)); } else printf("This position is false"); return 0; } int delete2() { int id1,i,j,k=getlong(stu); printf("Please enter the student number of the student you want to delete:"); scanf("%d",&id1); for(i=0;i<k;i++) { if(id1==stu[i].id) { for(j=i;j<k-1;j++) stu[j]=stu[j+1]; stu[j].id=0; printf("Delete complete,At this time, the number of students is:%d\n",getlong(stu)); break; } } if(i==k) printf("Sorry, there is no such person\n"); return 0; } int sexprint() { int i,k=getlong(stu),boynum=0,girlnum=0; char boy[100]="male"; char girl[100]="female"; printf("schoolboy:\n"); for(i=0;i<k;i++) if(strcmp(boy,stu[i].sex)==0) { printf("full name:%s\t Student No.:%d\t Gender:%s\t English:%f\t High number:%f\t computer:%f\n", stu[i].name, stu[i].id, stu[i].sex, stu[i].egl, stu[i].mat, stu[i].com); boynum++; } printf("The total number of boys is:%d\n",boynum); printf("girl student:\n"); for(i=0;i<k;i++) if(strcmp(girl,stu[i].sex)==0) { printf("full name:%s\t Student No.:%d\t Gender:%s\t English:%f\t High number:%f\t computer:%f\n", stu[i].name, stu[i].id, stu[i].sex, stu[i].egl, stu[i].mat, stu[i].com); girlnum++; } printf("The total number of girls is:%d\n",girlnum); return 0; } int yingyusort() { int i, j, k=getlong(stu); STUDENT temp; for (i = 0; i < k-1 ; i++) { for (j = 0; j < k - 1 - i; j++) if (stu[j].egl < stu[j+1].egl) { temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp; } } for (i = 0; i < k; i++) { if(stu[i].id != 0) printf("full name:%s\t Student No.:%d\t Gender:%s\t English:%f\t High number:%f\t computer:%f\t\n", stu[i].name, stu[i].id, stu[i].sex, stu[i].egl, stu[i].mat, stu[i].com); } return 0; }
The operation screenshot after each function call is attached here:
0. Save information and exit:
1. Display student information:
2. New student information:
3. Search student information:
4. Delete student information by student location:
Delete student information by student number:
5. Statistics of student gender information:
6. Sort by English scores: