c++定义一个有理数类,实现有理数的加减乘除以及化简成最简分数等,要代码。问题解决了还会加分。

2024-12-22 22:54:55
推荐回答(3个)
回答1:

//Rational.h
#ifndef RATIONAl_CLASS
#define RATIONAl_CLASS
class Rational
{
private:
int P,Q;
void Standardize(); //将有理数化为标准形式
int gcd (int m,int n); //求两个数的最大公约数
public:
Rational (int p=0, int q=1); //构造有理数分数,分子默认为0,分母默认为1
Rational (Rational &r); //拷贝构造函数
Rational (double ra); //构造函数,将实数转换为标准形式的有理数
virtual ~Rational (void); //虚析构函数
void Reduce (); //将分数化为最简形式
int GetNumerator(); //取分数的分子
int GetDenominator(); //取分数的分母
Rational& operator= (Rational& r); //将一个有理数对象赋值给另一个对象
Rational& operator= (double ra); //将一个实数赋值给一个有理数对象
Rational operator- (); //将一个有理数取反
friend double Float(Rational &r); //强制转换一个有理数对象为一个实数
//以下为两个有理数对象、一个有理数对象和一个实数、一个实数和一个有理数对象
//之间的关系运算符的重载
int operator== (Rational& r) const;
int operator== (double ra) const;
friend int operator== (double ra, Rational& r);

int operator!= (Rational& r) const;
int operator!= (double ra) const;
friend int operator!= (double ra, Rational& r);

int operator< (Rational& r) const;
int operator< (double ra) const;
friend int operator< (double ra, Rational& r);

int operator<= (Rational& r) const;
int operator<= (double ra) const;
friend int operator<= (double ra, Rational& r);
int operator> (Rational& r) const;
int operator> (double ra) const;
friend int operator> (double ra, Rational& r);

int operator>= (Rational& r) const;
int operator>= (double ra) const;
friend int operator>= (double ra, Rational& r);
//以下两个有理数对象、一个有理数对象和一个实数
//之间的普通四则运算的重载
Rational operator+ (Rational& r) const;
Rational operator+ (double ra) const;
void operator+= (Rational& r);
void operator+= (double ra);
Rational operator- (Rational& r) const;
Rational operator- (double ra) const;
void operator-= (Rational& r);
void operator-= (double ra);
Rational operator* (Rational& r) const;
Rational operator* (double ra) const;
void operator*= (Rational& r);
void operator*= (double ra);
Rational operator/ (Rational& r) const;
Rational operator/ (double ra) const;
void operator/= (Rational& r);
void operator/= (double ra);
//输入输出流的重载,输出为最简形式
friend ostream &operator<<(ostream &output,Rational &obj);
friend istream &operator>>(istream &input,Rational &obj);
int Iegal(void) const; //判断有理数对象的值是否合法
};
//一个实数和一个有理数对象之间的四则运算重载
Rational operator+ (double ra, Rational& r);
Rational operator- (double ra, Rational& r);
Rational operator* (double ra, Rational& r);
Rational operator/ (double ra, Rational& r);
void PrintMixedNumber(Rational X); //将一个有理数对象转换为带分数形式
Rational SolveEquation(Rational a,Rational b,Rational c); //求解一般的分数方程
#endif
二、函数实现部分:
//Rational.cpp
#include
using namespace std;
#include "Rational.h"
void Rational::Standardize () //将有理数化为标准形式
{
if(Q == 0)
{
cout<<"Error!"< exit(1);
}
else
if(Q < 0)
{
P= -P;
Q= -Q;
}
}
int Rational::gcd (int m,int n) //求两个数的最大公约数
{
int i,t;
if(m==0 || n==0)
{
cout<<"Error!"< exit(1);
}
if(n>m || n<-m)
{
t=n;
n=m;
m=t;
}
i=m%n;
if(i!=0)
do
{
m=n;
n=i;
i=m%n;
}while(m%n!=0);
return ((n>0)? n : -n);
}
Rational::Rational (int p, int q) //构造有理数分数,分子默认为0,分母默认为1
{
P=p;
Q=q;
Standardize();
}
Rational::Rational (Rational& r) //拷贝构造函数
{
P=r.P;
Q=r.Q;
Standardize();
}
Rational::Rational (double ra) //构造函数,将实数转换为标准形式的有理数
{
int base=1;
while(ra-(int)ra !=0)
{
ra=ra*10;
base=base*10;
}
P=ra;
Q=base;
}
Rational::~Rational (void) //虚析构函数
{}
void Rational::Reduce () //将分数化为最简形式
{
int max=gcd(P,Q);
P= P/max;
Q= Q/max;
}
int Rational::GetNumerator() //取分数的分子
{
return P;
}
int Rational::GetDenominator() //取分数的分母
{
return Q;
}
Rational& Rational::operator= (Rational& r) //将一个有理数对象赋值给另一个对象
{
P=r.GetNumerator();
Q=r.GetDenominator();
return *this;
}
Rational& Rational::operator= (double ra) //将一个实数赋值给一个有理数对象
{
Rational temp(ra);
*this=temp;
return *this;
}
Rational Rational::operator- () //将一个有理数取反
{
Rational temp(-GetNumerator(),GetDenominator());
return temp;
}
double Float(Rational &r) //强制转换一个有理数对象为一个实数
{
return (1.0*r.GetNumerator()/r.GetDenominator());
}
//以下为两个有理数对象、一个有理数对象和一个实数、一个实数和一个有理数对象
//之间的关系运算符的重载
int Rational::operator== (Rational& r) const
{
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
return (P*r_q==r_p*Q);
}
int Rational::operator== (double ra) const
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
return (P*temp_q==temp_p*Q);
}
int operator== (double ra, Rational& r)
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
r_p=r_p*temp_q;
temp_p=temp_p*r_q;
return (r_p==temp_p);
}

