C++:有理数Rational类

【问题描述】

定义一个有理数类Rational。该类存放分数形式的有理数。要求如下:

(1)定义私有变量x和y分别存放分子和分母。

(2)定义带默认参数值的构造函数,默认有理数为0(分子为0,分母为1)。

(3)定义成员函数Add,Sub,Mul和Div,分别用来完成两个有理数的加、减、乘、除运算。

(4)以X/Y形式打印有理数的Print函数。

(5)以浮点数形式打印有理数的PrintFloat函数。(说明:分数不需以最简形式存放,四则运算后亦不需约分。主函数中需要你写出输出语句,其中加法和乘法输出X/Y形式结果,减法和除法输出浮点数形式结果,两个分数的分子分母皆由键盘输入)

#include<iostream>
using namespace std;

class Rational 
{
	int x,y; //x-分子; y-分母 
public:
	Rational(int X = 0, int Y = 1)
	{
		x = X;
		y = Y;
	}
	void Add(Rational r1,Rational r2)
	{
		x = r1.x * r2.y + r1.y * r2.x;
		y = r1.y * r2.y;
		Print();
	}
	void Sub(Rational r1,Rational r2)
	{
		x = r1.x * r2.y - r1.y * r2.x;
		y = r1.y * r2.y;
		PrintFloat();
	}
	void Mul(Rational r1,Rational r2)
	{
		x = r1.x * r2.x;
		y = r1.y * r2.y;
		Print();
	}
	void Div(Rational r1,Rational r2)
	{
		x = r1.x * r2.y;
		y = r1.y * r2.x;
		PrintFloat();
	}
	void Print()
	{
		cout<<x<<"/"<<y<<endl;
	}
	void PrintFloat()
	{
		cout<<double(x)/y<<endl;//注意类型转换,否则无法得到小数 
	} 

};



int main()
{
	int x1,y1,x2,y2;
	cin>>x1>>y1>>x2>>y2;
	Rational r1(x1,y1); //有理数r1的分子分母分别为x1,y1 
	Rational r2(x2,y2);//有理数r2的分子分母分别为x2,y2 
	Rational r;  //有理数四则运算结果 
	r.Add(r1,r2); 
	r.Sub(r1,r2);
	r.Mul(r1,r2);
	r.Div(r1,r2);
	
	return 0;
}


【样例输入】

3 4 1 2

【样例输出】

10/8

0.25

3/8

1.5

  • 3
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 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++实现Rational的代码,包括了常见的运算符重载和一些基本的操作: ```c++ #include <iostream> #include <cmath> using namespace std; class Rational { public: Rational(int numerator = 0, int denominator = 1) : numerator_(numerator), denominator_(denominator) {} Rational(const Rational& other) : numerator_(other.numerator_), denominator_(other.denominator_) {} // 加法运算符重载 Rational operator+(const Rational& other) const { int new_numerator = numerator_ * other.denominator_ + other.numerator_ * denominator_; int new_denominator = denominator_ * other.denominator_; return Rational(new_numerator, new_denominator); } // 减法运算符重载 Rational operator-(const Rational& other) const { int new_numerator = numerator_ * other.denominator_ - other.numerator_ * denominator_; int new_denominator = denominator_ * other.denominator_; return Rational(new_numerator, new_denominator); } // 乘法运算符重载 Rational operator*(const Rational& other) const { int new_numerator = numerator_ * other.numerator_; int new_denominator = denominator_ * other.denominator_; return Rational(new_numerator, new_denominator); } // 除法运算符重载 Rational operator/(const Rational& other) const { int new_numerator = numerator_ * other.denominator_; int new_denominator = denominator_ * other.numerator_; return Rational(new_numerator, new_denominator); } // 等于运算符重载 bool operator==(const Rational& other) const { return numerator_ * other.denominator_ == denominator_ * other.numerator_; } // 不等于运算符重载 bool operator!=(const Rational& other) const { return !(*this == other); } // 大于运算符重载 bool operator>(const Rational& other) const { return numerator_ * other.denominator_ > denominator_ * other.numerator_; } // 小于运算符重载 bool operator<(const Rational& other) const { return numerator_ * other.denominator_ < denominator_ * other.numerator_; } // 大于等于运算符重载 bool operator>=(const Rational& other) const { return !(*this < other); } // 小于等于运算符重载 bool operator<=(const Rational& other) const { return !(*this > other); } // 转换为double型 double to_double() const { return (double)numerator_ / denominator_; } // 约分操作 void reduce() { int gcd = abs(__gcd(numerator_, denominator_)); numerator_ /= gcd; denominator_ /= gcd; } // 转换为字符串形式 string to_string() const { reduce(); if (denominator_ == 1) { return std::to_string(numerator_); } else { return std::to_string(numerator_) + "/" + std::to_string(denominator_); } } // 输入流运算符重载 friend istream& operator>>(istream& in, Rational& r) { in >> r.numerator_ >> r.denominator_; return in; } // 出流运算符重载 friend ostream& operator<<(ostream& out, const Rational& r) { out << r.to_string(); return out; } private: int numerator_; // 分子 int denominator_; // 分母 }; ``` 上述代码中,我们定义了一个Rational,它包含两个成员变量numerator和denominator,分别表示有理数分子分母。我们还定义了一些基本的运算符重载函数,包括加、减、乘、除、等于、不等于、大于、小于、大于等于和小于等于运算符。Rational还实现了一些基本的操作,如转换为double型、约分操作和转换为字符串形式等。其中,我们使用了C++11标准中的__gcd函数来求最大公约数。最后,我们还实现了输入出流运算符重载函数,使得我们可以方便地使用cin和cout来输入Rational型的数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值