A student achievement management system is created using the C language database,
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Define TRUE 1
# Define FLASE-1
Typedef struct student
{
Char name [10];
Char sex [10];
Char Class [10];
Float mark;
Int age;
Struct student * prev;
Struct student * next;
} Node;/* define struct */
Char strbuf [40];
Char strmark [10];
FILE * fp;/* define only the pointer of the FILE */
// Import the student information in the linked list to the text
Void save (node * h)
{
Char strbuf [40]; // defines a string to store the name, gender, and class in the linked list student information.
Char strmark [10]; // used to store the age and score of the student in the linked list.
If (fp = fopen ("D: \ student.txt", "wb") = NULL) // you can determine whether to open the text .!!!! Note that the text you want to store information should be added to a location other than drive C.
{
Printf ("this file cannot be opened ");
}
Node * p = h;
While (p) // determine whether p is null
{
Strcpy (strbuf, p-> name); // assign the name in the linked list to the string.
Strcat (strbuf ,".");
Strcat (strbuf, p-> sex );
Strcat (strbuf ,".");
Strcat (strbuf, p-> Class );
Strcat (strbuf ,".");
Itoa (p-> age, strmark, 10); // replace int type with string type
Strcat (strbuf, strmark );
Strcat (strbuf ,".");
Itoa (p-> mark, strmark, 10 );
Strcat (strbuf, strmark );
Fwrite (strbuf, 1, strlen (strbuf), fp); // write to text
Fwrite ("\ r \ n", 1, strlen ("\ r \ n"), fp); // line feed
P = p-> next;
}
Fclose (fp); // you must close the text. Otherwise, the storage fails.
Printf ("saved successfully \ n ");
}
// Print the information in the linked list
Void print (node * h)
{
If (h = NULL)
{
Return;
}
Else
{
Node * p = h;
While (p)
{
Printf ("Name: % s age: % d Gender: % s Class: % s score: %. 0f \ n ", p-> name, p-> age, p-> sex, p-> Class, p-> mark );
P = p-> next;
}
Printf ("\ n ");
}
}
// Used to re-insert text information into the linked list
Node * creat3 (node * h, char strstuname [10], char sex [10], char class1 [10], int age, float mark)
{
Node * p = (node *) malloc (sizeof (node ));
Memset (p, 0, sizeof (node ));
Strcpy (p-> name, strstuname );
Strcpy (p-> sex, sex );
Strcpy (p-> Class, class1 );
P-> age = age;
P-> mark = mark;
P-> next = NULL;
P-> prev = NULL;
If (h = NULL)
{
H = p;
}
Else
{
P-> next = h;
H-> prev = p;
H = p;
}
/* Printf ("name: % s age: % d Gender: % s Class: % s score: % f \ n", p-> name, p-> age, p-> sex, p-> Class, p-> mark );*/
/* Return h ;*/
}
// Add the information in the text to the linked list
Node * load (node * h)
{
Char strbuf [50] = {0 };
Char strstuname [10] = {0 };
Char strstusex [10] = {0}; // to store information in the text.
Char strstuage [10] = {0 };
Char strstuclass [10] = {0 };
Char strstumark [10] = {0 };
If (fp = fopen ("D: \ student.txt", "rb") = NULL)
{
Printf ("this file cannot be opened ");
Exit (0 );
}
Node * p = h;
Int ncount = 0;
Int j = 0;
While (NULL! = Fgets (strbuf, 50, fp ))
{
Int I = 0;
Ncount = 0;
J = 0;
For (I = 0; strbuf [I]! = '\ R'; I ++)
{
If (0 = ncount)
{
Strstuname [I] = strbuf [I];
If (strbuf [I] = '.')
{
Strstuname [I] = '\ 0 ';
Ncount ++;
}
}
Else if (1 = ncount)
{
Strstusex [j] = strbuf [I];
J ++;
If (strbuf [I] = '.')
{
Strstusex [J-1] = '\ 0 ';
Ncount ++;
J = 0;
}
}
Else if (2 = ncount)
{
Strstuclass [j] = strbuf [I];
J ++;
If (strbuf [I] = '.')
{
Strstuclass [J-1] = '\ 0 ';
Ncount ++;
J = 0;
}
}
Else if (3 = ncount)
{
Strstuage [j] = strbuf [I];
J ++;
If (strbuf [I] = '.')
{
Strstuage [J-1] = '\ 0 ';
Ncount ++;
J = 0;
}
}
Else
{
Strstumark [j] = strbuf [I];
J ++;
}
}
H = creat3 (h, strstuname, strstusex, strstuclass, atoi (strstuage), atoi (strstumark); // call the previous insert function to insert it into the linked list.
}
Printf ("imported successfully \ n ");
Fclose (fp );//!!!! Remember to close the text
Return h;
}
// Insert from the header
Node * creat (node * h)
{
While (1)
{
Node * p = (node *) malloc (sizeof (node ));
If (p = NULL)
Return h;
Memset (p, 0, sizeof (node ));
Printf ("Enter the student's age :");
Int age;
Scanf ("% d", & age );
If (age = 0)
{
Break;
}
P-> age = age;
Printf ("Enter the Student name :");
Scanf ("% s", & p-> name );
Printf ("Enter the student's Gender :");
Scanf ("% s", p-> sex );
Printf ("Enter the student's class :");
Scanf ("% s", & p-> Class );
Printf ("Enter the student's score ");
Scanf ("% f", & p-> mark );
P-> next = NULL;
P-> prev = NULL;
If (h = NULL)
{
H = p;
}
Else
{
P-> next = h;
H-> prev = p;
H = p;
}
}
Return h;
}
// Insert from the end
Node * creat1 (node * h)
{
While (1)
{
Node * p = (node *) malloc (sizeof (node ));
If (p = NULL)
Return h;
Memset (p, 0, sizeof (node ));
Printf ("Enter the student's age :");
Int age;
Scanf ("% d", & age );
If (age = 0)
{
Break;
}
P-> age = age;
Printf ("Enter the Student name :");
Scanf ("% s", & p-> name );
Printf ("Enter the student's Gender :");
Scanf ("% s", p-> sex );
Printf ("Enter the student's class :");
Scanf ("% s", & p-> Class );
Printf ("Enter the student's score ");
Scanf ("% f", & p-> mark );
P-> next = NULL;
P-> prev = NULL;
If (h = NULL)
{
H = p;
}
Else
{
Node * q = h;
While (q-> next)
{
Q = q-> next;
}
Q-> next = p;
P-> prev = q;
}
}
Return h;
}
Node * sort (node * h)
{
Int temp;
If (h = NULL)
{
Return h;
}
Else if (h-> next = NULL)
{
Return h;
}
Else
{
Node * p = h;
Node * I;
Node * q;
/* Node * p ;*/
While (p)
{
For (I = p; I-> next! = NULL; I = I-> next)
{
Q = I-> next;
If (I-> age <q-> age)
{
Temp = q-> age;
Q-> age = I-> age;
I-> age = temp;
}
}
P = p-> next;
}
Return h;
}
}
// Print from the end
Void Lastprint (node * h)
{
If (h = NULL)
{
Return;
}
Else
{
Node * p = h;
While (p)
{
Printf ("Name: % s age: % d Gender: % s Class: % s score: %. 0f \ n ", p-> name, p-> age, p-> sex, p-> Class, p-> mark );
P = p-> prev;
}
Printf ("\ n ");
}
}
// Query the score
Void select1 (node * h, float mark)
{
If (h = NULL)
{
Return;
}
Else
{
Node * p = h;
While (p)
{
If (p-> mark = mark)
{
Printf ("Name: % s age: % d Gender: % s Class: % s score :. 0% f \ n ", p-> name, p-> age, p-> sex, p-> Class, p-> mark );
}
P = p-> next;
}
Printf ("\ n ");
}
}
// Modify by name
Void update1 (node * h, char name [10])
{
Float mark;
If (h = NULL)
{
Return;
}
Else
{
Node * p = h;
While (p)
{
If (strcmp (p-> name, name) = 0)
{
Printf ("Enter the score to be modified ");
Scanf ("% f", & mark );
P-> mark = mark;
Printf ("Name: % s age: % d Gender: % s Class: % s score: %. 0f \ n ", p-> name, p-> age, p-> sex, p-> Class, p-> mark );
}
P = p-> next;
}
}
}
Node * Sort (node * h)
{
Float temp;
If (h = NULL)
{
Return h;
}
Else if (h-> next = NULL)
{
Return h;
}
Else
{
Node * p = h;
Node * I;
Node * q;
/* Node * p ;*/
While (p)
{
For (I = p; I-> next! = NULL; I = I-> next)
{
Q = I-> next;
If (I-> mark <q-> mark)
{
Temp = q-> mark;
Q-> mark = I-> mark;
I-> mark = temp;
}
}
P = p-> next;
}
}
}
// Print the sorted score
Void SortMark (node * h)
{
If (h = NULL)
{
Return;
}
Else
{
Node * p = h;
While (p)
{
Printf ("% f \ n", p-> mark );
P = p-> next;
}
Printf ("\ n ");
}
}
// Search by name
Void select2 (node * h, char name [10])
{
If (h = NULL)
{
Return;
}
Else
{
Node * p = h;
While (p)
{
If (strcmp (p-> name, name) = 0)
{
Printf ("Name: % s age: % d Gender: % s Class: % s score: %. 0f \ n ", p-> name, p-> age, p-> sex, p-> Class, p-> mark );
}
P = p-> next;
}
Printf ("\ n ");
Printf ("finding no such person ");
}
}
// Destroy the linked list
Void destroy (node * h)
{
If (h = NULL)
{
Return;
}
Else
{
Node * p = h;
While (p)
{
Free (p );
P = p-> next;
}
}
}
// Delete the information in the linked list by name
Int delete1 (node ** h, char name [10])
{
If (* h = NULL)
{
Return-1;
}
Else
{
Node * p = * h;
While (p)
{
If (strcmp (p-> name, name) = 0)
{
If (p = * h)
{
* H = (* h)-> next;
(* H)-> prev = NULL;
Free (p );
}
Else if (p-> next = NULL)
{
P-> prev-> next = NULL;
Free (p );
}
Else
{
P-> prev-> next = p-> next;
P-> next-> prev = p-> prev;
Free (p );
}
}
P = p-> next;
}
Return-1;
}
}
Int main (int argc, char * argv [])
{
Node * h = NULL;
Int temp;
Int da = 1;
Printf ("************************** start the Student Achievement Management System ****** * **************** \ n ");
Printf ("*************************** (1) insert **************************** \ n ") from the head of the linked list ");
Printf ("************************** (2) insert **************************** \ n ") from the end of the linked list ");
Printf ("*************************** (3) print Information in the linked list ********************** \ n ");
Printf ("************************** (4) search for student information by name and print *********************** \ n ");
Printf ("************************** (5) modify the score of a student by name: * ********************** \ n ");
Printf ("************************** (6) Delete student information by name: * ********************** \ n ");
Printf ("************************** (7) sorting of scores from large to small: * ********************** \ n ");
Printf ("************************** (8) write to text: * ********************** \ n ");
Printf ("************************** (9) read from the text: * ********************** \ n ");
Printf ("************************** (10) Exit the program: * ********************** \ n ");
While (da)
{
Printf ("enter a number to select the function \ n ");
Scanf ("% d", & temp );
Switch (temp)
{
Case 1: h = creat (h );
Break;
Case 2: creat1 (h );
Break;
Case 3: Print (h );
Break;
Case 4: char name [10];
Printf ("Enter the name to query ");
Scanf ("% s", & name );
Select2 (h, name );
Break;
Case 5:
Printf ("enter the name of the Student Score to be modified ");
Char name1 [10];
Scanf ("% s", & name1 );
Update1 (h, name1 );
Print (h );
Break;
Case 6:
Printf ("Enter the student information to delete ");
Char name2 [10];
Scanf ("% s", & name2 );
Delete1 (& h, name2 );
Print (h );
Break;
Case 7:
Sort (h );
SortMark (h );
Break;
Case 8:
Save (h );
Break;
Case 9:
H = load (h );
Break;
Case 10:
Da = 0;
Break;
}
}
}