1. ホーム
  2. c++

分数クラスのC++実装

2022-02-15 17:03:07
/*
 * fraction.h
 * This fraction class will store only the sign in the numerator position after entering a fraction, and the denominator will be positive. After addition, subtraction, multiplication and division operations, the fraction will be automatically divided, or you can perform the division operation yourself.
 * Created on: November 18, 2016
 *  
 */

#ifndef FRACTION_H_
#define FRACTION_H_


class fraction{

private:
	int numerator; //define numerator
	int denominator; //define denominator
	int maxFactor(int nr,int dr); //find the maximum convention

public:
	fraction(int numerator, int denominator = 1); //constructor
	void print() const; //output
	fraction operator+(const fraction& rv) const; //overload +
	fraction operator-(const fraction& rv) const; //overload -
	fraction operator*(const fraction& rv) const; //overload *
	fraction operator/(const fraction& rv) const; //overload /
	fraction operator-(); // overload the monomial operator -
	fraction& operator=(const fraction& rv); //overload =
	bool operator>(const fraction& rv) const; //overload >
	bool operator>=(const fraction& rv) const; //reload>=
	bool operator<(const fraction& rv) const; //overload<
	bool operator<=(const fraction& rv) const; //reload<=
	bool operator==(const fraction& rv) const; //overload==
	bool operator!=(const fraction& rv) const; //overload! =
	void fractionReduction(); //approximate fraction
	void reciprocal(); //reverse
	~fraction(); //destructor
};

//handle the case when the int type is on the left side of the binomial operator, on the right side it can be automatically substituted by the compiler into the constructor
const fraction operator+(int, const fraction&);
const fraction operator-(int, const fraction&);
const fraction operator*(int, const fraction&);
const fraction operator/(int, const fraction&);
const bool operator>(int, const fraction&);
const bool operator>=(int, const fraction&);
const bool operator<(int, const fraction&);
const bool operator<=(int, const fraction&);
const bool operator==(int, const fraction&);
const bool operator!=(int, const fraction&);

#endif /* FRACTION_H_ */

// ============================================================================
// Name : fraction.cpp
// Author : 
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
// ============================================================================

#include <iostream>
#include "fraction.h"
using namespace std;

#define ABS(x) ((x>0)?x:-x)

const fraction operator+(int x, const fraction& rv)
{
	return fraction(x) + rv;
}

const fraction operator-(int x, const fraction& rv)
{
	return fraction(x) - rv;
}

const fraction operator*(int x, const fraction& rv)
{
	return fraction(x) * rv;
}

const fraction operator/(int x, const fraction& rv)
{
	return fraction(x) / rv;
}

const bool operator>(int x, const fraction& rv)
{
	return fraction(x) > rv;
}

const bool operator>=(int x, const fraction& rv)
{
	return fraction(x) >= rv;
}

const bool operator<(int x, const fraction& rv)
{
	return fraction(x) < rv;
}

const bool operator<=(int x, const fraction& rv)
{
	return fraction(x) <= rv;
}

const bool operator==(int x, const fraction& rv)
{
	return fraction(x) == rv;
}

const bool operator!==(int x, const fraction& rv)
{
	return fraction(x) ! = rv;
}

fraction::fraction(int numerator, int denominator)
{
	if((numerator>=0 && denominator>0)|| (numerator<=0 && denominator<0))
	{
		this->numerator = ABS(numerator);
		this->denominator = ABS(denominator);
	}else{
		this->numerator = -ABS(numerator);
		this->denominator = ABS(denominator);
	}

}

fraction fraction::operator+(const fraction& rv) const
{
	fraction a = fraction(numerator*rv.denominator + rv.numerator*denominator, denominator*rv.denominator);
	a.fractionReduction();
	return a;
}

fraction fraction::operator-(const fraction& rv) const
{
	fraction a = fraction(numerator*rv.denominator - rv.numerator*denominator, denominator*rv.denominator);
	a.fractionReduction();
	return a;
}

fraction fraction::operator*(const fraction& rv) const
{
	fraction a = fraction(numerator * rv.numerator, denominator * rv.denominator);
	a.fractionReduction();
	return a;
}

fraction fraction::operator/(const fraction& rv) const
{
	fraction a = fraction(numerator * rv.denominator, denominator * rv.numerator);
	a.fractionReduction();
	return a;
}

fraction fraction::operator-()
{
	numerator = -numerator;
	return *this;
}

fraction& fraction::operator=(const fraction& rv)
{
	numerator=rv.numerator;
	denominator=rv.denominator;
	return *this;
}

bool fraction::operator>(const fraction& rv) const
{
	if(numerator*rv.denominator > rv.numerator*denominator)
			return true;
		else
			return false;
}

bool fraction::operator>=(const fraction& rv) const
{
	if(numerator*rv.denominator >= rv.numerator*denominator)
		return true;
	else
		return false;
}

bool fraction::operator<(const fraction& rv) const
{
	if(numerator*rv.denominator < rv.numerator*denominator)
		return true;
	else
		return false;
}

bool fraction::operator<=(const fraction& rv) const
{
	if(numerator*rv.denominator <= rv.numerator*denominator)
		return true;
	else
		return false;
}

bool fraction::operator==(const fraction& rv) const
{
	if(numerator*rv.denominator == rv.numerator*denominator)
		return true;
	else
		return false;
}

bool fraction::operator!=(const fraction& rv) const
{
	if(numerator*rv.denominator ! = rv.numerator*denominator)
		return true;
	else
		return false;
}

void fraction::print() const
{
	if(denominator==1){
		cout << numerator<< endl;
	}
	else
	    cout << numerator << "/" << denominator << endl;
}

