_Phoebe__ 2022-04-25 10:52 采纳率: 96.9%
浏览 68
已结题

后面的要求不会实现了 想知道怎么写

6 7 8怎么实现呢


// 用C++ STL设计学生成绩存储管理程序(第2阶段:对实验13中的学生成绩存储管理程序进行扩展)
//    (1)基本数据结构:
//学生类CStudent,包括以下数据成员:
//学号nID(整型),姓名strName(字符串),成绩mapScores(用map容器存储修读过的各门课程的课程号(整型)和成绩);
//课程类Course,包括以下数据成员:
//课程号nNo(整型),课程名strCourseName(字符串),学分fCredit(float型)
//(2)数据存储:用某种适当的STL容器存储所有学生的数据;
//(3)增加三个学生数据(每个学生修读若干门课程)到容器中,输出容器中所有学生的信息;
//(4)数据维护:实现对学生数据的增、删、改、查功能(包括增、删、改、查学生信息,以及各科成绩);
//(5)用某种适当的STL容器存储所有课程的信息(称为“课程数据表”);
//(6)输入学生成绩时需参照“课程数据表”(“课程数据表”中存在的课程才能输入成绩);
//(7)统计“课程数据表”中每门课程的平均分;(如果某门课程还没有学生成绩,则输出0)
//(8)对所有学生进行排序,排序规则为:根据该生成绩平均分从大到小排序,
//如果平均分相同,则根据学号从小到大排序。(可利用STL的sort算法)
#include<bits/stdc++.h>
using namespace std;
class CStudent
{
    int nID;
    string strName;
    map<int, double> mapScores;  //存储多门课程成绩
public:
    CStudent(int _nID, string _strName);
    void setID(int _nID);
    int getID();
    void setName(string _name);
    string getName();
    map<int, double> getScores();
    void insertScore(int cno, double score);  //增加一门课程成绩 cno是课程编号 
    void eraseScore(int cno);     //删除一门课程成绩
    void find(int cno);
    void modify(int cno,double score);
    void display();     //输出学生学号、姓名和各科成绩
};
class Course
{
    int nNo;
    string coursename;
    float fcredit;
};
CStudent::CStudent(int _nID,string _strName)
{
    nID=_nID;
    strName=_strName;
}
void CStudent::setID(int _nID)
{
    nID=_nID;
}
int CStudent::getID()
{
    return nID;
}
void CStudent::setName(string _name)
{
    strName=_name;
}
string CStudent::getName()
{
    return strName;
}
map<int,double> CStudent::getScores()
{
    return mapScores;
}
void CStudent::insertScore(int cno, double score)
{
   mapScores.insert(map<int,double>::value_type(cno,score));
}
void CStudent::eraseScore(int cno){
    for(map<int,double>::iterator it=mapScores.begin();it!=mapScores.end();it++)
    {
        if(it->first==cno){
            mapScores.erase(it);
            break;
        }
    }
}   
void CStudent::find(int cno){
    map<int,double>::iterator iter= mapScores.find (cno);
    if (iter!=mapScores.end()) 
       cout<<"It is:"<<iter->second<<endl;
    else cout<<"Not Found"<<endl;
}
void CStudent::modify(int cno,double score)
{
   map<int,double>::iterator iter=mapScores.find(cno);
   if (iter!=mapScores.end()) 
     iter->second=score;
}
void CStudent::display(){
    cout<<"Student ID:"<<nID<<" Name:"<<strName<<endl;
    cout<<"Grades of all subjects: "<<endl;
    for(map<int,double>::iterator iter=mapScores.begin();iter!=mapScores.end();iter++)
    {
        cout<<"Course number:"<<iter->first<<"Scores:"<<iter->second<<" "<<endl;
    }
    cout<<endl;
}
int main()
{
    list<CStudent> v1;
    list<Course> v2;
    CStudent stu1(1001,"Phoebe");
    stu1.insertScore(101,100);
    stu1.insertScore(102,99);
    stu1.insertScore(103,98);
    stu1.insertScore(106,99.5);
    stu1.display();
    CStudent stu2(1002,"Lilith");
    stu2.insertScore(101,95);
    stu2.insertScore(104,93);
    stu2.display();
    stu2.eraseScore(101);//测试eraseScore 
    stu2.display();
    stu1.find(102);
    stu1.find(105);
    stu1.modify(106,97);
    stu1.display();
}
  • 写回答

