My C/C ++ path-course 010 (simple example of structured data (Student Score Management ))

Source: Internet
Author: User

I have been busy recently. I haven't written it in a few days. Today, I am working on the application of structured structures (Student Achievement Management). I believe many of my friends have done this. I am ugly here and it is difficult to control C. So there are many not very good functions in it, you can complete it on your own.

 

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
/**
Simple instance used by struct:
1. Create a Student Score structure (including student ID, name, Chinese, mathematics, and English score)
Use a linked list to direct to the next student (because the number of input students is unknown, the array method is not good)
 
2. Insert, query, change, and delete student scores
*/
Typedef struct Student
{
Char no [4]; // a 3-digit student ID. If multiple student IDs are entered, an error occurs.
Char name [10];
Int chinese;
Int math;
Int english;
Struct Student * next; // point to the next Student
 
} StudentList;
Void init (StudentList *&);
Void select (StudentList *); // query the score
Void insert (StudentList * &); // enter the score
Void update (StudentList * &); // update the score
Void deleteData (StudentList *); // delete data
 
Void studentScoreManege ()
{
StudentList * stuList = NULL;
Int oprate;
Init (stuList); // Initialization
Do {
System ("cls ");
Fflush (stdin );
Puts ("************** Student Data Oprate *******************");
Puts ("* 1. select student score *");
Puts ("* 2. insert student score *");
Puts ("* 3. update student score *");
Puts ("* 4. delete student data *"); // delete All data. Although this is not the case, this is a simple design.
Puts ("* 0. exit *");
Puts ("************************************* ****************");
Printf ("please select a number :");

Scanf ("% d", & oprate );
Switch (oprate) // execute different operations based on different options
{
Case 1:
Select (stuList );
Break;
Case 2:
Insert (stuList );
Break;
Case 3:
Update (stuList );
Break;
Case 4:
DeleteData (stuList );
Break;
Case 0:
Default:
Break;
}
} While (oprate! = 0 );
}
// Initialize the header
Void init (StudentList * & stuList)
{
StuList = (StudentList *) malloc (sizeof (StudentList ));
StuList-> next = NULL;
}
 
// Query the score
Void select (StudentList * stuList)
{
StudentList * p = stuList-> next;
If (p = NULL)
{
Puts ("there is no data, please insert !! ");
Puts ("press any key to return !!! ");
Getchar ();
}
Puts ("*************** student score insert ******************** *");
Puts ("No \ tName \ tChinese \ tMath \ tEnglish ");
While (p! = NULL)
{
Printf ("% s \ t % d \ n", p-> no, p-> name, p-> chinese, p-> math, p-> english );
P = p-> next;
}
Fflush (stdin );
Puts ("press any key to return !!! ");
Getchar ();
}
// Enter the score www.2cto.com
Void insert (StudentList * & stuList)
{
StudentList * p, * s;
P = stuList;
While (p-> next! = NULL)
{
P = p-> next;
}
Do {
S = (StudentList *) malloc (sizeof (StudentList ));

Puts ("please insert student data (NO, Name, Chinese, Math, English ):");
Scanf ("% 3 s % 10 s", & s-> no, & s-> name );
Scanf ("% d", & s-> chinese, & s-> math, & s-> english );
// Determine the validity of the data. Check the repetition of student IDs by yourself.
If (s-> chinese <0 | s-> chinese> 100 | s-> math <0 | s-> math> 100 | s-> english <0 | s-> english> 100)
{
Puts ("input data error !!! ");
Return;
}
S-> next = NULL;
P-> next = s;
P = p-> next; // prepare for the next loop entry
Fflush (stdin); // clear the '\ n' of the buffer; otherwise, the getchar () below does not work.
Puts ("Continue? ENTER to yes or input no to exit "); // whether to continue inputting
} While (getchar () = '\ n ');
 
}
// Update
Void update (StudentList * & stuList)
{
StudentList * p = stuList-> next;
Char no [4]; // a 3-digit student ID. For example, you can enter a 4-digit student ID ~ Student ID. Here, we will handle the cross-border issue by ourselves.
Fflush (stdin); // clear the buffer first
Puts ("please input student No :");
Gets (no );
// Display the student data of the current student ID
If (p = NULL)
{
Puts ("there is no record, please insert !! ");
Puts ("press any key to return !!! ");
Getchar ();
Return;
}
Else
{
While (p! = NULL)
{
If (! Strcmp (p-> no, no) {// if found, it is displayed
Puts ("NO \ tNAME \ tChinese \ tMath \ tEnglish ");
Printf ("% s \ t % d \ n", p-> no, p-> name, p-> chinese, p-> math, p-> english );
Break;
}
Else {
P = p-> next;
}
}
If (p = NULL) // not found
{
Puts ("can't find this student's record !!! ");
Puts ("press any key to return !!! ");
Getchar ();
Return;
}
}
// It can only be updated in this simple way and improved by yourself.
Puts ("please update student data (Name, Chinese, Math, English ):");
Scanf ("% 10 s", & p-> name );
Scanf ("% d", & p-> chinese, & p-> math, & p-> english );
Fflush (stdin );
Puts ("update successful, press any key to return !!! ");
Getchar ();
}
// Delete data
Void deleteData (StudentList * stuList)
{
/* The previous part is similar to the update, but the pointer changes because it needs to be deleted,
For example, a-> B-> c. To delete B, you need a-> c before free (B ),
The operation is a-> next = B-> next
*/
StudentList * p = stuList, * q;
Char no [4]; // a 3-digit student ID. For example, you can enter a 4-digit student ID ~ Student ID. Here, we will handle the cross-border issue by ourselves.
Fflush (stdin); // clear the buffer first
Puts ("please input student No :");
Gets (no );
// Display the student data of the current student ID
If (p-> next = NULL)
{
Puts ("there is no record !!! ");
Puts ("press any key to return !!! ");
Getchar ();
Return;
}
Else
{
While (p-> next! = NULL)
{
If (! Strcmp (p-> next-> no, no) {// displays the result if it is found.
Puts ("NO \ tNAME \ tChinese \ tMath \ tEnglish ");
Printf ("% s \ t % d \ n", p-> next-> no, p-> next-> name, p-> next-> chinese, p-> next-> math, p-> next-> english );
Break;
}
Else {
P = p-> next;
}
}
If (p-> next = NULL) // not found
{
Puts ("can't find this student's record !!! ");
Puts ("press any key to return !!! ");
Getchar ();
Return;
}
}
Puts ("Are you sure delete this record? Press ENTER to confirm or other key to cancel !!! ");
If (getchar () = '\ n') // delete operation. if you do not understand this step, you can delete the google or baidu linked list.
{
Q = p-> next;
P-> next = q-> next;
Free (q); // releases the memory.
}
Puts ("delete successful, press any key to return !!! ");
Getchar ();
}


From mzlqh's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.