C++学习笔记(三十四)

在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第三十四篇博客。

本篇博客用C++实现了机房预约系统,本文是上部分。

本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。

目录

机房预约系统

系统需求介绍

程序结构

主菜单界面搭建以及提供登录接口

身份的抽象基类创建

学生类的创建

教师类创建

管理员类创建

全局文件添加

登录函数封装以及不同身份登录实现


机房预约系统

接下来用C++实现一个机房预约系统,由于内容很长,分为三篇博客。本篇博客是第一部分。

系统需求介绍

有三种身份:

学生代表申请使用机房。

教师审核学生的预约申请。

管理员给学生和教师创建账号。

机房有三间,其中1号机房最多容纳20人,2号机房最多容纳50人,3号机房最多容纳100人。

申请的订单由管理员负责清空。

学生可以预约未来一周内的机房使用,时间为周一至周五,要选择是上午还是下午。

教师审核预约,根据实际情况给出通过还是不通过。

首先进入登录界面,可选身份是学生代表,老师,管理员,或退出。

每个身份都要验证后进入子菜单,学生要输入学号姓名和密码,老师要输入编号姓名和密码,管理员要输入姓名和密码。

学生的功能有:

申请预约,查看自己的预约,查看所有的预约,取消预约和注销登录。

教师的功能有:

查看所有预约,审核预约和注销登录。

管理员的功能有:

添加账号,查看账号,查看机房,清空预约和注销登录。

程序结构

identity.h存放identity类的所有内容。administrator.h和administrator.cpp存放administrator类所有内容。student.cpp和student.h存放student类的所有内容。teacher.h和teacher.cpp存放teacher类所有内容。room.h存放room类所有内容。globalfile.h存放了几个关于文件名称的宏。reservesystem.cpp存放了控制程序流程的内容。

主菜单界面搭建以及提供登录接口

int main(void)
{
	while (true) {
		show_totalmenu();
		int choice;
		cin >> choice;

		switch (choice) {
		case 1:
			log_in(STUDENT_FILE, 1);
			break;
		case 2:
			log_in(TEACHER_FILE, 2);
			break;
		case 3:
			log_in(ADMINISTRATOR_FILE, 3);
			break;
		case 0:
			cout << "exit" << endl;
			break;
		default:
			cout << "Please enter 0,1,2 or 3" << endl;
			system("pause");
			system("cls");
			break;
		}
		if (choice == 0) {
			break;
		}
	}
	return 0;
}

这一部分在文件reservesystem.cpp中。

程序用死循环控制流程,首先展示菜单,然后要求用户输入选项。输入1 2 3都会进入log_in函数进行登录,但是参数会不一样,第一个参数是globalfile.h存放的关于文件名的宏。输入0提示退出程序并结束循环。输入其他选项要求重新输入。

void show_totalmenu(void)
{
	cout << "***Welcome to use our reverse system!***" << endl;
	cout << "*************** 1. student *************" << endl;
	cout << "*************** 2. teacher *************" << endl;
	cout << "*************** 3. administrator *******" << endl;
	cout << "*************** 0. exit ****************" << endl;
	cout << "Please enter your choice" << endl;
}

这一部分在文件reservesystem.cpp中。输出菜单。

身份的抽象基类创建

#pragma once
#include<iostream>
#include<string>
using namespace std;
class identity
{
public:
	string name;
	string password;
	virtual void show_menu() = 0;
};

这是identity.h的内容,实现了identity类,是对学校人员的抽象。name成员表示姓名,password成员表示登陆密码。show_menu函数是展示菜单,设置为纯虚函数,留给子类实现。

学生类的创建

#pragma once
#include<iostream>
#include<vector>
#include"room.h"
#include"identity.h"
using namespace std;
class student :public identity
{
public:
	int id;
	vector<room> vroom;
	student();
	student(int id, string name, string password);
	void show_menu();
	void make_reverse(void);
	void look_my_reverse(void);
	void look_all_reverse(void);
	void cancel_reverse();
};

这是student.h的内容,实现了student类。id表示学号,vroom存放room类对象。下面的函数在后面介绍。

教师类创建