int Rational::operator!= (Rational& r) const
{
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
return !(P*r_q==r_p*Q);
}
int Rational::operator!= (double ra) const
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
return !(P*temp_q==temp_p*Q);
}
int operator!= (double ra, Rational& r)
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
r_p=r_p*temp_q;
temp_p=temp_p*r_q;
return !(r_p==temp_p);
}

int Rational::operator< (Rational& r) const
{
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
return (P*r_q}
int Rational::operator< (double ra) const
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
return (P*temp_q}
int operator< (double ra, Rational& r)
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
r_p=r_p*temp_q;
temp_p=temp_p*r_q;
return (r_p}

int Rational::operator<= (Rational& r) const
{
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
return (P*r_q<=r_p*Q);
}
int Rational::operator<= (double ra) const
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
return (P*temp_q<=temp_p*Q);
}
int operator<= (double ra, Rational& r)
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
r_p=r_p*temp_q;
temp_p=temp_p*r_q;
return (r_p<=temp_p);
}
int Rational::operator> (Rational& r) const
{
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
return (P*r_q>r_p*Q);
}
int Rational::operator> (double ra) const
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
return (P*temp_q>temp_p*Q);
}
int operator> (double ra, Rational& r)
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
r_p=r_p*temp_q;
temp_p=temp_p*r_q;
return (r_p>temp_p);
}

int Rational::operator>= (Rational& r) const
{
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
return (P*r_q>=r_p*Q);
}
int Rational::operator>= (double ra) const
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
return (P*temp_q>=temp_p*Q);
}
int operator>= (double ra, Rational& r)
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
r_p=r_p*temp_q;
temp_p=temp_p*r_q;
return (r_p>=temp_p);
}
//以下为两个有理数对象、一个有理数对象和一个实数
//之间的普通四则运算的重载
Rational Rational::operator+ (Rational& r) const
{
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
Rational obj(P*r_q+r_p*Q,Q*r_q);
return obj;
}

Rational Rational::operator+ (double ra) const
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
Rational obj(P*temp_q+temp_p*Q,Q*temp_q);
return obj;
}
void Rational::operator+= (Rational& r)
{
P=P+r.GetNumerator();
Q=Q+r.GetDenominator();
}
void Rational::operator+= (double ra)
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
P=P*temp_q+temp_p*Q;
Q=temp_q*Q;
}
Rational Rational::operator- (Rational& r) const
{
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
Rational obj(P*r_q-r_p*Q,Q*r_q);
return obj;
}

Rational Rational::operator- (double ra) const
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
Rational obj(P*temp_q-temp_p*Q,Q*temp_q);
return obj;
}
void Rational::operator-= (Rational& r)
{
P=P-r.GetNumerator();
Q=Q-r.GetDenominator();
}
void Rational::operator-= (double ra)
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
P=P*temp_q-temp_p*Q;
Q=temp_q*Q;
}
Rational Rational::operator* (Rational& r) const
{
Rational temp(P*r.GetNumerator(), Q*r.GetDenominator());
return temp;
}

