项目场景:
师生校园游戏小程序
项目描述
1.有学生,老师
2.学生老师可以进出校门,每出一次校门被扣外勤分1分
3.每个老师只教一门课,给多个班上课
4.每个学生学习三门课,语文,数学,英语
5.能够找到指定学生指定科目的老师
6.每门考试后,80分以上外勤分+1
7.老师可对指定班级进行考试,学生成绩由随机数生成
每考一次试,老师外勤分+1
8.老师可以申请奖励,花费30外勤分,放一天假,学生可以申请奖励,花费15外勤分,奖金100元
9.使用student.txt与teacher.txt初始化学生老师信息
项目分析:
1.构建人类,并以此基础上实现学生类与老师类。
2.共有方法:添加在基类中,进出校门、申请奖励
3.老师类方法:考试
4.学生类方法:找到指定老师
5.数据的输入输出
解决方案:
人类的构建
#pragma once
#include <iostream>
#include <string>
using namespace std;
//课程
typedef enum {
CHINESE,
MATH,
ENGLISH
}CLASS;
class SchoolHuman
{
public:
SchoolHuman() {
name = "未命名";
score = 0;
inSchool = true;
}
SchoolHuman(string named) :name(named) {
score = 0;
inSchool = true;
}
int getScore() const {
return score;
}
//进校门
void enterSchoolDoor();
//出校门
void outSchoolDoor();
//加考勤分
bool addScore(int val);
//申请奖励
virtual void getprsent() {}
protected:
string name;
bool inSchool;
int score;
};
拥有数据,考勤分,名字,与是否在学校的状态,分别完成对应的函数
#include "SchoolHuman.h"
void SchoolHuman::enterSchoolDoor() {
if (inSchool) {
cout << "你在学校里呢!" << endl;
}
else {
cout << "你进校门了!" << endl;
inSchool = true;
}
}
void SchoolHuman::outSchoolDoor() {
if (inSchool) {
cout << "你出校门了!外勤分-1!" << endl;
score--;
inSchool = false;
}
else {
cout << "你正在校外哦!" << endl;
}
}
bool SchoolHuman::addScore(int val) {
score += val;
return true;
}
实现基类方法
学生类的构建
#pragma once
#include<vector>
#include<sstream>
#include "SchoolHuman.h"
class teacher;
static vector<teacher> teachers;
class Student :
public SchoolHuman
{
public:
Student();
Student(string name, string classs) : SchoolHuman(name) {
this->score = 10;
this->className = classs;
}
~Student() {
}
//获得班级
string getclass() const;
//打印科目信息
string printfClass(CLASS learrn) const;
//插入学生
void inputStudent(vector<Student>& students);
//获得奖励
void getprsent();
//打印友元函数
friend ostream& operator<<(ostream& os, Student& student);
//CLASS转string科目
CLASS kemureturnCLASS(string kemu);
//寻找指定老师
string findTeacher(string kemu, vector<teacher> teachers);
static vector<Student> students;
protected:
CLASS learn[3] = { CHINESE,MATH,ENGLISH };
string className;
};
ostream& operator<<(ostream& os, Student& student);
学生拥有班级,三个科目,需要寻找指定老师,并完善基类获得奖励函数,打印科目信息
#include "Student.h"
#include "teacher.h"
Student::Student() :SchoolHuman() {
score = 10;
className = "未知";
}
string Student::getclass() const {
return className;
}
string Student::printfClass(CLASS learrn) const {
stringstream ret;
switch (learrn) {
case CHINESE: ret << "语文"; break;
case MATH: ret << "数学"; break;
case ENGLISH: ret << "英语"; break;
default: ret << "未知";
}
return ret.str();
}
void Student::inputStudent(vector<Student>& students) {
string classss;
string name;
int n = 1;
while (1) {
cout << "请输入学生 " << n << "的姓名" << endl;
cin >> name;
cout << "请输入学生年级" << endl;
cin >> classss;
students.push_back(Student(name, classss));
n++;
}
}
void Student::getprsent() {
if (this->score <= 15) {
cout << "你的考勤分不够,不能兑换" << endl;
}
else {
cout << "恭喜你,获得100元" << endl;
score -= 15;
}
}
CLASS Student::kemureturnCLASS(string kemu) {
if (kemu == "语文") return CHINESE;
else if (kemu == "数学") return MATH;
else if (kemu == "英语") return ENGLISH;
else
{
cout << "没有该科目" << endl;
exit(1);
}
}
string Student::findTeacher(string kemu, vector<teacher> teachers) {
string flag = className;
int find = 0;
stringstream ret;
for (int i = 0; i < teachers.size(); i++) {
if (teachers[i].getkemu() == kemureturnCLASS(kemu)) {
if (teachers[i].findclass(flag)) {
ret << "学生" << name << "的科目" << kemu << "的老师信息是:" << teachers[i];
find = 1;
}
}
}
if (find == 1) {
return ret.str();
}
else return "没找到";
}
ostream& operator<<(ostream& os, Student& student) {
os << "姓名:" << student.name << " 班级:" << student.className << " 考勤分:" << student.getScore();
return os;
}
完成对应方法,并运算符重载<< 打印学生信息
老师类构建
#pragma once
#include<vector>
#include "SchoolHuman.h"
#include "Student.h"
class teacher :
public SchoolHuman
{
public:
teacher(string name, CLASS teached, vector<string> greads) : SchoolHuman(name) {
this->score = 20;
if ((int)teached < -1 && (int)teached > 2) {
cout << "课程选择错误" << endl;
exit(1);
}
this->teached = teached;
this->greads = greads;
}
//获得科目
CLASS getkemu();
//sting科目转CLASS科目
CLASS kemureturnCLASS(string kemu);
//是否带这个班
bool findclass(string val);
//考试
void Test(string gread, vector<Student>& students);
//打印有元
friend ostream& operator<<(ostream& os, teacher& teacher);
//打印所在班级
string printfGread();
//老师奖励
void getprsent();
//插入老师
void inputTeacher(vector<teacher>& teachers);
//打印课程
string printfClass(CLASS learrn) const;
//添加年级
void addGrread(string gread);
protected:
CLASS teached;
vector<string> greads;
};
老师可以进行考试获得奖励,完善相应代码
#include <fstream>
#include "teacher.h"
#include "Student.h"
CLASS teacher::getkemu() {
return teached;
}
bool teacher::findclass(string val) {
for (int i = 0; i < greads.size(); i++) {
if (greads[i] == val) {
return true;
}
}
return false;
}
/* void Test(int classd) {
if (0) {
cout << "你不带这个班" << endl;
return;
}
for (int i = 0; i < students.size(); i++) {
if (students[i].getclass() == classd) {
int chengji = rand() % 100;
if (chengji >= 80) {
students[i].addScore(1);
}
}
}
this->addScore(10);
}
*/
void teacher::Test(string gread, vector<Student>& students) {
int flag = 0;
for (int i = 0; i < greads.size(); i++) {
if (gread == greads[i]) {
flag = 1;
}
}
if (flag) {
for (int i = 0; i <students.size(); i++) {
if (students[i].getclass() == gread) {
int cj = rand() % 100;
//测试用
cj = 81;
if (cj > 80) {
students[i].addScore(1);
cout << students[i] << " 考试成绩为" << cj<< " 外勤分+1" << endl;
}
}
}
this->addScore(10);
cout << "老师考试完成,外勤分+10" << endl;
}
else {
cout << "你不带这个班" << endl;
}
}
string teacher::printfGread() {
stringstream ret;
int flag = 0;
for (int i = 0; i < greads.size(); i++) {
ret << greads[i] << " ";
flag = 1;
}
if (flag)return ret.str();
else return "打印失败";
}
void teacher::getprsent() {
if (this->score <= 30) {
cout << "你的考勤分不够,不能休假" << endl;
}
else {
cout << "恭喜你,明天不用上课了" << endl;
score -= 30;
}
}
void teacher::inputTeacher(vector<teacher>& teachers) {
int classd = -1;
vector<string> graded;
string classname;
string kecheng;
string name;
int st = 0;
int n = 1;
while (1) {
cout << "请输入老师 " << n << "的姓名" << endl;
cin >> name;
cout << "请输入想教课程" << endl;
cin >> kecheng;
if (kecheng == "语文") {
classd = 0;
}
else if (kecheng == "数学") {
classd = 1;
}
else if (kecheng == "英语") {
classd = 2;
}
cout << "输入所带班级个数,并依次输入班级" << endl;
cin >> st;
if (st > 0) {
for (int i = 0; i < st; i++) {
cin >> classname;
graded.push_back(classname);
}
}
teachers.push_back(teacher(name, (CLASS)classd, graded));
n++;
}
}
string teacher::printfClass(CLASS learrn) const {
stringstream ret;
switch (learrn) {
case CHINESE: ret << "语文"; break;
case MATH: ret << "数学"; break;
case ENGLISH: ret << "英语"; break;
default: ret << "未知";
}
return ret.str();
}
void teacher::addGrread(string gread) {
for (int i = 0; i < greads.size(); i++) {
if (gread == greads[i]) {
cout << "重复添加,添加失败" << endl;
return;
}
}
greads.push_back(gread);
}
CLASS teacher::kemureturnCLASS(string kemu) {
if (kemu == "语文") return CHINESE;
else if (kemu == "数学") return MATH;
else if (kemu == "英语") return ENGLISH;
else
{
cout << "没有该科目" << endl;
exit(1);
}
}
ostream& operator<<(ostream& os, teacher& teacher) {
os << "姓名:" << teacher.name << " 所带课程:" <<
teacher.printfClass(teacher.teached) << " 所带班级"
<< teacher.printfGread() << " 考勤分:" << teacher.getScore();
return os;
}
可以进行考试,并具有判定机制,是否为自己的科目以及打印80分以上的学生信息
文件读入
```cpp
#include<fstream>
#include"Student.h"
#include"teacher.h"
using namespace std;
//string科目转CLASS
CLASS kemureturnCLASS(string kemu) {
if (kemu == "语文") return CHINESE;
else if (kemu == "数学") return MATH;
else if (kemu == "英语") return ENGLISH;
else
{
cout << "没有该科目" << endl;
exit(1);
}
}
vector<Student> readstudent(const string&filename) {
ifstream stufile;
stufile.open(filename, ios::in);
if (!stufile.is_open()) {
cout << "打开失败" << endl;
return vector<Student> {};
}
//读取学生个数
int stu_int;
stufile >> stu_int;
if (stu_int <= 0) {
cout << "读取失败" << endl;
return vector<Student> {};
}
//读取输入学生信息
vector<Student> students;
string name, classname;
for (int i = 0; i < stu_int; i++) {
if (stufile >> name && stufile >> classname) {
students.push_back(Student(name, classname));
}
}
return students;
}
vector<teacher> readteacher(const string& fliename) {
ifstream tecfile;
tecfile.open(fliename, ios::in);
if (!tecfile.is_open()) {
cout << "打开失败了" << endl;
return vector<teacher> {};
}
//读取老师个数
int tec_int;
tecfile >> tec_int;
if (tec_int <= 0) {
cout << "读取失败" << endl;
return vector<teacher> {};
}
int class_int;
//读取老师信息
vector<teacher> teachers;
string name;
string subject;
string calssname;
for (int i = 0; i < tec_int; i++) {
//获取姓名
tecfile >> name;
//获取所教课程
tecfile >> subject;
//获取班级个数并读取班级
tecfile >> class_int;
if (class_int <= 0) {
cout << "读取失败" << endl;
return vector<teacher> {};
}
vector<string> classnames;
for (int i = 0; i < class_int; i++) {
if (tecfile >> calssname) {
classnames.push_back(calssname);
}
}
teachers.push_back(teacher(name, kemureturnCLASS(subject), classnames));
}
return teachers;
}
void printfstudent(vector<Student> students) {
cout << "---------------------students------------------------------------------" << endl;
for (int i = 0; i < students.size(); i++) {
cout << students[i] << endl;
}
cout << "---------------------end------------------------------------------" << endl;
}
void printfteacher(vector<teacher> teachers) {
cout << "----------------------teachers------------------------------------------" << endl;
for (int i = 0; i < teachers.size(); i++) {
cout << teachers[i] << endl;
}
cout << "---------------------end------------------------------------------" << endl;
}
在这个模块通过fstream实现文件的读写操作,并且打印学生老师信息
测试
#include <iostream>
#include"read.h"
int main() {
vector<Student> studets = readstudent("student.txt");
printfstudent(studets);
vector<teacher> teachers = readteacher("teacher.txt");
printfteacher(teachers);
//学生老师进出校门测试
cout << "--------------进出校门-------------" << endl;
teachers[0].enterSchoolDoor();
teachers[0].outSchoolDoor();
teachers[0].outSchoolDoor();
teachers[0].enterSchoolDoor();
//老师考试测试
cout << "--------------考试----------------" << endl;
teachers[0].Test("三年级",studets);
teachers[0].Test("一年级", studets);
//学生找老师
cout << "--------------找老师--------------" << endl;
cout << studets[6].findTeacher("语文", teachers) << endl;;
//学生老师奖励
cout << "------------学生老师奖励---------" << endl;
studets[2].getprsent();
teachers[4].getprsent();
//测试成功状态
studets[2].addScore(15);
teachers[4].addScore(30);
studets[2].getprsent();
teachers[4].getprsent();
return 0;
}
以上完成该项目需求功能
总结
在本次项目实现过程中出现过的问题
问题:
明明构建了学生类却无法使用vector作为对象
原因:
在头文件中实现所有内容,导致文件之间相互递归拷贝
解决方式
按照完成版本结构进行分类
标签:return,string,int,老师,信息管理,c++,name,include,cout From: https://blog.csdn.net/qq_52214204/article/details/144969514