问题描述
设计一个Fan风扇类,要求如下:
设计一个Rational有理数类,要求如下:
问题分析
Fan设计:
Rational设计:
源代码
Fan:
//main.cpp
#include <iostream>
#include<string>
#include "Fan.h"
using namespace std;
int main()
{
Fan a;
cout << "before:" << endl;
cout << a.status() << endl;
cout << "after:" << endl;
a.setColor("black");
a.setSpeed(3);
a.setOn(true);
cout << a.status() << endl;
cout << "***************" << endl;
Fan b(2,true,4.0,"blue");
cout << b.status() << endl;
cout << "***************" << endl;
Fan c(3,false,1.0,"red");
cout << c.status() << endl;
cout << "***************" << endl;
return 0;
}
//Fan.h
#include <string>
using namespace std;
class Fan{
public:
int SLOW = 1;
int MEDIUM = 2;
int FAST = 3;
Fan();
Fan(int s,bool o,double r,string c);
int getSpeed();
void setSpeed(int s);
bool getOn();
void setOn(bool o);
double getRadius();
string getColor();
void setColor(string str);
string status();
private:
int speed = 1;
bool on = false;
double radius = 5.0;
string color = "white";
};
//Fan.cpp
#include <iostream>
#include <string>
#include "Fan.h"
using namespace std;
Fan ::Fan(){}
Fan ::Fan(int s,bool o,double r,string c){
SLOW = 1;
MEDIUM = 2;
FAST = 3;
speed = s;
on = o;
radius = r;
color = c;
}
int Fan ::getSpeed(){
return speed;
}
void Fan ::setSpeed(int s){
speed = s;
}
bool Fan ::getOn(){
return on;
}
void Fan ::setOn(bool o){
on = o;
}
double Fan ::getRadius(){
return radius;
}
string Fan ::getColor(){
return color;
}
void Fan ::setColor(string str){
color = str;
}
string Fan ::status(){
string str = "";
if(on){
str = "speed: ";
if(speed == 1)
str += "SLOW ";
else if(speed == 2)
str += "MEDIUM ";
else
str += "FAST ";
str += "color: " + color + " radius: " + to_string(radius);
}
else{
str += "status: off color: " + color + " radius: " +to_string(radius);
}
return str;
}
Rational:
//main.cpp
#include <iostream>
#include "Rational.h"
using namespace std;
int main()
{
double x = 25.12;
Rational a(25,32);
a = a.transform_double(x);
a.output_Rational();
return 0;
}
//Rational.h
using namespace std;
class Rational{
public:
int x;
int y;
Rational();
Rational(int a,int b);
void setRational(int a,int b);
void easeRational();
Rational sum(Rational s);
Rational reduce(Rational r);
Rational multply(Rational m);
Rational divorce(Rational d);
void output_Rational();
Rational transform_double(double t);
};
//Rational.cpp
#include <iostream>
#include <math.h>
#include "Rational.h"
using namespace std;
Rational::Rational(){
x = 0;
y = 1;
}
Rational::Rational(int a,int b){
if(!b){
cout << "分母不能为零 已按照默认数值初始化!" << endl;
x = 0;
y = 1;
}
else{
x = a;
y =b;
}
}
void Rational::setRational(int a,int b){
if(!b){
cout << "分母不能为零!" << endl;
}
else{
x = a;
y =b;
}
}
void Rational::easeRational(){
if(!x){
y = 1;
}
else{
int a = x > y ? x : y;
int b = x < y ? x : y;
while(a % b){
int temp = b;
b = a % b;
a = temp;
}
x = x / b;
y = y / b;
}
}
Rational Rational::sum(Rational s){
Rational ss((x*s.y + y*s.x),(y*s.y));
ss.easeRational();
return ss;
}
Rational Rational::reduce(Rational r){
Rational rr((x*r.y - y*r.x),(y*r.y));
rr.easeRational();
return rr;
}
Rational Rational::multply(Rational m){
Rational mm(x*m.x,y*m.y);
mm.easeRational();
return mm;
}
Rational Rational::divorce(Rational d){
Rational dd((x*d.y),(y*d.x));
dd.easeRational();
return dd;
}
void Rational::output_Rational(){
cout << x << "/" << y << endl;
}
Rational Rational::transform_double(double t){
int p = 1000000;
Rational re((int)(p*t),p);
re.easeRational();
return re;
}
测试:
Fan:
Rational:
读者可自行设计测试样例进行测试,这里不再赘述
更多相关内容请参见