美文网首页
(十)Student

(十)Student

作者: GoodTekken | 来源:发表于2023-03-27 08:07 被阅读0次
#include <iostream>
#include <string>

using namespace std;

const int N = 5;   /*课程数*/
const int M = 2;   /*竞赛项目数*/

class Student
{
    protected:
        int stuNo;
        string name;
        double GPA;      /*综合成绩*/
        int (*grades)[2]; /*各门课程成绩和学分*/

    public:
        Student(const int stuNo, const string &name, int grades[][2])
        {
            this -> stuNo = stuNo;
            this -> name = name;
            this -> grades = grades;
        }

        virtual ~Student()
        {

        }

        int getStuNo()
        {
            return stuNo;
        }

        string getName()
        {
            return name;     
        }

        virtual double getGPA() = 0;   //(1)

        double computeWg()
        {
            int totalGrades = 0, totalCredits = 0;

            for(int i = 0; i < N ; i++)
            {
                totalGrades += grades[i][0] * grades[i][1];
                totalCredits += grades[i][1];
            }

            return GPA = (double)totalGrades / totalCredits;
        }
};

class ActStudent : public Student
{
    int Apoints;

    public:
        ActStudent(const int stuNo, const string &name, int gs[][2], int Apoints) : Student(stuNo, name, gs)               //(2)
        {
            this->Apoints = Apoints;
        }

        double getGPA()
        {
            return GPA = computeWg() + Apoints;    //(3)
        }
};

class CmpStudent : public Student
{
    private:
        int (*awards)[2];

    public:
        CmpStudent(const int stuNo, const string &name, int gs[][2], int awards[][2]):Student(stuNo,name,gs)             //(4)
        {
            this -> awards = awards;
        }

        double getGPA()
        {
            int Awards = 0;
            for(int i = 0; i < M ; i++)
            {
                // int a = awards[i][0];
                // int b = awards[i][1];
                Awards += awards[i][0]*awards[i][1];
            }

            return GPA = computeWg() + Awards;    //(5)
        }
};

void print_a(int a[][2], int n, int m)
{
    int i, j;
    
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
            printf("%d ", a[i][j]);
        
        printf("\n");
    }
}

int main()  //以计算3个学生的综合成绩为例进行测试
{
    int g1[][2] = {{80,3}, {90,2}, {95,3}, {85,4}, {86,3}},
        g2[][2] = {{60,3}, {60,2}, {60,3}, {60,4}, {65,3}},
        g3[][2] = {{80,3}, {90,2}, {70,3}, {65,4}, {75,3}}; //课程成绒
    int c3[][2] = {{2,3},  {3,3}};                         //竞赛成绩
    Student* students[3] =
    {
        new ActStudent (101, "John", g1, 3),           //3为活动分
        new ActStudent (102,"Zhang", g2, 0),
        new CmpStudent (103, "Li",   g3, c3), 
    };

    //输出每个学生的综合成绩
    for(int i = 0; i < 3; i++)
    {
        cout << students[i]->getGPA() <<endl;      //(6)
    }
    // print_a(c3,2,2);
    delete *students;

    return 0;
}

答案:
(1)virtual double getGPA() = 0;

(2)Student(stuNo, name, gs)

(3)computeWg() + Apoints

(4)Student(stuNo,name,gs)

(5)computeWg() + Awards

(6)students[i]->getGPA()

相关文章

网友评论

      本文标题:(十)Student

      本文链接:https://www.haomeiwen.com/subject/bjdordtx.html