实现一个分数类Fraction,能读写“1/2”样子的分数
时间: 2024-01-27 21:03:30 浏览: 55
好的,以下是一个简单的实现Fraction类的示例代码,可以读写“1/2”样子的分数:
```python
class Fraction:
def __init__(self, numerator, denominator):
self.numerator = numerator
self.denominator = denominator
def __str__(self):
return f"{self.numerator}/{self.denominator}"
@classmethod
def from_string(cls, s):
numerator, denominator = s.split('/')
return cls(int(numerator), int(denominator))
fraction = Fraction(1, 2)
print(fraction) # 输出 1/2
fraction2 = Fraction.from_string("3/4")
print(fraction2) # 输出 3/4
```
在这个示例代码中,我们创建了一个Fraction类,它有两个属性:numerator(分子)和denominator(分母)。我们定义了一个__str__方法,它返回一个字符串,表示这个分数的值。我们还定义了一个classmethod方法from_string,它可以从一个字符串中创建Fraction对象。
相关问题
用c++实现一个分数类Fraction,能读写“1/2”样子的分数。
下面是一个简单的 C++ 分数类 `Fraction` 的实现,它可以读写形如“1/2”样子的分数:
```c++
#include <iostream>
using namespace std;
class Fraction {
public:
Fraction(int n = 0, int d = 1) : numerator(n), denominator(d) {}
friend istream& operator>>(istream& in, Fraction& f);
friend ostream& operator<<(ostream& out, const Fraction& f);
private:
int numerator; // 分子
int denominator; // 分母
};
istream& operator>>(istream& in, Fraction& f) {
char c;
in >> f.numerator >> c >> f.denominator;
return in;
}
ostream& operator<<(ostream& out, const Fraction& f) {
out << f.numerator << '/' << f.denominator;
return out;
}
int main() {
Fraction f;
cout << "请输入一个分数:";
cin >> f;
cout << "你输入的分数是:" << f << endl;
return 0;
}
```
在这个实现中,`Fraction` 类有两个私有成员变量 `numerator` 和 `denominator`,分别表示分数的分子和分母。它有一个默认的构造函数,可以接受两个参数,用来初始化分子和分母。它还有两个友元函数 `operator>>` 和 `operator<<`,分别用于输入和输出分数。
在 `operator>>` 函数中,我们先读取分子,然后读取一个字符(即分数线),最后读取分母。在 `operator<<` 函数中,我们直接输出分子和分母,中间用分数线隔开。
在主函数中,我们先创建一个 `Fraction` 对象 `f`,然后让用户输入一个分数并读入到 `f` 中,最后输出 `f`。
编写一个分数类Fraction
好的,下面是一个简单的分数类Fraction的实现:
```python
class Fraction:
def __init__(self, numerator, denominator):
self.numerator = numerator
self.denominator = denominator
def __str__(self):
return f'{self.numerator}/{self.denominator}'
def __add__(self, other):
lcm = self.denominator * other.denominator // math.gcd(self.denominator, other.denominator)
numerator = (lcm // self.denominator) * self.numerator + (lcm // other.denominator) * other.numerator
return Fraction(numerator, lcm)
def __sub__(self, other):
lcm = self.denominator * other.denominator // math.gcd(self.denominator, other.denominator)
numerator = (lcm // self.denominator) * self.numerator - (lcm // other.denominator) * other.numerator
return Fraction(numerator, lcm)
def __mul__(self, other):
numerator = self.numerator * other.numerator
denominator = self.denominator * other.denominator
return Fraction(numerator, denominator)
def __truediv__(self, other):
numerator = self.numerator * other.denominator
denominator = self.denominator * other.numerator
return Fraction(numerator, denominator)
def simplify(self):
gcd = math.gcd(self.numerator, self.denominator)
self.numerator //= gcd
self.denominator //= gcd
def to_float(self):
return self.numerator / self.denominator
```
这个分数类支持分数的加减乘除和化简,并且可以将分数转换为浮点数。在初始化时需要提供分子和分母。可以使用 `__str__` 方法将分数转换为字符串形式输出,也可以使用 `simplify` 方法将分数化简。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)