定义一个有理数类Rational,它有两个私有的整型数据成员nume(分子)和deno(分母)/ 定义一个分数类,分子分母各为私有成员

定义一个有理数类Rational,它有两个私有的整型数据成员nume(分子)和deno(分母);成员函数包括:构造函数(两个整型参数、一个double类型参数、拷贝构造函数)、规范化函数(经过规范化处理,符号统一放在nume中,分子和分母为互质整数)、输出函数(正常处理值为0、整数、分母为0等边界条件)、各种常见的算术、逻辑、复合赋值运算符重载函数(其中一些用成员函数实现,一些用友元函数实现);自增自减运算符函数(成员函数实现)、流插入和流提取运算符函数(友元函数实现)。要求类定义存放在Rational.h文件中,成员函数和友元函数的定义写在Rational.cpp文件中。
编写main(),实例化几个Rational对象,并测试对应功能。代码写在main.cpp文件中。

#include<iostream>
#include<cstdlib>
#include<math.h>
#include<map>
#include<set>
#include<string>
using namespace std;
//定义一个有理数类Rational,它有两个私有的
//整型数据成员nume(分子)和deno(分母);成
//员函数包括:构造函数(两个整型参数、一个double
//类型参数、拷贝构造函数)、规范化函数(经过规范化
//处理,符号统一放在nume中,分子和分母为互质整数)
//、输出函数(正常处理值为0、整数、分母为0等边界条件)
//、各种常见的算术、逻辑、复合赋值运算符重载函数(其中
//一些用成员函数实现,一些用友元函数实现);自增自减运算
//符函数(成员函数实现)、流插入和流提取运算符函数
//(友元函数实现)。要求类定义存放在Rational.h文件中,
//成员函数和友元函数的定义写在Rational.cpp文件中。
//编写main(),实例化几个Rational对象,并测试对应功能。代码写在main.cpp文件中。
//(调试通过后将完整代码(3个文件前注释文件名)及运行截图粘贴到实验结果对应位置。)
class rational
{
public:
	rational();
	rational(int nume,int deno);
	rational(double decimals);
	rational(const rational &ra);
	void standardize();
	void print();
	rational& operator+=(const rational& ra);
	rational& operator-=(const rational& ra);
	rational& operator=(const rational& ra);
	rational& operator++();//前置
	rational& operator--();
	rational operator++(int);//后置
	rational operator--(int);
	friend ostream& operator<<(ostream& os, const rational& ra);
	friend istream& operator>>(istream& os, rational& ra);
	friend rational operator*(const rational& ra1, const rational& ra2);
	friend rational operator/(const rational& ra1, const rational& ra2);
	friend bool operator==(const rational& ra1, const rational& ra2);
	friend bool operator>(const rational& ra1, const rational& ra2);
	friend bool operator<(const rational& ra1, const rational& ra2);
	~rational();

private:
	int nume;
	int deno;
};
void rational::standardize(){
	if (nume != 0 && deno != 0) {
		int x = this->nume;
		int y = this->deno;
		int r = y;
		while (x % y != 0)
		{
			r = x % y;
			x = y;
			y = r;
		}
		this->nume /= r;
		this->deno /= r;
		if (this->deno < 0 && this->nume < 0) {
			this->deno = abs(this->deno);
			this->nume = abs(this->nume);
		}
		else if (this->deno < 0) {
			this->deno = abs(this->deno);
			this->nume = -this->nume;
		}
	}
}
void rational::print() {
	this->standardize();
	if (deno == 0)
		cout << "分母为0,该分数不存在" << endl;
	else if (nume == 0)
		cout << "该分数值为0" << endl;
	else if (deno == 1)
		cout << "该分数值为" << nume << endl;
	else
		cout << "该分数为" << nume << "/" << deno << endl;
}
rational& rational::operator+=(const rational& ra){
	int temp = this->deno;
	this->deno = temp * ra.deno;
	this->nume = this->nume * ra.deno + temp * ra.nume;
	this->standardize();
	return *this;
}
rational& rational::operator-=(const rational& ra){
	int temp = this->deno;
	this->deno= temp * ra.deno;
	this->nume= this->nume * ra.deno - temp * ra.nume;
	this->standardize();
	return *this;
}
rational& rational::operator=(const rational& ra){
	this->nume = ra.nume;
	this->deno = ra.deno;
	return *this;
}
rational& rational::operator++(){
	this->nume += this->deno;
	return *this;
}
rational& rational::operator--(){
	this->nume -= this->deno;
	return *this;
}
rational rational::operator++(int) {
	rational temp = *this;
	this->nume += this->deno;
	return temp;
}
rational rational::operator--(int) {
	rational temp = *this;
	this->nume -= this->deno;
	return temp;
}
ostream& operator<<(ostream& os, const rational& ra) {
	os << to_string(ra.nume) << "/" << to_string(ra.deno) << endl;
	return os;
}
istream& operator>>(istream& is, rational& ra){
	is >> ra.nume >> ra.deno;
	return is;
}
rational operator+(const rational& ra1, const rational& ra2) {
	rational sum=ra1;
	sum += ra2;
	return sum;
}
rational operator-(const rational& ra1, const rational& ra2) {
	rational sum = ra1;
	sum -= ra2;
	return sum;
}
rational operator*(const rational& ra1, const rational& ra2) {
	int nume = ra1.nume * ra2.nume;
	int deno = ra1.deno * ra2.deno;
	rational ra(nume, deno);
	return ra;
}
rational operator/(const rational& ra1, const rational& ra2) {
	int nume = ra1.nume * ra2.deno;
	int deno = ra1.deno * ra2.nume;
	rational ra(nume, deno);
	return ra;
}
bool operator==(const rational& ra1, const rational& ra2) {
	return ra1.nume == ra2.nume && ra1.deno == ra2.deno;
}
bool operator!=(const rational& ra1, const rational& ra2) {
	return !(ra1 == ra2);
}
bool operator<(const rational& ra1, const rational& ra2) {
	return !(ra1 > ra2) && ra1 != ra2;
}
bool operator>(const rational& ra1, const rational& ra2) {
	return ra1.nume* ra2.deno - ra2.nume * ra1.deno;
}
rational::rational() {}
rational::rational(int nume, int deno){
	this->nume = nume;
	this->deno = deno;
	this->standardize();
}
rational::rational(double decimals){
	this->standardize();
}
rational::rational(const rational& ra){
	this->nume = ra.nume;
	this->deno = ra.deno;
}