Rational Rational::operator* (double ra) const
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
Rational obj(P*temp_p,Q*temp_q);
return obj;
}
void Rational::operator*= (Rational& r)
{
P=P*r.GetNumerator();
Q=Q*r.GetDenominator();
}
void Rational::operator*= (double ra)
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
P=P*temp_p;
Q=temp_q*Q;
}
Rational Rational::operator/ (Rational& r) const
{
Rational temp(P*r.GetDenominator(),Q*r.GetNumerator());
return temp;
}

Rational Rational::operator/ (double ra) const
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
Rational obj(P*temp_q,Q*temp_p);
return obj;
}
void Rational::operator/= (Rational& r)
{
P=P*r.GetDenominator();
Q=Q*r.GetNumerator();
}
void Rational::operator/= (double ra)
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
P=P*temp_q;
Q=temp_p*Q;
}
//输入输出流的重载,输出为最简形式
ostream& operator<<(ostream &output,Rational &obj)
{
obj.Reduce();
output< output<<"/"< return output;
}
istream& operator>>(istream &input,Rational &obj)
{
int num,den;
char oper;
input>>num;
input>>oper;
input>>den;
Rational temp(num,den);
obj=temp;
return input;
}
int Rational::Iegal(void) const //判断有理数对象的值是否合法
{
if(Q==0)
return 0;
else
return 1;
}
//一个实数和一个有理数对象之间的四则运算重载
Rational operator+ (double ra, Rational& r)
{
Rational temp(ra);
temp=temp+r;
return temp;
}
Rational operator- (double ra, Rational& r)
{
Rational temp(ra);
temp=temp-r;
return temp;
}
Rational operator* (double ra, Rational& r)
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
Rational obj(temp_p*r_p,r_q*temp_q);
return obj;
}
Rational operator/ (double ra, Rational& r)
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
int r_p=r.GetNumerator(), r_q=r.GetDenominator();
Rational obj(temp_p*r_q,r_p*temp_q);
return obj;
}
void PrintMixedNumber(Rational X) //将一个有理数对象转换为带分数形式
{
int t;
X.Reduce();
t=X.GetNumerator()/X.GetDenominator();
cout<}
Rational SolveEquation(Rational a,Rational b,Rational c) //求解一般的分数方程
{
Rational temp;
if(a.GetNumerator()==0)
{
cout<<"Error!"< exit(1);
}
temp=(c-b)/a;
temp.Reduce();
return temp;
}
三、主函数(测试部分):
#include
using namespace std;
#include "Rational.h"
void main()
{
Rational A(-4,-8),C,D;
Rational B(10.05);
Rational E(B);
cout<<"Please input C: ";
cin>>C;
D=43.9;
cout<<"A = "< cout<<"B = "< cout<<"E = "< cout<<"B = E ? "<<(B==E)< cout<<"A != B ? "<<(A!=B)< cout<<"-B = "<<(-B)< cout<<"D(43.9) = "< cout<<"A+B = "<<(A+B)< cout<<"A-B = "<<(A-B)< cout<<"A+1.5 = "<<(A+1.5)< cout<<"1.5+A = "<<(1.5+A)< cout<<"A*1.5 = "<<(A*1.5)< cout<<"1.5*A = "<<(1.5*A)< cout<<"A/B = "<<(A/B)< cout<<"A/1.5 = "<<(A/1.5)< cout<<"1.5/A = "<<(1.5/A)< A*=1.5;
cout<<"A*=1.5 = "< cout<<"float(A) = "< cout<<"E 的带分数形式为: ";
PrintMixedNumber(E);
Rational X;
cout<<"A*X+B=C 解得: ";
X=SolveEquation(A,C,E);
cout<<"X = "<}

回答2:

看看这个:
#include
using namespace std;

class Rationalnumber; //提前声明
ostream& operator<<(ostream& out,Rationalnumber& ofs);

