用c++写该题的具体代码 以班级为单位进行选题,题目信息以文件形式存放, 包括:编号、题目、任务要求、完成时间 功能要求: 1学生信息包括:学号、姓名、班级; 2输入学号,随机抽取题目,限定每个题目只能被一个学生选中 3选题结果以文件形式存放,可以随时进行查询操作。
时间: 2024-02-22 15:59:32 浏览: 70
好的,以下是使用 C++ 实现该题的代码:
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;
// 定义学生信息结构体
struct Student {
string id;
string name;
string clazz;
};
// 定义题目信息结构体
struct Topic {
int id;
string title;
string requirement;
int deadline;
string selectedBy;
};
// 读取学生信息文件,返回学生信息数组
vector<Student> readStudents(string filename) {
vector<Student> students;
ifstream fin(filename);
if (!fin) {
cout << "Failed to open file " << filename << endl;
exit(1);
}
string line;
while (getline(fin, line)) {
Student student;
student.id = line.substr(0, 10);
student.name = line.substr(10, 20);
student.clazz = line.substr(30);
students.push_back(student);
}
fin.close();
return students;
}
// 读取题目信息文件,返回题目信息数组
vector<Topic> readTopics(string filename) {
vector<Topic> topics;
ifstream fin(filename);
if (!fin) {
cout << "Failed to open file " << filename << endl;
exit(1);
}
string line;
while (getline(fin, line)) {
Topic topic;
topic.id = stoi(line.substr(0, 4));
topic.title = line.substr(4, 30);
topic.requirement = line.substr(34, 50);
topic.deadline = stoi(line.substr(84));
topic.selectedBy = "";
topics.push_back(topic);
}
fin.close();
return topics;
}
// 根据学号获取学生信息
Student getStudentById(string id, vector<Student> students) {
for (const auto& student : students) {
if (student.id == id) {
return student;
}
}
cout << "Student with ID " << id << " not found!" << endl;
exit(1);
}
// 根据题目编号获取题目信息
Topic getTopicById(int id, vector<Topic> topics) {
for (const auto& topic : topics) {
if (topic.id == id) {
return topic;
}
}
cout << "Topic with ID " << id << " not found!" << endl;
exit(1);
}
// 将选题结果写入文件
void writeSelectionToFile(Topic topic, Student student) {
ofstream fout("selection.txt", ios::app);
if (!fout) {
cout << "Failed to open file selection.txt" << endl;
exit(1);
}
fout << student.id << " " << student.name << " " << student.clazz << " " << topic.id << " " << topic.title << endl;
fout.close();
}
// 查询选题结果
void querySelection() {
ifstream fin("selection.txt");
if (!fin) {
cout << "Failed to open file selection.txt" << endl;
exit(1);
}
string line;
while (getline(fin, line)) {
cout << line << endl;
}
fin.close();
}
int main() {
// 读取学生信息和题目信息
vector<Student> students = readStudents("students.txt");
vector<Topic> topics = readTopics("topics.txt");
// 随机抽取题目并限定每个题目只能被一个学生选中
srand(time(NULL));
vector<int> selectedTopicIds;
for (const auto& student : students) {
int topicId;
do {
topicId = rand() % topics.size() + 1;
} while (find(selectedTopicIds.begin(), selectedTopicIds.end(), topicId) != selectedTopicIds.end());
selectedTopicIds.push_back(topicId);
Topic topic = getTopicById(topicId, topics);
topic.selectedBy = student.id;
writeSelectionToFile(topic, student);
}
// 查询选题结果
querySelection();
return 0;
}
```
代码中的 `students.txt` 和 `topics.txt` 分别是学生信息和题目信息文件,其格式如下:
学生信息文件 `students.txt`:
```
20210001 张三 1班
20210002 李四 2班
20210003 王五 3班
20210004 赵六 4班
```
题目信息文件 `topics.txt`:
```
1 题目1 要求1 20220101
2 题目2 要求2 20220101
3 题目3 要求3 20220101
4 题目4 要求4 20220101
```
运行程序后,将随机抽取每个学生一个题目,并将选题结果保存在 `selection.txt` 文件中,其格式如下:
```
20210001 张三 1班 1 题目1
20210002 李四 2班 2 题目2
20210003 王五 3班 4 题目4
20210004 赵六 4班 3 题目3
```
程序还提供了选题结果查询功能,将 `querySelection()` 函数调用放到 `main()` 函数的任意位置即可查询选题结果。
阅读全文