有理数类的定义与实现

时间:2022-06-25 10:18:46

一个有理数是由一个分子和分母组成的a/b形式的数。我们知道一般程序语言里已经定义了整形类,浮点型,字符型,但并不支持有理数型。这对于分数的精确计算如1/3=0.33333....不能用double 或者float精确表示,所以有必要自己定义一个有理数类Rational类:

含有分子nume和分母deno两个数据域;

可以进行加减乘除比较,转换等多种函数

定义的类如下:

int gys(int x,int y){
x=abs(x);
y=abs(y);
int t=1;
int f=1;
while(t<=x&&t<=y){
int re1=x%t;
int re2=y%t;
if(re1==0&&re2==0) f=t;
t++;}
return f;}//求得最大公约数
int gbs(int x,int y){
x=abs(x);
y=abs(y);
int f=1;
int t=x*y;
while(t>=x&&t>=y){
int re1=t%x;
int re2=t%y;
if(re1==0&&re2==0) f=t;
t--;}
return f;}//求得最小公倍数
class Rational{
private:
long nume;
long deno;
public:
Rational(){nume=0;deno=1;}
Rational(long nume,long b){this->nume=nume;deno=b;}
long getnume(){return nume;}
long getdeno(){return deno;}
Rational getadd(Rational &s){
int deno1=gbs(deno,s.getdeno());
int fnume1=deno1/deno;
int fnume2=deno1/s.getdeno();
int nume1=fnume1*nume+fnume2*s.getnume();
int gbs=gys(deno1,nume1);
int nume2=nume1/gbs;
int deno2=deno1/gbs;
Rational add(nume2,deno2);
return add;}//有理数加法,按照四则运算分数相加法则
Rational getsub(Rational &s){
int deno1=gbs(deno,s.getdeno());
int fnume1=deno1/deno;
int fnume2=deno1/s.getdeno();
int nume1=fnume1*nume-fnume2*s.getnume();
int gbs=gys(deno1,nume1);
int nume2=nume1/gbs;
int deno2=deno1/gbs;
Rational sub(nume2,deno2);
return sub;}//有理数减法,按照四则运算分数相减法则
Rational getmin(Rational &s){
int nume1=nume*s.getnume();
int deno1=deno*s.getdeno();
int gys1=gys(nume1,deno1);
int nume2=nume1/gys1;
int deno2=deno1/gys1;
Rational sub(nume2,deno2);
return sub;}//有理数乘法,直接对应相乘,然后化简
Rational getdiv(Rational &s){
int snume=s.getdeno();
int sdeno=s.getnume();
Rational s1(snume,sdeno);
return getmin(s1);}//有理数除法,除数倒置以后的乘法
int com( Rational &s){
int deno1=gbs(deno,s.getdeno());
int fnume1=deno1/deno;
int fnume2=deno1/s.getdeno();
int nume1=fnume1*nume;
int nume2=fnume2*s.getnume();
if(nume1>nume2) return 1;
else if(nume1==nume2) return 0;
else return -1;}//有理数比较,先雷同有理数加减,化成分母相同,直接比较分子即可

bool equ(Rational &s){
if(com(s)==0) return true;
else return false;}//直接利用已有的比较函数
int intval(){
return nume/deno;}
double doubleval(){
return 1.0*nume/deno;}
string tostring(){
char *str=new char[22];
char *str1=new char[22];
if(deno==1) {itoa(nume,str,10);return str;}
else {itoa(nume,str,10);itoa(deno,str1,10);
string s1(str);
string s2(str1);
s1.append(1,'/');
s1.append(s2);
return s1;}}//涉及到数和字符串转换itoa(),涉及到字符串到string 转换
private:
long gcd(long x,long y){
x=abs(x);
y=abs(y);
long t=1;
long f=1;
while(t<=x&&t<=y){
int re1=x%t;
int re2=y%t;
if(re1==0&&re2==0) f=t;
t++;}
return f;}//求n,d的公约数
};

调用:

inline void dis(Rational &s){
cout<<"result:"<<s.getnume()<<"/"<<s.getdeno()<<endl;}
int main(){
int a=37,b=10;
Rational a1(2,6);
//cout<<"enter two:"<<endl;
//cin>>a>>b;
Rational b1(a,b);

Rational add=a1.getadd(b1);
dis(add);
Rational sub=a1.getsub(b1);
dis(sub);
Rational min=a1.getmin(b1);
dis(min);
Rational div=a1.getdiv(b1);
dis(div);
cout<<a1.com(b1)<<endl;
cout<<a1.equ(b1)<<endl;
cout<<a1.tostring()<<endl;
system("pause");
return 0;}

实现结果如图:

有理数类的定义与实现

这样以后不仅仅有int,float,double等数据类,还有有理数类参与计算了。