学生CPP成绩计算

发布时间 2023-05-09 09:45:50作者: 笠大

学生CPP成绩计算

给出下面的人员基类框架:

class Person
{

protected:

 string name;
 int age;

public:

 Person();      
 Person (string p_name, int p_age);
 void display () {cout<<name<<“:”<<age<<endl;}

};

建立一个派生类student,增加以下成员数据:

int ID;//学号
float cpp_score;//cpp上机成绩
float cpp_count;//cpp上机考勤
float cpp_grade;//cpp总评成绩
     //总评成绩计算规则:cpp_grade = cpp_score * 0.9 + cpp_count * 2;

增加以下成员函数:

student类的无参构造函数

student类的参数化构造函数//注意cpp_grade为上机成绩和考勤的计算结果

void print()//输出当前student的信息

             //其中cpp_grade输出保留一位小数
            //输出格式为ID name cpp_grade

生成上述类并编写主函数,根据输入的学生基本信息,建立一个学生对象,计算其cpp总评成绩,并输出其学号、姓名、总评成绩。

输入格式: 测试输入包含若干测试用例,每个测试用例占一行(学生姓名 学号 年龄 cpp成绩 cpp考勤)。当读入0时输入结束,相应的结果不要输出。

输入样例:

Bob 10001 18 75.5 4

Mike 10005 17 95.0 5

0

输出样例:

10001 Bob 75.9

10005 Mike 95.5

代码如下:


#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class Person {
protected:
    string name;
    int age;
public:
    Person();
    Person(string p_name, int p_age);
    void display() { cout << name << " " << age << endl; }
};
Person::Person() {

}
Person::Person(string p_name, int p_age) {
    name = p_name;
    age = p_age;
}
class Student :public Person {
protected:
    int ID;//学号
    float cpp_score;//cpp上机成绩
    float cpp_count;//cpp上机考勤
    float cpp_grade;//cpp总评成绩
    //总评成绩计算规则:cpp_grade = cpp_score * 0.9 + cpp_count * 2;
public:
    Student(){}
    Student(int id, string _name, float a, float b){
        ID = id;
        name = _name;
        cpp_score = a;
        cpp_count = b;
    }
    void Print() {
        cpp_grade= cpp_score * 0.9 + cpp_count * 2;
        cout << ID << " " << name << " " <<setiosflags(ios::fixed)<<setprecision(1)<< cpp_grade << endl;
    }
};
int main()
{
    string name;
    int _ID, age;
    float _score, _count;
    cin >> name;
    while (name!= "0")
    {
        cin>> _ID >> age >> _score >> _count;
        Student d(_ID, name, _score, _count);
        d.Print();
        cin >> name;
    }
    return 0;
    
    
}