2条回答 默认 最新

  • 关注

    6:根据课程数据表录入数据,也就是在输入数据时,先判断是否存在课程的编号,如果存在课程的编号,就继续输入数据,否则就提示不存在该课程。
    最好把课程表定义为全局变量,这样能够在Student类中访问。
    7:统计没门课程的平均分:遍历学生list,用一个map记录每门课的成绩和,最后输出
    8:可以使用sort函数进行排序。

    代码如下:

    
    
    // 用C++ STL设计学生成绩存储管理程序(第2阶段:对实验13中的学生成绩存储管理程序进行扩展)
    //    (1)基本数据结构:
    //学生类CStudent,包括以下数据成员:
    //学号nID(整型),姓名strName(字符串),成绩mapScores(用map容器存储修读过的各门课程的课程号(整型)和成绩);
    //课程类Course,包括以下数据成员:
    //课程号nNo(整型),课程名strCourseName(字符串),学分fCredit(float型)
    //(2)数据存储:用某种适当的STL容器存储所有学生的数据;
    //(3)增加三个学生数据(每个学生修读若干门课程)到容器中,输出容器中所有学生的信息;
    //(4)数据维护:实现对学生数据的增、删、改、查功能(包括增、删、改、查学生信息,以及各科成绩);
    //(5)用某种适当的STL容器存储所有课程的信息(称为“课程数据表”);
    //(6)输入学生成绩时需参照“课程数据表”(“课程数据表”中存在的课程才能输入成绩);
    //(7)统计“课程数据表”中每门课程的平均分;(如果某门课程还没有学生成绩,则输出0)
    //(8)对所有学生进行排序,排序规则为:根据该生成绩平均分从大到小排序,
    //如果平均分相同,则根据学号从小到大排序。(可利用STL的sort算法)
    #include<bits/stdc++.h>
    #include <iostream>
    #include <string>
    #include <list>
    #include <map>
    
    using namespace std;
    
    
    class Course
    {
    public://增加public修饰,否则变量都是私有成员
        int nNo;
        string coursename;
        float fcredit;
        Course(int id, string name, float cre)
        {
            nNo = id;
            coursename = name;
            fcredit = cre;
        }
    };
    
    list<Course> g_vCourse; //定义为全局变量,这样能够在Studnt函数中访问
    
    class CStudent
    {
        int nID;
        string strName;
        map<int, double> mapScores;  //存储多门课程成绩
    public:
    
        CStudent(int _nID, string _strName);
        void setID(int _nID);
        int getID();
        void setName(string _name);
        string getName();
        map<int, double> getScores();
        void insertScore(int cno, double score);  //增加一门课程成绩 cno是课程编号 
        void eraseScore(int cno);     //删除一门课程成绩
        void find(int cno);
        void modify(int cno, double score);
        void display();     //输出学生学号、姓名和各科成绩
    
        double getAve(); //获取平均成绩,方便后面排序
    };
    
    
    
    
    
    
    CStudent::CStudent(int _nID, string _strName)
    {
        nID = _nID;
        strName = _strName;
    }
    void CStudent::setID(int _nID)
    {
        nID = _nID;
    }
    int CStudent::getID()
    {
        return nID;
    }
    void CStudent::setName(string _name)
    {
        strName = _name;
    }
    string CStudent::getName()
    {
        return strName;
    }
    map<int, double> CStudent::getScores()
    {
        return mapScores;
    }
    void CStudent::insertScore(int cno, double score)
    {
        //判断是否存在该课程,解决第6题中的要求
        list<Course>::iterator it = g_vCourse.begin();
        for (; it != g_vCourse.end(); it++)
        {
            if (it->nNo == cno)
            {
                mapScores.insert(pair<int, double>(cno, score));
                //mapScores.insert(map<int, double>::value_type(cno, score));
                break;
            }
        }
        if (it == g_vCourse.end())
            cout << "该课程不在课程表中" << endl;
    
    }
    void CStudent::eraseScore(int cno) {
        for (map<int, double>::iterator it = mapScores.begin(); it != mapScores.end(); it++)
        {
            if (it->first == cno) {
                mapScores.erase(it);
                break;
            }
        }
    }
    void CStudent::find(int cno) {
        map<int, double>::iterator iter = mapScores.find(cno);
        if (iter != mapScores.end())
            cout << "It is:" << iter->second << endl;
        else cout << "Not Found" << endl;
    }
    void CStudent::modify(int cno, double score)
    {
        map<int, double>::iterator iter = mapScores.find(cno);
        if (iter != mapScores.end())
        {
            //iter->second = score; //无法通过iter修改数据
            //先删除原来的数据
            mapScores.erase(iter);
            mapScores.insert(pair<int, double>(cno, score));
        }
    
    }
    void CStudent::display() {
        cout << "Student ID:" << nID << " Name:" << strName << endl;
        cout << "Grades of all subjects: " << endl;
        for (map<int, double>::iterator iter = mapScores.begin(); iter != mapScores.end(); iter++)
        {
            cout << "Course number:" << iter->first << "Scores:" << iter->second << " " << endl;
        }
        cout << endl;
    }
    
    
    double CStudent::getAve()
    {
        map<int, double>::iterator it = mapScores.begin();
        double sum = 0;
        int count = 0;
        for (; it != mapScores.end(); it++)
        {
            sum += it->second;
            count++;
        }
        if (count == 0) return 0;
        return sum / count;
    }
    
    
    int comp(CStudent& s1, CStudent& s2)
    {
        if (s1.getAve() == s2.getAve())
            return s1.getID() < s2.getID();
        else
            return s1.getAve() > s2.getAve();
    }
    
    
    int main()
    {
        //设置课程表,根据课程表把所有课程保存到g_vCouse中
        g_vCourse.push_back(Course(101, "课程1", 3.0)); //课程名根据课程数据表修改一下
        g_vCourse.push_back(Course(102, "课程2", 3.0));
        g_vCourse.push_back(Course(103, "课程3", 3.0));
        g_vCourse.push_back(Course(104, "课程4", 3.0));
        g_vCourse.push_back(Course(105, "课程5", 3.0));
        g_vCourse.push_back(Course(106, "课程6", 3.0));
    
    
    
        list<CStudent> v1;
        CStudent stu1(1001, "Phoebe");
        stu1.insertScore(101, 100);
        stu1.insertScore(102, 99);
        stu1.insertScore(103, 98);
        stu1.insertScore(106, 99.5);
        stu1.display();
        CStudent stu2(1002, "Lilith");
        stu2.insertScore(101, 95);
        stu2.insertScore(104, 93);
        stu2.display();
        stu2.eraseScore(101);//测试eraseScore 
        stu2.display();
        stu1.find(102);
        stu1.find(105);
        stu1.modify(106, 97);
        stu1.display();
    
        //把学生信息插入v1
        v1.push_back(stu1);
        v1.push_back(stu2);
        //第7题,统计每门课的平均分
        map<int, double> mapAve;
        list<CStudent>::iterator it = v1.begin();
        for (; it != v1.end(); it++)
        {
            map<int, double> score = it->getScores();
            map<int, double>::iterator is = score.begin();
            for (; is != score.end(); is++)
            {
                //查找课程在mapAve中的位置,并更新数据
                map<int, double>::iterator iave = mapAve.find(is->first);
                if (iave == mapAve.end())
                    mapAve.insert(pair<int, double>(is->first, is->second));
                else
                {
                    double tt = (iave->second + is->second) / 2.0;
                    mapAve.erase(iave);
                    mapAve.insert(pair<int, double>(is->first, tt));
                }
            }
        }
        //遍历输出每门课的平均分
        cout << "每门课的平均分:" << endl;
        list<Course>::iterator ilist = g_vCourse.begin();
        map<int, double>::iterator ia = mapAve.begin();
        
        for (; ilist != g_vCourse.end(); ilist++)
        {
            ia = mapAve.find(ilist->nNo);
            if(ia != mapAve.end())
                cout << ia->first << "  " << ia->second << endl;
            else
                cout << ilist->nNo << "  " << "0" << endl;
    
        }
        //第8题,排序
        v1.sort(comp);
    
        //排序后输出
        cout << "根据平均成绩排序后输出:" << endl;
        list<CStudent>::iterator istu = v1.begin();
        for (; istu != v1.end(); istu++)
        {
            cout << istu->getID() << "  " << istu->getName() << "  " << istu->getAve() << endl;
        }
    
    
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月5日
  • 已采纳回答 4月27日
  • 创建了问题 4月25日