0
点赞
收藏
分享

微信扫一扫

C++编程练习||实现分数类Fraction1、实现分数的+,-,*,/ 2、逻辑运算==、!=、<、<=、>、>= 6种运输符号。 3、实现输出<<, 输入 >> 操作符重载。

题目:实现分数类Fraction

   class Fraction {

    int numerator, denominator;

    public:

    ....


   };

   要求:

1、实现分数的+,-,*,/

2、逻辑运算==、!=、<、<=、>、>= 6种运输符号。

3、实现输出<<, 输入  >> 操作符重载。

   样例1输入:

    1 2

    3 4

   样例1输出:

    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

    c == d is 1

   样例2输入:

    2 4

    3 4

   样例2输出:

    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

    c == d is 1

代码如下:

#include <iostream>
#include <cmath>

using namespace std;

class Fraction {
	private:
		int n;
		int d;

	public:
		Fraction(int c_n = 0, int c_d = 0) {
			n = c_n;
			d = c_d;
			this->simplify();
		}

		void simplify() {
			int temp = abs(n);
			while (temp > 1) {
				if (n % temp == 0 && d % temp == 0) {
					n /= temp;
					d /= temp;
				}
				temp--;
			}
			if (n * d >= 0) {
				n = abs(n);
				d = abs(d);
			} else {
				n = -abs(n);
				d = abs(d);
			}
		}

		friend istream& operator>> (istream& in, Fraction& f);

		friend ostream& operator<< (ostream& out, const Fraction& f);

		Fraction operator+ (const Fraction &f);

		Fraction operator- (const Fraction &f);

		Fraction operator* (const Fraction &f);

		Fraction operator/ (const Fraction &f);
		
		int operator== (const Fraction &f);
		
		int operator!= (const Fraction &f);
		
		int operator<= (const Fraction &f);
		
		int operator>= (const Fraction &f);
		
		int operator< (const Fraction &f);
		
		int operator> (const Fraction &f);
};

istream& operator>> (istream& in, Fraction& f) {
	in >> f.n >> f.d;
	f.simplify();
	return in;
}

ostream& operator<< (ostream& out, const Fraction& f) {
	out << f.n << "/" << f.d;
	return out;
}

Fraction Fraction::operator+ (const Fraction &f) {
	Fraction t((n * f.d + d * f.n), (d * f.d));
	t.simplify();
	return t;
}

Fraction Fraction::operator- (const Fraction &f) {
	Fraction t((n * f.d - d * f.n), (d * f.d));
	t.simplify();
	return t;
}

Fraction Fraction::operator* (const Fraction &f) {
	Fraction t((n * f.n), (d * f.d));
	t.simplify();
	return t;
}

Fraction Fraction::operator/ (const Fraction &f) {
	Fraction t((n * f.d), (d * f.n));
	t.simplify();
	return t;


int Fraction::operator== (const Fraction &f) {
	if(n == f.n && d == f.d) {
		return 1;
	} else {
		return 0;
	}
}

int Fraction::operator!= (const Fraction &f) {
	if(n == f.n && d == f.d) {
		return 0;
	} else {
		return 1;
	}
}

int Fraction::operator>= (const Fraction &f) {
	if(n * f.d >= d * f.n) {
		return 1;
	} else {
		return 0;
	}
}

int Fraction::operator<= (const Fraction &f) {
	if(n * f.d <= d * f.n) {
		return 1;
	} else {
		return 0;
	}
}

int Fraction::operator> (const Fraction &f) {
	if(n * f.d > d * f.n) {
		return 1;
	} else {
		return 0;
	}
}

int Fraction::operator< (const Fraction &f) {
	if(n * f.d < d * f.n) {
		return 1;
	} else {
		return 0;
	}
}

int main(int argc, char *argv[]) {

	Fraction a(1),b(1,3),c(-3,9),d(2,-6);

	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;
	cout << "c == d is " << (c == d) << endl;

	return 1;
}

运行结果:

C++编程练习||实现分数类Fraction1、实现分数的+,-,*,/  2、逻辑运算==、!=、<、<=、>、>= 6种运输符号。   3、实现输出<<, 输入  >> 操作符重载。_操作符重载

C++编程练习||实现分数类Fraction1、实现分数的+,-,*,/  2、逻辑运算==、!=、<、<=、>、>= 6种运输符号。   3、实现输出<<, 输入  >> 操作符重载。_ios_02


C++编程练习||实现分数类Fraction1、实现分数的+,-,*,/  2、逻辑运算==、!=、<、<=、>、>= 6种运输符号。   3、实现输出<<, 输入  >> 操作符重载。_#include_03

举报

相关推荐

【==】c++实现关系运算符重载

0 条评论