用c++编写一个考试系统
时间: 2023-08-04 21:20:11 浏览: 51
好的,一个基本的考试系统需要以下功能:
1. 登录系统:学生和老师可以登录系统,学生只能查看考试信息和成绩,老师可以添加、修改和删除考试信息和成绩。
2. 考试信息管理:包括考试名称、考试时间、考试科目和考试地点等信息。
3. 考试成绩管理:包括学生姓名、学生学号、考试成绩和考试排名等信息。
4. 考试题目管理:包括题目类型、题目难度、题目内容和答案等信息。
下面是一个简单的考试系统的代码实现:
```cpp
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<ctime>
using namespace std;
class ExamSystem {
private:
string examName; // 考试名称
string examTime; // 考试时间
string examSubject; // 考试科目
string examPlace; // 考试地点
vector<string> studentNames; // 学生姓名
vector<int> studentNumbers; // 学生学号
vector<int> studentScores; // 学生成绩
vector<string> questionTypes; // 题目类型
vector<int> questionDifficulties; // 题目难度
vector<string> questionContents; // 题目内容
vector<string> questionAnswers; // 题目答案
public:
// 构造函数
ExamSystem(string name, string time, string subject, string place) {
examName = name;
examTime = time;
examSubject = subject;
examPlace = place;
}
// 添加学生信息
void addStudent(string name, int number) {
studentNames.push_back(name);
studentNumbers.push_back(number);
studentScores.push_back(-1); // 初始化成绩为-1,表示未参加考试
}
// 添加考试题目
void addQuestion(string type, int difficulty, string content, string answer) {
questionTypes.push_back(type);
questionDifficulties.push_back(difficulty);
questionContents.push_back(content);
questionAnswers.push_back(answer);
}
// 开始考试
void startExam() {
// 输出考试信息
cout << "考试名称:" << examName << endl;
cout << "考试时间:" << examTime << endl;
cout << "考试科目:" << examSubject << endl;
cout << "考试地点:" << examPlace << endl;
// 随机生成考试题目
srand(time(NULL));
int n = questionTypes.size();
vector<int> questionIndices;
for (int i = 0; i < n; ++i) {
questionIndices.push_back(i);
}
random_shuffle(questionIndices.begin(), questionIndices.end());
// 开始考试
int m = studentNames.size();
for (int i = 0; i < m; ++i) {
cout << "\n考生姓名:" << studentNames[i] << endl;
cout << "考生学号:" << studentNumbers[i] << endl;
// 随机生成题目顺序
vector<int> order;
for (int j = 0; j < n; ++j) {
order.push_back(j);
}
random_shuffle(order.begin(), order.end());
// 输出考题
int score = 0;
for (int j = 0; j < n; ++j) {
int k = questionIndices[order[j]];
cout << "\n第" << j+1 << "题:\n";
cout << questionTypes[k] << " " << questionDifficulties[k] << " " << questionContents[k] << endl;
string answer;
cin >> answer;
if (answer == questionAnswers[k]) {
score += questionDifficulties[k];
}
}
// 输出考试成绩
studentScores[i] = score;
cout << "\n考试成绩:" << score << endl;
}
}
// 输出考试成绩
void outputScores() {
cout << "\n考试成绩单:\n";
int m = studentNames.size();
for (int i = 0; i < m; ++i) {
cout << studentNames[i] << " " << studentNumbers[i] << " " << studentScores[i] << endl;
}
}
};
int main() {
ExamSystem system("2021年高考", "2021-06-07 09:00", "语文、数学、英语", "北京市");
system.addStudent("张三", 2021001);
system.addStudent("李四", 2021002);
system.addStudent("王五", 2021003);
system.addQuestion("选择题", 2, "以下哪个不是 C++ 的关键字?\nA. int\nB. float\nC. double\nD. template", "B");
system.addQuestion("判断题", 1, "C++ 中 bool 类型的变量只能取 true 或 false 两个值。\nA. 对\nB. 错", "A");
system.addQuestion("填空题", 3, "C++ 中,头文件 iostream 中定义了输入输出流对象 cin 和 cout,它们分别是 ______ 和 ______ 的对象。\n", "istream ostream");
system.startExam();
system.outputScores();
return 0;
}
```
这个考试系统使用了类的封装,通过成员函数实现了添加学生信息、添加考试题目、开始考试和输出考试成绩等功能。在开始考试时,随机生成考试题目和题目顺序,并要求考生输入答案并计算成绩。最后输出考试成绩单。