包括实训名称,教师和学生三个类
1、将实训提姆写入文件以及从文件读出
2、增加提目
3、进行提目分配,每三个学生选择一个,进行登记
4、可以读出信息进行输出
5、自己扩展功能(可有可无)
尽量多标注。
实训题木(目)有(实验室管理系统
,简单加减乘除计算器
,实训分配与管理系统
,学生信息管理系统
,学校教师信息管理
,动物关系系统,
计算图形面积和体积计算器
)
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
using namespace std;
// 题目结构
struct TiMu {
int id; // 题目id
string title; // 题目标题
};
// 教师
struct Teacher {
int id; // 教师编号
string name; // 教师姓名
};
// 学生
struct Student {
int id; // 学生学号
string name; // 学生姓名
int tid; // 分配实训题目
};
class Manager {
public:
explicit Manager(const string &file); // 构造函数
bool LoadTiMu(); // 从文件中加载题目
bool SaveTiMu(); // 保存题目到文件
void AddTiMu(const string &title); // 添加题目
void AddStudent(int id, const string &name); // 添加学生信息
void AddTeacher(int id, const string &name); // 添加教师信息
void DistributeOrder(); // 顺序分配题目
void OutputTiMu(); // 输出题目信息
void OutputDistribute(); // 输出分配信息
private:
int getNextTiMu(int current); // 获取下一个分配的题目
private:
string mTitleFile; // 实训题目保存文件路径
map<int, TiMu> mTiMuMap; // 实训题目
map<int, Teacher> mTeacher; // 教师信息
map<int, Student> mStudent; // 学生信息
};
Manager::Manager(const string &file)
{
mTitleFile = file;
}
bool Manager::LoadTiMu()
{
ifstream fp;
fp.open(mTitleFile);
if (!fp.is_open()) {
return false;
}
// 读取实训题目文件,文件格式为:
// 0 实验室管理系统
// 1 简单加减乘除计算器
// ....
string line;
while (getline(fp, line)) { // 读取文件每一行
istringstream is(line);
TiMu tmp;
is >> tmp.id >> tmp.title;
mTiMuMap.insert(make_pair(tmp.id, tmp));
}
fp.close();
return true;
}
bool Manager::SaveTiMu()
{
ofstream fp;
fp.open(mTitleFile);
if (!fp.is_open()) {
return false;
}
for (map<int, TiMu>::iterator it = mTiMuMap.begin(); it != mTiMuMap.end(); ++it) {
TiMu &tmp = it->second;
fp << tmp.id << " " << tmp.title << endl; // 按上述加载文件的格式: id 题目名称 保存实训题目信息
}
fp.close();
return true;
}
void Manager::AddTiMu(const string &title)
{
int nextid = 0;
for (map<int, TiMu>::iterator it = mTiMuMap.begin(); it != mTiMuMap.end(); ++it) {
TiMu &tmp = it->second;
if (tmp.title == title) { // 如果待添加实训题目已经存在,则返回
return;
}
nextid = tmp.id + 1;
}
// 新增实训题目,id为内部递增序号
TiMu n;
n.id = nextid;
n.title = title;
mTiMuMap.insert(make_pair(n.id, n));
return;
}
void Manager::AddStudent(int id, const string &name)
{
if (mStudent.find(id) != mStudent.end()) { // 已存在学生信息时,直接返回
return;
}
Student st;
st.id = id;
st.name = name;
st.tid = -1;
mStudent.insert(make_pair(id, st));
return;
}
void Manager::AddTeacher(int id, const string &name)
{
if (mTeacher.find(id) != mTeacher.end()) { // 已存在教师信息时,直接返回
return;
}
Teacher th;
th.id = id;
th.name = name;
mTeacher.insert(make_pair(id, th));
return;
}
// 获取下一个分配的题目ID
int Manager::getNextTiMu(int current)
{
if (mTiMuMap.empty()) { // 没有实训题目,返回-1
return -1;
}
if (current == -1 || current == mTiMuMap.size() - 1) { // 当为-1或者最后一个题目时,下一个题目从头开始获取
return mTiMuMap.begin()->first;
}
return current + 1; // 内部题目id为递增序列,直接+1取下一个
}
void Manager::DistributeOrder()
{
int i = 0; // 计数, 每3个学生分配一个实训题目
int tid = getNextTiMu(-1);
for (map<int, Student>::iterator it = mStudent.begin(); it != mStudent.end(); ++it) {
it->second.tid = tid;
++i;
if (i % 3 == 0) { // 每3个学生为一组,获取下一组待分配题目ID
tid = getNextTiMu(tid);
}
}
}
void Manager::OutputTiMu()
{
cout << "已有实训题目如下:" << endl;
for (map<int, TiMu>::iterator it = mTiMuMap.begin(); it != mTiMuMap.end(); ++it) {
cout << it->second.title << endl;
}
}
void Manager::OutputDistribute()
{
cout << "学生分配实训题目信息如下:" << endl;
for (map<int, Student>::iterator it = mStudent.begin(); it != mStudent.end(); ++it) {
map<int, TiMu>::iterator it2 = mTiMuMap.find(it->second.tid);
if (it2 == mTiMuMap.end()) {
cout << "学生: 学号 " << it->second.id << ", 姓名 " << it->second.name << " 未分配实训题目" << endl;
} else {
cout << "学生: 学号 " << it->second.id << ", 姓名 " << it->second.name << " 分配实训题目 " << it2->second.title << endl;
}
}
}
void Menu(void)
{
cout << "1 添加题目" << endl;
cout << "2 添加学生信息" << endl;
cout << "3 添加教师信息" << endl;
cout << "4 输出题目信息" << endl;
cout << "5 分配实训题目" << endl;
cout << "6 输出分配信息" << endl;
cout << "7 退出" << endl;
}
int main(void)
{
string file = "timu.txt";
Manager ma(file);
if (!ma.LoadTiMu()) {
cout << "加载题目文件失败" << endl;
}
bool flag = true;
while (flag) {
Menu();
int n;
cin >> n;
// 根据Menu中设置,输入相关信息并处理
switch(n) {
case 1: {
string title;
cout << "输入实训题目" << endl;
cin >> title;
ma.AddTiMu(title);
break;
}
case 2: {
int id;
string name;
cout << "输入学生学号" << endl;
cin >> id;
cout << "输入学生姓名" << endl;
cin >> name;
ma.AddStudent(id, name);
break;
}
case 3: {
int id;
string name;
cout << "输入教师编号" << endl;
cin >> id;
cout << "输入教师姓名" << endl;
cin >> name;
ma.AddTeacher(id, name);
break;
}
case 4: {
ma.OutputTiMu();
break;
}
case 5: {
ma.DistributeOrder();
cout << "分配结束" << endl;
break;
}
case 6: {
ma.OutputDistribute();
break;
}
case 7: {
flag = false;
break;
}
}
}
ma.SaveTiMu();
return 0;
}
哈哈哈, 你要是把一楼的看了, 估计可以自己写出来
都是优秀的人儿!
一楼提供思路是好事的!
题主直接提供题目也是优秀!可以参考一楼,也可以去来源社区找找,总有适合你的!
哈哈哈,顶那个给链接的。
你直接来发作业,我直接给你链接,自己研究。
后排跟随
希望以上内容可以帮助到你
第一楼真全。