【问题描述】设计一个类,重载运算符为友元函数,使对整型的运算符=、+、-、*、/ 适用于分数运算。要求:(1)输出结果是最简分数(可以是带分数);(2)分母为1,只输出分子。

文章描述了一个C++类`CFraction`的设计,该类用于处理分数运算,包括加、减、乘、除,并确保结果是最简分数。通过友元函数重载了`+`、`-`、`*`、`/`运算符,实现了分数之间的计算,同时提供了输入输出的重载方法。样例展示了如何使用这个类进行分数运算并打印结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【问题描述】

设计一个类,重载运算符为友元函数,使对整型的运算符=、+、-、*、/ 适用于分数运算。要求:

(1)输出结果是最简分数(可以是带分数);

(2)分母为1,只输出分子。

参考的输入输出:

Input x: 1/6

Input y: 2/9

x+y=7/18

x-y=-1/18

x*y=1/27

x/y=3/4

【输入形式】

提示“Input x: ”,输入第一个分数。如:1/6

提示“Input y: ”,输入第二个分数。如:2/9

【输出形式】

提示并输出+、-、*、/的结果,如

x+y=7/18

x-y=-1/18

x*y=1/27

x/y=3/4

【样例输入】

Input x: 1/6

Input y: 2/9

【样例输出】

x+y=7/18

x-y=-1/18

x*y=1/27

x/y=3/4

【样例说明】

【评分标准】 


#include <iostream>
#include <cmath>
using namespace std;
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    CFraction(int nu=0,int de=1):nume(nu),deno(de) {}
    void simplify();

    //输入输出的重载
    friend istream &operator>>(istream &in,CFraction &x);
    friend ostream &operator<<(ostream &out,CFraction x);

    friend CFraction operator+(const CFraction &c1,const CFraction &c2);  //两个分数相加,结果要化简
    friend CFraction operator-(const CFraction &c1,const CFraction &c2);  //两个分数相减,结果要化简
    friend CFraction operator*(const CFraction &c1,const CFraction &c2);  //两个分数相乘,结果要化简
    friend CFraction operator/(const CFraction &c1,const CFraction &c2);  //两个分数相除,结果要化简

};

void CFraction::simplify()
{
    int a = 1;
	int n = nume;
    int m = deno;
    if(n%m==0||m%n==0)
		m=m<n?m:n;
	while((a=n%m)!=0)
	{
		n=m;
		m=a;
	}
    nume = nume/m;
    deno = deno/m;
};

istream  &operator>>(istream  &in,CFraction  &x)
{
    char t;
    in>>x.nume>>t>>x.deno;
    return in;
}

ostream  &operator<<(ostream  &out,CFraction  x)
{
    if(x.deno!=1 && x.deno > 0)
        out<<x.nume<<"/"<<x.deno;
    else if(x.deno == 1)
        out<<x.nume;
    else if(x.deno <0)
        out<<"-"<<x.nume<<"/"<<abs(x.deno);
    return out;
}

CFraction  operator+(const  CFraction  &c1,const  CFraction  &c2)
{
    CFraction c;
    c.nume = c1.nume * c2.deno + c2.nume * c1.deno;
    c.deno = c1.deno * c2.deno;
    c.simplify();
    return c;
}

CFraction  operator-(const  CFraction  &c1,const  CFraction  &c2)
{
    CFraction c;
    c.nume = c1.nume * c2.deno - c2.nume * c1.deno;
    c.deno = c1.deno * c2.deno;
    c.simplify();
    return c;
}

CFraction  operator*(const  CFraction  &c1,const  CFraction  &c2)
{
    CFraction c;
    c.nume = c1.nume * c2.nume;
    c.deno = c1.deno * c2.deno;
    c.simplify();
    return c;
}

CFraction  operator/(const  CFraction  &c1,const  CFraction  &c2)
{
    CFraction c;
    c.nume = c1.nume * c2.deno;
    c.deno = c1.deno * c2.nume;
    c.simplify();
    return c;
}

int main()
{
    CFraction x,y,s;
    cout<<"Input x: ";
    cin>>x;
    cout<<"Input y: ";
    cin>>y;
    s=x+y;
    cout<<"x+y="<<s<<endl;
    s=x-y;
    cout<<"x-y="<<s<<endl;
    s=x*y;
    cout<<"x*y="<<s<<endl;
    s=x/y;
    cout<<"x/y="<<s<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那不勒斯的萤火丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值