C++ 学生管理系统设计

本示例展示了一个简单的学籍管理系统的 C++ 设计,实现学生基本信息和成绩信息的录入、查询、删除和排序功能。

数据结构

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

// 定义学生基本信息结构体
struct StudentInfo {
    string id;
    string name;
    string gender;
    string dormitory;
    string phoneNumber;
};

// 定义学生成绩基本信息结构体
struct ScoreInfo {
    string id;
    string courseCode;
    string courseName;
    int credit;
    int normalScore;
    int experimentScore;
    int examScore;
    float totalScore;
    float earnedCredit;
};

文件读取

// 读取学生基本信息文件
vector<StudentInfo> readStudentInfoFile(string filename) {
    vector<StudentInfo> students;
    ifstream file(filename);
    if (!file) {
        cout << "无法打开文件" << filename << endl;
        return students;
    }
    string line;
    while (getline(file, line)) {
        StudentInfo student;
        student.id = line.substr(0, 2);
        student.name = line.substr(2, 6);
        student.gender = line.substr(8, 2);
        student.dormitory = line.substr(10, 3);
        student.phoneNumber = line.substr(13);
        students.push_back(student);
    }
    file.close();
    return students;
}

// 读取学生成绩基本信息文件
vector<ScoreInfo> readScoreInfoFile(string filename) {
    vector<ScoreInfo> scores;
    ifstream file(filename);
    if (!file) {
        cout << "无法打开文件" << filename << endl;
        return scores;
    }
    string line;
    while (getline(file, line)) {
        ScoreInfo score;
        score.id = line.substr(0, 2);
        score.courseCode = line.substr(2, 4);
        score.courseName = line.substr(6, 10);
        score.credit = stoi(line.substr(16, 1));
        score.normalScore = stoi(line.substr(17, 2));
        score.experimentScore = stoi(line.substr(19, 2));
        score.examScore = stoi(line.substr(21, 2));
        if (score.experimentScore == -1) {
            score.totalScore = score.normalScore * 0.3 + score.examScore * 0.7;
        } else {
            score.totalScore = score.normalScore * 0.15 + score.experimentScore * 0.15 + score.examScore * 0.7;
        }
        if (score.totalScore >= 90) {
            score.earnedCredit = score.credit;
        } else if (score.totalScore >= 80) {
            score.earnedCredit = score.credit * 0.8;
        } else if (score.totalScore >= 70) {
            score.earnedCredit = score.credit * 0.75;
        } else if (score.totalScore >= 60) {
            score.earnedCredit = score.credit * 0.6;
        } else {
            score.earnedCredit = score.credit * 0;
        }
        scores.push_back(score);
    }
    file.close();
    return scores;
}

功能实现

学生基本情况查询

// 学生基本情况查询
void searchStudentInfo(vector<StudentInfo> students) {
    int choice;
    cout << "请输入查询方式:1.学号 2.姓名" << endl;
    cin >> choice;
    if (choice == 1) {
        string id;
        cout << "请输入学号:" << endl;
        cin >> id;
        for (const auto& student : students) {
            if (student.id == id) {
                cout << "学号:" << student.id << "  姓名:" << student.name << "  性别:" << student.gender
                    << "  宿舍号码:" << student.dormitory << "  电话号码:" << student.phoneNumber << endl;
                return;
            }
        }
        cout << "未找到该学生信息" << endl;
    } else if (choice == 2) {
        string name;
        cout << "请输入姓名:" << endl;
        cin >> name;
        for (const auto& student : students) {
            if (student.name == name) {
                cout << "学号:" << student.id << "  姓名:" << student.name << "  性别:" << student.gender
                    << "  宿舍号码:" << student.dormitory << "  电话号码:" << student.phoneNumber << endl;
                return;
            }
        }
        cout << "未找到该学生信息" << endl;
    } else {
        cout << "无效的选择" << endl;
    }
}

宿舍学生基本信息查询

