//以下代码是用一个类来实现雨刷问题,仅供参考
#include <iostream.h> #include <stdlib.h> class WindshieldWiper{ //雨刷类定义 private: int wiperSpeed; //雨刷速度 int leverPosition; //控制杆位置,取值为0、1、2、3 int dialPosition; //刻度盘位置,取值为1、2、3 void setWiperSpeed(); //设置雨刷速度 void setLeverPosition(int); //设置控制杆位置 void setDialPosition(int); //设置刻度盘位置 public: WindshieldWiper(int w,int l,int d); int getWiperSpeed(); //获取雨刷速度 int getLeverPostion(); //获取控制杆位置 int getDialPosition(); //获取刻度盘位置 void senseLeverUp(); //感知控制杆升档 void senseLeverDown(); //感知控制杆降档 void senseDialUp(); //感知刻度盘升档 void senseDialDown(); //感知刻度盘降档 }; WindshieldWiper::WindshieldWiper(int w = 0,int l = 0,int d = 1) //构造函数 { wiperSpeed = w; leverPosition = l; dialPosition = d; } int WindshieldWiper::getDialPosition() { return dialPosition; } int WindshieldWiper::getLeverPostion() { return leverPosition; } int WindshieldWiper::getWiperSpeed() { return wiperSpeed; } void WindshieldWiper::setDialPosition(int d) { dialPosition = d; } void WindshieldWiper::setLeverPosition(int l) { leverPosition = l; } void WindshieldWiper::setWiperSpeed() { if(leverPosition == 0) //控制杆为0档 wiperSpeed = 0; else if(leverPosition == 1) //控制杆为1档 { if(dialPosition == 1) //刻度盘为1档 wiperSpeed = 4; else if(dialPosition == 2) //刻度盘为2档 wiperSpeed = 6; else wiperSpeed = 12; //刻度盘为3档 } else if(leverPosition == 2) //控制杆为2档 wiperSpeed = 30; else //控制杆为3档 wiperSpeed = 60; cout<<"Lever position is: "<<leverPosition<<endl; cout<<"Dial position is: "<<dialPosition<<endl; cout<<"Wiper speed is: "<<wiperSpeed<<endl; } void WindshieldWiper::senseDialDown() { switch(dialPosition) { case 1: break; case 2: setDialPosition(1);break; case 3: setDialPosition(2);break; } setWiperSpeed(); } void WindshieldWiper::senseDialUp() { switch(dialPosition) { case 1: setDialPosition(2);break; case 2: setDialPosition(3);break; case 3: break; } setWiperSpeed(); } void WindshieldWiper::senseLeverDown() { switch(leverPosition) { case 0: break; case 1: setLeverPosition(0);break; case 2: setLeverPosition(1);break; case 3: setLeverPosition(2);break; } setWiperSpeed(); } void WindshieldWiper::senseLeverUp() { switch(leverPosition) { case 0: setLeverPosition(1);break; case 1: setLeverPosition(2);break; case 2: setLeverPosition(3);break; case 3: break; } setWiperSpeed(); } void menu(WindshieldWiper &w) //菜单函数 { int flag; cout<<"Lever position is: "<<w.getLeverPostion()<<endl; cout<<"Dial position is: "<<w.getDialPosition()<<endl; cout<<"Wiper speed is: "<<w.getWiperSpeed()<<endl; while(1) { cout<<"==================Please choose operation============"<<endl; cout<<"\t==========1:Lever Up==========="<<endl; cout<<"\t==========2:Lever Down========="<<endl; cout<<"\t==========3:Dial Up============"<<endl; cout<<"\t==========4:Dial Down=========="<<endl; cout<<"\t==========0:Quit==============="<<endl; cout<<"====================================================="<<endl; cout<<"Input the number(0~4):"; cin>>flag; switch(flag) { case 1:w.senseLeverUp();break; case 2:w.senseLeverDown();break; case 3:w.senseDialUp();break; case 4:w.senseDialDown();break; case 0:exit(0); } } } //主函数 void main() { WindshieldWiper w(0,0,1); menu(w); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-25 19:39:35