background
As a beginner, basically the first year's curriculum will encounter a student management system with a black window, which needs to realize simple problems such as insertion, deletion, modification and display, as well as further functions such as sorting and analysis. This article mainly focuses on explanation, and the specific code will be downloaded separately.
text
The whole student management system is divided into nine parts, two structures, six functions and the final main function call.
Create structure
The whole system needs two structures: student part and class part.
The student section is defined as follows:
struct Student { int m_num = 0;//Student number string m_name = "";//full name double m_phy = 0;//Physics double m_mat = 0;//mathematics double m_eng = 0;//English double m_ave = 0;//average void _ave() { m_ave = (m_math + m_eng + m_phy) / 3; } //The average score can also be generated in the function in real time. Here, let it be generated automatically };
The class part is defined as follows:
const int MAX = 50;//Default 50 struct Class { Student stu[MAX];//Student array int count = 0;//Statistics of the number of students };
The class part is simple, simple can make it more convenient.
Create storage section
Generally, when learning C + +, the storage part mainly uses arrays. Of course, it does not rule out using the relatively fast voctor library directly. Here, the method of combining arrays and storage files is directly used. The array is represented in the class section. As for the storage file, it is the TXT text document directly used. Yes, it is the txt file downloaded from the daily network.
However, this function is very important in other subsequent processes. It is not good here. No matter how good other parts are, they will be scrapped.
void Read() { char c = '\0'; int n = 0; string read; room.count = 0; room.count += 1;//Used to count the number of students cout << "Reading file......" << endl;//Read part ifstream infile; infile.open("student.txt", ios::in); if (!infile.is_open()) { cerr << "open error" << endl; system("pause"); infile.close(); return; } else if (infile.eof()) { cerr << "open error" << endl; system("pause"); infile.close(); return; } while (infile.get(c)) { if (c == '\n') { room.stu[room.count]._ave(); room.count++; n = 0; read = ""; } else if (c == ' ' || c == '\t') { n++; switch (n) {//Data processing part case 1: room.stu[room.count].m_num = atoi(read.c_str()); break; case 2: room.stu[room.count].m_name = read; break; case 3: room.stu[room.count].m_phy = atof(read.c_str()); break; case 4: room.stu[room.count].m_mat = atof(read.c_str()); break; case 5: room.stu[room.count].m_eng = atof(read.c_str()); break; default: break; } read = ""; continue; } else { read += c; } } infile.close(); }
Add section
The insertion part is relatively simple, that is, after reading the file, generate a data column, and then insert new data to write back to the file.
void Add() { Read(); //Read() here is purely for statistical data. It's OK to delete it. It's just that the following students show problems. //However, in this way, a data structure teacher estimates that he will scold the spatial complexity=_=* ofstream outfile("student.txt", ios::app); if (!outfile){ cerr << "open error" << endl; system("pause"); outfile.close(); room.count = 0; return; } cout << "Please enter the number you want to add in turn" << room.count << "Student, student number, name, physics score, mathematics score, English score:" << endl; cin >> room.stu[room.count].m_num >> room.stu[room.count].m_name >> room.stu[room.count].m_phy >> room.stu[room.count].m_mat >> room.stu[room.count].m_eng; outfile << room.stu[room.count].m_num << "\t" << room.stu[room.count].m_name << "\t" << room.stu[room.count].m_phy << "\t" << room.stu[room.count].m_mat << "\t" << room.stu[room.count].m_eng << "\t" << endl; outfile.close(); room.count = 0; cout << "Successfully added!" << endl; }
Find section
As for why to write search first instead of write delete, the reason is that with search, it will be easy to delete anything.
//Determine whether the student is in the database int Judge1(int num) { Read();//Call the Read() function to get the data so that you can judge the relevant data later for (int i = 0; i <= room.count; i++) if (room.stu[i].m_num == num) return i;//If it exists, its subscript is returned return -1;//Indicates that there is no such information } int Judge2(string name) { Read();//Call the Read() function to get the data so that you can judge the relevant data later for (int i = 0; i <= room.count; i++) if (room.stu[i].m_name == name) return i;//If it exists, its subscript is returned return -1;//Indicates that there is no such information }
Delete part
void Delete() { int num; cout << "Please enter the student ID you want to delete:"; cin >> num; int k = Judge1(num);//Define a k to accept the return value of Judge(num), and then use it to judge whether the num exists if (k != -1)//If k is not equal to - 1, it indicates that the student to be deleted exists { ofstream outfile("student.txt", ios::ate); if (!outfile) { cerr << "open error" << endl; system("pause"); outfile.close(); room.count = 0; return; } for (int i = 1; i < room.count; i++) if (i != k) //Re write the rest of the data whose subscript is not equal to K (i.e. except the students to be deleted) to the disk and save it outfile << room.stu[i].m_num << "\t" << room.stu[i].m_name << "\t" << room.stu[i].m_phy << "\t" << room.stu[i].m_mat << "\t" << room.stu[i].m_eng << "\t" << endl; outfile.close(); room.count = 0; cout << "Delete succeeded!" << endl; } else { room.count = 0; cout << "This student does not exist in the database!" << endl; } }
Modified part
To implement the modification, you must find this brother first, so the previous search function is used (ps: the deleted part is also used).
//Modify a student's information void Change() { int num; cout << "Please enter the student number you want to modify:"; cin >> num; int k = Judge1(num); if (k != -1) { Read();//Read all student data, cout << "Please enter the modified student number in turn,full name,Physics achievement,Mathematics achievement,English achievement:" << endl; cin >> room.stu[k].m_num >> room.stu[k].m_name >> room.stu[k].m_phy >> room.stu[k].m_mat >> room.stu[k].m_eng; //Modify the student whose subscript is k ofstream outfile("student.txt", ios::out); if (!outfile) { cerr << "open error" << endl; system("pause"); outfile.close(); return; } for (int i = 1; i < room.count; i++)//After modification, write back to disk for saving outfile << room.stu[i].m_num << "\t" << room.stu[i].m_name << "\t" << room.stu[i].m_phy << "\t" << room.stu[i].m_mat << "\t" << room.stu[i].m_eng << "\t" << endl; outfile.close(); room.count = 0; cout << "Modified successfully!" << endl; } else { room.count = 0; cout << "This student does not exist in the database!" << endl; } }
Find section
In the basic part of a system, searching is still very important. You can't read millions of data one by one.
//Query a student's information (student number) void Search1() { int num; cout << "Please enter the student number you want to find"; cin >> num; int k = Judge1(num); if (k != -1) {//Find the student and print out their information cout << "Student number\t full name\t Physics achievement\t Mathematics achievement\t English achievement\t Average score" << endl; cout << room.stu[k].m_num << "\t" << room.stu[k].m_name << "\t" << room.stu[k].m_phy << "\t\t" << room.stu[k].m_mat << "\t\t" << room.stu[k].m_eng << "\t\t" << room.stu[k].m_ave << endl; } else cout << "This student does not exist in the database!" << endl; room.count = 0; } //Query a student's information (name) void Search2() { string name; cout << "Please enter the name of the student you want to find"; cin >> name; int k = Judge2(name); if (k != -1) {//Find the student and print out their information cout << "Student number\t full name\t Physics achievement\t Mathematics achievement\t English achievement\t Average score" << endl; cout << room.stu[k].m_num << "\t" << room.stu[k].m_name << "\t" << room.stu[k].m_phy << "\t\t" << room.stu[k].m_mat << "\t\t" << room.stu[k].m_eng << "\t\t" << room.stu[k].m_ave << endl; } else cout << "This student does not exist in the database!" << endl; room.count = 0; }
Display part
//Displays all information about the student void Display() { Read();//Read first cout << "Student number\t full name\t Physics achievement\t Mathematics achievement\t English achievement\t Average score" << endl; for (int i = 1; i < room.count; i++)//Post print cout << room.stu[i].m_num << "\t" << room.stu[i].m_name << "\t" << room.stu[i].m_phy << "\t\t" << room.stu[i].m_mat << "\t\t" << room.stu[i].m_eng << "\t\t" << room.stu[i].m_ave << endl; room.count = 0; }
Main function call
Basically here, the whole basic framework is almost complete.
//Displays all information about the student int main() { int choice = -1; while (choice != 0) {//Cycle until you choose exit system("cls"); cout << "\t\t\t--Student information management system--\n\n" << "\t\t\t| 1.Add a message |\n" << "\t\t\t| 2.Delete a message |\n" << "\t\t\t| 3.Modify student information |\n" << "\t\t\t| 4.Student ID query |\n" << "\t\t\t| 5.Student name query |\n" << "\t\t\t| 6.Display student information |\n" << "\t\t\t| 0.Exit the system |\n" << "Please select the desired operation:"; cin >> choice; switch (choice) { case 1:Add(); system("pause"); break; case 2:Delete(); system("pause"); break; case 3:Change(); system("pause"); break; case 4:Search1(); system("pause"); break; case 5:Search2(); system("pause"); break; case 6:Display(); system("pause"); break; case 0: break; default: cout << "Your choice is wrong!Please reselect!" << endl; system("pause"); break; } } return 0; }
ending
Here only the basic function creation in the student management system is listed.
Detailed source code link: Source code
In addition, I wrote the code when I was just in school. There will be something wrong. I hope the boss will not spray it.
Ps: due to our lack of knowledge, mistakes and omissions are inevitable. If you find errors and deficiencies in the article in the process of reading, you are welcome to exchange learning and correction.