rational::~rational()
{
}
int main() {
	int nume, deno;
	rational ra1, ra2;
	cout << "请输入分数一的分子和分母" << endl;
	cin >> ra1;
	cout << "请输入分数二的分子和分母" << endl;
	cin >> ra2;
	rational ra = ra1 + ra2;
	cout << "分数一加分数二等于";
	ra.print();
	ra = ra1 - ra2;
	cout << "分数一减分数二等于";
	ra.print();
	ra = ra1 * ra2;
	cout << "分数一乘分数二等于";
	ra.print();
	ra = ra1 / ra2;
	cout << "分数一除分数二等于";
	ra.print();
	ra1 += ra2;
	cout << "分数一+=分数二等于";
	ra1.print();
	ra1++;
	cout << "分数一自增等于";
	ra1.print();
}
  • 9
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
有理数)创建一个名为 Rational 的类,用于对分数进行算术运算。编写一个程序来测试你的类。使用整数变量来表示类的私有实例变量——分子分母。 提供一个构造函数,使该类的对象能够在声明时进行初始化。构造函数应以简化形式存储分数分数 2/4 等价于 1/2,并将作为分子中的 1 和分母中的 2 存储在对象中。 如果没有提供初始值设定项,请提供默认值为 1 的无参数构造函数。 提供执行以下每个操作的公共方法: a) 将两个有理数相加:相加的结果应以简化形式存储。 b) 两个有理数相减:相减的结果应以简化形式存储。 c) 将两个有理数相乘:相乘的结果应以简化形式存储。 d) 将两个有理数相除:相除的结果应以简化形式存储。 e) 以 a/b 的形式返回有理数的字符串表示形式,其中 a 是分子,b 是分母。 f) 以浮点格式返回有理数的字符串表示形式. (考虑提供格式化功能,类的用户能够指定小数点右侧的精度位数。) 【Sample output 1】 Enter numerator 1: 12 Enter denominator 1: 3 Enter numerator 2: 5 Enter denominator 2: 14 Enter precision: 3 a=4/1 b=5/14 a + b = 61/14 = 4.357 a - b = 51/14 = 3.643 a * b = 10/7 = 1.429 a / b = 56/5 = 11.200 【Sample output 2】 Enter numerator 1: 1 Enter denominator 1: 4 Enter numerator 2: 75 Enter denominator 2: 35 Enter precision: 1 a=1/4 b=15/7 a + b = 67/28 = 2.4 a - b = -53/28 = -1.9 a * b = 15/28 = 0.5 a / b = 7/60 = 0.1 Note: The red texts are inputed ,others are output texts. Just use a space to seperate words
下面是一个简单的C++代码示例: ```c++ #include <iostream> using namespace std; class Rational { private: int numerator; // 分子 int denominator; // 分母 public: // 构造函数 Rational(int n = 0, int d = 1) : numerator(n), denominator(d) {} // 重载比较运算符 bool operator==(const Rational& rhs) const { return numerator == rhs.numerator && denominator == rhs.denominator; } bool operator!=(const Rational& rhs) const { return !(*this == rhs); } bool operator<(const Rational& rhs) const { return numerator * rhs.denominator < rhs.numerator * denominator; } bool operator<=(const Rational& rhs) const { return *this < rhs || *this == rhs; } bool operator>(const Rational& rhs) const { return !(*this <= rhs); } bool operator>=(const Rational& rhs) const { return !(*this < rhs); } // 设置分子分母 void set(int n, int d) { numerator = n; denominator = d; } // 输出有理数 void print() const { cout << numerator << "/" << denominator << endl; } }; int main() { Rational a(1, 2), b(2, 3), c(1, 2); if (a == c) { cout << "a == c" << endl; } if (a != b) { cout << "a != b" << endl; } if (a < b) { cout << "a < b" << endl; } if (a <= c) { cout << "a <= c" << endl; } if (b > a) { cout << "b > a" << endl; } if (c >= a) { cout << "c >= a" << endl; } return 0; } ``` 该代码定义了一个有理数类`Rational`,包含分子分母两个私有成员变量,以及构造函数、重载比较运算符、设置分子分母、输出有理数等公有成员函数。在`main`函数中,我们对这些运算符进行了测试和输出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值