/* * * * header file (. h)* * * * * * * */
#include "stdio.h" /*I/O function */
#include "stdlib.h" /* Other notes */
#include "string.h" /* string function */
# includes "conio.h"/* screen operation function */
#include "mem.h" /* Memory operation function */
#include "ctype.h" /* character manipulation function */
#include "alloc.h" /* Dynamic address assignment function */
# define n 3/* Define constant */
Typedef struct z 1 /* define data structure */
{
Character number [11];
char name[ 15];
int score[N];
Floating sum;
Floating average;
Int order;
struct z 1 * next;
} students;
/* The following is the function prototype */
Student * init (); /* Initialization function */
Student * create (); /* Create a linked list */
Student * Delete (student * h); /* Delete record */
Invalid printing (student * h); /* Show all records */
Void search (student * h); /* Find */
Void save (student * h); /* Save */
Student * load (); /* Read in record */
Void computer (student * h); /* Calculate the total score and average score */
Student * insert (student * h); /* Insert record */
void append(); /* Additional records */
Student * sorting (student * h); /* Sort */
int menu _ select(); /* Menu function */
/* * * * * Main function starts * * * * *
Master ()
{
int I;
Student * head; /* Linked list definition header pointer */
head = init(); /* Initialize linked list */
clr SCR(); /* Clear the screen */
for(; ; )/* infinite loop */
{
Switch(menu_select()) /* Calls the main menu function and returns an integer as the condition of the switch statement */
The value of {/* is different, and the function executed is different. Break cannot be omitted */
Case 0: head = init (); Break; /* Perform initialization */
case 1:head = create(); Break; /* Create a linked list */
Case 2: head=delete (head); Break; /* Delete record */
Case 3: printing (header); Break; /* Show all records */
Case 4: Search (header); Break; /* Find records */
Case 5: rescue (head); Break; /* Save the file */
Situation 6: head = load (); Break; /* Read file */
Case 7: computer (head); Break; /* Calculate the total score and average score */
Case 8: head = insert (head); Break; /* Insert record */
Case 9: head = sort (head); Break; /* Sort */
Case10: append (); Break; /* Additional records */
Case 1 1: export (0); /* If the menu return value is 14, the program ends */
}
}
}
/* menu function, the return value is integer */
menu_select()
{
Char * menu [] = {* * * * * * * * menu * * * * * * * * * * * * * * * * * * * * *,/* defines the menu string array */
"0. Initialize list", /* Initialize */
"1. input list",/* input record */
"2. Delete the record from the list",/* Delete the record from the table */
"3. Print list",/* Displays all records in the single linked list */
"4. Search records by name",/* Find records by name */
"5. Save the file",/* Save the records in the single linked list to a file */
"6. Load the file",/* Read the record from the file */
"7. Calculate scores",/* Calculate the total score and average score of all students */
"8. Insert the record into the list",/* Insert the record into the table */
"9. Sort to generate new files", /* Sort */
"10. Append record to file",/* Append record to file */
"1 1. Exit"}; /* Exit */
char s[3]; /* Save the selection number in character form */
int c,I; /* Define an integer variable */
gotoxy( 1,25); /* Move the cursor */
Printf ("Press any key to enter the menu ... \ n"); /* Press any key to enter the main menu */
getch(); /* Enter any key */
clr SCR(); /* Clear the screen */
gotoxy( 1, 1); /* Move the cursor */
Textcolor (yellow); /* Set the text display color to yellow */
Textbackground (blue); /* Set the background color to blue */
gotoxy( 10,2); /* Move the cursor */
putch(0xc 9); /* Output the upper left corner border ┏ */
for(I = 1; I & lt44; i++)
putch(0x CD); /* Output the top border horizontal line */
putch(0x bb); /* Output the upper right corner border ┓ */
for(I = 3; I & lt20; i++)
{
gotoxy( 10,I); putch(0x ba); /* Output left vertical line */
gotoxy(54,I); putch(0x ba);
}/* Output right vertical line */
gotoxy( 10,20); putch(0x c8); /* Output the upper left corner border ┗ */
for(I = 1; I & lt44; i++)
putch(0x CD); /* Output bottom border horizontal line */
putch(0x BC); /* Output the lower right corner border ┛ */
Window (1 1, 3,53,19); /* Make a window showing menus, and the size of the window is designed according to the number of menus */
clr SCR(); /* Clear the screen */
for(I = 0; I< 13; I++) /* Output main menu array */
{
gotoxy( 10,I+ 1);
cprintf("%s ",menu[I]);
}
Textbackground (black); /* Set the background color to black */
Window (1, 1, 80, 25); /* Restore the original window size */
gotoxy( 10,2 1); /* Move the cursor */
Do {
Printf ("\ nEnter your choice (0 ~14):"); /* Display prompt information outside the menu window */
scanf("%s ",s); /* Enter options */
c = atoi(s); /* Convert the input string into an integer */
} while(c & lt; 0 | | c >; 1 1); /* The selected item is not between 0 and 14. Please re-enter */
Return to c; /* Returns the option, and the main program calls the corresponding function according to the number */
}
Student *init ()
{
Returns NULL
}
/* Create a linked list */
Student * Create ()
{
int I; int s;
STUDENT *h=NULL, * info/* student pointer to structure */
for(; ; )
{
Info= (student *)malloc(sizeof)); /* Apply for space */
If (! Info) /* If the pointer information is empty */
{
Printf(" \ n Out of memory "); /* Output memory overflow */
Return NULL/* Return null pointer */
}
Enter ("input number:", information-> No,11); /* Enter the student number and check */
if(info-& gt; There is no [0]=='@') delimiter; /* If the first character of the student number is @, the input is over */
Enter ("Enter name:", information-> Name,15); /* Enter the name and check */
Printf ("Please enter %d score \n", n); /* Prompt to start entering scores */
s = 0; /* Calculate the total score of each student, with an initial value of 0*/
/*N courses are circulated n times */
/* Save the total score */
/* Average */
/***********************************************************/
/* */
/* */
/* // TODO: Add your code here */
/* */
/* */
/***********************************************************/
Information-> Order = 0; /* The value before sorting is 0 */
Information-> next = h; /* Take the first node as the successor of the new input node */
H = info/* The new input node is the new head node */
}
Return (h); /* Return the head pointer */
}
/* Enter a string and verify its length */
Input (char * prompt, char *s, int count)
{
char p[255];
Do {
Printf (prompt); /* Display prompt information */
scanf("%s ",p); /* Enter the string */
If(strlen(p)>count)printf("\n too long! \ n "); /* Check the length, and then re-enter */
}while(strlen(p) > count);
strcpy(s,p); /* Copy the input string to the string s */
}
/* Output the node information in the linked list */
Invalid print (student *h)
{
int I = 0; /* Number of statistical records */
Student * p;; /* Move the pointer */
clr SCR(); /* Clear the screen */
p = h; /* The initial value is the head pointer */
printf(" \ n \ n \ n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
printf(" | rec | nO | name | sc 1 | sc2 | sc3 | sum | ave | order | \ n ");
printf(" |-|-|-|-|-|-|-|-|-|-| \ n ");
And (p! = empty)
{
i++;
printf(" | % 3d | %- 10s | %- 15s | % 4d | % 4d | % 4d | % 4.2f | % 4.2f | % 3d | \ n ",I,p-& gt; No, P-> Name, p-> Score [0], p-> Score [1],
p->; Score [2], p-> Sum, p- > average, p->; Order);
p = p-& gt; Next;
}
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * end * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
}
/* Delete record */
Student * Delete (student *h)
{
Students *p, * q;; /*p is the node pointer to be deleted, and q is its predecessor pointer */
char s[ 1 1]; /* Store student number */
clr SCR(); /* Clear the screen */
Printf ("Please delete no \ n"); /* Display prompt information */
scanf("%s ",s); /* Enter the student number of the record to be deleted */
q = p = h; /* Assign initial values to q and p */
/* When the recorded student number is not what you are looking for, or the pointer is not empty */
/***********************************************************/
/* */
/* */
/* // TODO: Add your code here */
/* */
/* */
/***********************************************************/
If(p==NULL) /* If p is empty, the node is not in the linked list */
printf("\nlist no %s student\n ",s);
Else /*p is not empty, showing the found record information */
{
Printf ("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n").
printf(" | no | name | sc 1 | sc2 | sc3 | sum | ave | order | \ n ");
printf(" |-|-|-|-|-|-|-|-|-| \ n ");
printf(" | %- 10s | %- 15s | % 4d | % 4d | % 4d | % 4.2f | % 4.2f | % 3d | \ n ",p-& gt; I don't know,
p->; Name, p-> Score [0], p-> Score [1], p-> Score [2], p-> Sum,
p->; Average value, p-> Order);
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * end * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
getch(); /* After pressing any key, delete */
If(p==h) /* If p==h, the deleted node is the head node */
h = p-& gt; Next; /* Modify the head pointer to point to the next record */
other
q->; next = p-& gt; Next; /* is not a head pointer, and the successor node of P is regarded as the successor node of Q */
Free (p); /* Release the node space indicated by p */
Printf("\n %s students were not deleted \n ",s);
Printf ("Don't forget to save \ n"); /* Don't forget to save the file after deletion */
}
Return (h); /* Return the head pointer */
}
/* Find records */
Invalid search (student *h)
{
Student * p;; /* Move the pointer */
char s[ 15]; /* Character array for storing names */
clr SCR(); /* Clear the screen */
Printf ("Please enter a search name \ n");
scanf("%s ",s); /* Enter name */
p = h; /* Assign the head pointer to p*/
while(strcmp(p-& gt; Name, s) and amp. & ampp! =NULL) /* When the name of the record is not the name you are looking for, or the pointer is not empty */
p = p-& gt; Next; /* Move the pointer to the next node */
If(p==NULL) /* If the pointer is empty */
printf("\nlist no %s student\n ",s); /* shows no students */
Else /* Display the found record information */
{
Printf ("\ n \ n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *.
printf(" | nO | name | sc 1 | sc2 | sc3 | sum | ave | order | \ n ");
printf(" |-|-|-|-|-|-|-|-|-| \ n ");
printf(" | %- 10s | %- 15s | % 4d | % 4d | % 4d | % 4.2f | % 4.2f | % 3d | \ n ",p-& gt; I don't know,
p->; Name, p-> Score [0], p-> Score [1], p-> Score [2], p-> Sum, p- > average, p->; Order);
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * end * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
}
}
/* Insert record */
Student * insert (student *h)
{
Students *p, *q, * information; /*p points to the insertion position, q is its predecessor, and info refers to the newly inserted record */
char s[ 1 1]; /* Save the student number of the insertion point */
int s 1,I;
Printf ("Please enter the location before the number \ n");
scanf("%s ",s); /* Enter the student number of the insertion point */
Printf ("\ nPlease create a new record \ n"); /* Prompt for record information */
Info= (student *)malloc(sizeof)); /* Apply for space */
If (! Information)
{
Printf(" \ n Out of memory "); /* Memory overflow if not applied */
Return NULL/* Return null pointer */
}
Enter ("input number:", information-> No,11); /* Enter the student number */
Enter ("Enter name:", information-> Name,15); /* Enter name */
Printf ("Please enter %d score \n", n); /* Prompt for a score */
s 1 = 0; /* Save the total score of the new record, with an initial value of 0*/
for(I = 0; I & ltn;; I++) /*N courses cycle N times to enter scores */
{
Do{ /* Verify the data to ensure that it is between 0~ 100 */
Printf ("score %d:", I+1);
Scanf("%d ",& Info-> score[I]);
if(info-& gt; score[I]& gt; 100 | | information->; score[I]& lt; 0)
Printf ("bad data, repeated input \ n");
} while(info-& gt; score[I]& gt; 100 | | information->; score[I]& lt; 0);
s 1 = s 1+info-& gt; Score [I]; /* Calculate the total score */
}
Information-> sum = s 1; /* Store the total score in a new record */
Information-> Average value = (float) s1/n; /* Calculate the average score */
Information-> Order = 0; /* Grade allocation 0*/
Information-> Next = NULL/* Set the subsequent pointer to null */
p = h; /* Assign pointer to p*/
q = h; /* Assign a pointer to q*/
while(strcmp(p-& gt; No, s)&; & ampp! =NULL) /* Find the insertion position */
{
q = p; /* Save the pointer P as the predecessor of the next P */
p = p-& gt; Next; /* Move the pointer P backward */
}
If(p==NULL) /* If the p pointer is empty, no node is specified */
If(p==h) /* At the same time, p equals h, indicating that the linked list is empty */
H = info/* The new record is the head node */
other
q->; Next = info/*p is empty, but p is not equal to H. Insert new node at the end of the table */
other
If (p==h) /*p is not empty, the specified node */
{
Information-> next = p; /* If p is equal to h, insert a new node before the first node */
H = info/* The new node is the new head node */
}
other
{
Information-> next = p; /* Not the head node, but somewhere in the middle, and the successor of the new node is p*/
q->; Next = info/* The new node is the successor node of Q */
}
Printf("\n-%s students have been inserted-\n ",info-& gt;; Name); Printf("-Don't forget to save-\ n "); /* Save the prompt */
Return (h); /* Return the head pointer */
}
/* Save data to a file */
Invalid save (student *h)
{
FILE * fp/* defines a pointer to the file */
Student * p;; /* Define the moving pointer */
char outfile[ 10]; /* Save the output file name */
Printf ("input and output file names, such as c: \ \ f1\ \ te.txt: \ n"); /* Prompt file name format information */
scanf("%s ",outfile);
If ((FP = fopen (outfile, "WB))= = null)/* Open a binary file for output, or create */
{
Printf ("Unable to open file \ n");
Exit (1);
}
Printf ("\ nsave file ......................."); /* Open the file, prompting that it is being saved */
p = h; /* Move the pointer away from the head pointer */
And (p! =NULL) /* If p is not empty */
{
Fwrite(p, sizeof (student), 1, fp); /* Write a record */
p = p-& gt; Next; /* The pointer moves backward */
}
fclose(FP); /* Close the file */
Printf("-saved successfully! ! -\ n "); /* display saved successfully */
}
/* Read data from file */
Student * Load ()
{
Students *p, *q, * h = NULL/* define the record pointer variable */
FILE * fp/* defines a pointer to the file */
char infile[ 10]; /* Save file name */
Printf ("Enter infile name, for example, c: \ \ f1\ \ te.txt: \ n"); scanf("%s ",infile); /* Enter the file name */
If ((FP = fopen (infile, "Rb))= = null)/* Open binary file in read mode */
{
Printf ("Unable to open file \ n"); /* If it cannot be opened, end the program */
Exit (1);
}
Printf("\n-Loading file! -\ n ");
P= (student *)malloc(sizeof)); /* Apply for space */
If (! p)
{
Printf ("Out of memory! \ n "); /* If there is no application, the memory overflows */
Return to h; /* Returns a null pointer */
}
h = p; /* as a head pointer applied to spaces */
And (! Feof(fp)) /* Read data circularly until the end of the file */
{
If (1! =fread(p,sizeof(STUDENT), 1,fp))
Break; /* If the data is not read, jump out of the loop */
p->; Next= (student *)malloc(sizeof); /* Apply for space for the next node */
If (! p->; Next)
{
Printf ("Out of memory! \ n "); /* If there is no application, the memory overflows */
Return to h;
}
q = p; /* Save the pointer of the current node as the pointer of the next node */
p = p-& gt; Next; /* The pointer moves backward, and the data link is re-read into the current footer */
}
q->; Next = NULL/* The successor pointer of the last node is empty */
fclose(FP); /* Close the file */
Printf("-You have successfully read the data from the file! ! ! -\ n ");
Return to h; /* Return the head pointer */
}
/* Append record to file */
Void addition ()
{
FILE * fp/* defines a pointer to the file */
Student * information; /* New record pointer */
int s 1,I;
char infile[ 10]; /* Save file name */
Printf ("\ nPlease create a new record \ n");
Info= (student *)malloc(sizeof)); /* Apply for space */
If (! Information)
{
Printf(" \ n Out of memory "); /* Not applied, memory overflow */
Return;
}
Enter ("input number:", information-> No,11); /* Call inputs to enter your student number */
Enter ("Enter name:", information-> Name,15); /* Call Input to enter a name */
Printf ("Please enter %d score \n", n); /* Prompt level */
s 1 = 0;
for(I = 0; I & ltn;; i++)
{
Do {
Printf ("score %d:", I+1);
Scanf("%d ",& Info-> score[I]); /* Enter the score */
if(info-& gt; score[I]& gt; 100 | | information->; score[I]& lt; 0)printf ("bad data, repeated input \ n");
} while(info-& gt; score[I]& gt; 100 | | information->; score[I]& lt; 0); /* Performance data verification */
s 1 = s 1+info-& gt; Score [I]; /* Find the total score */
}
Information-> sum = s 1; /* Save the total score */
Information-> Average value = (float) s1/n; /* Find the average score */
Information-> Order = 0; /* The initial ranking value is 0*/
Information-> Next = NULL/* Assign the new record subsequent pointer to null */
Printf ("Enter infile name, for example, c: \ \ f1\ \ te.txt: \ n"); scanf("%s ",infile); /* Enter the file name */
If ((fp = fopen (infile, "ab))= = null)/* Open the file by adding data at the end of the binary file */
{
Printf ("Unable to open file \ n"); /* The monitor cannot be turned on */
Exit (1); /* Exit the program */
}
Printf("\n-Append record! -\ n ");
If (1! = fwrite (info, sizeof (student), 1, FP))/* Write file operation */
{
Printf("-file write error! -\ n ");
Return; /* Return */
}
Printf("-Append succeeded! ! -\ n ");
fclose(FP); /* Close the file */
}
/* Sort */
Student * Sorting (Student *h)
{
int I = 0; /* Save ranking */
Students *p, *q, *t, * h1; /* Define temporary pointer */
h 1 = h-& gt; Next; /* Take the next node pointed by the original header pointer as the header pointer */
h-& gt; Next = NULL/* The first node is the head node of the new table */
/* Sort when the original table is not empty */
/***********************************************************/
/* */
/* */
/* // TODO: Add your code here */
/* */
/* */
/***********************************************************/
p = h; /* The sorted head pointer is assigned to P, and it is ready to fill in the ranking */
And (p! =NULL) /* When p is not empty, do the following */
{
i++; /* Node serial number */
p->; order = I; /* Distribution level */
p = p-& gt; Next; /* The pointer moves backward */
}
Printf ("Sorting succeeded! ! ! \ n "); /* Sorting succeeded */
Return to h; /* Return the head pointer */
}
/* Calculate the total score and average value */
Invalid computer (student *h)
{
Student * p;; /* Define the moving pointer */
int I = 0; /* The initial value of the number of saved records is 0*/
Length s = 0;; /* The initial total score is 0*/
Floating average = 0; /* The average initial value is 0*/
p = h; /* Start from the head pointer */
And (p! =NULL) /* Handled when p is not empty */
{
s+= p-& gt; Sum; /* Cumulative total score */
i++; /* Number of statistical records */
p = p-& gt; Next; /* The pointer moves backward */
}
Average value = (float) s/i; /* Find the average score, divide it into floating-point numbers, always divide it into integers, and do type conversion */
Printf("\n-the total score of all students is: %ld, the average is %5.2f\n ",s, the average);
}