C ++ STL List implements a simple student information management system

Source: Internet
Author: User
C ++
STL List implements a simple student information management system

Problem description:
Student. dat is a document with 20 student records (including student ID, name, and score. Programming is required to query, sort, insert, and delete data.
Basic functions of the system:
A. the following page must be displayed:
****************************************
1 -------------- Query
2 -------------- sorting
3 -------------- insert
4 -------------- Delete
****************************************
Select 1-4 to determine which operation to perform.
B. if you select 1, the following interface is displayed:
****************************************
1.1 ---------- query by student ID
1.2 ---------- query by name
1.3 ---------- query by score
****************************************
You can select 1.1-1.3 to determine which operation to perform. In this example, query by student ID is implemented by the binary method, and query by name is implemented by the sequential method; you can query students whose scores are less than MB by score. Find the students and output the student records to the screen. If no student is found, the related information is displayed.
C. If option 2 is selected, the score is sorted in descending order, and the name and student ID order are adjusted accordingly.
D. If you select 3, insert a new student record in the order of student IDs and save the result to the student. dat file.
E. If you select 4, delete the records of the specified student and save the results to the student. dat file.
F. The above functions are compiled into subfunctions, which are called by the main function.
The C ++ code is as follows:

# Include <iostream> # include <fstream> # include <list> # include <cmath> # include <string. h> # define max_stu 100 // maximum number of students read/** Zheng Haibo blog.csdn.net/nuptboyzhb/ * Email: zhb931706659@126.com */using namespace STD; class student {public: char * Name; char * ID; int grade; student () {name = new char [strlen ("anonymous -----") + 1]; id = new char [strlen ("noidinput ------") + 1]; grade = 0;} student (char * pname, char * PID, int pgrade): grade (pgrade) {name = new char [strlen (pname) + 1]; strcpy (name, pname); Id = new char [strlen (PID) + 1]; strcpy (ID, pid);} student (const student & RHs): grade (RHS. grade) {name = new char [strlen (RHS. name) + 1]; strcpy (name, RHS. name); Id = new char [strlen (RHS. ID) + 1]; strcpy (ID, RHS. ID);} student & operator = (const student & RHs) {name = new char [strlen (RHS. name) + 1]; strcpy (name, RHS. name); Id = new char [strlen (RHS. ID) + 1]; strcpy (ID, RHS. ID); grade = RHS. grade; return * This;} // overload the = Operator // for sorting purposes, we consider that two student objects are "equal" // if they have the same grade bool operator = (const student & RHs) const {return (grade = RHS. grade )? True: false;} // overload the <operator // for sorting purposes, we consider that a student object is "less than" another // if it's grade is less than the other object's grade bool operator <(const student & RHs) const {return (Grade <RHS. grade )? True: false;} // overload the> operator // for sorting purposes, we consider that a student object is "greater than" another // if it's grade is greater than the other object's grade bool operator> (const student & RHs) const {return (grade> RHS. grade )? True: false;} // display the student information void print () {cout <name <"" <id <"" <Grade <Endl ;} // constructor ~ Student () {Delete [] Name; Delete [] ID ;}}; list <student> lst; // student linked list, used to store student data void print (list <student> lst, char * Name) // input all students in the linked list {list <student >:: iterator it; cout <name <":" <Endl; For (IT = lst. begin (); it! = Lst. end (); ++ it) IT-> Print (); cout <Endl;} void screena () // display screen operation a {cout <"***************************** * *********** "<Endl; cout <"1 -------------- query" <Endl; cout <"2 -------------- sort" <Endl; cout <"3 -------------- Insert" <Endl; cout <"4 -------------- Delete" <Endl; cout <"5 -------------- display" <Endl; cout <"6 -------------- save" <Endl; cout <"7 -------------- clear screen" <Endl; cout <"************************************ * * ** "<Endl;} void screenb () // display screen query {system (" CLS "); cout <"************************************ * *** "<Endl; cout <"1 ---------- query by student ID" <Endl; cout <"2 ---------- query by name" <Endl; cout <"3 ---------- query by score" <Endl; cout <"4 ---------- return" <Endl; cout <"************************************ * *** "<Endl ;} void searchbyid () // search by student ID {cout <"------- enter student ID" <Endl; char TID [12]; memset (TID ); cin> tid; bool flag = false; List <s Tudent>: iterator it; for (IT = lst. Begin (); it! = Lst. end (); ++ it) {If (strcmp (IT-> ID, tid) = 0) {cout <"---- found, the student information is as follows: ----- "<Endl; it-> Print (); flag = true; break ;}}if (flag = false) {cout <" not found! "<Endl ;}void searchbyname () // search for {cout by name <" ------- enter name: "<Endl; char tname [12]; memset (tname,); CIN> tname; bool flag = false; List <student >:: iterator it; for (IT = lst. begin (); it! = Lst. end (); ++ it) {If (strcmp (IT-> name, tname) = 0) {cout <"---- found, the student information is as follows: ----- "<Endl; it-> Print (); flag = true; break ;}}if (flag = false) {cout <" not found! "<Endl ;}void searchbygrade () // search for {cout by score <" ------- enter the score: "<Endl; int tgrade; CIN> tgrade; bool flag = false; List <student>: iterator it; for (IT = lst. begin (); it! = Lst. end (); ++ it) {If (IT-> grade = tgrade) {cout <"---- found, the student information is as follows: -----" <Endl; it-> Print (); flag = true; break ;}}if (flag = false) {cout <"not found! "<Endl ;}void sortbygrade () // sort by score, from high to low {system (" CLS "); cout <" ------- sort by score, the result is as follows: "<Endl; lst. sort (greater <student> (); List <student>: iterator it; for (IT = lst. begin (); it! = Lst. end (); ++ it) {It-> Print () ;}} void insertstudent () // insert a student {system ("CLS "); cout <"------- enter the student ID" <Endl; char TID [12]; memset (TID,); CIN> tid; cout <"------- enter name:" <Endl; char tname [12]; memset (tname,); CIN> tname; cout <"------- enter the score:" <Endl; int tgrade; CIN> tgrade; Student Stu (tname, tid, tgrade); lst. push_back (Stu); List <student>: iterator it; for (IT = lst. begin (); it! = Lst. end (); ++ it) {It-> Print () ;}} void deletestudent () // delete a student as required {system ("CLS "); cout <"------- enter the student ID to delete:" <Endl; char TID [12]; memset (TID,); CIN> tid; bool flag = false; List <student>: iterator it; for (IT = lst. begin (); it! = Lst. end (); ++ it) {If (strcmp (IT-> ID, tid) = 0) {cout <"---- found, the student information is as follows: ----- "<Endl; it-> Print (); lst. erase (it); cout <"deleted! "<Endl; flag = true; break; }}if (flag = false) {cout <" not found! "<Endl ;}void inputdata () // read data from the file {cout <" reading data from the file... "<Endl; ifstream ifile (" student. dat "); If (! Ifile) {cout <"student. dat cannot be opened! "<Endl; return;} Char ch; int I; for (I = 0; I <max_stu; I ++) // read count {string s_name, s_id, s_grade; if (! Ifile. Get (CH) {cout <"the file has been read! "<Endl; return;} while (Ch! = '#') // Read name {If (CH = '') // skip the space {ifile. get (CH); continue;} s_name + = CH; ifile. get (CH);} ifile. get (CH); While (Ch! = '#') // Read student ID {If (CH = '') {ifile. get (CH); // skip space continue;} s_id + = CH; ifile. get (CH);} ifile. get (CH); While (Ch! = '\ N') // read score {If (CH = '') {ifile. Get (CH); continue;} s_grade + = CH; If (! Ifile. Get (CH) {cout <"the file has been read! "<Endl; return ;}} student temp; strcpy (temp. name, s_name.c_str (); strcpy (temp. ID, s_id.c_str (); temp. grade = atoi (s_grade.c_str (); lst. push_back (temp);} ifile. close (); System ("CLS");} void saveasfile () {system ("CLS"); ofstream ofile ("student. dat ", IOS: Out); If (! Ofile) {cout <"failed to open the file! "<Endl; return ;}list <student >:: iterator it; for (IT = lst. Begin (); it! = Lst. end (); ++ it) {ofile <it-> name <"#" <it-> id <"#" <it-> Grade <"\ n ";} cout <"saved... "<Endl; ofile. close (); return;} int main () {inputdata (); // read data from the file char ch; screena (); While (CIN> CH) {Switch (CH) {Case '1': screenb (); While (CIN> CH) {int flag = 0; Switch (CH) {Case '1 ': searchbyid (); break; Case '2': searchbyname (); break; Case '3': searchbygrade (); break; Case '4': Flag = 1; break; default: Flag = 1; break;} If (flag = 1) {break;} break; Case '2': // sort sortbygrade (); break; case '3': // insert student insertstudent (); break; Case '4': // Delete student deletestudent (); break; Case '5 ': // print (LST, "--------- current data list as follows"); break; Case '6': // Save the data to the saveasfile (); break; case '7': // clear screen system ("CLS"); break; default: Return 0;} screena () ;}cout <"system exited" <Endl; return 0 ;}

Student. DAT:

Zhang Shan # b11010101 #98
Li Si # b11010101 #67
Wang Wu # b11010101 #88
Li Hua # b11010101 #76
Li Yang # b11010101 #55
Zhang Wei # b11010101 #87
Wang Dawei # b11010101 #89
Li Xiaoming # b11010101 #92
Zhang Shanyi # b11010101 #98
Li Siyi # b11010101 #67
Wang Wuyi # b11010101 #88
Li Huayi # b11010101 #76
Li yangyi # b11010101 #55
Zhang Weiyi # b11010101 #87
Wang da'er # b11010101 #89
Li xiaodeng # b11010101 #92


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.