PTA 7-3 学生CPP成绩计算 (15分) c++ 第5章类继承

题目

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

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

AC代码

#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<iomanip>
#include<iostream>


using namespace std;
class Person {
protected:
     string name;
     int age;
public:
     Person();
     Person (string p_name, int p_age){
        name=p_name;
        age=p_age;
     }
     //又是一个无用的花架子
     void display () {cout<<name<<":"<<age<<endl;}
};
class student:public Person
{
public:
    student(string s,int a,int b,float c,float d):Person(s,b){
        ID=a;
        cpp_score=c;
        cpp_count=d;
        cpp_grade=cpp_score * 0.9 + cpp_count * 2;
    }
    void show ()
    {
        cout<<ID<<" "<<name<<" ";
        printf("%.1f\n",cpp_grade);
    }
protected:
    int ID;//学号
    float cpp_score;//cpp上机成绩
    float cpp_count;//cpp上机考勤
    float cpp_grade;
};
int main()
{   string s;
	int a,b;
	float c,d;
	cin>>s;
	while(s.at(0)!='0'){
        cin>>a>>b>>c>>d;
        student stu(s,a,b,c,d);
        stu.show();
        cin>>s;
	}
	return 0;
}

没什么想说的


哈,哈哈


  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
复数类的操作主要包括以下内容: 1. 实部和虚部的获取和设置 2. 模长的计算 3. 复数加、减、乘、除运算 4. 复数的共轭和相反数 下面是一个简单的复数类实现: ```python class ComplexNumber: def __init__(self, real=0, imag=0): self.real = real self.imag = imag def get_real(self): return self.real def set_real(self, real): self.real = real def get_imag(self): return self.imag def set_imag(self, imag): self.imag = imag def modulus(self): return (self.real ** 2 + self.imag ** 2) ** 0.5 def __add__(self, other): return ComplexNumber(self.real + other.real, self.imag + other.imag) def __sub__(self, other): return ComplexNumber(self.real - other.real, self.imag - other.imag) def __mul__(self, other): return ComplexNumber(self.real * other.real - self.imag * other.imag, self.real * other.imag + self.imag * other.real) def __truediv__(self, other): denominator = other.real ** 2 + other.imag ** 2 return ComplexNumber((self.real * other.real + self.imag * other.imag) / denominator, (self.imag * other.real - self.real * other.imag) / denominator) def conjugate(self): return ComplexNumber(self.real, -self.imag) def opposite(self): return ComplexNumber(-self.real, -self.imag) ``` 这里我们用到了 Python 中的魔术方法,即以双下划线开头和结尾的方法,例如 `__add__`、`__sub__`、`__mul__` 和 `__truediv__` 分别表示复数的加、减、乘、除运算。这些方法可以让我们像使用内置类型一样使用自定义类型。 下面是一些使用示例: ```python # 创建复数对象 z1 = ComplexNumber(1, 2) z2 = ComplexNumber(3, 4) # 获取实部和虚部 print(z1.get_real(), z1.get_imag()) # 1 2 # 设置实部和虚部 z1.set_real(5) z1.set_imag(6) print(z1.get_real(), z1.get_imag()) # 5 6 # 计算模长 print(z1.modulus()) # 7.810249675906654 # 复数加、减、乘、除运算 print(z1 + z2) # (8+10j) print(z1 - z2) # (-2-2j) print(z1 * z2) # (-7+36j) print(z1 / z2) # (0.44+0.08j) # 复数的共轭和相反数 print(z1.conjugate()) # (5-6j) print(z1.opposite()) # (-5-6j) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值