From 4a7155149ceb6bb9dffc841a00b940b4a91caa1d Mon Sep 17 00:00:00 2001 From: Xi Xu <11871931+xi_xu@user.noreply.gitee.com> Date: Fri, 23 Jun 2023 11:44:22 +0000 Subject: [PATCH] add code --- Administrator.cpp | 193 +++++++++++++++++++++++++++++++++++++++ Administrator.h | 33 +++++++ CMakeLists.txt | 6 ++ Student.cpp | 50 +++++++++++ Student.h | 42 +++++++++ main.cpp | 225 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 549 insertions(+) create mode 100644 Administrator.cpp create mode 100644 Administrator.h create mode 100644 CMakeLists.txt create mode 100644 Student.cpp create mode 100644 Student.h create mode 100644 main.cpp diff --git a/Administrator.cpp b/Administrator.cpp new file mode 100644 index 0000000..2ece2f4 --- /dev/null +++ b/Administrator.cpp @@ -0,0 +1,193 @@ +// Copyright (c) 2023 Xi Xu +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "Administrator.h" +#include +#include +#include +#include +#include + +int Administrator::maxno = 0; + +Student Administrator::getstu(int i) +{ + return stu[i]; +} + +int Administrator::getmaxno() +{ + maxno++; + return maxno; +} + +int Administrator::add(char *i_name, bool i_gender, int i_age, char *i_major, int i_cls, double i_score) +{ + Student temp(getmaxno(), i_name, i_gender, i_age, i_major, i_cls, i_score); + stu.push_back(temp); + return maxno; +} + +int Administrator::find(int i_no) +{ + for (int i = 0; i < stu.size(); i++) + if (stu[i].getno() == i_no) + return i; + return -1; +} + +int Administrator::find(char *i_name) +{ + for (int i = 0; i < stu.size(); i++) + if (strcmp(stu[i].getname(), i_name) == 0) + return i; + return -1; +} + +std::vector Administrator::findbycls(char *i_major, int i_cls) +{ + std::vector tmp; + for (int i = 0; i < stu.size(); i++) + if (strcmp(stu[i].getmajor(), i_major) == 0 && stu[i].getcls() == i_cls) + tmp.push_back(i); + return tmp; +} + +bool Administrator::erase(int i_no) +{ + int i = find(i_no); + if (i == -1) + return false; + else + { + stu.erase(stu.begin() + i); + return true; + } +} + +bool Administrator::erase(char *i_name) +{ + int i = find(i_name); + if (i == -1) + return false; + else + { + stu.erase(stu.begin() + i); + return true; + } +} + +bool Administrator::edit(int i_no, char *i_name, bool i_gender, int i_age, char *i_major, int i_cls, double i_score) +{ + int i = find(i_no); + if (i == -1) + return false; + else + { + stu[i].setname(i_name); + stu[i].setgender(i_gender); + stu[i].setage(i_age); + stu[i].setmajor(i_major); + stu[i].setcls(i_cls); + stu[i].setscore(i_score); + return true; + } +} + +void Administrator::display() +{ + if (!stu.empty()) + { + std::cout << " No. Name Gender Age Major Class Score" << std::endl; + for (Student s : stu) + { + std::cout << s << std::endl; + } + } + else + std::cout << "No information exists!" << std::endl; +} + +struct ascende +{ + bool operator()(const Student &__x, const Student &__y) const + { + return __x.getscore() - __y.getscore() < 0; + } +}; + +struct descende +{ + bool operator()(const Student &__x, const Student &__y) const + { + return __x.getscore() - __y.getscore() > 0; + } +}; + +void Administrator::sequence(bool ascending) +{ + if (ascending) + sort(stu.begin(), stu.end(), ascende()); + else + sort(stu.begin(), stu.end(), descende()); +} + +double Administrator::average() +{ + double tmp = 0.0; + for (Student s : stu) + tmp += s.getscore(); + tmp /= stu.size(); + return tmp; +} + +void Administrator::loadfile() +{ + std::ifstream in("Student_Information.txt"); + if (!in) + { + std::cout << "Unable to open the file \"Student_Information.txt\"!" << std::endl; + return; + } + Student s; + int no; + char name[MAXN]; + char gender[MAXN]; + int age; + char major[MAXN]; + int cls; + double score; + while (in >> no >> name >> gender >> age >> major >> cls >> score) + { + s.setno(no); + s.setname(name); + if (strcmp(gender, "male") == 0) + s.setgender(true); + else if (strcmp(gender, "female") == 0) + s.setgender(false); + s.setage(age); + s.setmajor(major); + s.setcls(cls); + s.setscore(score); + stu.push_back(s); + }; + in.close(); + for (Student s : stu) + if (s.getno() > maxno) + maxno = s.getno(); +} + +void Administrator::writefile() +{ + std::ofstream out("Student_Information.txt"); + for (Student s : stu) + { + out << std::setw(4) << s.getno() << std::setw(16) << s.getname(); + if (s.getgender()) + out << " male"; + else + out << " female"; + out << std::setw(5) << s.getage() << std::setw(16) << s.getmajor() << std::setw(7) << s.getcls() << std::setw(7) << s.getscore() << std::endl; + } + out.close(); +} \ No newline at end of file diff --git a/Administrator.h b/Administrator.h new file mode 100644 index 0000000..eb43d5f --- /dev/null +++ b/Administrator.h @@ -0,0 +1,33 @@ +// Copyright (c) 2023 Xi Xu +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef STUDENT_INFORMATION_MANAGEMENT_SYSTEM_ADMINISTRATOR_H +#define STUDENT_INFORMATION_MANAGEMENT_SYSTEM_ADMINISTRATOR_H + +#include "Student.h" +#include + +class Administrator +{ +private: + std::vector stu; + static int maxno; + +public: + Student getstu(int i); + static int getmaxno(); + int add(char *i_name, bool i_gender, int i_age, char *i_major, int i_cls, double i_score); + int find(int i_no); + int find(char *i_name); + std::vector findbycls(char *i_major, int i_cls); + bool erase(int i_no); + bool erase(char *i_name); + bool edit(int i_no, char *i_name, bool i_male, int i_age, char *i_major, int i_cls, double i_score); + void display(); + void sequence(bool ascending = true); + double average(); + void loadfile(); + void writefile(); +}; + +#endif // STUDENT_INFORMATION_MANAGEMENT_SYSTEM_ADMINISTRATOR_H \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c9e445d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(Student_Information_Management_System) + +set(CMAKE_CXX_STANDARD 23) + +add_executable(Student_Information_Management_System Student.h Student.cpp Administrator.h Administrator.cpp main.cpp) \ No newline at end of file diff --git a/Student.cpp b/Student.cpp new file mode 100644 index 0000000..a8110af --- /dev/null +++ b/Student.cpp @@ -0,0 +1,50 @@ +// Copyright (c) 2023 Xi Xu +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "Student.h" +#include +#include +#include + +Student::Student() {} +Student::Student(int i_no, char *i_name, bool i_gender, int i_age, char *i_major, int i_cls, double i_score) +{ + no = i_no, strcpy(name, i_name), is_male = i_gender, age = i_age, strcpy(major, i_major), cls = i_cls, score = i_score; +} + +void Student::setno(int i_no) { no = i_no; } +void Student::setname(char *i_name) { strcpy(name, i_name); } +void Student::setgender(bool i_gender) { is_male = i_gender; } +void Student::setage(int i_age) { age = i_age; } +void Student::setmajor(char *i_major) { strcpy(major, i_major); } +void Student::setcls(int i_cls) { cls = i_cls; } +void Student::setscore(double i_score) { score = i_score; } + +int Student::getno() const { return no; } +char *Student::getname() const +{ + char *ret = new char[MAXN]; + strcpy(ret, name); + return ret; +} +bool Student::getgender() const { return is_male; } +int Student::getage() const { return age; } +char *Student::getmajor() const +{ + char *ret = new char[MAXN]; + strcpy(ret, major); + return ret; +} +int Student::getcls() const { return cls; } +double Student::getscore() const { return score; } + +std::ostream &operator<<(std::ostream &out, const Student &s) +{ + out << std::setw(4) << s.no << std::setw(16) << s.name; + if (s.is_male) + out << " male"; + else + out << " female"; + out << std::setw(5) << s.age << std::setw(16) << s.major << std::setw(7) << s.cls << std::setw(7) << s.score; + return out; +} \ No newline at end of file diff --git a/Student.h b/Student.h new file mode 100644 index 0000000..a494215 --- /dev/null +++ b/Student.h @@ -0,0 +1,42 @@ +// Copyright (c) 2023 Xi Xu +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef STUDENT_INFORMATION_MANAGEMENT_SYSTEM_STUDENT_H +#define STUDENT_INFORMATION_MANAGEMENT_SYSTEM_STUDENT_H + +#include + +const int MAXN = 20; + +class Student +{ +protected: + int no; + char name[MAXN]; + bool is_male; + int age; + char major[MAXN]; + int cls; + double score; + +public: + Student(); + Student(int, char *, bool, int, char *, int, double); + void setno(int); + void setname(char *); + void setgender(bool); + void setage(int); + void setmajor(char *); + void setcls(int); + void setscore(double); + int getno() const; + char *getname() const; + bool getgender() const; + int getage() const; + char *getmajor() const; + int getcls() const; + double getscore() const; + friend std::ostream &operator<<(std::ostream &, const Student &); +}; + +#endif // STUDENT_INFORMATION_MANAGEMENT_SYSTEM_STUDENT_H \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..50dbcbf --- /dev/null +++ b/main.cpp @@ -0,0 +1,225 @@ +// Copyright (c) 2023 Xi Xu +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "Administrator.h" +#include +#include +#include +#include + +using namespace std; + +void Login(Administrator &admin); +void Menu(Administrator &admin); + +int main() +{ + Administrator admin; + Login(admin); + return 0; +} + +void print() { cout << " No. Name Gender Age Major Class Score" << endl; } + +void Login(Administrator &admin) +{ + char password[MAXN] = "", input[MAXN]; + std::ifstream in("password.dat", ios::binary); + in.read(password, MAXN); + in.close(); + if (strcmp("", password) != 0) + { + cout << "Please enter your password to log in to the Student Information Management System: "; + cin >> input; + if (strcmp(input, password) != 0) + { + cout << "Wrong password!" << endl; + exit(0); + } + } + cout << "Do you want to modify or create a password? (Y/N)" << endl; + cin >> input; + if (strcmp(input, "N") == 0 || strcmp(input, "n") == 0) + cout << endl; + else if (strcmp(input, "Y") == 0 || strcmp(input, "y") == 0) + { + cout << "Please enter a password: "; + cin >> input; + std::ofstream out("password.dat", ios::binary); + out.write(input, MAXN); + out.close(); + cout << "Password modification or creation successful!" << endl + << endl; + } + Menu(admin); +} + +void Menu(Administrator &admin) +{ + char input1[MAXN], input4[MAXN]; + int input2, input5, input7; + bool input3; + double input6; + cout << "Welcome to the Student Information Management System!" << endl; + admin.loadfile(); + while (true) + { + cout << endl + << "Please enter instructions: Find(F) Add(A) Edit(E) Delet(D) Sort(S) Average(V) Display(P) Quit(Q): "; + cin >> input1; + // #################### Find #################### + if (strcmp("F", input1) == 0 || strcmp("f", input1) == 0) + { + cout << "Find by no.(0) or name(1) or class(2): "; + cin >> input2; + // Find by no. + if (input2 == 0) + { + cout << "Please enter no.: "; + cin >> input2; + int i = admin.find(input2); + if (i == -1) + cout << "Not found!" << endl; + else + { + print(); + cout << admin.getstu(i) << endl; + } + continue; + } + // Find by name + else if (input2 == 1) + { + cout << "Please enter name: "; + cin >> input1; + int i = admin.find(input1); + if (i == -1) + cout << "Not found!" << endl; + else + { + print(); + cout << admin.getstu(i) << endl; + } + continue; + } + // Find by class + else if (input2 == 2) + { + cout << "Please enter major: "; + cin >> input1; + cout << "class: "; + cin >> input2; + vector vi = admin.findbycls(input1, input2); + if (!vi.size()) + cout << "Not found!" << endl; + else + { + print(); + for (int i : vi) + { + cout << admin.getstu(i) << endl; + } + } + continue; + } + else + cout << "Enter error!" << endl; + } + // #################### Add #################### + else if (strcmp("A", input1) == 0 || strcmp("a", input1) == 0) + { + cout << "Please enter name: "; + cin >> input1; + cout << "male(1) or female(0): "; + cin >> input3; + cout << "age: "; + cin >> input2; + cout << "major: "; + cin >> input4; + cout << "class: "; + cin >> input5; + cout << "score: "; + cin >> input6; + cout << "no.: " << admin.add(input1, input3, input2, input4, input5, input6) << endl; + continue; + } + // #################### Edit #################### + else if (strcmp("E", input1) == 0 || strcmp("e", input1) == 0) + { + cout << "Please enter no.: "; + cin >> input2; + if (admin.find(input2) == -1) + cout << "Not exist!" << endl; + else + { + cout << "name: "; + cin >> input1; + cout << "male(1) or female(0): "; + cin >> input3; + cout << "age: "; + cin >> input7; + cout << "major: "; + cin >> input4; + cout << "class: "; + cin >> input5; + cout << "score: "; + cin >> input6; + admin.edit(input2, input1, input3, input7, input4, input5, input6); + cout << "Edited successfully!" << endl; + } + continue; + } + // #################### Delete #################### + else if (strcmp("D", input1) == 0 || strcmp("d", input1) == 0) + { + cout << "Delete by no.(1) or name(0): "; + cin >> input3; + if (input3) + { + cout << "Enter no.: "; + cin >> input2; + if (admin.erase(input2)) + cout << "Deleted successfully!" << endl; + else + cout << "Not exist!" << endl; + continue; + } + else + { + cout << "Enter name: "; + cin >> input1; + if (admin.erase(input1)) + cout << "Deleted successfully!" << endl; + else + cout << "Not exist!" << endl; + continue; + } + } + // #################### Sort #################### + else if (strcmp("S", input1) == 0 || strcmp("s", input1) == 0) + { + cout << "Ascending(1) or descending(0) order: "; + cin >> input3; + admin.sequence(input3); + cout << "Sorted successfully!" << endl; + } + // #################### Average #################### + else if (strcmp("V", input1) == 0 || strcmp("v", input1) == 0) + cout << "Average score: " << admin.average() << endl; + // #################### Display #################### + else if (strcmp("P", input1) == 0 || strcmp("p", input1) == 0) + admin.display(); + // #################### Quit #################### + else if (strcmp("Q", input1) == 0 || strcmp("q", input1) == 0) + { + cout << "Quit successfully!" << endl; + break; + } + else + { + cout << "Enter error!" << endl; + continue; + } + } + admin.writefile(); +} \ No newline at end of file