class Rationalnumber //分数
{
int fz,fm; //分子,分母
int Gcd(); //求fz和fm的最大公约数
public:
Rationalnumber(); //无参构造函数,创建1/1对象
Rationalnumber(int pfz,int pfm=1);//pfz为分子,pfm为分母
void Set(int pfz,int pfm); //设置值:pfz为分子,pfm为分母
void Display(); //输出形如"x/y"的分数
void Division();//约分
double ToDec(); //化为小数

Rationalnumber operator+(Rationalnumber& ofs);// 重载+
Rationalnumber operator-(Rationalnumber& ofs);// 重载-
Rationalnumber operator*(Rationalnumber& ofs);// 重载*
Rationalnumber operator/(Rationalnumber& ofs);// 重载/

Rationalnumber& operator+=(Rationalnumber& ofs);
Rationalnumber& operator-=(Rationalnumber& ofs);
Rationalnumber& operator*=(Rationalnumber& ofs);
Rationalnumber& operator/=(Rationalnumber& ofs);

friend ostream& operator<<(ostream& out,Rationalnumber& ofs);

}; //End Of class Rationalnumber

int Rationalnumber::Gcd(){
int x=abs(fz),y=abs(fm),r;
r=x % y;
while( r!=0 ){
x=y; y=r; r=x % y;
}
return y;
}

Rationalnumber::Rationalnumber(){ //无参构造函数,创建1/1对象
fz=1; fm=1;
}

Rationalnumber::Rationalnumber(int pfz,int pfm){//pfz为分子,pfm为分母
fz=pfz; fm=pfm;
}

void Rationalnumber::Set(int pfz,int pfm){ //设置值:pfz为分子,pfm为分母
fz=pfz; fm=pfm;
Division(); //约分
}

void Rationalnumber::Display(){//输出形如"x/y"的分数
cout <}

void Rationalnumber::Division(){//约分
int t=Gcd();
fz/=t; fm/=t;
if(fm<0) { fz=-fz; fm=-fm; } //使负号在分子上
}

double Rationalnumber::ToDec(){ //化为小数
return double(fz)/fm;
}

Rationalnumber Rationalnumber::operator+(Rationalnumber& ofs){ //重载+
Rationalnumber t;
t.fz=fz*ofs.fm+ofs.fz*fm;
t.fm=fm*ofs.fm;
t.Division(); //约分
return t;
}

Rationalnumber Rationalnumber::operator-(Rationalnumber& ofs){ //重载-
Rationalnumber t;
t.fz=fz*ofs.fm-ofs.fz*fm;
t.fm=fm*ofs.fm;
t.Division(); //约分
return t;
}

Rationalnumber Rationalnumber::operator*(Rationalnumber& ofs){ //重载*
Rationalnumber t;
t.fz=fz*ofs.fz; t.fm=fm*ofs.fm;
t.Division(); //约分
return t;
}

Rationalnumber Rationalnumber::operator/(Rationalnumber& ofs){ //重载/
Rationalnumber t;
t.fz=fz*ofs.fm; t.fm=fm*ofs.fz;
t.Division(); //约分
return t;
}

Rationalnumber& Rationalnumber::operator+=(Rationalnumber& ofs){
Rationalnumber t(*this);
fz=t.fz*ofs.fm+ofs.fz*t.fm;
fm=t.fm*ofs.fm;
Division(); //约分
return *this;
}

Rationalnumber& Rationalnumber::operator-=(Rationalnumber& ofs){
Rationalnumber t(*this);
fz=t.fz*ofs.fm-ofs.fz*t.fm;
fm=t.fm*ofs.fm;
Division(); //约分
return *this;
}

Rationalnumber& Rationalnumber::operator*=(Rationalnumber& ofs){
Rationalnumber t(*this);
fz=t.fz*ofs.fz; fm=t.fm*ofs.fm;
Division(); //约分
return *this;
}

Rationalnumber& Rationalnumber::operator/=(Rationalnumber& ofs){
Rationalnumber t(*this);
fz=t.fz*ofs.fm; fm=t.fm*ofs.fz;
Division(); //约分
return *this;
}

ostream& operator<<(ostream& out,Rationalnumber& ofs){
ofs.Display();
return out;
}

void main()
{
Rationalnumber fs1(1,2),fs2(1,3),fs3,fs4,fs5,fs6;
fs3=fs1+fs2;
fs4=fs1-fs2;
fs5=fs1*fs2;
fs6=fs1/fs2;
cout <<"fs1=" < cout <<"fs2=" < cout <<"fs3=fs1+fs2=" < cout <<"fs4=fs1-fs2=" < cout <<"fs5=fs1*fs2=" < cout <<"fs6=fs1/fs2=" <}

回答3:

网上都有---哎!