// 宿舍学生基本信息查询
void searchDormitoryInfo(vector<StudentInfo> students) {
    string dormitory;
    cout << "请输入宿舍号码:" << endl;
    cin >> dormitory;
    for (const auto& student : students) {
        if (student.dormitory == dormitory) {
            cout << "学号:" << student.id << "  姓名:" << student.name << "  性别:" << student.gender
                << "  宿舍号码:" << student.dormitory << "  电话号码:" << student.phoneNumber << endl;
        }
    }
}

成绩查询

// 成绩查询
void searchScoreInfo(vector<ScoreInfo> scores) {
    string id;
    cout << "请输入学号:" << endl;
    cin >> id;
    cout << "学号:" << id << endl;
    float totalCredit = 0;
    float earnedTotalCredit = 0;
    for (const auto& score : scores) {
        if (score.id == id) {
            cout << "课程编号:" << score.courseCode << "  课程名称:" << score.courseName
                << "  综合成绩:" << score.totalScore << "  实得学分:" << score.earnedCredit << endl;
            totalCredit += score.credit;
            earnedTotalCredit += score.earnedCredit;
        }
    }
    cout << "共修:" << totalCredit << "科,实得总学分为:" << earnedTotalCredit << endl;
}

删除学生及其成绩信息

// 删除学生及其成绩信息
void deleteStudentInfo(string id, vector<StudentInfo>& students, vector<ScoreInfo>& scores) {
    students.erase(remove_if(students.begin(), students.end(), [id](const StudentInfo& student) {
        return student.id == id;
    }), students.end());
    scores.erase(remove_if(scores.begin(), scores.end(), [id](const ScoreInfo& score) {
        return score.id == id;
    }), scores.end());
}

排序功能

// 排序功能
bool compareByTotalScore(const ScoreInfo& score1, const ScoreInfo& score2) {
    return score1.totalScore < score2.totalScore;
}

bool compareByEarnedCredit(const ScoreInfo& score1, const ScoreInfo& score2) {
    return score1.earnedCredit < score2.earnedCredit;
}

void sortScores(vector<ScoreInfo>& scores) {
    int choice;
    cout << "请选择排序方式:1.综合成绩升序 2.综合成绩降序 3.实得学分升序 4.实得学分降序" << endl;
    cin >> choice;
    switch (choice) {
        case 1:
            sort(scores.begin(), scores.end(), compareByTotalScore);
            break;
        case 2:
            sort(scores.rbegin(), scores.rend(), compareByTotalScore);
            break;
        case 3:
            sort(scores.begin(), scores.end(), compareByEarnedCredit);
            break;
        case 4:
            sort(scores.rbegin(), scores.rend(), compareByEarnedCredit);
            break;
        default:
            cout << "无效的选择" << endl;
            break;
    }
}

主函数

int main() {
    vector<StudentInfo> students = readStudentInfoFile("A.txt");
    vector<ScoreInfo> scores = readScoreInfoFile("B.txt");

    int choice;
    while (true) {
        cout << "请选择功能:1.数据录入 2.学生基本情况查询 3.宿舍学生基本信息查询 4.成绩查询 5.删除功能 6.排序功能 7.退出" << endl;
        cin >> choice;
        switch (choice) {
            case 1:
                // 数据录入功能
                // TODO: 实现数据录入功能
                break;
            case 2:
                // 学生基本情况查询
                searchStudentInfo(students);
                break;
            case 3:
                // 宿舍学生基本信息查询
                searchDormitoryInfo(students);
                break;
            case 4:
                // 成绩查询
                searchScoreInfo(scores);
                break;
            case 5:
                // 删除功能
                string id;
                cout << "请输入学号:" << endl;
                cin >> id;
                deleteStudentInfo(id, students, scores);
                break;
            case 6:
                // 排序功能
                sortScores(scores);
                break;
            case 7:
                // 退出
                return 0;
            default:
                cout << "无效的选择" << endl;
                break;
        }
    }
}

总结

该示例代码展示了使用 C++ 设计一个简单的学籍管理系统的基本步骤,包含数据结构定义、文件读取、功能实现和主函数。请根据您的实际需求对代码进行完善和扩展。


原文地址: https://www.cveoy.top/t/topic/prfp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录