利用运算符重载进行有理数的计算

定义一个有理数类。一个有理数类是一个可以表示成两个整数相除的数,如1/2、2/3、4/5。一个有理数类有两个整数成员表示:分子和分母。要求能检测分母为0问题,能化简,重载+、-、*、/运算符完成四则运算,重载“==”符号判断两个有理数是否相等。

#include <iostream>
using namespace std;
class Youli
{
public:
    Youli(double r=0.0,double i=0.0);
    friend Youli operator +(Youli a,Youli b);//重载二元加
    friend Youli operator -(Youli a,Youli b);//重载二元减
    friend Youli operator *(Youli a,Youli b);
    friend Youli operator /(Youli a,Youli b);
    friend Youli operator==(Youli a,Youli b);
    void display();
    void huajian();
private:
    double zi,mu;
};
Youli ::Youli(double r,double i)
{
   zi=r;mu=i;
   
}
Youli operator +(Youli a,Youli b)
{
    int x;
    int y;
    if(a.mu==0||b.mu==0)
    {
        cout<<"分母不可以为0"<<endl;
    
    }
    else
    {
        x=a.zi*b.mu+b.zi*a.mu;
    y=a.mu*b.mu;
    Youli temp(x,y);
    temp.huajian();
    //return temp;
    //cout<<"jsdho"<<endl;
    temp.display();
    }
   
}
Youli operator -(Youli a,Youli b)
{
    if(a.mu==0||b.mu==0)
    {
        cout<<"分母不可以为0"<<endl;
    }
    else
    {
        int x;
    int y;
    x=a.zi*b.mu-b.zi*a.mu;
    y=a.mu*b.mu;
    Youli m(x,y);
    m.huajian();
    //return m;
    //cout<<"jdiaj0"<<endl;
    m.display();
    }
    
}
Youli operator *(Youli a,Youli b)
{
    if(a.mu==0||b.mu==0)
    {
        cout<<"分母不可以为0"<<endl;
    }
    else
    {
            int x;
    int y;
    x=a.zi*b.zi;
    y=a.mu*b.mu;
    Youli t(x,y);
    t.huajian();
    //return t;
    t.display();
    }

}
Youli operator /(Youli a,Youli b)
{
    if(a.mu==0||b.mu==0)
    {
        cout<<"分母不可以为0"<<endl;
    }
    else
    {
        int x;
    int y;
    x=a.zi*b.mu;
    y=a.mu*b.zi;
    Youli di(x,y);
    di.huajian();
    //return di;
    di.display();
    }
    
}
Youli operator==(Youli a,Youli b)
{
    if(a.mu==0||b.mu==0)
    {
        cout<<"分母不可以为0"<<endl;
    }
    else
    {
    if(a.mu*b.zi&&a.mu==b.zi)
    {
        cout<<"EQUAL"<<endl;
        
     }
     else {
         cout<<"UNEQUAL"<<endl;
     }
     }
}
void Youli::huajian()
{
    int m,n,i;
    n=this->mu;
    m=this->zi;
    if(this->mu==this->zi)
    {
    n=1;
    m=1;
    }
    if(m>n)
    {
    i=n;
    }
for(i=m;i>=1;i--)
    {
        if((m%i==0)&&(n%i==0))
        {
            //cout<<i<<" ";
            m=m/i;
            n=n/i;
        }
    }
    if(m<0)
    {
        m=-m;
        if(m>n)
    {
    i=n;
    }
for(i=m;i>=1;i--)
    {
        if((m%i==0)&&(n%i==0))
        {
            //cout<<i<<" ";
            m=m/i;
            n=n/i;
        }
    }
    m=-m;
    }
        this->zi=m;
        this->mu=n;
    
}
void Youli::display()
{
        if(zi==1&&mu==1){
                cout<<1<<endl;
            }
            else if(zi==0)
            {
                cout<<0;
            }
            else
            {
                cout<<zi<<"/"<<mu<<endl;
            }
}
int main()
{
    Youli a(1,8);
    Youli b(1,2);
    Youli c;
    Youli d;
    Youli e;
    Youli f;
    f=a/b;
    f=a==b;
    e=a*b;
    d=a-b;
    c=a+b;
    
}

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
有理数)创建一个名为 Rational 的,用于对分数进行算术算。编写一个程序来测试你的。使用整数变量来表示的私有实例变量——分子和分母。 提供一个构造函数,使该的对象能够在声明时进行初始化。构造函数应以简化形式存储分数。分数 2/4 等价于 1/2,并将作为分子中的 1 和分母中的 2 存储在对象中。 如果没有提供初始值设定项,请提供默认值为 1 的无参数构造函数。 提供执行以下每个操作的公共方法: a) 将两个有理数相加:相加的结果应以简化形式存储。 b) 两个有理数相减:相减的结果应以简化形式存储。 c) 将两个有理数相乘:相乘的结果应以简化形式存储。 d) 将两个有理数相除:相除的结果应以简化形式存储。 e) 以 a/b 的形式返回有理数的字符串表示形式,其中 a 是分子,b 是分母。 f) 以浮点格式返回有理数的字符串表示形式. (考虑提供格式化功能,的用户能够指定小数点右侧的精度位数。) 【Sample output 1】 Enter numerator 1: 12 Enter denominator 1: 3 Enter numerator 2: 5 Enter denominator 2: 14 Enter precision: 3 a=4/1 b=5/14 a + b = 61/14 = 4.357 a - b = 51/14 = 3.643 a * b = 10/7 = 1.429 a / b = 56/5 = 11.200 【Sample output 2】 Enter numerator 1: 1 Enter denominator 1: 4 Enter numerator 2: 75 Enter denominator 2: 35 Enter precision: 1 a=1/4 b=15/7 a + b = 67/28 = 2.4 a - b = -53/28 = -1.9 a * b = 15/28 = 0.5 a / b = 7/60 = 0.1 Note: The red texts are inputed ,others are output texts. Just use a space to seperate words
好的,以下是一个简单的有理数的定义: ```python class Rational: def __init__(self, numerator, denominator): self.numerator = numerator self.denominator = denominator def __str__(self): return f"{self.numerator}/{self.denominator}" def simplify(self): gcd = self._gcd(self.numerator, self.denominator) self.numerator //= gcd self.denominator //= gcd def _gcd(self, a, b): if b == 0: return a return self._gcd(b, a % b) def __add__(self, other): numerator = self.numerator * other.denominator + other.numerator * self.denominator denominator = self.denominator * other.denominator return Rational(numerator, denominator) def __sub__(self, other): numerator = self.numerator * other.denominator - other.numerator * self.denominator denominator = self.denominator * other.denominator return Rational(numerator, denominator) def __mul__(self, other): numerator = self.numerator * other.numerator denominator = self.denominator * other.denominator return Rational(numerator, denominator) def __truediv__(self, other): numerator = self.numerator * other.denominator denominator = self.denominator * other.numerator return Rational(numerator, denominator) ``` 这个有两个成员变量:分子和分母。它还有一个 `__str__` 方法,用于将有理数转换为字符串。它还有一个 `simplify` 方法,用于将有理数化简为最简形式。它还有四个运算符重载方法:`__add__`、`__sub__`、`__mul__` 和 `__truediv__`,用于实现有理数的加、减、乘、除算。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值