C++编程,定义一个有理数类,急!

要求:为有理数定义一个类。有理数是有限的数,表示成两个相除的整数。但是除法不会真正执行,它只是用于标识,比如1/2,2/3,15/32,65/4等。你应该将有理数表示成2... 要求:为有理数定义一个类。有理数是有限的数,表示成两个相除的整数。但是除法不会真正执行,它只是用于标识,比如1/2, 2/3,15/32,65/4等。你应该将有理数表示成2个int值,分别称为numerator(分子)和denominator(分母)。
您提供的构造函数应该根据int值对来构造对象,该构造函数应获取2个int参数。由于所有int值都同时是一个有理数(比如2/1或17/1),所以还应该提供接收单个int参数的构造函数。

提供成员函数add(加),sub(减),mul(乘),div(除),它们能返回一个有理数。提供一个less(小于)函数返回一个bool值。提供一个成员函数neg,它没有参数,返回的是调用对象的相反数。例如,s+t要写成s.add(b), s<t要写成s.less(t)。

提供input和output成员函数,采取2/3或者37/51这样的形式,从键盘获取有理数,也能将有理数写到屏幕。

提供一个main函数,用它来全面测试您所实现的类。

定义函数时,可以参考以下公式:

a/b+c/d = (a*d+b*c) / (b*d)
a/b-c/d = (a*d-b*c) / (b*d)
(a/b)*(c/d) = (a*c) / (b*d)
(a/b)/(c/d) = (a*d) / (c*b)
-(a/b)= (-a/b)
(a/b)<(c/d) means (a*d) < (c*b)
(a/b)= =(c/d) means (a*d) = = (c*b)

所有的正负号都放在分子部分,分母保持为正数。

好的话我再加分!!!急!!
展开
 我来答
x68251
2011-06-02 · TA获得超过602个赞
知道小有建树答主
回答量:470
采纳率:0%
帮助的人:273万
展开全部
#include "RationalTest.h"
#include <iostream>
using namespace std;

class Rational
{
private:
int numerator;//分子
int denominator;//分母
public:
Rational()
{
numerator = 0;
denominator = 0;
}
~Rational()
{

}
Rational(const Rational& rat)
{
numerator = rat.numerator;
denominator = rat.denominator;
}
Rational(int _numerator,int _denominator)
{
numerator = _numerator;
denominator = _denominator;
}
public:
int GetNumerator()
{
return this->numerator;
}
void SetNumerator(int _numerator)
{
this->numerator = _numerator;
}
int GetDenominator()
{
return this->denominator;
}
void SetDenominator(int _denominator)
{
this->denominator = _denominator;
}
public:
Rational Add(const Rational& rat)
{
Rational result;
result.numerator = this->numerator * rat.denominator + this->denominator * rat.numerator ;
result.denominator = this->denominator * rat.denominator;
if(result.denominator < 0)
{
result.numerator = -result.numerator;
result.denominator = -result.denominator;
}
return result;
}
Rational Sub(const Rational& rat)
{
Rational result;
result.numerator = this->numerator * rat.denominator - this->denominator * rat.numerator ;
result.denominator = this->denominator * rat.denominator;
if(result.denominator < 0)
{
result.numerator = -result.numerator;
result.denominator = -result.denominator;
}
return result;
}
Rational Mul(const Rational& rat)
{
Rational result;
result.numerator = this->numerator * rat.numerator ;
result.denominator = this->denominator * rat.denominator;
if(result.denominator < 0)
{
result.numerator = -result.numerator;
result.denominator = -result.denominator;
}
return result;
}
Rational Div(const Rational& rat)
{
Rational result;
result.numerator = this->numerator * rat.denominator ;
result.denominator = rat.numerator * this->denominator;
if(result.denominator < 0)
{
result.numerator = -result.numerator;
result.denominator = -result.denominator;
}
return result;
}
Rational Neg()
{
Rational result;
if(this->denominator < 0 )
{
result.numerator = -this->numerator;
result.denominator = -this->denominator;
}
else
{
return *this;
}
return result;
}
bool Less(const Rational& rat)
{
return (this->numerator * rat.denominator) < (rat.numerator * this->denominator);
}
bool Greater(const Rational& rat)
{
return !Less(rat);
}
bool Equal(const Rational& rat)
{
return (this->numerator * rat.denominator) == (rat.numerator * this->denominator);
}
void Output()
{
cout<<"有理数: "<<GetNumerator()<<"/"<<GetDenominator()<<endl;
}
void Input()
{
cout<<"输入分子:";
cin>>this->numerator;
cout<<"输入分母:";
cin>>this->denominator;
}
};

int main()
{
Rational r1,r2,r3,r4;
r1.Input();
r1.Output();
r2.SetNumerator(6);
r2.SetDenominator(-13);
r2.Output();
r3.SetNumerator(-2);
r3.SetDenominator(3);
r3.Output();

r4 = r1.Neg();
r4.Output();

bool b1;

r4 = r1.Add(r2);
r4.Output();
r4 = r1.Sub(r2);
r4.Output();
r4 = r1.Div(r2);
r4.Output();
r4 = r1.Mul(r2);
r4.Output();
b1 = r1.Less(r3);
cout<<b1<<endl;
b1 = r1.Greater(r3);
cout<<b1<<endl;
b1 = r1.Equal(r3);
cout<<b1<<endl;

getchar();
return 0;
}
追问
运行时出错啊!!c:\program files\microsoft visual studio\myprojects\yu\wei\cheng.cpp(1) : fatal error C1083: Cannot open include file: 'RationalTest.h': No such file or directory
执行 cl.exe 时出错.
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式