【C++学习】运算符重载:自己写一个分数类

前言

相必读者您在编程生活中,应该会想过让自己的程序能够使用分数的运算、输出;也许命令行不方便实现像在纸上那样的自然书写显示,但是如同1/23/4这样的显示,或许也足以使自己感到一点成就感。

现在学习了C++,有了运算符重载等方便的功能,让我们能够方便的实现并使用分数的功能。

之前老师布置过一个【多项式的处理】的作业,奈何其仅支持正整数次数、小数系数;也是出于对它进行一个升级、让他能够支持分数的目的,我便决定开始写这样一个分数类。

这里有几篇博客,虽然我这里可能并没有实现提到的功能,但是它们帮助了我很多,欢迎大家浏览参考:

C++分数类 - 拱垲 - 博客园
C语言将循环小数/有限小数转换为分数 - CSDN
小数转化为分数 - 小白的个人总结 - 博客园

您也可以在搜索引擎中搜索相应的关键字,寻找您喜爱的内容。

2020/8/9 更新

  • 代码中的求最大公因数、最小公倍数的函数可以采用__gcd()__lcm(),这两个模板函数貌似是GNU的函数,需要包含“algorithm”头文件。详情可见这篇博客__gcd()
  • 也可以采用C++17提供的std::gcd()std::lcm()
  • 据一位朋友指点,还可尝试位运算优化的gcd,可以在搜索引擎中检索“gcd”+“位运算”等关键字获取相关内容。
  • 放几个链接:
    GCD最大公约数
    二进制GCD算法解析
    笔试面试中常见的位运算用法

设计

我希望这个分数类大概能够满足这些功能:

fraction a, b;
cin >> a >> b;
cout << a + b;
a += 1;
a -= b;
a = a + 5;
if (a > b) cout << a;
else cout << b;

总结一下大概就是:基本四则运算、输入输出、大小判断;那么类的声明大概长这个样子:

class fraction
{
   
private:
    int A;  //numerator
    int B;  //denominator
public:
    fraction();
    fraction(int );
    fraction(int ,int );
    fraction(const char* );
    
    void reduce();
    void print();
    bool if_int() const;

    friend istream& operator>> (istream&, fraction&);
    friend ostream& operator<< (ostream&, const fraction&);
    
    void add(const fraction& );
    void add(int );
    void subtract(const fraction& );
    void multiply(const fraction& );
    void divide(const fraction& );
    double value() const;

    //使 分数 与 分数 之间的运算成立
    fraction operator+ (const fraction& );
    fraction operator- (const fraction& );
    fraction operator* (const fraction& );
    fraction operator/ (const fraction& );
    
    void operator+= (const fraction& );
    void operator-= (const fraction& );
    void operator*= (const fraction& );
    void operator/= (const fraction& );
    
    //使 分数 与 整数 之间的运算成立
    fraction operator+ (int );
    fraction operator- (int );
    fraction operator* (int );
    fraction operator/ (int );
    
    void operator+= (int );
    void operator-= (int );
    void operator*= (int );
    void operator/= (int );

    friend fraction operator+ (int, const fraction& );
    friend fraction operator- (int, const fraction& );
    friend fraction operator* (int, const fraction& );
    friend fraction operator/ (int, const fraction& );

    //使大小判断成立
    int compare(const fraction& );
    bool operator== (const fraction& );
    bool operator!= (const fraction& );
    bool operator> (const fraction& );
    bool operator< (const fraction& );
    bool operator>= (const fraction& );
    bool operator<= (const fraction& );

    bool operator== (int);
    bool operator== (double );
    bool operator!= (int );
    bool operator> (int );
    bool operator< (int );
    bool operator>= (int );
    bool operator<= (int );
};

笔者用两个整数,分别存储分子和分母,由此得以保存一个分数(存储时,分数将被约分至最简);如果需要,可以使用long long int型数据。

最大公约数、最小公倍数

由于操作过程中,我们可能需要求分子分母的最大公约数、最小公倍数,所以写两个函数:

int gcd(int x,int y) //辗转相除法求最大公因数
{
   
	int t;
	while (y!=0) {
   
		t=x%y;
		x=y;
		y=t;
	}
	return
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值