(矿字号)高级语言程序设计 第九章 类(2)

第九章 类(2)

1.学生成绩类2

【问题描述】定义学生成绩类Score,其私有数据成员有学号、姓名、物理、数学、外语、平均成绩。补全Score类及主函数,使得程序能在一行中一次输出该生的学号、姓名、物理、数学、外语、平均成绩。

【输入形式】输入学生的学号、姓名、物理、数学、外语。(学号为不超过10位的数字;姓名为长度不超过10位的英文;物理数学外语成绩为0-100的整数)

【输出形式】输出学生的学号、姓名、物理、数学、外语以及平均成绩。

【样例输入】

081531 WangXiaoming 100 82 99

【样例输出】

081531 WangXiaoming 100 82 99 93.67

【样例说明】平均成绩保留到小数后两位。

#include <iostream>
#include <string>
#include <cstdio>
#include <iomanip>
using namespace std;
class Score {
private:
    string Id, Name;
    int Phy, Math, Eng;
    double Ave;
public:
    Score(string id, string name, int phy, int math, int eng)
    : Id(id),Name(name),Phy(phy),Math(math),Eng(eng){}
    friend void Average(Score&score) 
    {
       score.Ave=(double)(score.Phy+score.Math+score.Eng)/3;
    }
    void Print() {
       cout<<Id<<" "<<Name<<" "<<Phy<<" "<<Math<<" "<<Eng<<" "; 
               cout<<fixed<<setprecision(2)<<Ave;
    }
};
int main() {
    string id, name;
    int phy, math, eng;
    cin >> id >> name >> phy >> math >> eng;
    Score sco(id,name,phy,math,eng
);
    Average(sco);
    sco.Print();
}

2.电视类

【问题描述】 补全设计一个TV类和一个Remote类。Remote类的成员函数是TV类的友元, 电视类有状态、频道和音量基本属性,默认初始频道为5,默认初始音量为20。状态有开和关(-1表示关机状态,其他为开机状态)。 在主函数根据输入的op值进行不同操作。补全代码使程序满足如下要求。

【输入形式】

当op==1时,

输入电视操作命令如下:

OFF_ON(切换电视开关机状态)

VOL_UP(电视音量+1)

VOL_DOWN(电视音量-1)

CHA_NEXT(电视频道+1)

CHA_PRE(电视频道-1)

CHA_TO x(0<=x<=100,将电视频道切到x)

VOL_TO x(0<=x<=100,将电视音量切到x)

其中CHA_TO与VOL_TO通过调用友元类实现。

当op==2时,输出当前电视状态。

当op==3时,结束程序。

【输出形式】

当op==2时,输出当前电视状态,具体格式见样例。

【样例输入】 【对应样例输出】

2

The TV is OFF

1 OFF_ON

2

The TV is ON
The channel is 5
The volume is 20

1 VOL_UP

2

The TV is ON

The channel is 5

The volume is 21

1 CHA_TO 30

2

The TV is ON

The channel is 30

The volume is 21

1 VOL_TO 30
2

The TV is ON

The channel is 30

The volume is 30

3

注意输入后会有相应的输出。但在测试用例中,输入数据放在一起,输出会集中体现。

#include <iostream>
using namespace std;
// 前向声明
class TV;

// Remote类声明(其中的几个函数为友元)
class Remote
{
public:
    Remote() {};
    void volume_to(TV &tv, int x);
    void channel_to(TV &tv, int x);
};
//TV 类声明
class TV
{
private:
    int state;
    int channel;
    int volume;
public:
    // 声明友元函数
    friend void Remote::volume_to(TV &tv, int x);
    friend void Remote::channel_to(TV &tv, int x);
    // 成员函数
    TV() {};
    TV(int st) :channel(5),volume(20),state(st){}
    void onoff() {
        if(state==-1)
                {
                    state=0;
                }
                else
                {
                    state=-1;
                }
    }
    void cha_next() {
        channel++; 
    }
    void cha_pre() {
        channel--;
    }
    void vol_up() {
        volume++;
    }
    void vol_down() {
        volume--;
    }
    void print() {
        if(state == -1) {
            cout<<"The TV is OFF"<<endl;
        } else {
            cout<<"The TV is ON"<<endl;
                        cout<<"The channel is "<<channel<<endl;
                        cout<<"The volume is "<<volume<<endl;
        }
    }
};
// 友元函数定义
void Remote::volume_to(TV &tv, int x) {
    tv.volume = x;
}
void Remote::channel_to(TV &tv, int x) {
    tv.channel = x;
}
int main()
{
    int x, op;
    string s;
    TV tv(-1);
    Remote rem;
    while(1) {
        cin >> op;
        if(op == 1) {
            cin >> s;
            if(s == "OFF_ON") tv.onoff();
            else if(s == "VOL_UP") tv.vol_up();
            else if(s == "VOL_DOWN") tv.vol_down();
            else if(s == "CHA_NEXT") tv.cha_next();
            else if(s == "CHA_PRE") tv.cha_pre();
            else if(s == "CHA_TO") {
                cin >> x;
                rem.channel_to(tv, x);
            } else if(s == "VOL_TO") {
                cin >> x;
                rem.volume_to(tv, x);
            }
        } else if(op == 2){
            tv.print();
        } else {
            break;
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值