#include <iostream> using namespace std; class Fraction { public: Fraction(); //缺省构造函数设置分子为0,分母为1 Fraction(int n); //只有1 个参数,设置分子为n,分母为1 Fraction(int n, int d); //设置分子为n,分母为d void setValue(int n, int d); //设置分子和分母 int getNum(); //获取分子值 int getDen(); //获取分母值 double getDoubleValue(); //获取分数对应的小数值 void output(); //按分数形式显式分数, 按真分数或假分数形式输出 private: int num; //分子 int den; //分母 }; int main() { Fraction f1, f2(2), f3(-3,4); int num,den; cin>>num>>den; f1.setValue(num,den); cout<<"Fraction f1 is "; f1.output(); cout<<", its double value is: "<<f1.getDoubleValue()<<endl; cout<<"Fraction f2 is "; cout<<f2.getNum()<<'/'<<f2.getDen(); cout<<"\nFraction f3 is "; f3.output(); return 0; } /* 请在这里填写答案
时间: 2023-12-10 19:37:39 浏览: 45
答案如下:
```c++
#include <iostream>
using namespace std;
class Fraction {
public:
Fraction(); //缺省构造函数设置分子为0,分母为1
Fraction(int n); //只有1个参数,设置分子为n,分母为1
Fraction(int n, int d); //设置分子为n,分母为d
void setValue(int n, int d); //设置分子和分母
int getNum(); //获取分子值
int getDen(); //获取分母值
double getDoubleValue(); //获取分数对应的小数值
void output(); //按分数形式显式分数, 按真分数或假分数形式输出
private:
int num; //分子
int den; //分母
};
Fraction::Fraction() {
num = 0;
den = 1;
}
Fraction::Fraction(int n) {
num = n;
den = 1;
}
Fraction::Fraction(int n, int d) {
num = n;
den = d;
}
void Fraction::setValue(int n, int d) {
num = n;
den = d;
}
int Fraction::getNum() {
return num;
}
int Fraction::getDen() {
return den;
}
double Fraction::getDoubleValue() {
return (double)num / den;
}
void Fraction::output() {
int integer = num / den;
int remainder = num % den;
if (integer == 0 && num < 0) {
remainder = -remainder;
}
if (remainder == 0) {
cout << integer;
}
else if (integer == 0) {
cout << remainder << '/' << den;
}
else {
cout << integer << ' ' << abs(remainder) << '/' << den;
}
}
int main() {
Fraction f1, f2(2), f3(-3, 4);
int num, den;
cin >> num >> den;
f1.setValue(num, den);
cout << "Fraction f1 is ";
f1.output();
cout << ", its double value is: " << f1.getDoubleValue() << endl;
cout << "Fraction f2 is " << f2.getNum() << '/' << f2.getDen() << endl;
cout << "Fraction f3 is ";
f3.output();
return 0;
}
```
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)