[C++] Rational number operations

Problem description: A
rational number is a number that can be converted into a fraction. For example, 2/3, 533/920, and -12/49 are all rational numbers, but irrational numbers. In C++, rational numbers are not pre-defined. You can define a rational number class when needed, and store the numerator and denominator of the rational number in two integer variables. Various operations on rational numbers can be implemented with overloaded operators.

Basic requirements:
define and implement a rational number class, perform arithmetic operations on rational numbers through the overloaded operators +, -, *, /, and use the overloaded operator == to determine whether two rational numbers are equal. Write an optimization function, its role is to reduce the common denominator of rational numbers, that is, there is no common divisor (except 1) between the numerator and denominator of the rational number stored. In addition, we must define a function that converts rational numbers to real numbers, plus a constructor and a rational number output function.

Test data:
In the application, create a number of rational number objects, make the value of each rational number object different through the constructor with parameters, and then perform various operations, output the results of the operations, and verify their correctness.
Realization hint: If
there are two rational numbers a/b and c/d, then there are:
(1) Rational number addition numerator = a d + b c; denominator = b d
(2) Rational number subtraction numerator = a
d-b c; denominator =b d
(3) Multiplying rational numbers with numerator = a c; denominator = b d
(4) Dividing rational numbers with numerator = a d; denominator = b c
(5) Overload insertion (<<) and extraction (>>) operations Symbol, which makes it possible to directly input and output rational numbers.

Set the rational number input format:
integer 1 integer 2 // integer 1 is the numerator, integer 2 is the denominator. The
rational number output format is:
numerator/denominator

The optimization function should be executed when the rational number object is created, and it needs to be executed after performing various other operations, so as to ensure that the stored rational numbers are always optimal. For judging whether two rational numbers are equal, because the rational numbers are optimized after performing various operations, to judge whether two rational numbers are equal, it is only necessary to judge whether the numerator and denominator of the two are equal.

RationalNumber.h

#pragma once
class RationalNumber{
    
    
private:
	int numerator;
	int denominator;
public:
	RationalNumber();
	RationalNumber(int, int);
	int gcd(int, int);
	void set(int, int);
	void print();
	void add(RationalNumber);
	void sub(RationalNumber);
	void mul(RationalNumber);
	void div(RationalNumber);
	bool operator==(RationalNumber);
};

RationalNumber.cpp

#include "RationalNumber.h"
#include<iostream>
using namespace std;
RationalNumber::RationalNumber(){
    
    }
RationalNumber::RationalNumber(int numerator, int denominator) {
    
    
	int temp = gcd(numerator, denominator);
	this->numerator = numerator / temp;
	this->denominator = denominator / temp;
}
int RationalNumber::gcd(int n,int m) {
    
    
	int t;
	if (n < m) {
    
    
		t = n;
		n = m;
		m = t;
	}
	if (n % m == 0) {
    
    
		return m;
	}
	else {
    
    
		return gcd(m, n % m);
	}
}
void RationalNumber::set(int n, int d)
{
    
    
	numerator = n;
	denominator = d;
	int temp = gcd(numerator, denominator);
	numerator /= temp;
	denominator /= temp;
}
void RationalNumber::print() {
    
    
	cout << numerator << "/" << denominator << "\n";
}

void RationalNumber::add(RationalNumber r)
{
    
    
	numerator = numerator * r.denominator + denominator * r.numerator;
	denominator = r.denominator * denominator;
	int temp = gcd(numerator, denominator);
	numerator /= temp;
	denominator /= temp;
}

void RationalNumber::sub(RationalNumber r)
{
    
    
	numerator = numerator * r.denominator - denominator * r.numerator;
	denominator = r.denominator * denominator;
	int temp = gcd(numerator, denominator);
	numerator /= temp;
	denominator /= temp;
}

void RationalNumber::mul(RationalNumber r)
{
    
    
	numerator *= r.numerator;
	denominator *= r.denominator;
	int temp = gcd(numerator, denominator);
	numerator /= temp;
	denominator /= temp;
}

void RationalNumber::div(RationalNumber r)
{
    
    
	numerator *= r.denominator;
	denominator *= r.numerator;
	int temp = gcd(numerator, denominator);
	numerator /= temp;
	denominator /= temp;
}

bool RationalNumber::operator==(RationalNumber r)
{
    
    
	if (numerator == r.numerator &&
		denominator == r.denominator) {
    
    
		return true;
}
	return false;
}

test.cpp

#include <iostream>
using namespace std;
#include"RationalNumber.h";
int main() {
    
    
	RationalNumber r1, r2;
	r1.set(7,26);
	r2.set(6, 26);
	r1.print();
	r2.print();
	r1.add(r2);
	r1.print();
	bool flag =  r1 == RationalNumber(1, 2);
	if (flag) {
    
    
		cout << "Yes";
	}
	else
	{
    
    
		cout << "No";
	}
}

Guess you like

Origin blog.csdn.net/weixin_48180029/article/details/113001558