PTA 6-6 分数 分数 10 作者 翁恺 单位 浙江大学

题目:在这里插入图片描述

裁判测试程序样例:

#include <iostream>
using namespace std;

/* 请在这里填写答案 */

int main(void)
{
    Fraction f1;
    Fraction f2;
    cin >> f1 >> f2;
    cout << f1+f2 << endl;
    cout << (double)(f1+f2) << endl;
    cout << f1-f2 << endl;
    cout << f1*f2 << endl;
    cout << f2/f1 << endl;
    double d;
    cin >> d;
    Fraction f((long long)(d*1e16), 1e16);
    cout << f << endl;
    f=0.5;
    cout << f << endl;
    Fraction*p = &f1;
    f1=*p;
    cout << f1 << endl;
    cin >> f1;
    f1 = f1*Fraction(2.0);
    cout << f1 << endl;
}

输入样例:

1/2
1/3
0.5
1/4

输出样例:

5/6
0.833333
1/6
1/6
2/3
1/2
1/2
1/2
1/2

答案:

class Fraction
{
public:
    Fraction(){}
    Fraction(double m)
    {

        this->fenzi = (long long )m * 10;
        this->fenmu = 10;
        huajian(*this);
    }
    Fraction(long long x, double y)
    {
        this->fenzi = x;
        this->fenmu = (long long)y;
        huajian(*this);
    }
    operator double()
    {
        return (double)(1.0*this->fenzi / this->fenmu);
    }
    friend Fraction& huajian(Fraction& m);
    friend istream& operator>>(istream& in, Fraction& f);
    friend Fraction operator+(Fraction& x, Fraction& y);
    friend ostream& operator<<(ostream& out, Fraction m);
    friend Fraction operator-(Fraction& x, Fraction& y);
    friend Fraction operator*(Fraction& x, Fraction y);
    friend Fraction operator/(Fraction& x, Fraction& y);
    Fraction& operator=(Fraction x)
    {
        fenzi = x.fenzi;
        fenmu = x.fenmu;
        return *this;
    }
    Fraction& operator=(double x)
    {
        Fraction temp;
        temp.fenzi = (long long)(x * 10.0);
        temp.fenmu = 10;
        huajian(temp);
        *this = temp;
        return *this;
    }

private:
    long long  fenzi;
    long long  fenmu;
};
Fraction& huajian(Fraction& m)
{
    long long  c;
    long long max;
    long long a = m.fenzi;
    long long b = m.fenmu;
    if (a > b)
    {
        c = a % b;
        while (c != 0)
        {
            a = b;
            b = c;
            c = a % b;
        }
        max = b;
    }
    else if(a<b)
    {
        c = b % a;
        while (c != 0)
        {
            b = a;
            a = c;
            c = b % a;
        }
        max = a;
    }
    else max=a;
    m.fenmu /= max;
    m.fenzi /= max;
    return m;
}
istream& operator>>(istream& in, Fraction& f)
{
    char c;
    in >> f.fenzi;
    in >>  c;
    in >> f.fenmu;
    return in;
}
ostream& operator<<(ostream& out, Fraction m)
{
    out << m.fenzi << '/' << m.fenmu;
    return out;
}
Fraction operator+(Fraction& x, Fraction& y)
{
    Fraction temp;
    temp.fenzi = x.fenzi * y.fenmu + y.fenzi * x.fenmu;
    temp.fenmu = x.fenmu * y.fenmu;
    huajian(temp);
    return temp;
}
Fraction operator-(Fraction& x, Fraction& y)
{
    Fraction temp;
    temp.fenzi = x.fenzi * y.fenmu - x.fenmu * y.fenzi;
    temp.fenmu = x.fenmu * y.fenmu;
    huajian(temp);
    return temp;
}
Fraction operator*(Fraction& x, Fraction y)
{
    Fraction temp;
    temp.fenzi = x.fenzi * y.fenzi;
    temp.fenmu = x.fenmu * y.fenmu;
    huajian(temp);
    return temp;
}
Fraction operator/(Fraction& x, Fraction& y)
{
    Fraction temp;
    temp.fenzi = x.fenzi * y.fenmu;
    temp.fenmu = x.fenmu * y.fenzi;
    huajian(temp);
    return temp;
}

结果
在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值