int fraction::maxFactor(int nr,int dr)
{
 int th=nr,tl=dr;
 if(nr<dr)
   {
	  th=dr;
      tl=nr;
   }
 if(th%tl==0)
    return tl;
 else
    return maxFactor(tl,th%tl);
}

void fraction::fractionReduction()
{
	int a = maxFactor(ABS(denominator), ABS(numerator));
	if(a>1){
		denominator /= a;
		numerator /= a;
	}

	if((numerator>=0 && denominator>0)|| (numerator<=0 && denominator<0))
		{
			numerator = ABS(numerator);
			denominator = ABS(denominator);
		}else{
			numerator = -ABS(numerator);
			denominator = ABS(denominator);
	 }
}

void fraction::reciprocal()
{
	if(numerator>=0)
		{
		    int temp = numerator;
			numerator = denominator;
			denominator = temp;
		}else{
			int temp = numerator;
			numerator = -denominator;
			denominator = ABS(temp);
	}
}

fraction::~fraction()
{
}










// ============================================================================
// Name : fraction.cpp
// Author : 
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
// ============================================================================

#include <iostream>
#include "fraction.h"
using namespace std;

#define ABS(x) ((x>0)?x:-x)

const fraction operator+(int x, const fraction& rv)
{
	return fraction(x) + rv;
}

const fraction operator-(int x, const fraction& rv)
{
	return fraction(x) - rv;
}

const fraction operator*(int x, const fraction& rv)
{
	return fraction(x) * rv;
}

const fraction operator/(int x, const fraction& rv)
{
	return fraction(x) / rv;
}

const bool operator>(int x, const fraction& rv)
{
	return fraction(x) > rv;
}

const bool operator>=(int x, const fraction& rv)
{
	return fraction(x) >= rv;
}

const bool operator<(int x, const fraction& rv)
{
	return fraction(x) < rv;
}

const bool operator<=(int x, const fraction& rv)
{
	return fraction(x) <= rv;
}

const bool operator==(int x, const fraction& rv)
{
	return fraction(x) == rv;
}

const bool operator!==(int x, const fraction& rv)
{
	return fraction(x) ! = rv;
}

fraction::fraction(int numerator, int denominator)
{
	if((numerator>=0 && denominator>0)|| (numerator<=0 && denominator<0))
	{
		this->numerator = ABS(numerator);
		this->denominator = ABS(denominator);
	}else{
		this->numerator = -ABS(numerator);
		this->denominator = ABS(denominator);
	}

}

fraction fraction::operator+(const fraction& rv) const
{
	fraction a = fraction(numerator*rv.denominator + rv.numerator*denominator, denominator*rv.denominator);
	a.fractionReduction();
	return a;
}

fraction fraction::operator-(const fraction& rv) const
{
	fraction a = fraction(numerator*rv.denominator - rv.numerator*denominator, denominator*rv.denominator);
	a.fractionReduction();
	return a;
}

fraction fraction::operator*(const fraction& rv) const
{
	fraction a = fraction(numerator * rv.numerator, denominator * rv.denominator);
	a.fractionReduction();
	return a;
}

fraction fraction::operator/(const fraction& rv) const
{
	fraction a = fraction(numerator * rv.denominator, denominator * rv.numerator);
	a.fractionReduction();
	return a;
}

fraction fraction::operator-()
{
	numerator = -numerator;
	return *this;
}

fraction& fraction::operator=(const fraction& rv)
{
	numerator=rv.numerator;
	denominator=rv.denominator;
	return *this;
}

bool fraction::operator>(const fraction& rv) const
{
	if(numerator*rv.denominator > rv.numerator*denominator)
			return true;
		else
			return false;
}

bool fraction::operator>=(const fraction& rv) const
{
	if(numerator*rv.denominator >= rv.numerator*denominator)
		return true;
	else
		return false;
}

bool fraction::operator<(const fraction& rv) const
{
	if(numerator*rv.denominator < rv.numerator*denominator)
		return true;
	else
		return false;
}

bool fraction::operator<=(const fraction& rv) const
{
	if(numerator*rv.denominator <= rv.numerator*denominator)
		return true;
	else
		return false;
}

bool fraction::operator==(const fraction& rv) const
{
	if(numerator*rv.denominator == rv.numerator*denominator)
		return true;
	else
		return false;
}

bool fraction::operator!=(const fraction& rv) const
{
	if(numerator*rv.denominator ! = rv.numerator*denominator)
		return true;
	else
		return false;
}

void fraction::print() const
{
	if(denominator==1){
		cout << numerator<< endl;
	}
	else
	    cout << numerator << "/" << denominator << endl;
}

int fraction::maxFactor(int nr,int dr)
{
 int th=nr,tl=dr;
 if(nr<dr)
   {
	  th=dr;
      tl=nr;
   }
 if(th%tl==0)
    return tl;
 else
    return maxFactor(tl,th%tl);
}

void fraction::fractionReduction()
{
	int a = maxFactor(ABS(denominator), ABS(numerator));
	if(a>1){
		denominator /= a;
		numerator /= a;
	}

	if((numerator>=0 && denominator>0)|| (numerator<=0 && denominator<0))
		{
			numerator = ABS(numerator);
			denominator = ABS(denominator);
		}else{
			numerator = -ABS(numerator);
			denominator = ABS(denominator);
	 }
}

void fraction::reciprocal()
{
	if(numerator>=0)
		{
		    int temp = numerator;
			numerator = denominator;
			denominator = temp;
		}else{
			int temp = numerator;
			numerator = -denominator;
			denominator = ABS(temp);
	}
}

fraction::~fraction()
{
}