#include<iostream>
using namespace std;
int gcd(int a,int b);
class Fraction
{
public:
Fraction();
Fraction(int a,int b);
Fraction(const Fraction &c); //复制
~Fraction();
Fraction operator+(const Fraction &c);
Fraction operator-(const Fraction &c);
Fraction operator*(const Fraction &c);
Fraction operator/(const Fraction &c);
Fraction operator+=(const Fraction &c);
Fraction operator-=(const Fraction &c);
//friend ostream& operator<<(ostream& i,Fraction &b);
//friend ostream& operator<<(ostream& o,Fraction &b);
Fraction operator*(const int a); //分数与整数的乘法
Fraction operator/(const int a); //分数与整数的除法
friend Fraction doubleChange(double a); //将实数转换为分数
double fractionChange(); //将分数转换为实数
Fraction operator-();
Fraction operator++();
Fraction operator++(int);
bool operator==(const Fraction &c);
bool operator!=(const Fraction &c);
bool operator>(const Fraction &c);
bool operator<(const Fraction &c);
void Show();
void Input();
private:
int num; //分子
int den; //分母
};
Fraction::Fraction() //默认构造函数
{
num=0;
}
Fraction::Fraction(int a,int b) //带参数的构造函数
{
num=a;
den=b;
}
Fraction::Fraction(const Fraction &c) //复制构造函数
{
num=c.num;
den=c.den;
}
Fraction::~Fraction()
{
//cout<<"执行析构函数"<<endl;
}
void Fraction::Show() //输出
{
int a=0,b=0;
a=num/gcd(num,den);
b=den/gcd(num,den);
if(a==0)
cout<<0<<endl;
else if(b==1)
cout<<a<<endl;
else if(a>0&&b<0)
cout<<-a<<"/"<<-b<<endl;
else
cout<<a<<"/"<<b<<endl;
}
void Fraction::Input() // 输入
{
cin>>num>>den;
}
Fraction Fraction::operator+(const Fraction &c) //分数的加法
{
return Fraction(den*c.num+num*c.den,den*c.den);
}
Fraction Fraction::operator-(const Fraction &c) //分数的减法
{
return Fraction(c.den*num-c.num*den,den*c.den);
}
Fraction Fraction::operator*(const Fraction &c) //分数的乘法
{
return Fraction(num*c.num,den*c.den);
}
Fraction Fraction::operator/(const Fraction &c) //分数的除法
{
return Fraction(num*c.den,den*c.num);
}
Fraction Fraction::operator-() //负号运算符
{
return Fraction(-num,den);
}
Fraction Fraction::operator++() //自加(前置)
{
num+=den;
return Fraction(num,den);
}
Fraction Fraction::operator++(int) //自加(后置)
{
int c=0;
c=num;
num+=den;
return Fraction(c,den);
}
bool Fraction::operator==(const Fraction &c) //相等判断
{
double num1=0.0,num2=0.0;
num1=(double)num/den;
num2=(double)c.num/c.den;
if(num1==num2)
return true;
else
return false;
}
bool Fraction::operator!=(const Fraction &c) //不等判断
{
double num1=0.0,num2=0.0;
num1=(double)num/den;
num2=(double)c.num/c.den;
if(num1!=num2)
return true;
else
return false;
}
bool Fraction::operator>(const Fraction &c) //大小判断
{
double num1=0.0,num2=0.0;
num1=(double)num/den;
num2=(double)c.num/c.den;
if(num1>num2)
return true;
else
return false;
}
bool Fraction::operator<(const Fraction &c) //大小判断
{
double num1=0.0,num2=0.0;
num1=(double)num/den;
num2=(double)c.num/c.den;
if(num1<num2)
return true;
else
return false;
}
Fraction Fraction::operator+=(const Fraction &c) //+=运算符
{
num=num*c.den+den*c.num;
den=den*c.den;
return Fraction(num,den);
}
Fraction Fraction::operator-=(const Fraction &c) //-=运算符
{
num=num*c.den-den*c.num;
den=den*c.den;
return Fraction(num,den);
}
Fraction Fraction::operator*(const int a) //分数和整数的乘法
{
return Fraction(num*a,den);
}
Fraction Fraction::operator/(const int a) //分数和整数的除法
{
return Fraction(num,den*a);
}
double Fraction::fractionChange() //分数转换为实数
{
return (double)num/den;
}
Fraction doubleChange(double a) //实数转换为分数
{
int jc=1;
for(int i=0;;i++)
{
if((int)a==a)
break;
else
a*=10;
}
if(i==0)
return Fraction(a,1);
else
{
for(int j=1;j<=i;j++)
jc*=10;
return Fraction(a,jc);
}
}
int main()
{
Fraction a1(7,2),a2(3,-2),a3(a1),a4,a5,a6,a8(12,13),a9(2,11),a10; //下面的全是测试数据
double x=0.0;
cout<<"a1: ";
a1.Show();
cout<<endl<<"a2: ";
a2.Show();
cout<<endl<<"a3: ";
a3.Show();
cout<<endl<<"a4: ";
a4.Show();
cout<<endl;
cout<<"a1+a2= ";
a4=a1+a2;
a4.Show();
cout<<"a1-a2= ";
a4=a1-a2;
a4.Show();
cout<<"a1*a2= ";
a4=a1*a2;
a4.Show();
cout<<"a1/a2= ";
a4=a1/a2;
a4.Show();
cout<<">>a5=-a1 a5: ";
a5=-a1;
a5.Show();
cout<<"++a1: ";
a5=++a1;
a5.Show();
cout<<"a1++: ";
a5=a1++;
a5.Show();
cout<<"a1: ";
a1.Show();
cout<<"Input num and den\n";
a6.Input();
cout<<"Output : ";
a6.Show();
cout<<"\n\n\n"<<"(a1==a2)?\t";
if(a1==a2)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
cout<<"(a1!=a2)?\t";
if(a1!=a2)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
cout<<"(a1>a2)?\t";
if(a1>a2)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
cout<<"(a1<a2)?\t";
if(a1<a2)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
cout<<"\n\n";
cout<<">>a2+=a3 a2: ";
a2+=a3;
a2.Show();
cout<<">>a2-=a3 a2: ";
a2-=a3;
a2.Show();
cout<<"分数与整数相乘\na8: ";
a8.Show();
cout<<"a8*4: ";
a6=a8*4;
a6.Show();
cout<<"分数与整数相除\na8: ";
a8.Show();
cout<<"a8/4: ";
a6=a8/4;
a6.Show();
cout<<"将a9(2/11)分数转换"<<endl;
x=a9.fractionChange();
cout<<"2/11= "<<x<<endl;
double y=0.21;
cout<<"将x(0.21)转换为分数\n";
a10=doubleChange(y);
a10.Show();
return 0;
}
int gcd(int a,int b) //最大公约数
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
初学cpp<类+运算符重载>
最新推荐文章于 2022-02-22 09:27:52 发布