C++——友元类模拟电视机操作

TV.h

//
// Created by JAN on 2022/1/26.
//

#ifndef C___TV_H
#define C___TV_H
#include <iostream>
class TV
{
private:
    enum { VOL_MIN, VOL_MAX = 100};
    enum { Off, On};
    enum { MIN, MAX = 900};
    int channel;
    int nit;
    int vol;
public:
    std::string name;
    bool state;
    explicit TV(std::string  _name = "no name", bool s = Off) : name(_name), state(s), vol(50), nit(50), channel(0) {}
    bool channel_up () ;
    bool channel_down () ;
    bool nit_up();
    bool nit_down();
    bool vol_up ();
    bool vol_down();
    void on_off();
    void show() const;
    friend class RC;
};

class RC
{
private:
    enum { ELE_MIN, ELE_MAX = 100};
    //注意这里的mutable和类中的常函数的关系
    //RC和TV中的常函数是关联的
    mutable int ele;
    bool ele_down() const;
public:
    TV *control_TV;

    explicit RC(TV *t = nullptr) : ele(90), control_TV(t) {}
    bool channel_up();
    bool channel_down();
    bool channel_to(int num);
    bool vol_up();
    bool vol_down();
    bool vol_to(int num);
    bool nit_up();
    bool nit_down();
    bool nit_to(int num);
    void ele_full();
    void on_off();
    bool show_control_TV() const;
    void show() const;

    void menu();
};
#endif //C___TV_H

TV.cpp

//
// Created by JAN on 2022/1/27.
//

#include "TV.h"
using std::cout;
using std::endl;

void TV::show() const
{
    cout << "名称:" << name << endl
         << "频道:" << channel << endl
         << "音量:" << vol << endl
         << "亮度:" << nit << endl;
}
bool TV::nit_down()
{
    if(nit > VOL_MIN){
        nit--;
        return true;
    }else{
        cout << "已经是最小亮度" << endl;
        return false;
    }
}

bool TV::nit_up()
{
    if(nit < VOL_MAX){
        nit++;
        return true;
    }else{
        cout << "已经是最大亮度了" << endl;
        return false;
    }
}
bool TV::channel_down()
{
    if(channel > MIN){
        channel--;
        return true;
    }else{
        cout << "这已经是第一个频道了" << endl;
        return false;
    }

}

bool TV::channel_up()
{
    if(channel < MAX){
        channel++;
        return true;
    }else{
        cout << "这已经是最后一个频道了" << endl;
        return false;
    }
}


bool TV::vol_down()
{
    if(vol - 10 >= VOL_MIN){
        vol -= 10;
        return true;
    }else if(vol - 5 >= VOL_MIN){
        vol -= 5;
        return true;
    }else if(vol - 1 >= VOL_MIN){
        vol -= 1;
        return true;
    }else{
        cout << "这已经是最小的音量了" << endl;
        return false;
    }
}

bool TV::vol_up()
{
    if(vol + 10 <= VOL_MAX){
        vol += 10;
        return true;
    }else if(vol + 5 <= VOL_MAX){
        vol += 5;
        return true;
    }else if(vol + 1 <= VOL_MAX){
        vol += 1;
        return true;
    }else{
        cout << "这已经是最大的音量了" << endl;
        return false;
    }
}

void TV::on_off()
{
    state ^= On;
}

void RC::on_off()
{
    if(ele_down())
        control_TV->on_off();
}
bool RC::channel_down()
{
    if(ele_down())
        return control_TV ->channel_down();
    else
        return false;
}

bool RC::channel_up()
{
    if(ele_down())
        return control_TV->channel_up();
    else
        return false;
}

bool RC::channel_to(int num)
{
    if (ele_down()) {
        if (num >= control_TV->MIN && num <= control_TV->MAX) {
            control_TV->channel = num;
            return true;
        } else{
            cout << "请合法输入" << endl;
            return false;
        }
    }else
        return false;
}


void RC::ele_full()
{
    cout << "已更换电池" << endl;
    ele = ELE_MAX;
}

bool RC::nit_to(int num)
{
    if(ele_down()){
        if(num >= ELE_MIN && num <= ELE_MAX){
            control_TV->nit = num;
            return true;
        }else{
            cout << "请合法输入" << endl;
            return false;
        }
    }else
        return false;
}

bool RC::show_control_TV() const
{
    if(control_TV == nullptr){
        cout << "未连接电视" << endl;
        return false;
    }else{
        cout << "链接的电视为" << control_TV->name << endl;
        return true;
    }
}

bool RC::vol_down()
{
    if(ele_down())
        return control_TV->vol_down();
    else
        return false;
}

bool RC::vol_up()
{
    if(ele_down())
        return control_TV->vol_up();
    else
        return false;
}


bool RC::vol_to(int num)
{
    if(ele_down()){
        if(num>=ELE_MIN && num <= ELE_MAX){
            control_TV->vol = num;
            return true;
        }else{
            cout << "请合法输入" << endl;
            return false;
        }
    }else
        return false;
}
bool RC::ele_down() const {
    if(ele > ELE_MIN){
        ele--;
        return true;
    }else{
        cout << "遥控器电量不足" << endl;
        return false;
    }
}

bool RC::nit_down()
{
    if(ele_down())
        return control_TV->nit_down();
    else
        return false;
}

bool RC::nit_up()
{
    if(ele_down())
        return control_TV->nit_up();
    else
        return false;
}

void RC::show() const
{
    if(ele_down()){
        show_control_TV();
        control_TV->show();
    }
}

void RC::menu()
{
    cout << "nit up: 9, nit down: 3, nit to: 6" << endl
         << "vol up: 8, vol down: 2, vol to: 5" << endl
         << "TC  up: 7, TC  down: 1, TC  to: 4" << endl
         << "TV on and off: 0, exit control: -" << endl
         << "TV show: s, ele full: e" << endl;
    char c;
    while((std::cin>>c) && (c != '-')){
        switch (c) {
            case '1' :
            {
                channel_down();
                break;
            }
            case '2' :
            {
                vol_down();
                break;
            }
            case '3' :
            {
                nit_down();
                break;
            }
            case '4' :
            {
                int num;
                cout << "请输入电视节目" << endl;
                std::cin >> num;
                channel_to(num);
                break;
            }
            case '5' :
            {
                int num;
                cout << "请输入音量" << endl;
                std::cin >> num;
                vol_to(num);
                break;
            }
            case '6' :
            {
                int num;
                cout << "请输入亮度" << endl;
                std::cin >> num;
                nit_to(num);
                break;
            }
            case '7' :
            {
                channel_up();
                break;
            }
            case '8' :
            {
                vol_up();
                break;
            }
            case '9' :
            {
                nit_up();
                break;
            }
            case '0' :
            {
                on_off();
                break;
            }
            case 's' :
            {
                show();
                break;
            }
            case 'e' :
            {
                ele_full();
                break;
            }
            default :
            {
                cout << "请合法输入" << endl;
                break;
            }
        }
    }
}

main.cpp

#include "TV.h"

int main()
{
    std::string name = "PHILISP";
    TV *ptv = new TV (name,false);
    RC rc(ptv);
    rc.menu();
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值