在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第三十五篇博客。
本篇博客用C++实现了机房预约系统,本文是中部分。
本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。
目录
接下来用C++实现一个机房预约系统,由于内容很长,分为三篇博客。本篇博客是第二部分。
- void do_administrator(identity* person)
- {
- cout << "Welcome, " << person->name << " !" << endl;
- administrator* adm = (administrator*)person;
-
- while (true) {
- adm->show_menu();
- int choice;
- cout << "Please enter your choice" << endl;
- cin >> choice;
-
- if (choice == 1) {
- adm->add_account();
- }
- else if (choice == 2) {
- adm->look_account();
- }
- else if (choice == 3) {
- adm->look_room();
- }
- else if (choice == 4) {
- adm->clear_record();
- }
- else if (choice == 0) {
- delete adm;
- cout << "Have logged out" << endl;
- system("pause");
- system("cls");
- break;
- }
- else {
- cout << "Enter error, please enter 1,2,3,4 or 0" << endl;
- system("pause");
- system("cls");
- }
- }
- }
这是reservesystem.cpp中的内容。首先给出提示欢迎用户,然后将多态的指针向下转型,以调用子类的成员函数。随后进入循环,给出菜单并要求用户选择选项,每个选项进入不同的成员函数,输入0就销毁此指针并表示注销,随后退出此函数。输入其他选项就要求用户重新输入。
- void administrator::show_menu()
- {
- cout << "****************************************" << endl;
- cout << "********** 1. add account **************" << endl;
- cout << "********** 2. look account *************" << endl;
- cout << "********** 3. look room ****************" << endl;
- cout << "********** 4. clear reservation ********" << endl;
- cout << "********** 0. log out ******************" << endl;
- cout << "****************************************" << endl;
- }
这是administrator.cpp的内容,输出菜单。
- administrator::administrator(string name, string password)
- {
- this->name = name;
- this->password = password;
- vstudent.clear();
- vteacher.clear();
- init_vector();
- vroom.clear();
- }
这是administrator类的构造函数,将传入信息进行赋值,随后将vstudent vteacher和vroom清空并获取学生和教师信息。
- void administrator::init_vector(void)
- {
- ifstream ifs;
- ifs.open(STUDENT_FILE, ios::in);
-
- int sid;
- string sname;
- string spassword;
-
- while (ifs >> sid && ifs >> sname && ifs >> spassword) {
- student s(sid, sname, spassword);
- this->vstudent.push_back(s);
- }
- ifs.close();
-
- ifs.open(TEACHER_FILE, ios::in);
- int tid;
- string tname;
- string tpassword;
-
- while (ifs >> tid && ifs >> tname && ifs >> tpassword) {
- teacher t(tid, tname, tpassword);
- this->vteacher.push_back(t);
- }
- ifs.close();
- }
首先遍历存放学生信息的文件,将信息读取并构建一个student类对象,随后加入vstudent容器中,直至文件结束。然后遍历存放教师信息的文件,将信息读取并构建一个teacher类对象,随后加入vteacher容器中,直至文件结束。
- void administrator::add_account(void)
- {
- while (true) {
- int choice;
- cout << "********** 1. add student **************" << endl;
- cout << "********** 2. add teacher **************" << endl;
- cout << "Please enter the choice" << endl;
- cin >> choice;
-
- if (choice == 1) {
- ofstream ofs;
- ofs.open(STUDENT_FILE, ios::in | ios::app);
- int id;
-
- while (true) {
- cout << "Please enter the student's number" << endl;
- cin >> id;
-
- bool flag = check_repeat(id, 1);
- if (flag == false) {
- break;
- }
- else {
- cout << "It is repeat,please enter again" << endl;
- }
- }
-
- string name;
- cout << "Please enter the name" << endl;
- cin >> name;
- string password;
- cout << "Please enter the password" << endl;
- cin >> password;
-
- ofs << id << " " << name << " " << password << endl;
- ofs.close();
-
- student s(id, name, password);
- vstudent.push_back(s);
- cout << "Done" << endl;
- system("pause");
- system("cls");
- break;
- }
- else if (choice == 2) {
- ofstream ofs;
- ofs.open(TEACHER_FILE, ios::in | ios::app);
-
- int id;
- while (true) {
- cout << "Please enter the teacher's number" << endl;
- cin >> id;
- bool flag = check_repeat(id, 2);
- if (flag == false) {
- break;
- }
- else {
- cout << "It is repeat,please enter again" << endl;
- }
- }
-
- string name;
- cout << "Please enter the name" << endl;
- cin >> name;
- string password;
- cout << "Please enter the password" << endl;
- cin >> password;
-
- ofs << id << " " << name << " " << password << endl;
- ofs.close();
-
- teacher t(id, name, password);
- vteacher.push_back(t);
-
- cout << "Done" << endl;
- system("pause");
- system("cls");
- break;
- }
- else {
- cout << "Enter error" << endl;
- system("pause");
- system("cls");
- }
- }
- }
首先进入一个循环,给出输入1增加学生输入2增加教师,要求用户输入,输入1和2就在完成添加功能后退出循环,否则要求用户重新输入。
如果输入1,首先要求输入学号,并用check_repeat函数检查重复(这个函数下面会放),重复就要求重新输入,不重复就可进行下面操作。然后输入学生姓名和密码,将这些信息写入存放学生信息的文件中,同时构建一个student对象并加入vstudent中。
如果输入2,首先要求输入教师编号,并用check_repeat函数检查重复(这个函数下面会放),重复就要求重新输入,不重复就可进行下面操作。然后输入教师姓名和密码,将这些信息写入存放教师信息的文件中,同时构建一个teacher对象并加入vteacher中。
- bool administrator::check_repeat(int id, int type)
- {
- if (type == 1) {
- for (vector
::iterator it = vstudent.begin(); it != vstudent.end(); ++it) { - if (it->id == id) {
- return true;
- }
- }
- return false;
- }
- else if (type == 2) {
- for (vector
::iterator it = vteacher.begin(); it != vteacher.end(); ++it) { - if (it->id == id) {
- return true;
- }
- }
- return false;
- }
- }
这是check_repeat函数,检查重复。如果是学生,就遍历vstudent,如果有学号重复就返回true,否则返回false。如果是教师,就遍历vteacher,如果有编号重复就返回true,否则返回false。
- void administrator::look_account(void)
- {
- while (true) {
- int choice;
- cout << "********** 1. look student *************" << endl;
- cout << "********** 2. look teacher *************" << endl;
- cout << "Please enter the choice" << endl;
- cin >> choice;
-
- if (choice == 1) {
- cout << "Here are all student's information" << endl;
- for_each(vstudent.begin(), vstudent.end(), lookstudent);
- break;
- }
- else if (choice == 2) {
- cout << "Here are all teacher's information" << endl;
- for_each(vteacher.begin(), vteacher.end(), lookteacher);
- break;
- }
- else {
- cout << "Please enter 1 or 2" << endl;
- }
- }
- system("pause");
- system("cls");
- }
首先进入一个循环,给出输入1显示学生输入2显示教师,要求用户输入,输入1和2就在完成显示功能后退出循环,否则要求用户重新输入。输入1就遍历vstudent输出学生信息,输入2就遍历vteacher输出教师信息。
- void lookstudent(student s) {
- cout << "The student's id is " << s.id << " and the name is " << s.name << " and the password is " << s.password << endl;
- }
-
- void lookteacher(teacher t) {
- cout << "The teacher's id is " << t.id << " and the name is " << t.name << " and the password is " << t.password << endl;
- }
这是for_each算法用到的函数。
- #pragma once
- #include
- using namespace std;
- class room
- {
- public:
- int num;
- int capacity;
- };
这是room类的内容,实现了room类对象。成员变量num表示编号,capacity表示容量。
- void administrator::look_room(void)
- {
- ifstream ifs;
- ifs.open(ROOM_FILE, ios::in);
-
- int num;
- int capacity;
- while (ifs >> num && ifs >> capacity) {
- room r;
- r.num = num;
- r.capacity = capacity;
- vroom.push_back(r);
- }
-
- for (vector
::iterator it = vroom.begin(); it != vroom.end(); ++it) { - cout << "The num is " << it->num << " and the capacity is " << it->capacity << endl;
- }
- ifs.close();
- system("pause");
- system("cls");
- }
这是look_room函数,先从文件中获取信息并构建对象加入vroom中,随后遍历vroom输出相关信息。
- void administrator::clear_record(void)
- {
- ofstream ofs(REVERSE_FILE, ios::trunc);
- ofs.close();
- cout << "It has been cleared" << endl;
- system("pause");
- system("cls");
- }
进入此函数后,清空文件并提示已清空。
- void do_student(identity* person)
- {
- cout << "Welcome " << person->name << '!' << endl;
- student* stu = (student*)person;
-
- while (true) {
- stu->show_menu();
- int choice;
- cout << "Please enter your choice " << endl;
- cin >> choice;
-
- if (choice == 1) {
- stu->make_reverse();
- }
- else if (choice == 2) {
- stu->look_my_reverse();
- }
- else if (choice == 3) {
- stu->look_all_reverse();
- }
- else if (choice == 4) {
- stu->cancel_reverse();
- }
- else if (choice == 0) {
- delete stu;
- cout << "Have logged out" << endl;
- system("pause");
- system("cls");
- break;
- }
- else {
- cout << "Enter error, please enter 1,2,3,4 or 0" << endl;
- system("pause");
- system("cls");
- }
- }
- }
这是reservesystem.cpp中的内容。首先给出提示欢迎用户,然后将多态的指针向下转型,以调用子类的成员函数。随后进入循环,给出菜单并要求用户选择选项,每个选项进入不同的成员函数,输入0就销毁此指针并表示注销,随后退出此函数。输入其他选项就要求用户重新输入。
- void student::show_menu(void)
- {
- cout << "****************************************" << endl;
- cout << "********** 1. apply reservation ********" << endl;
- cout << "********** 2. look my reservation ******" << endl;
- cout << "********** 3. look all reservation *****" << endl;
- cout << "********** 4. cancel reservation *******" << endl;
- cout << "********** 0. log out ******************" << endl;
- cout << "****************************************" << endl;
- }
这是student类的show_menu成员函数,输出菜单。
- student::student(int id, string name, string password)
- {
- this -> id = id;
- this->name = name;
- this->password = password;
-
- ifstream ifs;
- ifs.open(ROOM_FILE, ios::in);
-
- int num;
- int capacity;
- while (ifs >> num && ifs >> capacity) {
- room r;
- r.num = num;
- r.capacity = capacity;
- vroom.push_back(r);
- }
- ifs.close();
- }
这是student类的构造函数,将传入的信息赋值,随后遍历存储机房信息的文件,获取机房信息并加入vroom。
- void student::make_reverse(void){
- int daychoice;
- while (true) {
- cout << "*The open time is from Monday to Friday*" << endl;
- cout << "********** 1. Monday *******************" << endl;
- cout << "********** 2. Tuesday ******************" << endl;
- cout << "********** 3. Wednesday ****************" << endl;
- cout << "********** 4. Thursday *****************" << endl;
- cout << "********** 5. Friday *******************" << endl;
-
- cout << "Please enter the choice" << endl;
- cin >> daychoice;
- if (daychoice >= 1 && daychoice <= 5) {
- break;
- }
- else {
- cout << "Please enter 1,2,3,4 or 5" << endl;
- }
- }
-
- system("pause");
- system("cls");
- int timechoice;
- while (true) {
- cout << "***Please select morning or afternoon***" << endl;
- cout << "********** 1. morning ******************" << endl;
- cout << "********** 2. afternoon ****************" << endl;
- cout << "Please enter the choice" << endl;
- cin >> timechoice;
-
- if (timechoice == 1 || timechoice == 2) {
- break;
- }
- else {
- cout << "Please enter again" << endl;
- }
- }
- system("pause");
- system("cls");
-
- cout << "The room " << vroom[0].num << " has " << vroom[0].capacity << " seats" << endl;
- cout << "The room " << vroom[1].num << " has " << vroom[1].capacity << " seats" << endl;
- cout << "The room " << vroom[2].num << " has " << vroom[2].capacity << " seats" << endl;
-
- int roomchoice;
- while (true) {
- cout << "Please select the room" << endl;
- cin >> roomchoice;
- if (roomchoice >= 1 && roomchoice <= 3) {
- break;
- }
- else {
- cout << "Please enter again" << endl;
- }
- }
-
- cout << "Done,it is waiting to be audited" << endl;
- ofstream ofs(REVERSE_FILE, ios::app);
- ofs << "date:" << daychoice << " " << " time:" << timechoice << " " << " id:" << id << " ";
- ofs << " name:" << name << " " <<" room:" << roomchoice <<" "<< " status:" << "auditing" << endl;
- ofs.close();
- system("pause");
- system("cls");
- }
首先提示用户1-5代表周一至周五,要求用户选择日期(输入错误要求重新输入)。然后提示1表示上午2表示下午,要求用户选择上午还是下午,同样输入错误要求重新输入。然后给出机房信息,要求用户选择机房,同样输入错误要求重新输入。随后完成申请的功能,提示状态是未批准,将信息写入文件中。