分数的实现
#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED
struct Num
{
int numerator; //分子
int denominator; //分母
} ;
class Fraction
{
public:
Fraction();
Fraction(int,int);
~Fraction();
void show();
Fraction operator+(Fraction);
Fraction operator-(Fraction);
Fraction operator*(Fraction);
Fraction operator/(Fraction);
private:
Num number;
int TheMax(int,int);
int TheMin(int,int);
} ;
#endif // FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED
struct Num
{
int numerator; //分子
int denominator; //分母
} ;
class Fraction
{
public:
Fraction();
Fraction(int,int);
~Fraction();
void show();
Fraction operator+(Fraction);
Fraction operator-(Fraction);
Fraction operator*(Fraction);
Fraction operator/(Fraction);
private:
Num number;
int TheMax(int,int);
int TheMin(int,int);
} ;
#endif // FRACTION_H_INCLUDED
#include
"
fraction.h
"
Fraction::Fraction()
{
number.numerator = 1;
number.denominator = 1;
}
Fraction::Fraction( int i, int j)
{
if(j == 0)
{
cout<<"分母不能为0!!!"<<endl;
exit(1);
}
else
{
if(j < 0)
{
i = -i;
j = abs(j);
}
number.numerator = i;
number.denominator = j;
if(i != 0 && j != 0)
{
int max = TheMax(number.numerator,number.denominator);
number.numerator /= max;
number.denominator /= max;
}
}
}
Fraction:: ~ Fraction()
{
}
void Fraction::show()
{
if(number.numerator == 0)
{
cout<<0<<endl;
}
else if(number.denominator == 1)
{
cout<<number.numerator<<endl;
}
else if(number.numerator == number.denominator)
{
cout<<1<<endl;
}
else
{
cout<<number.numerator<<'/'<<number.denominator<<endl;
}
}
Fraction Fraction:: operator + (Fraction i)
{
if(this->number.numerator == 0)
{
return i;
}
else if(i.number.numerator == 0)
{
return *this;
}
else
{
int min = TheMin(this->number.denominator,i.number.denominator);
this->number.numerator = this->number.numerator*(min/this->number.denominator)+i.number.numerator*(min/i.number.denominator);
this->number.denominator = min;
return Fraction(this->number.numerator,this->number.denominator);
}
}
Fraction Fraction:: operator - (Fraction i)
{
if(this->number.numerator == 0)
{
i.number.numerator = -i.number.numerator;
return i;
}
else if(i.number.numerator == 0)
{
return *this;
}
else
{
int min = TheMin(this->number.denominator,i.number.denominator);
this->number.numerator = this->number.numerator*(min/this->number.denominator)-i.number.numerator*(min/i.number.denominator);
this->number.denominator = min;
return Fraction(this->number.numerator,this->number.denominator);
}
}
Fraction Fraction:: operator * (Fraction i)
{
return Fraction(this->number.numerator*i.number.numerator,this->number.denominator*i.number.denominator);
}
Fraction Fraction:: operator / (Fraction i)
{
return Fraction(this->number.numerator*i.number.denominator,this->number.denominator*i.number.numerator);
}
// 最小公倍数
int Fraction::TheMin( int i, int j)
{
i = abs(i);
j = abs(j);
int max,min;
max = (i>j)?i:j;
min = (i>j)?j:i;
for(int a = 1;;a++)
{
if(max*a%min == 0)
{
return max*a;
}
}
}
// 最大公约数
int Fraction::TheMax( int i, int j)
{
i = abs(i);
j = abs(j);
int max,min,mid;
max = (i>j)?i:j;
min = (i>j)?j:i;
if(max%min == 0)
{
return min;
}
for(int a = 2;;a++)
{
if(min%a == 0)
{
return TheMax(max,min/a);
}
}
}
Fraction::Fraction()
{
number.numerator = 1;
number.denominator = 1;
}
Fraction::Fraction( int i, int j)
{
if(j == 0)
{
cout<<"分母不能为0!!!"<<endl;
exit(1);
}
else
{
if(j < 0)
{
i = -i;
j = abs(j);
}
number.numerator = i;
number.denominator = j;
if(i != 0 && j != 0)
{
int max = TheMax(number.numerator,number.denominator);
number.numerator /= max;
number.denominator /= max;
}
}
}
Fraction:: ~ Fraction()
{
}
void Fraction::show()
{
if(number.numerator == 0)
{
cout<<0<<endl;
}
else if(number.denominator == 1)
{
cout<<number.numerator<<endl;
}
else if(number.numerator == number.denominator)
{
cout<<1<<endl;
}
else
{
cout<<number.numerator<<'/'<<number.denominator<<endl;
}
}
Fraction Fraction:: operator + (Fraction i)
{
if(this->number.numerator == 0)
{
return i;
}
else if(i.number.numerator == 0)
{
return *this;
}
else
{
int min = TheMin(this->number.denominator,i.number.denominator);
this->number.numerator = this->number.numerator*(min/this->number.denominator)+i.number.numerator*(min/i.number.denominator);
this->number.denominator = min;
return Fraction(this->number.numerator,this->number.denominator);
}
}
Fraction Fraction:: operator - (Fraction i)
{
if(this->number.numerator == 0)
{
i.number.numerator = -i.number.numerator;
return i;
}
else if(i.number.numerator == 0)
{
return *this;
}
else
{
int min = TheMin(this->number.denominator,i.number.denominator);
this->number.numerator = this->number.numerator*(min/this->number.denominator)-i.number.numerator*(min/i.number.denominator);
this->number.denominator = min;
return Fraction(this->number.numerator,this->number.denominator);
}
}
Fraction Fraction:: operator * (Fraction i)
{
return Fraction(this->number.numerator*i.number.numerator,this->number.denominator*i.number.denominator);
}
Fraction Fraction:: operator / (Fraction i)
{
return Fraction(this->number.numerator*i.number.denominator,this->number.denominator*i.number.numerator);
}
// 最小公倍数
int Fraction::TheMin( int i, int j)
{
i = abs(i);
j = abs(j);
int max,min;
max = (i>j)?i:j;
min = (i>j)?j:i;
for(int a = 1;;a++)
{
if(max*a%min == 0)
{
return max*a;
}
}
}
// 最大公约数
int Fraction::TheMax( int i, int j)
{
i = abs(i);
j = abs(j);
int max,min,mid;
max = (i>j)?i:j;
min = (i>j)?j:i;
if(max%min == 0)
{
return min;
}
for(int a = 2;;a++)
{
if(min%a == 0)
{
return TheMax(max,min/a);
}
}
}
#include
<
math.h
>
#include < cctype >
#include < iostream >
#include < string >
using namespace std;
#include " fraction.cpp "
int main()
{
Fraction fen(1,2);
Fraction fen1(2,3);
(fen-fen1).show();
system("pause");
return 0;
}
#include < cctype >
#include < iostream >
#include < string >
using namespace std;
#include " fraction.cpp "
int main()
{
Fraction fen(1,2);
Fraction fen1(2,3);
(fen-fen1).show();
system("pause");
return 0;
}