有理数运算重载
要求:有理数为最简有理数,若分子与分母整除输出整数。
思想总结:
简单代码思想:
1.有理数简化: 判断分子分母大小,取较小值作为辅助变量,用循环for( i=min; i>1; i-- )判断分子分母同时%i,判断余数,当余数为0表示有公因数,且最大公约数将是第一个被提取出。
2.有理数加减:判断操作数双方分母是否相同,用累加方式找最小公倍数,直到双方分母相同,在对分子进行相加减。
3.整数输出:判断分子是否能整除分母,若余数为0,则满足条件。
4.打印:调用行为print,依次对分子分母根据格式 (分子 “/” 分母 )进行输出
代码优化:
1.有理数简化:采用欧立德算法,循环对除数进行求模,在将被除数作为除数,直到余数为0,即获得最大公约数,即当前除数为最大公因数。
2.有理数加减:不用找最小公倍数,直接用公式,这样操作只需一步,效率高。
例如:a/b + a1/b1 = (a * b1 + a1 * b) / b*b1
3.打印:将std类中的左移运算符<<重载,使它能够输出Ration格式的数据。
初版本
#include <iostream>
#include <string>
#include <stdlib.h>
class Ration
{
public:
Ration();
Ration(int d, int m);
Ration operator+(Ration d);
Ration operator-(Ration d);
Ration operator*(Ration d);
Ration operator/(Ration d);
void print();
private:
int molecule;
int denominator;
};
Ration::Ration()
{
denominator = 0;
molecule = 0;
}
Ration::Ration(int d, int m)
{
denominator = d;
molecule = m;
}
Ration Ration::operator+(Ration d)
{
Ration c;
int mtmp = molecule, dtmp = denominator;
int mtmp2 = d.molecule, dtmp2 = d.denominator;
while( mtmp != mtmp2 )
{
if(mtmp < mtmp2)
{
mtmp += molecule;
dtmp += denominator;
}
else if( mtmp > mtmp2 )
{
mtmp2 += d.molecule;
dtmp2 += d.denominator;
}
}
c.molecule = mtmp;
c.denominator = dtmp + dtmp2;
return c;
}
Ration Ration::operator-(Ration d)
{
Ration c;
int mtmp = molecule, dtmp = denominator;
int mtmp2 = d.molecule, dtmp2 = d.denominator;
while( mtmp != mtmp2 )
{
if(mtmp < mtmp2)
{
mtmp += molecule;
dtmp += denominator;
}
else if( mtmp > mtmp2 )
{
mtmp2 += d.molecule;
dtmp2 += d.denominator;
}
}
c.molecule = mtmp;
c.denominator = dtmp - dtmp2;
return c;
}
Ration Ration::operator*(Ration d)
{
Ration c;
c.molecule = molecule * d.molecule;
c.denominator = denominator * d.denominator;
return c;
}
Ration Ration::operator/(Ration d)
{
Ration c;
c.molecule = molecule * d.denominator;
c.denominator = denominator * d.molecule;
return c;
}
void Ration::print()
{
int i=0;
if( abs(molecule) > abs(denominator) )
{
for(i=abs(denominator); i>1; i--)
{
if( denominator % i == 0 && molecule % i == 0 )
{
molecule /= i;
denominator /= i;
break;
}
}
}
else
{
for(i=abs(molecule); i>1; i--)
{
if( denominator % i == 0 && molecule % i == 0 )
{
molecule /= i;
denominator /= i;
break;
}
}
}
if( denominator % molecule == 0 )
std::cout << "整数:" << denominator << std::endl;
else
std::cout << denominator << " / " << molecule << std::endl;
}
int main()
{
Ration r1(3,8),r2(13,8),r3;
r3 = r1 + r2;
r3.print();
r3 = r1 - r2;
r3.print();
r3 = r2 * r1;
r3.print();
r3 = r2 / r1;
r3.print();
std::cin.get();
return 0;
}
优化版本
#include <iostream>
#include <string>
#include <stdlib.h>
class Ration
{
public:
Ration();
Ration(int m, int d);
Ration operator+(Ration d);
Ration operator-(Ration d);
Ration operator*(Ration d);
Ration operator/(Ration d);
private:
void normalize();//简化
int molecule;//分子
int denominator;//分母
friend std::ostream& operator<<(std::ostream &os, Ration f);
};
Ration::Ration()
{
molecule = 0;
denominator = 0;
}
Ration::Ration(int m, int d)
{
molecule = m;
denominator = d;
normalize();//简化
}
void Ration::normalize()
{
//分母不为负数,若为负数则将符号提到分子
if( denominator < 0 )
{
denominator = -denominator;
molecule = -molecule;
}
//欧几里德算法
int a = abs( molecule );
int b = abs( denominator );
//最大公约数
while( b > 0 )
{
int t = a%b;
a=b;
b=t;
}
molecule /= a;
denominator /= a;
}
Ration Ration::operator+(Ration d)
{
int a = molecule, a1 = denominator;
int b = d.molecule, b1 = d.denominator;
int c = a*b1 + a1*b;
int f = a1 * b1;
return Ration(c, f);
}
Ration Ration::operator-(Ration d)
{
int a = molecule, a1 = denominator;
int b = d.molecule, b1 = d.denominator;
int c = a*b1 - a1*b;
int f = a1 * b1;
return Ration(c, f);
}
Ration Ration::operator*(Ration d)
{
int a = molecule, a1 = denominator;
int b = d.molecule, b1 = d.denominator;
int c = a * b;
int f = a1 * b1;
return Ration(c, f);
}
Ration Ration::operator/(Ration d)
{
int a = molecule, a1 = denominator;
int b = d.molecule, b1 = d.denominator;
int c = a * b1;
int f = a1 * b;
return Ration(c, f);
}
void Ration::print()
{
if( molecule % denominator == 0 )
std::cout << "整数:" << molecule << std::endl;
else
std::cout << molecule << "/" << denominator << std::endl;
}
std::ostream &operator<<(std::ostream &os, Ration f);
int main()
{
Ration r1(3,8),r2(13,8),r3;
std::cout << r1 << "+" << r2 << "=" << (r1+r2) << std::endl;
std::cout << r1 << "-" << r2 << "=" << (r1-r2) << std::endl;
std::cout << r1 << "*" << r2 << "=" << (r1*r2) << std::endl;
std::cout << r1 << "/" << r2 << "=" << (r1/r2) << std::endl;
return 0;
}
std::ostream &operator<<(std::ostream &os, Ration f)
{
os << f.molecule << "/" << f.denominator;
return os;
}