C++ RationalNumber有理数类的实现

目录

 

完整版代码

主函数

RationalNumber.h

RationalNumber.cpp


 

完整版代码

主函数

#include<iostream>
#include"RationalNumber.h"
#include"GCDandLCM.h"
using namespace std;
int main()
{
	RationalNumber c(7, 3), d(3, 9), x;
	c.printRational();
	cout << " + ";
	d.printRational();
	cout << " = ";
	x = c + d; // test overloaded operators + and =
	x.printRational();
	cout << '\n';
	c.printRational();
	cout << " - ";
	d.printRational();
	cout << " = ";
	x = c - d; // test overloaded operators - and =
	x.printRational();
	cout << '\n';
	c.printRational();
	cout << " * ";
	d.printRational();
	cout << " = ";
	x = c * d; // test overloaded operators * and =
	x.printRational();
	cout << '\n';
	c.printRational();
	cout << " / ";
	d.printRational();
	cout << " = ";
	x = c / d; // test overloaded operators / and =
	x.printRational();
	cout << '\n';
	c.printRational();
	cout << " is:\n";
	// test overloaded greater than operator
	cout << ((c > d) ? " > " : " <= ");
	d.printRational();
	cout << " according to the overloaded > operator\n";
	// test overloaded less than operator
	cout << ((c < d) ? " < " : " >= ");
	d.printRational();
	cout << " according to the overloaded < operator\n";
	// test overloaded greater than or equal to operator
	cout << ((c >= d) ? " >= " : " < ");
	d.printRational();
	cout << " according to the overloaded >= operator\n";
	// test overloaded less than or equal to operator
	cout << ((c <= d) ? " <= " : " > ");
	d.printRational();
	cout << " according to the overloaded <= operator\n";
	// test overloaded equality operator
	cout << ((c == d) ? " == " : " != ");
	d.printRational();
	cout << " according to the overloaded == operator\n";
	// test overloaded inequality operator
	cout << ((c != d) ? " != " : " == ");
	d.printRational();
	cout << " according to the overloaded != operator" << endl;
	return 0;
}

RationalNumber.h

#pragma once
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H
class RationalNumber
{
public:
	RationalNumber(int = 0, int = 1); // default constructor
	RationalNumber operator+(const RationalNumber&); // addition
	RationalNumber operator-(const RationalNumber&); // subtraction
	RationalNumber operator*(const RationalNumber&); // multiplication
	RationalNumber operator/(RationalNumber&); // division
		// relational operators
	bool operator>(const RationalNumber&) const;
	bool operator<(const RationalNumber&) const;
	bool operator>=(const RationalNumber&) const;
	bool operator<=(const RationalNumber&) const;
	// equality operators
	bool operator==(const RationalNumber&) const;
	bool operator!=(const RationalNumber&) const;
	void printRational() const; // display rational number
	
private:
	int numerator; // private variable numerator
	int denominator; // private variable denominator
	void reduction(); // function for fraction reduction
}; // end class RationalNumber
#endif

RationalNumber.cpp

#include "RationalNumber.h"
#include<iostream>
#include "GCDandLCM.h"
using namespace std;
RationalNumber::RationalNumber(int x, int y) // default constructor
{
	numerator = x;
	denominator = y;
	reduction();
}
RationalNumber RationalNumber::operator+(const RationalNumber& x) // addition
{
	return RationalNumber(numerator * x.denominator + denominator * x.numerator, denominator * x.denominator);
}
RationalNumber RationalNumber::operator-(const RationalNumber& x) // subtraction
{
	return RationalNumber(numerator * x.denominator - denominator * x.numerator, denominator * x.denominator);
}
RationalNumber RationalNumber::operator*(const RationalNumber& x) // multiplication
{
	return RationalNumber(numerator * x.numerator, denominator * x.denominator);
}
RationalNumber RationalNumber::operator/(RationalNumber& x) // division
{
	return RationalNumber(numerator * x.denominator, denominator * x.numerator);
}
	// relational operators
bool RationalNumber::operator>(const RationalNumber& x) const
{
	int temp1, temp2, Temp;
	Temp = lcm(x.denominator, denominator);
	temp1 = Temp / denominator;
	temp2 = Temp / x.denominator;
	int a, b;
	a = numerator * temp1;
	b = x.numerator * temp2;
	if (a > b)
		return true;
	return false;
}
bool RationalNumber::operator<(const RationalNumber& x) const
{
	int temp1, temp2, Temp;
	Temp = lcm(x.denominator, denominator);
	temp1 = Temp / denominator;
	temp2 = Temp / x.denominator;
	int a, b;
	a = numerator * temp1;
	b = x.numerator * temp2;
	if (a < b)
		return true;
	return false;
}
bool RationalNumber::operator>=(const RationalNumber& x) const
{
	return !(*this < x);
}
bool RationalNumber::operator<=(const RationalNumber& x) const
{
	return !(*this > x);
}
// equality operators
bool RationalNumber::operator==(const RationalNumber& x) const
{
	if (numerator == x.numerator && denominator == x.denominator)
		return true;
	return false;
}
bool RationalNumber::operator!=(const RationalNumber& x) const
{
	return !(*this == x);
}
void RationalNumber::printRational() const // display rational number
{
	if (numerator == 0)
		cout << 0;
	else if (denominator == 1)
		cout << numerator;
	else cout << numerator << "/" << denominator;
}
void RationalNumber::reduction() // function for fraction reduction
{
	int largest, gcd = 1;
	if (numerator > denominator)
		largest = numerator;
	else largest = denominator;
	for (int loop = largest; loop >= 2; loop--)
		if (numerator % loop == 0 && denominator % loop == 0)
		{
			gcd = loop;
			break;
		}
	numerator /= gcd;
	denominator /= gcd;
}

 

