You need to enable JavaScript to run this app. C++学生成绩管理程序极值计算、排序、检索功能实现咨询
优惠活动
大模型
产品
解决方案
定价
更多

C++学生成绩管理程序极值计算、排序、检索功能实现咨询

学生信息管理程序实现方案

完整可运行代码

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

class student
{
public:
    char Firstname[20];
    char Lastname[20];
    int sn;
    float scores[5];
    float avg;
    float min_score;
    float max_score;
public:
    // 录入函数:负责录入学生所有信息并计算平均分、单科极值
    void input()
    {
        cout << "请输入学生的名、姓、学号:" << endl;
        cin >> Firstname >> Lastname >> sn;
        cout << "请输入5门课的成绩:" << endl;
        float sum = 0;
        min_score = 100;
        max_score = 0;
        for(int i=0; i<5; i++){
            cin >> scores[i];
            sum += scores[i];
            if(scores[i] < min_score) min_score = scores[i];
            if(scores[i] > max_score) max_score = scores[i];
        }
        avg = sum / 5;
    }

    // 打印函数:负责打印单个学生的全部信息
    void print()
    {
        cout << "====================" << endl;
        cout << "姓名:" << Lastname << Firstname << endl;
        cout << "学号:" << sn << endl;
        cout << "5门课成绩:";
        for(int i=0; i<5; i++){
            cout << scores[i] << " ";
        }
        cout << endl;
        cout << "平均成绩:" << avg << endl;
        cout << "单科最低分:" << min_score << endl;
        cout << "单科最高分:" << max_score << endl;
        cout << "====================" << endl;
    }
};

// 排序比较规则:按姓的字典序排序,姓相同则按名排序
bool cmp_stu_name(const student& a, const student& b)
{
    int last_cmp = strcmp(a.Lastname, b.Lastname);
    if(last_cmp != 0){
        return last_cmp < 0;
    }
    return strcmp(a.Firstname, b.Firstname) < 0;
}

int main()
{
    student stus[10];
    // 1. 录入10名学生信息
    for(int i=0; i<10; i++){
        cout << "正在录入第" << i+1 << "名学生的信息:" << endl;
        stus[i].input();
    }

    // 2. 计算所有学生的平均分极值
    float global_min_avg = 100, global_max_avg = 0;
    for(int i=0; i<10; i++){
        if(stus[i].avg < global_min_avg) global_min_avg = stus[i].avg;
        if(stus[i].avg > global_max_avg) global_max_avg = stus[i].avg;
    }
    cout << endl << "所有学生的最低平均分为:" << global_min_avg << endl;
    cout << "所有学生的最高平均分为:" << global_max_avg << endl << endl;

    // 3. 按姓名排序并输出
    sort(stus, stus+10, cmp_stu_name);
    cout << "按姓名排序后的学生列表:" << endl;
    for(int i=0; i<10; i++){
        stus[i].print();
    }

    // 4. 学生信息检索
    cout << endl << "请输入要检索的学生姓名或学号:";
    int search_sn;
    if(cin >> search_sn){
        // 输入为数字,按学号检索
        bool found = false;
        for(int i=0; i<10; i++){
            if(stus[i].sn == search_sn){
                cout << "检索结果:" << endl;
                stus[i].print();
                found = true;
                break;
            }
        }
        if(!found) cout << "未找到对应学号的学生" << endl;
    }else{
        // 输入为字符串,按姓名检索
        cin.clear();
        char keyword[20];
        cin >> keyword;
        bool found = false;
        for(int i=0; i<10; i++){
            if(strcmp(stus[i].Firstname, keyword) == 0 || strcmp(stus[i].Lastname, keyword) == 0){
                cout << "检索结果:" << endl;
                stus[i].print();
                found = true;
            }
        }
        if(!found) cout << "未找到对应姓名的学生" << endl;
    }

    return 0;
}

功能实现说明

  • 录入与打印分离:input()成员函数负责所有信息录入和基础计算,print()成员函数负责单条学生信息输出,完全独立
  • 全体平均分极值计算:遍历学生数组的avg字段即可快速拿到全局最低和最高平均分
  • 姓名排序:调用标准库sort函数,自定义比较规则实现按姓名字典序排序
  • 信息检索:判断输入内容类型,分别匹配学号和姓名字段,匹配成功就输出对应学生信息

内容的提问来源于stack exchange,提问作者sina kh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 12:57:00