当前位置:首页 C++ > 正文

C++学生成绩管理系统

作者:野牛程序员:2023-08-12 16:25:24 C++阅读 2586

C++学生成绩管理系统是一个可以用来记录、管理和展示学生学习成绩的程序。以下是一个简单的示例,展示了一个基本的C++学生成绩管理系统的结构,包括添加学生信息、录入成绩、计算平均成绩等功能。

#include <iostream>
#include <string>
#include <vector>

using namespace std;

// 学生类
class Student {
public:
    string name;
    int id;
    vector<int> scores;

    Student(string n, int i) : name(n), id(i) {}

    void addScore(int score) {
        scores.push_back(score);
    }

    double calculateAverageScore() {
        if (scores.empty()) {
            return 0.0;
        }

        int sum = 0;
        for (int score : scores) {
            sum += score;
        }

        return static_cast<double>(sum) / scores.size();
    }
};

int main() {
    vector<Student> students;

    while (true) {
        cout << "1. 添加学生信息" << endl;
        cout << "2. 录入学生成绩" << endl;
        cout << "3. 计算平均成绩" << endl;
        cout << "4. 退出" << endl;

        int choice;
        cin >> choice;

        if (choice == 1) {
            string name;
            int id;
            cout << "请输入学生姓名:";
            cin >> name;
            cout << "请输入学生学号:";
            cin >> id;
            students.push_back(Student(name, id));
            cout << "学生信息已添加" << endl;
        } else if (choice == 2) {
            int id, score;
            cout << "请输入学生学号:";
            cin >> id;
            cout << "请输入成绩:";
            cin >> score;

            for (Student &student : students) {
                if (student.id == id) {
                    student.addScore(score);
                    cout << "成绩已录入" << endl;
                    break;
                }
            }
        } else if (choice == 3) {
            int id;
            cout << "请输入学生学号:";
            cin >> id;

            for (const Student &student : students) {
                if (student.id == id) {
                    double average = student.calculateAverageScore();
                    cout << "平均成绩为:" << average << endl;
                    break;
                }
            }
        } else if (choice == 4) {
            cout << "已退出" << endl;
            break;
        } else {
            cout << "无效的选项,请重新输入" << endl;
        }
    }

    return 0;
}

请注意,这只是一个简单的示例,实际的学生成绩管理系统可能需要更多的功能和错误处理。可以根据自己的需求扩展和修改这个示例。


野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
相关推荐

最新推荐

热门点击