编写一个分数类fraction ,其分子、分母为整数,通过重载运算符+、-、*、/ ,实现该类数据之间的四则运算
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/06 15:44:26
![编写一个分数类fraction ,其分子、分母为整数,通过重载运算符+、-、*、/ ,实现该类数据之间的四则运算](/uploads/image/z/11525117-5-7.jpg?t=%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E5%88%86%E6%95%B0%E7%B1%BBfraction+%2C%E5%85%B6%E5%88%86%E5%AD%90%E3%80%81%E5%88%86%E6%AF%8D%E4%B8%BA%E6%95%B4%E6%95%B0%2C%E9%80%9A%E8%BF%87%E9%87%8D%E8%BD%BD%E8%BF%90%E7%AE%97%E7%AC%A6%2B%E3%80%81-%E3%80%81%2A%E3%80%81%2F+%2C%E5%AE%9E%E7%8E%B0%E8%AF%A5%E7%B1%BB%E6%95%B0%E6%8D%AE%E4%B9%8B%E9%97%B4%E7%9A%84%E5%9B%9B%E5%88%99%E8%BF%90%E7%AE%97)
编写一个分数类fraction ,其分子、分母为整数,通过重载运算符+、-、*、/ ,实现该类数据之间的四则运算
编写一个分数类fraction ,其分子、分母为整数,通过重载运算符+、-、*、/ ,实现该类数据之间的四则运算
编写一个分数类fraction ,其分子、分母为整数,通过重载运算符+、-、*、/ ,实现该类数据之间的四则运算
#include
using namespace std;
class fraction{
public:
fraction(int n=1,int d=1){
num=n;
den=d;
}
fraction& operator=(const fraction& other){
num=other.num;
den=other.den;
return *this;
}
fraction operator+(const fraction& other){
fraction res;
res.num=num*other.den+den*other.num;
res.den=den*other.den;
return res;
}
fraction operator-(const fraction& other){
fraction res;
res.num=num*other.den-den*other.num;
res.den=den*other.den;
return res;
}
fraction operator*(const fraction& other){
return fraction(num*other.num,den*other.den);
}
fraction operator/(const fraction& other){
return fraction(num*other.den,den*other.num);
}
void display(){
cout