Catalog
I. conclusion
I haven't written the code for a long time. I read the python list and dictionary again
About the chain list of C:
The nodes of linked list can be realized by nesting lists and dictionaries
About sorting:
sorted() is a python function,
Sorting with sorted() is a relatively simple method
Reference: lambda function and sorted function in python
https://blog.csdn.net/qq_25041667/article/details/102258338
The blogger wrote in great detail
2, C code
Operating environment: VS2017
Here is the code:
/* Student achievement management procedure To work out a management program for statistics of students' examination scores. Suppose that the student's scores are stored in a file in the form of a student's record, Each student record contains the following information: name, student number and grades of each course. The program has the following functions: to find the total score of each course, average score, by name, Search for student records and display them by student number, browse all student scores and display student information from high to low by total score, etc. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define SWN 3 /* Course number */ #define NAMELEN 20 /* Maximum characters of name */ #define CODELEN 10 /* Maximum characters of student ID */ #define FNAMELEN 80 /* Maximum characters of file name */ #define BUFLEN 80 /* Maximum number of characters in buffer */ /* Course name table */ char schoolwork[SWN][NAMELEN + 1] = { "Chinese","Mathematic","English" }; struct record { char name[NAMELEN + 1]; /* Full name */ char code[CODELEN + 1]; /* Student ID */ int marks[SWN]; /* Course scores */ int total; /* Total score */ }stu; struct node { char name[NAMELEN + 1]; /* Full name */ char code[CODELEN + 1]; /* Student ID */ int marks[SWN]; /* Course scores */ int total; /* Total score */ struct node *next; /* Subsequent table element pointer */ }*head; /* Head pointer of linked list */ int total[SWN]; /* Total score of each course */ FILE *stfpt; /* field name pointer */ char stuf[FNAMELEN]; /* file name */ /* Read a record from the specified file */ int readrecord( FILE *fpt, struct record *rpt) { char buf[BUFLEN]; int i; if ( fscanf_s(fpt, "%s", buf, BUFLEN) != 1) return 0; /* End of file */ strncpy_s(rpt->name, buf, NAMELEN); fscanf_s(fpt, "%s", buf, BUFLEN); strncpy_s(rpt->code, buf, CODELEN); for (i = 0; i < SWN; i++) fscanf_s(fpt, "%d", &rpt->marks[i]); for (rpt->total = 0, i = 0; i < SWN; i++) rpt->total += rpt->marks[i]; return 1; } /* Write a record to the specified file */ void writerecord( FILE *fpt, struct record *rpt) { int i; fprintf(fpt, "%s\n", rpt->name); fprintf(fpt, "%s\n", rpt->code); for (i = 0; i < SWN; i++) fprintf(fpt, "%d\n", rpt->marks[i]); return; } /* Show student records */ void displaystu( struct record *rpt) { int i; printf("\nName : %s\n", rpt->name); printf("Code : %s\n", rpt->code); printf("Marks :\n"); for (i = 0; i < SWN; i++) printf(" %-15s : %4d\n", schoolwork[i], rpt->marks[i]); printf("Total : %4d\n", rpt->total); } /* Calculate the total score of each single subject */ int totalmark( char *fname) { FILE *fp; struct record s; int count, i; errno_t err; err = fopen_s(&fp, fname, "r"); if ( err != 0) { printf("Can't open file %s.\n", fname); return 0; } for (i = 0; i < SWN; i++) total[i] = 0; count = 0; while (readrecord(fp, &s) != 0) { for (i = 0; i < SWN; i++) total[i] += s.marks[i]; count++; } fclose(fp); return count; /* Number of records returned */ } /* List student information */ void liststu( char *fname) { FILE *fp; struct record s; errno_t err; err = fopen_s(&fp, fname, "r"); if (err != 0) { printf("Can't open file %s.\n", fname); return; } while (readrecord(fp, &s) != 0) { displaystu(&s); printf("\n Press ENTER to continue...\n"); while ( getchar() != '\n'); //getchar() != '\n' } fclose(fp); return; } /* Construct linked list */ struct node *makelist( char *fname) { struct node *h, *p, *v, *u; FILE *fp; errno_t err; err = fopen_s(&fp, fname, "r"); if (err != 0) { printf("Can't open file %s.\n", fname); return NULL; } h = p = v = u = NULL; p = (struct node *)malloc(sizeof(struct node)); while (readrecord(fp, (struct record *)p) != 0) { v = h; while ( v && p->total <= v->total) // So the total score of this list is from big to small { u = v; v = v->next; } if (v == h) h = p; else u->next = p; p->next = v; p = (struct node *)malloc(sizeof(struct node)); } free(p); fclose(fp); return h; } /* Display all elements of the linked list in sequence */ void displaylist(struct node *h) { while (h != NULL) { displaystu((struct record *)h); printf("\n Press ENTER to continue...\n"); while (getchar() != '\n'); h = h->next; } return; } /* Find student records by student name */ int retrievebyn(char *fname, char *key) { FILE *fp; int c; struct record s; errno_t err; err = fopen_s(&fp, fname, "r"); if (err != 0) { printf("Can't open file %s.\n", fname); return 0; } c = 0; while (readrecord(fp, &s) != 0) { if (strcmp(s.name, key) == 0) { displaystu(&s); c++; } } fclose(fp); if (c == 0) printf("The student %s is not in the file %s.\n", key, fname); return 1; } /* Find student records by Student ID */ int retrievebyc(char *fname, char *key) { FILE *fp; int c; struct record s; errno_t err; err = fopen_s(&fp, fname, "r"); if (err != 0) { printf("Can't open file %s.\n", fname); return 0; } c = 0; while (readrecord(fp, &s) != 0) { if (strcmp(s.code, key) == 0) { displaystu(&s); c++; break; } } fclose(fp); if (c == 0) printf("The student %s is not in the file %s.\n", key, fname); return 1; } def main(): #int i, j, n; #char c; char buf[BUFLEN]; FILE *fp; struct record s; print("Please input the students marks record file's name: ", end = '') stuf = input() errno_t err; err = fopen_s(&fp, stuf, "r"); if( err != 0 ) { printf("The file %s doesn't exit, do you want to creat it? (Y/N) ", stuf); getchar(); c = getchar(); if c == 'Y' or c == 'y': err = fopen_s(&fp, stuf, "w"); printf("Please input the record number you want to write to the file: "); scanf_s("%d", &n); for i in rang( 0, n): print("Input the student's name: ", end = '') s.name = intput() print("Input the student's code: ", end = '') s.code = intput() for j in range( 0, SWN): printf("Input the %s mark: ", schoolwork[j]); scanf_s("%d", &s.marks[j]); print("\n"); writerecord(fp, &s) fclose(fp); } } fclose(fp); i = input() system("cls"); puts("Now you can input a command to manage the records."); puts("m : mean of the marks."); puts("t : total of the marks."); puts("n : search record by student's name."); puts("c : search record by student's code."); puts("l : list all the records."); puts("s : sort and list the records by the total."); puts("q : quit!"); while (1) { puts("Please input command:"); scanf_s("%c", &c, 1); /* Enter selection command */ if (c == 'q' || c == 'Q') { puts("\n Thank you for your using."); break; /* q,End program run */ } switch (c) { case 'm': /* Calculate the average score */ case 'M': if ((n = totalmark(stuf)) == 0) { puts("Error!"); break; } printf("\n"); for (i = 0; i < SWN; i++) printf("%-15s's average is: %.2f.\n", schoolwork[i], (float)total[i] / n); break; case 't': /* final scoring */ case 'T': if ((n = totalmark(stuf)) == 0) { puts("Error!"); break; } printf("\n"); for (i = 0; i < SWN; i++) printf("%-15s's total mark is: %d.\n", schoolwork[i], total[i]); break; case 'n': /* Find records by student's name */ case 'N': printf("Please input the student's name you want to search: "); scanf_s("%s", buf, 80); retrievebyn(stuf, buf); break; case 'c': /* Find records by student number */ case 'C': printf("Please input the student's code you want to search: "); scanf_s("%s", buf, 80); retrievebyc(stuf, buf); break; case 'l': /* List all student records */ case 'L': liststu(stuf); break; case 's': /* Display by total score from high to low */ case 'S': if ((head = makelist(stuf)) != NULL) displaylist(head); break; default: break; } } system("pause"); return 0; }
3, Python code
Operating environment: Pycharm, python3.74
Here is the code:
"""Student achievement management procedure //To work out a management program for statistics of students' examination scores. //Suppose that the student's scores are stored in a file in the form of a student's record, //Each student record contains the following information: name, student number and grades of each course. //The program has the following functions: to find the total score of each course, average score, by name, //Search for student records by student number and display them, browse all student scores and //Display student information from high to low according to the total score. """ import os swn = 3 # Course number #Course name table schoolwork = [ "Chinese", "Mathematic", "English"] # Total score of each course total = [ 0 for i in range(swn)] # It mainly stores the student dictionary in the student list, # Storing student information in the student dictionary is equivalent to a node in the linked list, # student = {"name": ,"code": ,"marks":{ schoolwork[i]: ,}}, # "marks"The value of is also a dictionary student_list = [] def add_student(): """New student records to be entered in the file""" new_students = [] # n is the number of records we need to enter n = int(input("Please input the record number " "you want to write to the file: ")) for i in range( 0, n): name = input("Input the student's name: ") code = input("Input the student's code: ") marks = {} for j in range( 0, swn): print("Input the %s mark: " % schoolwork[j], end = "") marks[ schoolwork[j]] = int( input()) print("\n") student_record = {"name":name.title(), "code":code, "marks":marks} new_students.append(student_record) return new_students def create_file( filename): """ //Create log folders and log files :param filename: :return: """ path = filename[0:filename.rfind("/")] if not os.path.isdir(path): # Create without folder os.makedirs(path) if not os.path.isfile(filename): # Create without file fd = open(filename, mode="w", encoding="utf-8") fd.close() else: pass def write_file( filename, new_students): """Input the new student record into the file""" fp = open(filename, "w", encoding='utf-8') fp.write(str(new_students)) fp.close() print("Data saved to %s succeeded!" % filename) fp.close() def read_file( filename): """Reading data from a file into a variable""" global student_list if os.path.exists( filename): f = open( filename, 'r', encoding='utf-8') ret = f.read() student_list = eval(ret) f.close() for s in student_list: s["total"] = 0 for value in s["marks"].values(): s["total"] = s["total"] + value return def total_mark(): """ Calculate the total score of each single subject """ for i in range( 0, swn): total[i] = 0 count = 0 for s in student_list: for i in range( 0, swn): total[i] = total[i] + s["marks"][schoolwork[i]] count = count + 1 return count # Number of records returned def display_stu( student): """ Show student records """ print("Name : ", student["name"]) print("Code : ", student["code"]) print("Marks :") for key,value in sorted( student["marks"].items()): print(" %-15s : %4d" % ( key, value)) print("Total : %4d" % student["total"]) def list_stu(): """List student information """ for s in student_list: display_stu(s) print("\n\t Press Blank " "space to continue...") e = input() while (e != ' '): e = input() print("\n\t END") def display_list(): """ The total score is displayed from large to small""" #The dictionary is embedded in the list sorted() for s in sorted(student_list, key=lambda k: k["total"], reverse=True): display_stu(s) print("\n\t Press Blank " "space to continue...") e = input() while (e != ' '): e = input() print("\n\t END") def show_list(): """ menu""" print("\nNow you can input a command to manage the records.") print("m : mean of the marks.") #Average score print("t : total of the marks.") #final scoring print("n : search record by student's name.") #Search records by student name print("c : search record by student's code.") #Search records by Student ID print("l : list all the records.") #List all records print("s : sort and list the records by the total.") #Sort and list records by total print("q : quit!\n") #Sign out def retrieve_n( key): """Find student records by student name""" c = 0 for s in student_list: if key == s["name"]: display_stu(s) c = c + 1 if c == 0: print("The student %s is not " "in the file." % key) return 1 def retrieve_c( key): """Find student records by Student ID""" c = 0 for s in student_list: if key == s["code"]: display_stu(s) c = c + 1 break if c == 0: print("The student %s is not " "in the file." % key) return 1 def main(): os.system("cls") stuf = input("Please input the students " "marks record file's name: ") #file name #Call the function "check whether the student record file is created" if not os.path.exists(stuf): c = input("The file %s doesn't exit," "do you want to creat it? (Y/N) ") if c == 'Y' or c == 'y': create_file( stuf) write_file( stuf, add_student()) else: print("\n\tThank you for your using.") return #Students' information will be saved in student list read_file( stuf) if not student_list: print("\n\tStudent information is not stored in the file.") print("\t\t\tThank you for your using.") return i = input("Press any key to clear the " "screen, then the menu will appear") os.system("cls") while 1: show_list() c = input("\nPlease input command:") # Enter selection command os.system("cls") if c == 'q' or c == 'Q': print("\n\tThank you for your using.") break # Calculate average score of single subject elif c == 'm' or c == 'M': n = total_mark() if n == 0: print("\nError!") continue print("") for i in range( 0, swn): print("%-15s's average is: %.2f." % ( schoolwork[i], (float)(total[i] / n)) ) # Calculate the total score of single subject elif c == 't' or c == 'T': if total_mark() == 0: print("Error!") continue print("") for i in range( 0, swn): print("%-15s's total mark is: %d." % (schoolwork[i], total[i]) ) #Find records by student's name elif c == 'n' or c == 'N': buf = input("Please input the student's " "name you want to search: ") retrieve_n(buf.title()) # Find records by student number elif c == 'c' or c == 'C': buf = input("Please input the student's " "code you want to search: ") retrieve_c(buf) # List all student records elif c == 'l' or c == 'L': list_stu() # Display by total score from high to low elif c == 's' or c == 'S': display_list() else: print("Input command error, please input again") main()