首页 > 编程语言 >c++ 老师学生类信息管理实现

c++ 老师学生类信息管理实现

时间:2025-01-06 18:58:31浏览次数:10  
标签:return string int 老师 信息管理 c++ name include cout

项目场景:

师生校园游戏小程序


项目描述

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

相关文章

  • 优雅处理任务取消: C++20 的 Cooperative Cancellation
    在现代软件开发中,异步和并发操作是不可避免的.然而,当任务需要取消时,如何优雅地终止任务,释放资源并保持系统的稳定性,往往是一个挑战.C++20引入了stop_token,stop_source和stop_callback三大工具,为我们提供了灵活的协作式任务取消机制.本文通过一个模拟......
  • Foxit PDF SDK 10.1 for Windows (C++ Library)
    FoxitPDFSDKforWindows强大且易于使用的核心API,用于渲染、查看、注释、签名、保护和管理PDF中的表单。快速集成功能强大且易于使用的C++、C#、C、Python和Java核心API。系统要求:WindowsXP、Vista、7、8和10(32位和64位);WindowsServer2003、2008、2012和......
  • C++从入门到精通(第6版)PDF、EPUB免费下载
    电子版仅供预览,下载后24小时内务必删除,支持正版,喜欢的请购买正版书籍点击原文去下载内容简介《C++从入门到精通(第6版)》从初学者角度出发,以通俗易懂的语言和丰富多彩的实例,详细讲解了C++程序开发需要掌握的知识。本书分为4篇共19章:第1篇是基础知识,包括绪论,数据类型,运算符与表......
  • 10. C++对象模型和this指针
    1. 成员变量和成员函数分开存储在C++中,类内的成员变量和成员函数分开存储只有非静态成员变量才属于类的对象.classPerson{public:Person(){mA=0;}//非静态成员变量占对象空间intmA;//静态成员变量不占对象空间staticintmB;//函数也不占对象空间,所有函数共......
  • (2024最新毕设合集)基于Django的电影资讯共享平台-10223|可做计算机毕业设计JAVA、PHP、
    目录摘要Abstract1绪论1.1研究背景1.2研究意义1.3论文结构与章节安排2电影资讯共享平台系统分析2.1可行性分析2.1.1技术可行性分析2.1.2经济可行性分析2.1.3 社会可行性2.1.4法律可行性分析2.2系统功能分析2.2.1功能性分析2.2.2非功能性分析2.......
  • 以C++为低延迟的解决方案总结
    前言某天在知乎上看到的问题https://www.zhihu.com/question/23185359/answer/137034841,《以C++为核心语言的高频交易系统是如何做到低延迟的》,感觉问题和答案都非常好,所以想在此归纳和总结一下优秀的答案作为今后的参考要点总结为了解决这个问题,可以结合我之前记录的C++......
  • 启航数据结构算法之雅舟,悠游C++智慧之旅——线性艺术:顺序表之细腻探索
    人无完人,持之以恒,方能见真我!!!共同进步!!文章目录一、线性表的概念二、顺序表1.概念与结构2.顺序表的分类静态顺序表动态顺序表三、顺序表的实现1.顺序表的结构2.顺序表的初始化和销毁初始化函数销毁函数3.顺序表的扩容4.顺序表的尾插和头插尾插函数头插函数5.顺序......
  • C++26 函数契约(Contract)概览
    文章目录1.什么是契约编程?契约编程的三大核心:2.C++26契约编程的语法语法示例3.契约检查模式3.1.`default`模式3.2.`audit`模式3.3.`axiom`模式检查模式的设置4.契约编程与传统`assert`的区别示例对比5.契约编程的应用场景6.注意事项7.示例:带契约......
  • C++ 实现定时器
    冬天的午后,寒意略显温柔,不像晨时那样刺骨,也不像入夜之时的冰冷。阳光倾斜落在阳台上。想必它是耀眼的,照在屋外树梢上仅剩的几片叶子上,闪闪发光,有些晃眼。学习自:零声教育的视频1.什么是定时器定时器是一种用于在未来某个时间点执行某个任务的机制。在操作系统中,定时器是一......
  • 【C++动态规划】2088. 统计农场中肥沃金字塔的数目|2104
    本文涉及知识点C++动态规划LeetCode2088.统计农场中肥沃金字塔的数目有一个矩形网格状的农场,划分为m行n列的单元格。每个格子要么是肥沃的(用1表示),要么是贫瘠的(用0表示)。网格图以外的所有与格子都视为贫瘠的。农场中的金字塔区域定义如下:区域内格子数......