C++程序设计(PEARSON版)第三版14.2答案

博客介绍了在C++中重写14.2节的Rational类,为分子和分母设计新内部表示,用整型数组int r[2],r[0]为分子、r[1]为分母,且函数签名不变,旧客户程序无需修改仍适用于新类,还给出了类头文件和函数代码,代码在VS2019通过。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(展示封装的好处)重写14.2节中的Rational类,为分子和分母设计新的内部表示形式。定义一个两个元素的整型数组

int r[2];

使用人r[0]做分子,r[1]做分母。Rational类中的函数签名不进行任何改动,因此使用旧Rational类的客户程序无需进行任何修改。仍可适用于新rationa类。

其实上面说了那么多,就一个意思,展示Rational的好处,操作也就一个:替换

下面上代码:

以下代码在VS2019上获得通过!

类头文件代码:

#pragma once
#include <string>
using namespace std;
class Rational
{
public:
	Rational();
	Rational(int num, int der);
	int getNumerator()const;
	int getDenominator()const;
	void reset(int num, int der);
	Rational add(const Rational& secondRational)const;
	Rational subtract(const Rational& SecondRational) const;
	int CompareTo(const Rational& secondRational)const;
	bool operator<(const Rational& SecondRational)const;
	string toString()const;
	double doubleValue()const;
	Rational& operator+=(const Rational& secondRational);
private:
	int r[2];
	static int gcd(int n, int d);
};

类函数代码:

#include "Rational.h"
#include<cstdlib>
#include<sstream>
Rational::Rational()
{
	r[0] = 0;
	r[1] = 1;
}
Rational::Rational(int numerator, int denominator)
{
	int factor = gcd(numerator, denominator);
	r[0] = ((denominator > 0) ? 1 : -1) * numerator / factor;
	r[1] = abs(denominator) / factor;
}

int Rational::getNumerator()const
{
	return r[0];
}
int Rational::getDenominator() const
{
	return r[1];
}
int Rational::gcd(int n, int d)
{
	int n1 = abs(n);
	int n2 = abs(d);
	int gcd = 1;
	for (int k = 1; k <= n1 && k <= n2; k++)
	{
		if (n1 % k == 0 && n2 % k == 0)
		{
			gcd = k;
		}
	}
	return gcd;
}
Rational Rational::add(const Rational & secondRational)const
{
	int n = r[0] * secondRational.getDenominator() +
		r[1] * secondRational.getNumerator();
	int d = r[1] * secondRational.getDenominator();
	return Rational(n, d);
}
Rational Rational::subtract(const Rational & SecondRational) const
{
	int n = r[0] * SecondRational.getDenominator()
		- r[1] * SecondRational.getNumerator();
	int d = r[1]* SecondRational.getDenominator();
	return Rational(n, d);
}
int Rational::CompareTo(const Rational & secondRational)const
{
	Rational temp = subtract(secondRational);
	if (temp.getNumerator() < 0)
		return -1;
	else if (temp.getNumerator() == 0)
		return 0;
	else
		return 1;
}
bool Rational::operator<(const Rational & SecondRational)const
{
	if (CompareTo(SecondRational) < 0)
	{
		return true;
	}
	else
		return false;
}
string Rational::toString() const
{
	stringstream ss;
	ss << r[0];
	if (r[1] > 1)
		ss << "/" << r[1];
	return ss.str();
}
double Rational::doubleValue() const
{
	return 1.0* getNumerator() / getDenominator();
}
Rational& Rational::operator+=(const Rational & secondRational)
{
	*this = add(secondRational);
	return*this;
}

由于客户端程序无需改动(也就是主函数代码文档无需改动),所以无需修改遵照原稿。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值