展开全部
#include<iostream>
using namespace std;
class Fraction
{
private:
int numtor;
int dentor;
public:
Fraction();
Fraction(int n,int d){numtor=n;dentor=d;}
~Fraction(){cout<<"constructor"<<endl;};
Fraction operator-(); //运算符-()用于对数值取负
Fraction operator+();//运算符+()用于对保持数值不变
Fraction operator!();//运算符!()用于对反转数值
friend Fraction operator+(Fraction&fra1,Fraction&fra2);//运算符+()用于相加两个Faraction对象
friend Fraction operator*(Fraction&fra1,Fraction&fra2);//运算符*()用于相乘两个Faraction对象
void display();
};
Fraction::Fraction()
{
numtor=0;
dentor=1;
}
Fraction Fraction::operator-()//运算符-()用于对数值取负
{
int d=dentor;
int n=(-numtor);
Fraction f(n,d);
return f;
}
Fraction Fraction:: operator+()//运算符+()用于对保持数值不变
{
int n=numtor;
int d=dentor;
Fraction f(n,d);
return f;
}
Fraction Fraction::operator!()//运算符!()用于对反转数值
{
int n=dentor;
int d=numtor;
Fraction f(n,d);
return f;
}
void Fraction::display()
{
cout<<numtor<<"/"<<dentor<<endl;
}
//*****************************
Fraction operator+(Fraction& fra1,Fraction& fra2)//运算符+()用于相加两个Faraction对象
{
int n=fra1.numtor*fra2.dentor+fra1.dentor*fra2.numtor;
int d=fra1.dentor*fra2.dentor ;
Fraction f(n,d);
return f;
}
Fraction operator*(Fraction& fra1,Fraction& fra2)//运算符*()用于相乘两个Faraction对象
{
int n=fra1.numtor*fra2.numtor ;
int d=fra1.dentor*fra2.dentor;
Fraction f(n,d);
return f;
}
int main()
{
Fraction fra1(5,8);
Fraction fra2(2,7);
Fraction fra3;
Fraction fra4;
(-fra1).display();
(+fra1).display();
(!fra1).display();
//fra3=fra1+fra2;
//fra4=fra1*fra2;
(fra1+fra2).display();
(fra1*fra2).display();
system("pause");
return 0;
}
using namespace std;
class Fraction
{
private:
int numtor;
int dentor;
public:
Fraction();
Fraction(int n,int d){numtor=n;dentor=d;}
~Fraction(){cout<<"constructor"<<endl;};
Fraction operator-(); //运算符-()用于对数值取负
Fraction operator+();//运算符+()用于对保持数值不变
Fraction operator!();//运算符!()用于对反转数值
friend Fraction operator+(Fraction&fra1,Fraction&fra2);//运算符+()用于相加两个Faraction对象
friend Fraction operator*(Fraction&fra1,Fraction&fra2);//运算符*()用于相乘两个Faraction对象
void display();
};
Fraction::Fraction()
{
numtor=0;
dentor=1;
}
Fraction Fraction::operator-()//运算符-()用于对数值取负
{
int d=dentor;
int n=(-numtor);
Fraction f(n,d);
return f;
}
Fraction Fraction:: operator+()//运算符+()用于对保持数值不变
{
int n=numtor;
int d=dentor;
Fraction f(n,d);
return f;
}
Fraction Fraction::operator!()//运算符!()用于对反转数值
{
int n=dentor;
int d=numtor;
Fraction f(n,d);
return f;
}
void Fraction::display()
{
cout<<numtor<<"/"<<dentor<<endl;
}
//*****************************
Fraction operator+(Fraction& fra1,Fraction& fra2)//运算符+()用于相加两个Faraction对象
{
int n=fra1.numtor*fra2.dentor+fra1.dentor*fra2.numtor;
int d=fra1.dentor*fra2.dentor ;
Fraction f(n,d);
return f;
}
Fraction operator*(Fraction& fra1,Fraction& fra2)//运算符*()用于相乘两个Faraction对象
{
int n=fra1.numtor*fra2.numtor ;
int d=fra1.dentor*fra2.dentor;
Fraction f(n,d);
return f;
}
int main()
{
Fraction fra1(5,8);
Fraction fra2(2,7);
Fraction fra3;
Fraction fra4;
(-fra1).display();
(+fra1).display();
(!fra1).display();
//fra3=fra1+fra2;
//fra4=fra1*fra2;
(fra1+fra2).display();
(fra1*fra2).display();
system("pause");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询