C++ 学工号登录系统

偷偷拿来记录一下萌新的cs路——day 37 暑假培训小练习2

说明:创建student.txt 和 teacher.txt 两个文件,开始时在teacher.txt 中添加一个老师(管理员),登录该账号添加学生or老师账号,再登录其他账号进行操作。

globalFile.h 存储信息为文件,分学生和老师

#pragma once
#define STUDENT_FILE "student.txt"
#define TEACHER_FILE "teacher.txt"

 Identity.h 创建用户名和密码

#pragma once
#include<iostream>
#include<string>
using namespace std;

class Identity {
public:
	virtual void operMenu() = 0;
	string m_Name;  // 用户名
	string m_Pwd;  // 密码
};

student.h 学生特性

#pragma once
#include"Identity.h"
#include"globalFile.h"
#include<iostream>
using namespace std;

class Student :public Identity {
public:
	Student();  // 构造函数
	Student(int id, string name, string pwd);  // 拷贝构造函数
	virtual void operMenu();  // 操作界面
	void showInf();  // 功能:显示学生信息

	int m_Id = 0;
};

 teacher.h 老师特性

#pragma once
#include"globalFile.h"
#include"Identity.h"
#include"student.h"
#include<iostream>
using namespace std;

class Teacher :public Identity {
public:
	Teacher();
	Teacher(int empId, string name, string pwd);
	virtual void operMenu();
	void addStu();  // 功能:添加人员账号
	void showStu();  // 功能:显示学生信息
	void deleteStu();  // 功能:删除学生账号
	int m_EmpId = 0;
};

student.cpp 定义学生类

#include"student.h"
using namespace std;

Student::Student() {
}

Student::Student(int id, string name, string pwd) {
	this->m_Id = id;
	this->m_Name = name;
	this->m_Pwd = pwd;
}

void Student::operMenu() {
	cout << "Welcome, " << this->m_Name << endl;
	cout << "Your information: " << endl;
}

void Student::showInf() {
    // 暂时没有写这个功能qwq
}

由于学生没有进行操作的需求,这里的operMenu只显示信息。

teacher.cpp 定义老师类

#include"teacher.h"
#include<fstream>
using namespace std;

Teacher::Teacher() {
}

Teacher::Teacher(int empId, string name, string pwd) {
	this->m_EmpId = empId;
	this->m_Name = name;
	this->m_Pwd = pwd;
}

void Teacher::operMenu() {
	cout << "Welcome, " << this->m_Name << endl;
	cout << "*************************" << endl;
	cout << "*** 1. Add account    ***" << endl;
	cout << "*** 2. Show student   ***" << endl;
	cout << "*** 3. Delete student ***" << endl;
	cout << "*** 0. Quit           ***" << endl;
	cout << "*************************" << endl;
}

void Teacher::addStu() {
	string fileName;
	string tip;
	ofstream ofs;  // 文件操作对象

	int select = 0;
	cout << "Create a student account (1) or a teacher account (2)?" << endl;
	cin >> select;

	if (select == 1) {
		fileName = STUDENT_FILE;
		tip = "Please enter the student id:";
	}
	else {
		fileName = TEACHER_FILE;
		tip = "Please enter the employee id: ";
	}

	ofs.open(fileName, ios::out | ios::app);  // 写入文件

	int id;
	string name, pwd;

	cout << tip;  // 输入职工号
	cin >> id;
	
	cout << "Please enter the name: ";
	cin >> name;
	cout << "Please enter the password: ";
	cin >> pwd;

	ofs << id << " " << name << " " << pwd << " " << endl;  // 向文件添加数据
	cout << "Add student succeed! " << endl;
	
	system("pause");
	system("cls");

	ofs.close();
}

void Teacher::showStu() {
    // 暂时没有qwq	
}

void Teacher::deleteStu() {
    // 暂时没有qwq
}

addStu用到了写入文件的知识(还未更新此部分内容)。

main.cpp

#include"globalFile.h"
#include"Identity.h"
#include"student.h"
#include"teacher.h"
#include<fstream>
using namespace std;

// 主菜单界面
void Menu() {
	cout << endl;
	cout << "************************" << endl;
	cout << "***     1. Student   ***" << endl;
	cout << "***     2. Teacher   ***" << endl;
	cout << "***     0. Quit      ***" << endl;
	cout << "************************" << endl;
}

// 学生菜单界面
void studentMenu(Identity*& student) {
	while (1) {
		student->operMenu();
		Student* stu = (Student*)student;
		cout << "Press any key to quit. " << endl;
		system("pause");
		system("cls");
		return;
	}
}

// 老师菜单界面
void teacherMenu(Identity *&teacher) {
	while (1) {
		teacher->operMenu();
		Teacher* man = (Teacher*)teacher;
		int select = 0;
		cin >> select;
		if (select == 1) {
			//cout << "Add student: " << endl;
			man->addStu();
			cout << endl;
		}
		else if (select == 2) {
			cout << "Show student: " << endl;
			man->showStu();
			cout << endl;
		}
		else if (select == 3) {
			cout << "Delete student: " << endl;
			man->deleteStu();
			cout << endl;
		}
		else {
			delete teacher;
			cout << "Quit succeed. " << endl;
			system("pause");
			system("cls");
			return;
		}
	}
}

// 登录
void Login(string fileName, int type) {
	Identity* person = NULL;  // 父类指针指向子类对象
	// 读文件
	ifstream ifs;
	ifs.open(fileName, ios::in);
	// 文件不存在
	if (!ifs.is_open()) {
		cout << "File does not exist qwq" << endl;
		ifs.close();
		return;
	}

	int id = 0;
	string name, pwd;

	// 学生登录
	if (type == 1) {
		cout << "Please enter your student id: ";
		cin >> id;
	}
	// 老师登录
	else if (type == 2) {
		cout << "Please enter your employee id: ";
		cin >> id;
	}

	cout << "Please enter your username: ";
	cin >> name;
	cout << "Please enter your password: ";
	cin >> pwd;

	// 登录验证
	// 学生
	if (type == 1) {
		int fId;
		string fName, fPwd;
		while (ifs >> fId && ifs >> fName && ifs >> fPwd) {
			if (id == fId && name == fName && pwd == fPwd) {
				cout << "Student login succeed! awa " << endl;
				system("pause");
				system("cls");
				person = new Student(id, name, pwd);
				studentMenu(person);
				return;
			}
		}
	}
	// 老师
	else if (type == 2) {
		int fId;
		string fName, fPwd;
		while (ifs >> fId && ifs >> fName && ifs >> fPwd) {
			if (id == fId && name == fName && pwd == fPwd) {
				cout << "Teacher login succeed! awa " << endl;
				system("pause");
				system("cls");
				person = new Teacher(id, name, pwd);
				teacherMenu(person);
				return;
			}
		}
	}

	cout << "Login failed. qwq " << endl;
	system("pause");
	system("cls");
	return;
}

int main() {
	int select = 0;
	
	while (1) {
		Menu();
		cin >> select;
		switch (select) {
			// 学生登录
		case 1:
			Login(STUDENT_FILE, 1);
			break;
			// 老师登录
		case 2:
			Login(TEACHER_FILE, 2);
			break;
		case 0:
			cout << "GoodBye~" << endl;
			return 0;
		default:
			cout << "Please choose one of the choices. ";
			break;
		}
	}

	return 0;
}

 运行结果

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值