c++实现以下程序:学生成绩管理系统 基本要求: 定义学生(student)类,其中至少包括姓名、性别、学号、班级和四门功课的成绩。 功能要求: 1、设计菜单实现功能选择; 2、输入功能:输入学生信息,并保存到文件中; 3、计算每个学生的总分与平均分并能按照学生的不同信息排序整理,如按学号,按单科成绩、按平均成绩等; 4、能根据学号修改学生信息; 5、能根据学号删除学生信息; 6、查询功能: 1)能够根据学号查询学生信息; 2)能够根据姓名、班级查询学生信息; 3)统计学生成绩,按照班级和科目计算平均分。 7、所有的增加、修改、删除能同步到文件;也从文件读取数据到程序。
时间: 2023-12-14 16:05:09 浏览: 116
以下是C++实现的学生成绩管理系统,包含基本要求和功能要求:
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 学生类
class Student {
public:
string name; // 姓名
string gender; // 性别
string id; // 学号
string cls; // 班级
int scores[4]; // 四门功课的成绩
// 构造函数
Student() {}
// 构造函数
Student(string name, string gender, string id, string cls, int s1, int s2, int s3, int s4) {
this->name = name;
this->gender = gender;
this->id = id;
this->cls = cls;
scores[0] = s1;
scores[1] = s2;
scores[2] = s3;
scores[3] = s4;
}
// 获取总分
int getTotal() {
int total = 0;
for (int i = 0; i < 4; i++) {
total += scores[i];
}
return total;
}
// 获取平均分
double getAverage() {
return getTotal() / 4.0;
}
// 根据学号排序
static bool compareById(Student& s1, Student& s2) {
return s1.id < s2.id;
}
// 根据单科成绩排序
static bool compareByScore(Student& s1, Student& s2) {
int total1 = 0, total2 = 0;
for (int i = 0; i < 4; i++) {
total1 += s1.scores[i];
total2 += s2.scores[i];
}
return total1 < total2;
}
// 根据平均分排序
static bool compareByAverage(Student& s1, Student& s2) {
return s1.getAverage() < s2.getAverage();
}
// 显示学生信息
void display() {
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "学号:" << id << endl;
cout << "班级:" << cls << endl;
cout << "语文成绩:" << scores[0] << endl;
cout << "数学成绩:" << scores[1] << endl;
cout << "英语成绩:" << scores[2] << endl;
cout << "物理成绩:" << scores[3] << endl;
cout << "总分:" << getTotal() << endl;
cout << "平均分:" << getAverage() << endl;
}
// 获取学生信息字符串
string getInfoString() {
string info = "";
info += name + " ";
info += gender + " ";
info += id + " ";
info += cls + " ";
info += to_string(scores[0]) + " ";
info += to_string(scores[1]) + " ";
info += to_string(scores[2]) + " ";
info += to_string(scores[3]);
return info;
}
// 从字符串中解析学生信息
static Student parseFromInfoString(string info) {
vector<string> tokens;
size_t pos = 0;
while ((pos = info.find(" ")) != string::npos) {
tokens.push_back(info.substr(0, pos));
info.erase(0, pos + 1);
}
tokens.push_back(info);
Student s;
s.name = tokens[0];
s.gender = tokens[1];
s.id = tokens[2];
s.cls = tokens[3];
s.scores[0] = stoi(tokens[4]);
s.scores[1] = stoi(tokens[5]);
s.scores[2] = stoi(tokens[6]);
s.scores[3] = stoi(tokens[7]);
return s;
}
};
// 学生管理类
class StudentManager {
private:
vector<Student> students; // 学生列表
string filename; // 文件名
// 从文件中读取学生信息
void readFromFile() {
ifstream infile(filename);
string line;
while (getline(infile, line)) {
students.push_back(Student::parseFromInfoString(line));
}
infile.close();
}
// 将学生信息写入文件
void writeToFile() {
ofstream outfile(filename);
for (auto student : students) {
outfile << student.getInfoString() << endl;
}
outfile.close();
}
public:
// 构造函数
StudentManager(string filename) {
this->filename = filename;
readFromFile();
}
// 显示菜单
void displayMenu() {
cout << "请选择以下操作:" << endl;
cout << "1. 输入学生信息" << endl;
cout << "2. 修改学生信息" << endl;
cout << "3. 删除学生信息" << endl;
cout << "4. 查询学生信息" << endl;
cout << "5. 统计学生成绩" << endl;
cout << "6. 退出程序" << endl;
}
// 输入学生信息
void addStudent() {
string name, gender, id, cls;
int s1, s2, s3, s4;
cout << "请输入学生姓名:";
cin >> name;
cout << "请输入学生性别:";
cin >> gender;
cout << "请输入学生学号:";
cin >> id;
cout << "请输入学生班级:";
cin >> cls;
cout << "请输入语文成绩:";
cin >> s1;
cout << "请输入数学成绩:";
cin >> s2;
cout << "请输入英语成绩:";
cin >> s3;
cout << "请输入物理成绩:";
cin >> s4;
Student student(name, gender, id, cls, s1, s2, s3, s4);
students.push_back(student);
writeToFile();
cout << "添加成功!" << endl;
}
// 修改学生信息
void updateStudent() {
string id;
cout << "请输入要修改的学生学号:";
cin >> id;
for (auto& student : students) {
if (student.id == id) {
string name, gender, cls;
int s1, s2, s3, s4;
cout << "请输入学生姓名:";
cin >> name;
cout << "请输入学生性别:";
cin >> gender;
cout << "请输入学生班级:";
cin >> cls;
cout << "请输入语文成绩:";
cin >> s1;
cout << "请输入数学成绩:";
cin >> s2;
cout << "请输入英语成绩:";
cin >> s3;
cout << "请输入物理成绩:";
cin >> s4;
student.name = name;
student.gender = gender;
student.cls = cls;
student.scores[0] = s1;
student.scores[1] = s2;
student.scores[2] = s3;
student.scores[3] = s4;
writeToFile();
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
// 删除学生信息
void deleteStudent() {
string id;
cout << "请输入要删除的学生学号:";
cin >> id;
for (auto it = students.begin(); it != students.end(); it++) {
if (it->id == id) {
students.erase(it);
writeToFile();
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
// 查询学生信息
void queryStudent() {
int choice;
cout << "请选择查询方式:" << endl;
cout << "1. 根据学号查询" << endl;
cout << "2. 根据姓名、班级查询" << endl;
cin >> choice;
switch (choice) {
case 1:
queryById();
break;
case 2:
queryByNameOrClass();
break;
default:
cout << "无效的选择!" << endl;
break;
}
}
// 根据学号查询学生信息
void queryById() {
string id;
cout << "请输入要查询的学生学号:";
cin >> id;
for (auto student : students) {
if (student.id == id) {
student.display();
return;
}
}
cout << "未找到该学生!" << endl;
}
// 根据姓名或班级查询学生信息
void queryByNameOrClass() {
string keyword;
cout << "请输入要查询的关键词:";
cin >> keyword;
for (auto student : students) {
if (student.name == keyword || student.cls == keyword) {
student.display();
}
}
}
// 统计学生成绩
void statistics() {
int choice;
cout << "请选择统计方式:" << endl;
cout << "1. 按班级统计" << endl;
cout << "2. 按科目统计" << endl;
cin >> choice;
switch (choice) {
case 1:
statisticsByClass();
break;
case 2:
statisticsBySubject();
break;
default:
cout << "无效的选择!" << endl;
break;
}
}
// 按班级统计学生成绩
void statisticsByClass() {
string cls;
cout << "请输入要统计的班级:";
cin >> cls;
int count = 0;
int total[4] = { 0 };
for (auto student : students) {
if (student.cls == cls) {
count++;
for (int i = 0; i < 4; i++) {
total[i] += student.scores[i];
}
}
}
if (count == 0) {
cout << "未找到该班级的学生!" << endl;
return;
}
cout << "班级:" << cls << endl;
cout << "人数:" << count << endl;
cout << "语文平均分:" << total[0] / static_cast<double>(count) << endl;
cout << "数学平均分:" << total[1] / static_cast<double>(count) << endl;
cout << "英语平均分:" << total[2] / static_cast<double>(count) << endl;
cout << "物理平均分:" << total[3] / static_cast<double>(count) << endl;
}
// 按科目统计学生成绩
void statisticsBySubject() {
int total[4] = { 0 };
for (auto student : students) {
for (int i = 0; i < 4; i++) {
total[i] += student.scores[i];
}
}
cout << "语文平均分:" << total[0] / static_cast<double>(students.size()) << endl;
cout << "数学平均分:" << total[1] / static_cast<double>(students.size()) << endl;
cout << "英语平均分:" << total[2] / static_cast<double>(students.size()) << endl;
cout << "物理平均分:" << total[3] / static_cast<double>(students.size()) << endl;
}
// 排序学生信息
void sortStudents() {
int choice;
cout << "请选择排序方式:" << endl;
cout << "1. 按学号排序" << endl;
cout << "2. 按单科成绩排序" << endl;
cout << "3. 按平均成绩排序" << endl;
cin >> choice;
switch (choice) {
case 1:
sort(students.begin(), students.end(), Student::compareById);
break;
case 2:
sort(students.begin(), students.end(), Student::compareByScore);
break;
case 3:
sort(students.begin(), students.end(), Student::compareByAverage);
break;
default:
cout << "无效的选择!" << endl;
break;
}
cout << "排序完成!" << endl;
}
};
int main() {
string filename = "students.txt";
StudentManager manager(filename);
int choice;
do {
manager.displayMenu();
cin >> choice;
switch (choice) {
case 1:
manager.addStudent();
break;
case 2:
manager.updateStudent();
break;
case 3:
manager.deleteStudent();
break;
case 4:
manager.queryStudent();
break;
case 5:
manager.statistics();
break;
case 6:
cout << "程序已退出!" << endl;
break;
default:
cout << "无效的选择!" << endl;
break;
}
cout << endl;
} while (choice != 6);
return 0;
}
```
使用方法:
1. 将程序保存为 `main.cpp` 文件。
2. 在同一目录下创建 `students.txt` 文件,用于存储学生信息。
3. 编译并运行程序,根据菜单选择相应的操作。
阅读全文