扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
#includeiostream
公司主营业务:网站制作、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出珙县免费做网站回馈大家。
using namespace std;
//numerator 分子
//denominator分母
int gcd(int m,int n) //求最大公约数函数,用于约分
{
if(n==0)return m;
return gcd(n,m%n);
}
class fraction
{
private:
int numer;
int denom;
public:
fraction(int n=0,int d=1)//带默认参数的构造函数,整数与分数的运算迎刃而解
{
numer=n;
denom=d;
}
void rev(fraction a){numer=a.denom;denom=a.numer;}//倒数
friend ostream operator(std::ostream, const fraction); //重载输出
friend istream operator(std::istream, fraction); //重载输入
friend bool operator==(const fraction, const fraction); //判断有理数是否相等,以下类似
friend bool operator(const fraction, const fraction);
friend bool operator(const fraction, const fraction);
friend bool operator=(const fraction, const fraction);
friend bool operator=(const fraction, const fraction);
friend fraction operator +(const fraction,const fraction);//重载运算符
friend fraction operator -(const fraction,const fraction);
friend fraction operator *(const fraction,const fraction);
friend fraction operator /(const fraction,const fraction);
friend bool operator!=(const fraction, const fraction);
};
fraction operator +(const fraction a,const fraction b)
{
fraction p;
p.numer=a.numer*b.denom+b.numer*a.denom;
p.denom=a.denom*b.denom;
return p;
}
fraction operator -(const fraction a,const fraction b)
{
fraction p;
p.numer=a.numer*b.denom-b.numer*a.denom;
p.denom=a.denom*b.denom;
return p;
}
fraction operator *(const fraction a,const fraction b)
{
fraction p;
p.numer=a.numer*b.numer;
p.denom=a.denom*b.denom;
return p;
}
fraction operator /(const fraction a,const fraction b)
{
fraction p;
p.numer=a.numer*b.denom;
p.denom=a.denom*b.numer;
return p;
}
ostream operator(ostream output,const fraction num)
{
int a=gcd(num.numer,num.denom);
if((num.denom/a)==1)
outputnum.numer/a;
else if(num.denom/a0)
output-num.numer/a"/"-num.denom/a;
else outputnum.numer/a"/"num.denom/a;
return output;
}
istream operator(istreaminput, fraction num)
{
inputnum.numernum.denom;
return input;
}
bool operator==(const fractionnum1, const fractionnum2)
{
return (num1.numer*num2.denom)==(num2.numer*num1.denom);
}
bool operator!=(const fraction num1, const fraction num2)
{
return (num1.numer*num2.denom)!=(num2.numer*num1.denom);
}
bool operator(const fractiona, const fractionb)
{
return (a.numer*b.denom)(b.numer*a.denom);
}
bool operator(const fractiona, const fractionb)
{
return (a.numer*b.denom)(b.numer*a.denom);
}
bool operator=(const fractiona, const fractionb)
{
return (a.numer*b.denom)=(b.numer*a.denom);
}
bool operator=(const fractiona, const fractionb)
{
return (a.numer*b.denom)=(b.numer*a.denom);
}
int main()
{
fraction zrf(1,7),ssh(26,65);
fraction p;
p.rev(ssh);
cout"ssh="ssh"\n1/ssh="pendl;
cout"zrf="zrf"\nssh="ssh'\n' ;
cout"(zrf==ssh)="(zrf==ssh)"\n(zrfssh)="(zrfssh)endl ;
cout"zrf+ssh="zrf+sshendl;
cout"zrf*ssh="zrf*sshendl;
cout"ssh-zrf="ssh-zrfendl;
cout"zrf-ssh="zrf-sshendl;
cout"zrf/ssh="zrf/sshendl;
return 0;
}
//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 iostream
using namespace std;
#include "Rational.h"
void Rational::Standardize () //将有理数化为标准形式
{
if(Q == 0)
{
cout"Error!"endl;
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!"endl;
exit(1);
}
if(nm || 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 ((n0)? 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_qr_p*Q);
}
int Rational::operator (double ra) const
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
return (P*temp_qtemp_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_ptemp_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_qr_p*Q);
}
int Rational::operator (double ra) const
{
Rational temp(ra);
int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();
return (P*temp_qtemp_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_ptemp_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();
outputobj.GetNumerator();
output"/"obj.GetDenominator();
return output;
}
istream operator(istream input,Rational obj)
{
int num,den;
char oper;
inputnum;
inputoper;
inputden;
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();
coutt"("(X.GetNumerator()-t*X.GetDenominator())"/"X.GetDenominator()")"endl;
}
Rational SolveEquation(Rational a,Rational b,Rational c) //求解一般的分数方程
{
Rational temp;
if(a.GetNumerator()==0)
{
cout"Error!"endl;
exit(1);
}
temp=(c-b)/a;
temp.Reduce();
return temp;
}
三、主函数(测试部分):
#include iostream
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: ";
cinC;
D=43.9;
cout"A = "Aendl;
cout"B = "Bendl;
cout"E = "Eendl;
cout"B = E ? "(B==E)endl;
cout"A != B ? "(A!=B)endl;
cout"-B = "(-B)endl;
cout"D(43.9) = "Dendl;
cout"A+B = "(A+B)endl;
cout"A-B = "(A-B)endl;
cout"A+1.5 = "(A+1.5)endl;
cout"1.5+A = "(1.5+A)endl;
cout"A*1.5 = "(A*1.5)endl;
cout"1.5*A = "(1.5*A)endl;
cout"A/B = "(A/B)endl;
cout"A/1.5 = "(A/1.5)endl;
cout"1.5/A = "(1.5/A)endl;
A*=1.5;
cout"A*=1.5 = "Aendl;
cout"float(A) = "Float(A)endl;
cout"E 的带分数形式为: ";
PrintMixedNumber(E);
Rational X;
cout"A*X+B=C 解得: ";
X=SolveEquation(A,C,E);
cout"X = "Xendl;
}
1 有理数类
class Fraction
{
private:
int _Numerator; //分子
int _Denominator; //分母
public:
Fraction(){}
Fraction(const int Numerator,const int Denominator);
friend Fraction operator+(const Fraction frac1,const Fraction frac2);
friend Fraction operator-(const Fraction frac1,const Fraction frac2);
friend Fraction operator*(const Fraction frac1,const Fraction frac2);
friend Fraction operator/(const Fraction frac1,const Fraction frac2);
bool operator(const Fraction frac);
bool operator(const Fraction frac);
bool operator==(const Fraction frac);
bool operator!=(const Fraction frac);
void simp();
friend ostream operator(ostream out,Fraction frac);
};
2 包含类
class A
{
class B // B类属于A类的成员
{
//成员
};
//A类的其他成员
};
3 派生类
class A
{
};
//Book是从PrintedDocument中派生的
class B:A
{
//成员表
};
希望对你有所帮助!
结构体定义有理数及其操作
#includeiostreamusing namespace std;struct RationalT { // 有理数类型的定义 int num; // 分子 int den; // 分母};RationalT CreateRational(int num, int den) // 创建一个有理数{ RationalT r; r.num = num; r.den = den; return r;}RationalT AddRational(RationalT r1, RationalT r2) // 有理数加法{ RationalT r; int fac; r.num = r1.num * r2.den + r2.num * r1.den; r.den = r1.den * r2.den; fac = r.num r.den ? r.num : r.den; // 将结果化成最简分式 while (r.num % fac != 0 || r.den % fac != 0) -- fac; r.num /= fac; r.den /= fac; return r;}RationalT MultiplyRational(RationalT r1, RationalT r2) // 有理数乘法{ RationalT r; int fac; r.num = r1.num * r2.num; r.den = r1.den * r2.den; fac = r.num r.den ? r.num : r.den; // 将结果化成最简分式 while (r.num % fac != 0 || r.den % fac != 0) -- fac; r.num /= fac; r.den /= fac; return r;}double GetRational(RationalT r) // 将有理数转换成小数{ return double(r.num) / r.den;}void PrintRational(RationalT r) // 以分数形式输出有理数{ cout r.num "/" r.den endl;}void main(){ struct RationalT r1,r2,r3,r4; r1 = CreateRational(2,3); r2 = CreateRational(3,8); r3 = AddRational(r1,r2); r4 = MultiplyRational(r1,r2); PrintRational(r3); PrintRational(r4); coutGetRational(r4)endl; cin.get();cin.get();}/*25/241/40.25*/
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流