#pragma once
#include<iostream>
#include<string>
#include"identity.h"
using namespace std;
class teacher :public identity
{
public:
	int id;
	teacher();
	teacher(int id, string name, string password);
	void show_menu();
	void look_reverse(void);
	void audit_reverse(void);
};

这是teacher.h的内容,实现了teacher类。id表示教师编号。成员函数在后面介绍。

管理员类创建

#pragma once
#include<iostream>
#include<string>
#include<vector>
#include"student.h"
#include"teacher.h"
#include"identity.h"
#include"room.h"
using namespace std;

class administrator :public identity
{
public:
	administrator();
	administrator(string name, string password);
	void show_menu();
	void init_vector(void);
	bool check_repeat(int id, int type);

	void add_account(void);
	void look_account(void);
	void look_room(void);
	void clear_record(void);

	vector<student> vstudent;
	vector<teacher> vteacher;
	vector<room> vroom;
};

这是administrator.h的内容,实现了administrator类。vroom存放room类对象,vstudent存放student类对象,vteacher存放teacher类对象。成员函数在后文介绍。

全局文件添加

#include<iostream>
#include<fstream>
using namespace std;
#define ADMINISTRATOR_FILE "administrator.txt"
#define STUDENT_FILE "student.txt"
#define TEACHER_FILE "teacher.txt"
#define ROOM_FILE "room.txt"
#define REVERSE_FILE "reverse.txt"

这是globalfile.h的内容,将很多用到文件的名称字符串定义为宏。

登录函数封装以及不同身份登录实现

void log_in(string filename, int type)
{
	identity* person = NULL;
	ifstream ifs;
	ifs.open(filename, ios::in);

	if (ifs.is_open() == false) {
		cout << "The file does not exist" << endl;
		return;
	}

	int id;
	string name;
	string password;

	if (type == 1) {
		cout << "Please enter the id" << endl;
		cin >> id;
	}
	else if(type == 2) {
		cout << "Please enter the id" << endl;
		cin >> id;
	}
	cout << "Please enter the name" << endl;
	cin >> name;
	cout << "Please enter the password" << endl;
	cin >> password;

	if (type == 1) {
		int rid;
		string rname;
		string rpassword;

		while (ifs >> rid && ifs >> rname && ifs >> rpassword) {
			if (rid == id && rname == name && rpassword == password) {
				cout << "Succeed to log in" << endl;
				person = new student(id, name, password);
				system("pause");
				system("cls");

				do_student(person);
				system("pause");
				system("cls");
				return;
			}
		}
	}
	else if (type == 2) {
		int rid;
		string rname;
		string rpassword;

		while (ifs >> rid && ifs >> rname && ifs >> rpassword) {
			if (rid == id && rname == name && rpassword == password) {
				cout << "Succeed to log in" << endl;
				person = new teacher(id, name, password);
				system("pause");
				system("cls");

				do_teacher(person);
				system("pause");
				system("cls");
				return;
			}
		}
	}
	else if (type == 3) {
		string rname;
		string rpassword;

		while (ifs >> rname && ifs >> rpassword) {
			if (rname == name && rpassword == password) {
				cout << "Succeed to log in" << endl;
				system("pause");
				system("cls");

				person = new administrator(name, password);
				do_administrator(person);
				system("pause");
				system("cls");
				return;
			}
		}
	}

	cout << "Fail to log in " << endl;
	system("pause");
	system("cls");
}

这一部分在文件reservesystem.cpp中。首先要求用户输入密码和姓名(学生还需要输入学号,教师还需要输入教师编号)。如果是要处理学生,则从文件中获取信息,如果检测到一致就提示登录成功,用信息构建一个对象,随后作为参数传递给do_student函数(后面介绍),否则提示登录失败。如果要处理教师,则从文件获取信息,检测到一致就提示登录成功,用信息构建一个对象,作为参数传递给do_teacher函数(后面)介绍,否则提示登陆失败。如果是要处理管理员,就从文件中获取信息,如果检测到一致就提示登录成功,用信息创建一个对象,随后作为参数传递给do_administrator函数(后面介绍),否则提示登录失败。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值