Fraction类实现对分数的+ - * / << >>重载运算
Problem description:
create a class called Fraction with 2 private fields Numerator, Denominator. And a public constructor that sets Numerator and Denominator to 1 by default.
class Fraction {
int numerator, denominator;
public:
....
};
1、overload + - * /
2、overload ==、!=、<、<=、>、>=
3、overload output/input operator << and >>
后缀代码:
//StudybarCommentBegin
int main(int argc, char *argv[]) {
Fraction a(1),b(1,3);
cin>>a>>b;
cout << "a= " << a << ", b = " << b << endl;
cout << a << " + " << b << " = " << a + b << endl;
cout << a << " - " << b << " = " << a - b << endl;
cout << a << " * " << b << " = " << a * b << endl;
cout << a << " / " << b << " = " << a / b << endl;
cout << "a == b is " << (a == b) << endl;
cout << "a != b is " << (a != b) << endl;
cout << "a <= b is " << (a <= b) << endl;
cout << "a >= b is " << (a >= b) << endl;
cout << "a < b is " << (a < b) << endl;
cout << "a > b is " << (a > b) << endl;
return 1;
}
//StudybarCommentEnd
---------------------------------------------------------------------
Sample input:
1 2
3 4
Sample output
a= 1/2, b = 3/4
1/2 + 3/4 = 5/4
1/2 - 3/4 = -1/4
1/2 * 3/4 = 3/8
1/2 / 3/4 = 2/3
a == b is 0
a != b is 1
a <= b is 1
a >= b is 0
a < b is 1
a > b is 0
Sample input:
2 4
3 4
Sample output:
a= 1/2, b = 3/4
1/2 + 3/4 = 5/4
1/2 - 3/4 = -1/4
1/2 * 3/4 = 3/8
1/2 / 3/4 = 2/3
a == b is 0
a != b is 1
a <= b is 1
a >= b is 0
a < b is 1
a > b is 0
Answer:
/*
----------------------------------------------------
代码思想:
1.将两个分数彻底通分,即两个分母相同(如:1/2与3/4==>4/8与6/8)
2.对分子进行加减乘除
3.最后的结果传入<<重载,通过最大公约数进行化简
*/
#include<iostream>
using namespace std;
class Fraction
{
public:
Fraction(int aa = 1, int bb = 1) :a(aa), b(bb) {};
~Fraction();
Fraction operator+(Fraction x);
Fraction operator-(Fraction x);
Fraction operator*(Fraction x);
Fraction operator/(Fraction x);
int operator>(Fraction x);
int operator<(Fraction x);
int operator==(Fraction x);
int operator!=(Fraction x);
int operator>=(Fraction x);
int operator<=(Fraction x);
friend istream& operator >>(istream& mycin, Fraction& z);
friend ostream& operator <<(ostream& mycout, const Fraction& z);
private:
int a, b;
};
Fraction::~Fraction()
{
}
Fraction Fraction::operator+(Fraction x)
{
Fraction tmp;
tmp.a = this->a * x.b + x.a * this->b;
tmp.b = this->b * x.b;
return tmp;
}
Fraction Fraction::operator-(Fraction x)
{
Fraction tmp;
tmp.a = this->a * x.b - x.a * this->b;
tmp.b = this->b * x.b;
return tmp;
}
Fraction Fraction::operator*(Fraction x)
{
Fraction tmp;
tmp.a = this->a * x.a;
tmp.b = this->b * x.b;
return tmp;
}
Fraction Fraction::operator/(Fraction x)
{
Fraction tmp;
tmp.a = this->a * x.b;
tmp.b = this->b * x.a;
return tmp;
}
int Fraction::operator>(Fraction x)
{
if (this->a * x.b > x.a * this->b) return 1;
else return 0;
}
int Fraction::operator<(Fraction x)
{
if (this->a * x.b < x.a * this->b) return 1;
else return 0;
}
int Fraction::operator==(Fraction x)
{
if (this->a * x.b == x.a * this->b) return 1;
else return 0;
}
int Fraction::operator!=(Fraction x)
{
if (this->a * x.b != x.a * this->b) return 1;
else return 0;
}
int Fraction::operator>=(Fraction x)
{
if (this->a * x.b >= x.a * this->b) return 1;
else return 0;
}
int Fraction::operator<=(Fraction x)
{
if (this->a * x.b <= x.a * this->b) return 1;
else return 0;
}
istream& operator >>(istream& mycin, Fraction& z)
{
cin >> z.a >> z.b;
return mycin;
}
ostream& operator <<(ostream& mycout, const Fraction& z)
{
int x(z.a), y(z.b);
int flag(0);
if (x < 0)
{
x = -x; flag = 1;
}
for (int i = x; i >= 1; i--)
{//找最大公约数
if ((x % i == 0) && (y % i == 0))
{
x = x / i;
y = y / i;
}
}
if (flag == 1) x = -x;
cout << x << '/' << y;
return mycout;
}
//StudybarCommentBegin
int main(int argc, char* argv[]) {
Fraction a(1), b(1, 3);
cin >> a >> b;
cout << "a= " << a << ", b = " << b << endl;
cout << a << " + " << b << " = " << a + b << endl;
cout << a << " - " << b << " = " << a - b << endl;
cout << a << " * " << b << " = " << a * b << endl;
cout << a << " / " << b << " = " << a / b << endl;
cout << "a == b is " << (a == b) << endl;
cout << "a != b is " << (a != b) << endl;
cout << "a <= b is " << (a <= b) << endl;
cout << "a >= b is " << (a >= b) << endl;
cout << "a < b is " << (a < b) << endl;
cout << "a > b is " << (a > b) << endl;
return 1;
}
//StudybarCommentEnd