用c++写以班级为单位进行选题,题目信息以文件形式存放, 包括:编号、题目、任务要求、完成时间 功能要求: 1录入学生信息包括:学号、姓名、班级; 2输入学号,随机抽取题目,限定每个题目只能被一个学生选中 3选题结果以文件形式存放,可以随时进行查询操作。
时间: 2024-02-06 18:02:54 浏览: 22
好的,您的问题是如何用 C++ 实现以班级为单位进行选题,包括录入学生信息、随机抽取题目、选题结果存放以及查询操作。
以下是一个简单的 C++ 实现,包括学生信息的录入、题目的读取、随机抽取题目、选题结果的存放和查询操作。
```c++
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <map>
using namespace std;
// 定义学生结构体
struct Student {
string id; // 学号
string name; // 姓名
string cls; // 班级
};
// 定义题目结构体
struct Topic {
int id; // 编号
string title; // 题目
string requirement; // 任务要求
string deadline; // 完成时间
};
// 读取题目信息
vector<Topic> read_topics(string filename) {
vector<Topic> topics;
ifstream fin(filename);
if (!fin) {
cout << "无法打开文件:" << filename << endl;
exit(1);
}
while (!fin.eof()) {
Topic topic;
fin >> topic.id >> topic.title >> topic.requirement >> topic.deadline;
topics.push_back(topic);
}
fin.close();
return topics;
}
// 录入学生信息
void input_students(vector<Student>& students) {
int n;
cout << "请输入学生人数:";
cin >> n;
for (int i = 0; i < n; i++) {
Student student;
cout << "请输入第" << i+1 << "个学生的信息:" << endl;
cout << "学号:";
cin >> student.id;
cout << "姓名:";
cin >> student.name;
cout << "班级:";
cin >> student.cls;
students.push_back(student);
}
}
// 随机抽取题目
Topic select_topic(vector<Topic>& topics, map<int, string>& selected) {
int n = topics.size();
int idx = rand() % n;
Topic topic = topics[idx];
while (selected.find(topic.id) != selected.end()) {
idx = rand() % n;
topic = topics[idx];
}
selected[topic.id] = "";
return topic;
}
// 选题结果存放
void save_results(map<string, Topic>& results, string filename) {
ofstream fout(filename);
if (!fout) {
cout << "无法打开文件:" << filename << endl;
exit(1);
}
for (auto iter = results.begin(); iter != results.end(); iter++) {
fout << iter->first << " " << iter->second.id << " " << iter->second.title << endl;
}
fout.close();
}
// 查询选题结果
void query_results(map<string, Topic>& results) {
string id;
cout << "请输入学号:";
cin >> id;
if (results.find(id) == results.end()) {
cout << "该学生未选题" << endl;
} else {
Topic topic = results[id];
cout << "学号:" << id << endl;
cout << "题目编号:" << topic.id << endl;
cout << "题目:" << topic.title << endl;
}
}
int main() {
srand(time(NULL));
vector<Student> students;
input_students(students);
vector<Topic> topics = read_topics("topics.txt");
map<int, string> selected;
map<string, Topic> results;
for (auto iter = students.begin(); iter != students.end(); iter++) {
Student student = *iter;
cout << "学号:" << student.id << " 姓名:" << student.name << " 班级:" << student.cls << endl;
Topic topic = select_topic(topics, selected);
cout << "选题结果:" << topic.id << " " << topic.title << endl;
results[student.id] = topic;
}
save_results(results, "results.txt");
query_results(results);
return 0;
}
```
该程序首先读取题目信息,然后让用户输入学生信息,接着随机抽取题目并记录选题结果,最后将选题结果存放到文件中。用户可以随时进行查询操作,以查看自己的选题结果。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.png)