有点小问题,有时间改

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
(Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object as 1 in the numerator and 2 in the denominator. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform each of the following operations: a. Add two Rational numbers: The result of the addition should be stored in reduced form. b. Subtract two Rational numbers: The result of the subtraction should be stored in reduced form. c. Multiply two Rational numbers: The result of the multiplication should be stored in reduced form. d. Divide two Rational numbers: The result of the division should be stored in reduced form. e. Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. f. Print Rational numbers in floating-point format. (Consider providing formatting capabilities that enable the user of the class to specify the number of digits of precision to the right of the decimal point.) – 提示: – 有理数是有分子、分母以形式a/b表示的数,其中a是分子,b是分母。例如,1/3,3/4,10/4。 – 有理数的分母不能为0,分子却可以为0。每个整数a等价于有理数a/1。有理数用于分数的精确计算中。例如1/3=0.0000…,它不能使用数据类型double或float的浮点格式精确表示出来,为了得到准确结果,必须使用有理数。 – Java提供了整数和浮点数的数据类型,但是没有提供有理数的类型。 – 由于有理数与整数、浮点数有许多共同特征,并且Number类是数字包装的根类,因此,把有理数类Rational定义为Number类的一个子类是比较合适的。由于有理数是可比较的,那么Rational类也应该实现Comparable接口。+下页图中描述了Rational类已将其与Number类和Comparable接口的关系。 –
下面是一个简单的实现RationalNumber类的C++代码: ```c++ #include <iostream> using namespace std; class RationalNumber { private: int numerator; // 分子 int denominator; // 分母 public: RationalNumber() { numerator = 0; denominator = 1; } RationalNumber(int numerator, int denominator) { this->numerator = numerator; this->denominator = denominator; simplify(); // 约分 } // 获取分子 int getNumerator() const { return numerator; } // 获取分母 int getDenominator() const { return denominator; } // 赋值运算符重载 RationalNumber& operator=(const RationalNumber& other) { if (this != &other) { numerator = other.numerator; denominator = other.denominator; } return *this; } // 加法运算符重载 RationalNumber operator+(const RationalNumber& other) { int newNumerator = numerator * other.denominator + other.numerator * denominator; int newDenominator = denominator * other.denominator; return RationalNumber(newNumerator, newDenominator); } // 减法运算符重载 RationalNumber operator-(const RationalNumber& other) { int newNumerator = numerator * other.denominator - other.numerator * denominator; int newDenominator = denominator * other.denominator; return RationalNumber(newNumerator, newDenominator); } // 乘法运算符重载 RationalNumber operator*(const RationalNumber& other) { int newNumerator = numerator * other.numerator; int newDenominator = denominator * other.denominator; return RationalNumber(newNumerator, newDenominator); } // 除法运算符重载 RationalNumber operator/(const RationalNumber& other) { int newNumerator = numerator * other.denominator; int newDenominator = denominator * other.numerator; return RationalNumber(newNumerator, newDenominator); } // 约分 void simplify() { int gcd = getGCD(numerator, denominator); numerator /= gcd; denominator /= gcd; if (denominator < 0) { numerator *= -1; denominator *= -1; } } // 获取最大公约数 int getGCD(int a, int b) { if (b == 0) { return a; } return getGCD(b, a % b); } // 输出有理数 void print() const { if (denominator == 1) { cout << numerator << endl; } else { cout << numerator << "/" << denominator << endl; } } }; int main() { RationalNumber r1(2, 3); RationalNumber r2(3, 4); RationalNumber r3 = r1 + r2; RationalNumber r4 = r1 - r2; RationalNumber r5 = r1 * r2; RationalNumber r6 = r1 / r2; cout << "r1 = "; r1.print(); cout << "r2 = "; r2.print(); cout << "r1 + r2 = "; r3.print(); cout << "r1 - r2 = "; r4.print(); cout << "r1 * r2 = "; r5.print(); cout << "r1 / r2 = "; r6.print(); return 0; } ``` 在该代码中,定义了一个 `RationalNumber` 类,包含了分子和分母两个私有成员变量,以及一些公有成员函数,包括构造函数、赋值运算符重载、加法、减法、乘法、除法运算符重载、约分、获取最大公约数、输出有理数等。在 `main` 函数中,创建了两个有理数对象 `r1` 和 `r2`,并对其进行了加、减、乘、除等运算,最后输出了结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ItsNorth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值