首页 > 编程语言 >【C++】实现一个有理数类

【C++】实现一个有理数类

时间:2022-10-06 22:11:20浏览次数:59  
标签:有理数 den 实现 C++ int num Rational operator include

有理数:指所有可以用两个整数的商来表示的数

image

image

Rational.h

Rational类的接口

#ifndef _RATIONAL_H
#define _RATIONAL_H

#include <iostream>
#include <string>

class Rational {
public:
    Rational();
    Rational(int n);
    Rational(int x, int y);

    std::string toString();

    friend Rational operator+(Rational r1, Rational r2);
    friend Rational operator-(Rational r1, Rational r2);
    friend Rational operator*(Rational r1, Rational r2);
    friend Rational operator/(Rational r1, Rational r2);

private:
    int num; // 分子
    int den; // 分母


};

std::ostream &operator<<(std::ostream & os, Rational rat);

Rational operator+(Rational r1, Rational r2);
Rational operator-(Rational r1, Rational r2);
Rational operator*(Rational r1, Rational r2);
Rational operator/(Rational r1, Rational r2);

#endif //LEETCODE_RATIONAL_H

Rational.cpp

Rational类的实现

//
// Created by NFYD on 10/6/2022.
//

#include "rational.h"

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int gcd(int x, int y);

Rational::Rational() {
    num = 0;
    den = 1;
}

Rational::Rational(int n) {
    num = n;
    den = 1;
}

Rational::Rational(int x, int y) {
    if (y == 0) printf("Rational: Division by zero!");
    if (x == 0) {
        num = 0;
        den = 1;
    } else {
        int g = gcd(abs(x), abs(y));
        num = x / g;
        den = abs(y) / g;
        if (y < 0) num = -num;
    }
}

string Rational::toString() {
    if (den == 1) {
        return integerToString(num);
    } else {
        return integerTostring(num) + "/" + integrToString(den);
    }
}

ostream & operator<<(ostream & os, Rational rat) {
    return os << rat.toString();
}

Rational operator+(Rational r1, Rational r2) {
    return Rational(r1.num * r2.den + r2.num * r1.den, r1 .den * r2.den);
}

Rational operator-(Rational r1, Rational r2) {
    return Rational(r1.num * r2.den - r2.num * r1.den, r1 .den * r2.den);
}

Rational operator*(Rational r1, Rational r2) {
    return Rational(r1.num * r2.num, r1.den * r2.den);
}

Rational operator/(Rational r1, Rational r2) {
    return Rational(r1.num * r2.den, r1.den * r2.num);
}

int gcd(int x, int y) {
    int r = x % y;
    while (r != 0) {
        x = y;
        y = r;
        r = x % y;
    }
    return y;
}

/* gcd也可以写成这种
int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}
*/

标签:有理数,den,实现,C++,int,num,Rational,operator,include
From: https://www.cnblogs.com/Tshaxz/p/16758660.html

相关文章

  • Java二分查找代码实现
    Java二分查找代码实现及原理简要分析代码原理描述前提:已经有一个排好序的数组(否则需要先排序)定义左边界left,右边界right,确定搜索范围,循环执行二分查找(第3、4步骤)......
  • More Effective C++中文版 pdf
    高清扫描版下载链接:https://pan.baidu.com/s/1XpmUcUW-3tCqGlmZUqNVMg点击这里获取提取码 ......
  • 多种PID算法用C语言来实现
    原文链接:https://blog.csdn.net/Nirvana_Tai/article/details/105409311,随后整理验证,再补充(一)前言  PID算法在工业应用中随处可见。大学期间,想做各类科创也少不了PI......
  • fibnacci数列递归实现
    fibnacci数列递归实现什么是fibnacci数列斐波那契数列(Fibonaccisequence),又称“黄金分割”数列,因数学家莱昂纳多·斐波那契(LeonardoFibonacci)以兔子繁殖为例子而引入,故......
  • Fibnacci数列递归实现
    1.什么是Fibnacci数列?斐波那契数列(Fibonaccisequence),又称黄金分割数列,因数学家莱昂纳多·斐波那契(LeonardoFibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指......
  • C++ 条件变量(condition_variable)
    生产者消费者模式的代码(以下代码参考链接):#include<iostream>#include<thread>#include<mutex>#include<queue>#include<windows.h>#include<condition_variabl......
  • 实现进制转化伪代码
    伪代码如下Write"Enterthenewbase"ReadnewBaseWrite"Enterthenumbertobeconverted"ReaddecimalNumberSetquotientto1WHILE......
  • C++里的花括号{},块,作用域
    在C/C++中大括号指明了变量的作用域,在大括号内声明的局部变量其作用域自变量声明开始,到大括号之后终结,举例如下:intmain(){inta=0;{intb=0;......
  • c++小游戏
    #include<bits/stdc++.h>#include<windows.h>#include<stdio.h>#include<conio.h>#include<time.h>usingnamespacestd;intmain(){MessageBox(NULL,TEXT("......
  • STN在mnist数据集上的实现
    stn在mnist上的实现个人博客-https://cxy-sky.github.io/代码参考来源:PyTorch框架实战系列(3)——空间变换器网络STN_DanielYuz的博客-CSDN博客理论:Pytorch中的仿射变......