From be4a556f0ba1af0e83c75c24d9e8d2c1415f3bb6 Mon Sep 17 00:00:00 2001 From: flykhan Date: Thu, 27 Jul 2023 16:28:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8B=E5=85=83=E7=BB=83=E4=B9=A0:=20?= =?UTF-8?q?=E7=94=B5=E8=A7=86=E6=9C=BA=E5=92=8C=E9=81=A5=E6=8E=A7=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day4/d7.cpp | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 day4/d7.cpp diff --git a/day4/d7.cpp b/day4/d7.cpp new file mode 100644 index 0000000..4c5c199 --- /dev/null +++ b/day4/d7.cpp @@ -0,0 +1,183 @@ +#include +#include +#include + +using namespace std; + +// 声明遥控器类 +class Remote; + +// 定义电视类 +class Television +{ + friend class Remote; + +public: + enum + { + On, // 电视开关状态 + Off + }; + enum + { + minVol, + maxVol = 100 // 音量从 0 到 100 + }; + enum + { + minChannel = 1, + maxChannel = 255 // 频道范围 1 到 255 + }; + + Television() + { + mStatus = Off; // 状态 + mVolume = minVol; // 声音 + mChannel = minChannel; // 频道 + } + +public: + void power() // 开关机 + { + this->mStatus = (this->mStatus == On ? Off : On); + } + + void VolumeUp() // 调高音量 + { + if (this->mVolume >= maxVol) + return; + this->mVolume++; + } + + void VolumeDown() // 调低音量 + { + if (this->mVolume <= minVol) + return; + this->mVolume--; + } + + void ChannelUp() // 向上更换电视频道 + { + if (this->mChannel >= maxChannel) + return; + this->mChannel++; + } + + void ChannelDown() // 向下更换电视频道 + { + if (this->mChannel <= minChannel) + return; + this->mChannel--; + } + + void ShowTeleState() // 展示当前电视状态信息 + { + cout << "开机状态: " << (mStatus == On ? "已开机" : "已关机") << endl; + if (mStatus == On) + { + cout << "当前音量: " << mVolume << endl; + cout << "当前频道: " << mChannel << endl; + } + cout << "----------------------" << endl; + } + +private: + int mStatus; // 电视状态 + int mVolume; // 声音 + int mChannel; // 频道 +}; + +// 定义遥控器 +class Remote +{ +public: + Remote(Television *tv) // 构造函数 + { + pTV = tv; + } + +public: + void power() // 开关机 + { + pTV->power(); + } + + void VolumeUp() // 调高音量 + { + pTV->VolumeUp(); + } + + void VolumeDown() // 调低音量 + { + pTV->VolumeDown(); + } + + void ChannelUp() // 向上更换电视频道 + { + pTV->ChannelUp(); + } + + void ChannelDown() // 向下更换电视频道 + { + pTV->ChannelDown(); + } + + // 设置频道,遥控器新增功能 + void SetChannel(int channel) + { + if (channel < Television::minChannel || channel > Television::maxChannel) + return; + pTV->mChannel = channel; + } + + void ShowTeleState() // 展示当前电视状态信息 + { + pTV->ShowTeleState(); + } + +private: + Television *pTV; +}; + +// 直接操作电视 +void test01() +{ + Television tv; + tv.ShowTeleState(); + tv.power(); // 开机 + tv.VolumeUp(); // 音量 +1 + tv.VolumeUp(); + tv.VolumeUp(); + tv.VolumeUp(); + tv.ChannelUp(); // 频道 +1 + tv.ChannelUp(); + tv.ShowTeleState(); +} + +// 通过遥控器操作电视 +void test02() +{ + // 创建电视 + Television tv; + + // 创建遥控器 + Remote rmt(&tv); + + rmt.ShowTeleState(); + rmt.power(); // 开机 + rmt.VolumeUp(); // 音量 +1 + rmt.VolumeUp(); + rmt.VolumeUp(); + rmt.VolumeUp(); + rmt.ChannelUp(); // 频道 +1 + rmt.ChannelUp(); + rmt.SetChannel(100); + rmt.ShowTeleState(); +} + +int main() +{ + // test01(); + test02(); + return 0; +}