【东华大学oj】15 学生成绩转换(友元函数)(面向对象)

本文介绍了一个名为Student的C++类,包含学号和成绩属性,以及友元函数Score_Trans用于将百分制成绩转换为等级并输出。程序展示了如何在主函数中创建学生对象并调用该函数进行成绩评级。
摘要由CSDN通过智能技术生成
#include<iostream>
#include <cstring>
#include <cmath>
using namespace std;

class Student
{

public:

    void Set_StuNum(int num)
    {
        stu_num=num;
    }

    int Get_StudNum()
    {
        return stu_num;
    }

    void Set_Score(float sco)
    {
        score=sco;
    }

    float Get_Score()
    {
        return score;
    }

    friend void Score_Trans(Student &s);

private:

    int stu_num; //学号

    float score; //分数

};
void Score_Trans(Student &s)
{
    if(s.Get_Score()>=90) cout<<s.Get_StudNum()<<" "<<"优";
    else if(s.Get_Score()>=80) cout<<s.Get_StudNum()<<" "<<"良";
    else if(s.Get_Score()>=70) cout<<s.Get_StudNum()<<" "<<"中";
    else if(s.Get_Score()>=60) cout<<s.Get_StudNum()<<" "<<"及格";
    else cout<<s.Get_StudNum()<<" "<<"不及格";
}
int main()
{

    int n, i, stu_num, max_stu_num;

    float score, max_score;

    cin>>n;



    Student stu[n];

    for( i = 0; i < n; i++ )
    {

        cin>>stu_num>>score;

        stu[i].Set_StuNum(stu_num);

        stu[i].Set_Score(score);

    }



    for( i = 0; i < n; i++ )
    {

        Score_Trans(stu[i]);

        if(i < n - 1) cout<<endl;

    }



    return 0;

}

作者: 冯向阳时间限制: 1S章节: 类与对象

问题描述 :

描述:有一个学生类Student,包括学号、成绩(百分制)。要求设计一个友元函数Score_Trans,将百分制成绩转换成对应的等级输出到屏幕上:大于等于90:优;80~90:良;70~79:中;60~69:及格;小于60:不及格。

Student类如下:

Student类如下:

class Student{ 

   public:

      void Set_StuNum(int); //自行设计

      int Get_StudNum();  //自行设计

      void Set_Score(float); //自行设计

      float Get_Score();  //自行设计

      friend void Score_Trans(Student &s);

   private:   

      int stu_num; //学号

      float score; //分数   

};

要求使用以下main函数测试:

int main(){ 

  int n, i, stu_num, max_stu_num;

  float score, max_score;

  cin>>n;

  Student stu[n];

  for( i = 0; i < n; i++ ){

    cin>>stu_num>>score; 

    stu[i].Set_StuNum(stu_num);

    stu[i].Set_Score(score);

  }

  for( i = 0; i < n; i++ ){

    Score_Trans(stu[i]);

    if(i < n - 1) cout<<endl;

  }

  return 0;

}

输入说明 :

第一行:学生数目n。

第二行至第n+1行:每行输入:学号 分数。 二者之间以空格分隔,行与行之间无空行。

输出说明 :

第一行之n行:学生学号 分数登机。二者之间以空格分隔,行与行之间无空行

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ixll625

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值