用c++实现一个单门课程成绩管理系统。进入系统时,用户可选择身份:1.教师;2学生。 以教师身份进入后,如果上次已保存了数据,可将文件中数据读取出来,并允许修改或添加新数据。如果为第一次录入(之前无保存的数据),则允许教师录入相关数据。具体功能包括: 1.可录入或修改课程信息,包括:课程编号、课程名称; 2.可录入、修改、删除学生(数量不限)成绩,成绩项目包含两项:平时成绩、期末成绩,并按照各50%比例自动算出总成绩;学生基本信息包括学号、姓名;示例如下: 请录入“C++编程”课程成绩 学号:201901 姓名:张三 平时成绩:60 期末成绩:80 3.信息显示功能,可显示课程编号、课程名称、所有学生的成绩单。成绩单格式如下: 学号 姓名 平时成绩 期末成绩 总成绩 5.能够将上述数据保存在文件中,二进制、文本文件均可。 6.可读取上述文件。 以学生身份进入后,如果已保存了成绩数据,可将文件中数据读取出来,并允许查询。如果尚无保存数据,提示尚无成绩信息。具体功能包括: 1.信息显示功能,可显示课程编号、课程名称、所有学生的成绩单。
时间: 2024-02-17 09:02:06 浏览: 68
好的,我可以给你提供一份代码示例,你可以参考一下:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Course {
private:
string code;
string name;
vector<Student> students;
public:
Course(string code, string name) {
this->code = code;
this->name = name;
}
void addStudent(Student student) {
students.push_back(student);
}
void removeStudent(string id) {
for (int i = 0; i < students.size(); i++) {
if (students[i].getId() == id) {
students.erase(students.begin() + i);
break;
}
}
}
void modifyStudent(string id, double score1, double score2) {
for (int i = 0; i < students.size(); i++) {
if (students[i].getId() == id) {
students[i].setScore1(score1);
students[i].setScore2(score2);
break;
}
}
}
void showScore() {
cout << "课程编号:" << code << endl;
cout << "课程名称:" << name << endl;
cout << "学号\t姓名\t平时成绩\t期末成绩\t总成绩" << endl;
for (int i = 0; i < students.size(); i++) {
Student student = students[i];
cout << student.getId() << "\t" << student.getName() << "\t" << student.getScore1() << "\t" << student.getScore2() << "\t" << student.getScore() << endl;
}
}
void saveToFile() {
ofstream fout("score.txt");
if (fout.is_open()) {
fout << code << endl << name << endl << students.size() << endl;
for (int i = 0; i < students.size(); i++) {
Student student = students[i];
fout << student.getId() << "\t" << student.getName() << "\t" << student.getScore1() << "\t" << student.getScore2() << endl;
}
fout.close();
}
else {
cout << "文件无法打开!" << endl;
}
}
void readFromFile() {
ifstream fin("score.txt");
if (fin.is_open()) {
fin >> code >> name;
int size;
fin >> size;
for (int i = 0; i < size; i++) {
string id;
string name;
double score1;
double score2;
fin >> id >> name >> score1 >> score2;
Student student(id, name, score1, score2);
students.push_back(student);
}
fin.close();
}
else {
cout << "文件无法打开!" << endl;
}
}
};
class Student {
private:
string id;
string name;
double score1;
double score2;
public:
Student(string id, string name, double score1, double score2) {
this->id = id;
this->name = name;
this->score1 = score1;
this->score2 = score2;
}
string getId() {
return id;
}
string getName() {
return name;
}
double getScore1() {
return score1;
}
double getScore2() {
return score2;
}
double getScore() {
return score1 * 0.5 + score2 * 0.5;
}
void setScore1(double score1) {
this->score1 = score1;
}
void setScore2(double score2) {
this->score2 = score2;
}
};
class Teacher {
private:
Course course;
public:
Teacher(string code, string name) : course(code, name) {
ifstream fin("score.txt");
if (fin.is_open()) {
course.readFromFile();
fin.close();
}
else {
cout << "文件无法打开!" << endl;
}
}
void addStudent(Student student) {
course.addStudent(student);
}
void removeStudent(string id) {
course.removeStudent(id);
}
void modifyStudent(string id, double score1, double score2) {
course.modifyStudent(id, score1, score2);
}
void showScore() {
course.showScore();
}
void saveToFile() {
course.saveToFile();
}
};
class StudentSystem {
private:
Course course;
public:
StudentSystem() {
ifstream fin("score.txt");
if (fin.is_open()) {
course.readFromFile();
fin.close();
}
}
void showScore() {
course.showScore();
}
};
int main() {
int choice;
cout << "请选择身份:1.教师;2.学生" << endl;
cin >> choice;
if (choice == 1) {
Teacher teacher("001", "C++编程");
while (true) {
int operation;
cout << "请选择操作:1.录入学生信息;2.修改学生成绩;3.删除学生成绩;4.显示成绩单;5.保存数据;6.退出" << endl;
cin >> operation;
if (operation == 1) {
string id, name;
double score1, score2;
cout << "请输入学生信息:" << endl;
cout << "学号:";
cin >> id;
cout << "姓名:";
cin >> name;
cout << "平时成绩:";
cin >> score1;
cout << "期末成绩:";
cin >> score2;
Student student(id, name, score1, score2);
teacher.addStudent(student);
}
else if (operation == 2) {
string id;
double score1, score2;
cout << "请输入学生信息:" << endl;
cout << "学号:";
cin >> id;
cout << "平时成绩:";
cin >> score1;
cout << "期末成绩:";
cin >> score2;
teacher.modifyStudent(id, score1, score2);
}
else if (operation == 3) {
string id;
cout << "请输入学生信息:" << endl;
cout << "学号:";
cin >> id;
teacher.removeStudent(id);
}
else if (operation == 4) {
teacher.showScore();
}
else if (operation == 5) {
teacher.saveToFile();
}
else if (operation == 6) {
break;
}
else {
cout << "无效操作!" << endl;
}
}
}
else if (choice == 2) {
StudentSystem studentSystem;
studentSystem.showScore();
}
else {
cout << "无效身份!" << endl;
}
return 0;
}
```
注意:这只是一个简单的示例代码,还有很多可以优化的地方,例如输入校验、异常处理等。此外,如果你想要使用文件存储数据,你需要确保文件的读写权限,并且要保证文件名和路径的正确性。
阅读全文