C++实现分数四则运算
#include <iostream>
using namespace std;
// 辗转相除法求最大公约数(12和18的最大公约数:6)
int gcd (int a, int b){
a = (a < 0) ? (a = -a) : (a = a);
b = (b < 0) ? (b = -b) : (b = b);
while (b != 0){
int rem = a % b;
a = b;
b = rem;
}
return a;
}
// 求最小公倍数(12和18的最小公倍数:36)
int lcm (int a, int b){
return a * b / gcd(a, b);
}
class Fraction{
public:
int molecule; // 分子
int denominar; // 分母
// 构造函数,参数:分子,分母(默认为1)
Fraction (int mo, int de = 1){
if (mo > 0 && de < 0){ // 若负号在分母上,则移至分子上
mo = -mo;
de = -de;
}
else if (mo < 0 && de < 0){ // 分子与分母均为负,结果得正
mo = -mo;
de = -de;
}
else{
mo = mo;
de = de;
}
int g = gcd(mo, de); // 求分子与分母的最大公约数
if (g != 0){ // 在约分前,先判断最大公约数是否为0
this->molecule = mo / g;
this->denominar = de / g;
}
}
// 拷贝构造函数
Fraction (const Fraction &F){
int mo = F.molecule;
int de = F.denominar;
if (mo > 0 && de < 0){ // 若负号在分母上,则移至分子上
mo = -mo;
de = -de;
}
else if (mo < 0 && de < 0){ // 分子与分母均为负,结果得正
mo = -mo;
de = -de;
}
else{
mo = mo;
de = de;
}
int g = gcd(mo, de); // 求分子与分母的最大公约数
if (g != 0){ // 在约分前,先判断最大公约数是否为0
this->molecule = mo / g;
this->denominar = de / g;
}
}
// 析构函数
~Fraction (){
// 什么也不做
}
// 重载运算符:+
Fraction operator + (const Fraction &other){
if ((this->denominar != 0) && (other.denominar != 0)){ // 判断分母是否为0
int de = lcm(this->denominar, other.denominar);
int mo = (this->molecule * de / this->denominar) + (other.molecule * de / other.denominar);
return Fraction(mo, de);
}
else
return Fraction(1, 0);
}
// 重载运算符:-
Fraction operator - (const Fraction &other){
if ((this->denominar != 0) && (other.denominar != 0)){ // 判断分母是否为0
int de = lcm(this->denominar, other.denominar);
int mo = (this->molecule * de / this->denominar) - (other.molecule * de / other.denominar);
return Fraction(mo, de);
}
else
return Fraction(1, 0);
}
// 重载运算符:*
Fraction operator * (const Fraction &other){
if ((this->denominar != 0) && (other.denominar != 0)){ // 判断分母是否为0
int de = this->denominar * other.denominar;
int mo = this->molecule * other.molecule;
return Fraction(mo, de);
}
else
return Fraction(1, 0);
}
// 重载运算符:/
Fraction operator / (const Fraction &other){
if ((this->denominar != 0) && (other.denominar != 0)){ // 判断分母是否为0
int de = this->denominar * other.molecule;
int mo = this->molecule * other.denominar;
return Fraction(mo, de);
}
else
return Fraction(1, 0);
}
// 重载运算符:<
bool operator < (const Fraction &other){
if ((this->denominar != 0) && (other.denominar != 0)){
int l = lcm(this->denominar, other.denominar);
int l1 = this->molecule * l / this->denominar;
int l2 = other.molecule * l / other.denominar;
return l1 < l2;
}
else
return false;
}
// 重载运算符:>
bool operator > (const Fraction &other){
if ((this->denominar != 0) && (other.denominar != 0)){
int l = lcm(this->denominar, other.denominar);
int l1 = this->molecule * l / this->denominar;
int l2 = other.molecule * l / other.denominar;
return l1 > l2;
}
else
return false;
}
// 重载运算符:<=
bool operator <= (const Fraction &other){
if ((this->denominar != 0) && (other.denominar != 0)){
int l = lcm(this->denominar, other.denominar);
int l1 = this->molecule * l / this->denominar;
int l2 = other.molecule * l / other.denominar;
return l1 <= l2;
}
else
return false;
}
// 重载运算符:>=
bool operator >= (const Fraction &other){
if ((this->denominar != 0) && (other.denominar != 0)){
int l = lcm(this->denominar, other.denominar);
int l1 = this->molecule * l / this->denominar;
int l2 = other.molecule * l / other.denominar;
return l1 >= l2;
}
else
return false;
}
// 重载运算符:==
bool operator == (const Fraction &other){
if ((this->denominar != 0) && (other.denominar != 0)){
int l = lcm(this->denominar, other.denominar);
int l1 = this->molecule * l / this->denominar;
int l2 = other.molecule * l / other.denominar;
return l1 == l2;
}
else
return false;
}
// 重载运算符:!=
bool operator != (const Fraction &other){
if ((this->denominar != 0) && (other.denominar != 0)){
int l = lcm(this->denominar, other.denominar);
int l1 = this->molecule * l / this->denominar;
int l2 = other.molecule * l / other.denominar;
return l1 != l2;
}
else
return false;
}
// 重载赋值运算符:=
void operator = (const Fraction &F)
{
int mo = F.molecule;
int de = F.denominar;
if (mo > 0 && de < 0){ // 若负号在分母上,则移至分子上
mo = -mo;
de = -de;
}
else if (mo < 0 && de < 0){ // 分子与分母均为负,结果得正
mo = -mo;
de = -de;
}
else{
mo = mo;
de = de;
}
int g = gcd(mo, de); // 求分子与分母的最大公约数
if (g != 0){ // 在约分前,先判断最大公约数是否为0
this->molecule = mo / g;
this->denominar = de / g;
}
}
// 重载输入流:>>
friend std::istream &operator >> (std::istream &input, Fraction &F)
{
input >> F.molecule >> F.denominar;
int mo = F.molecule;
int de = F.denominar;
if (mo > 0 && de < 0){ // 若负号在分母上,则移至分子上
mo = -mo;
de = -de;
}
else if (mo < 0 && de < 0){ // 分子与分母均为负,结果得正
mo = -mo;
de = -de;
}
else{
mo = mo;
de = de;
}
int g = gcd(mo, de); // 求分子与分母的最大公约数
if (g != 0){ // 在约分前,先判断最大公约数是否为0
F.molecule = mo / g;
F.denominar = de / g;
}
return input;
}
// 重载输出流:<<
friend std::ostream &operator << (std::ostream &output, Fraction &F)
{
if (F.denominar == 1 && F.molecule >= 0) // 分子大于等于0,分母为1
output << F.molecule;
else if (F.denominar == 1 && F.molecule < 0)// 分子小于0,分母为1
output << "(" << F.molecule << ")";
else if (F.denominar == 0) // 分母为0
output << "NaN";
else
output << "(" << F.molecule << "/" << F.denominar << ")";
return output;
}
};
int main(){
int a, b;
cout << "请输入第一个分数的分子与分母:";
Fraction num1(1);
cin >> num1;
cout << num1 << endl;
cout << "请输入第二个分数的分子与分母:";
Fraction num2(1);
cin >> num2;
cout << num2 << endl;
cout << "请输入第三个分数的分子与分母:";
Fraction num7(1);
cin >> num7;
cout << num7 << endl;
cout << endl << "测试所有功能:" << endl;
Fraction num3 = num1 + num2;
Fraction num4 = num1 - num2;
Fraction num5 = num1 * num2;
Fraction num6 = num1 / num2;
cout << num1 << " + " << num2 << " = " << num3 << endl;
cout << num1 << " - " << num2 << " = " << num4 << endl;
cout << num1 << " * " << num2 << " = " << num5 << endl;
cout << num1 << " / " << num2 << " = " << num6 << endl;
cout << num1 << " < " << num2 << " ? " << (num1 < num2) << endl;
cout << num1 << " > " << num2 << " ? " << (num1 > num2) << endl;
cout << num1 << " <= " << num2 << " ? " << (num1 <= num2) << endl;
cout << num1 << " >= " << num2 << " ? " << (num1 >= num2) << endl;
cout << num1 << " == " << num2 << " ? " << (num1 == num2) << endl;
cout << num1 << " != " << num2 << " ? " << (num1 != num2) << endl;
cout << endl << "测试赋值功能:" << endl;
num2 = num7;
num3 = num1 + num2;
num4 = num1 - num2;
num5 = num1 * num2;
num6 = num1 / num2;
cout << num1 << " + " << num2 << " = " << num3 << endl;
cout << num1 << " - " << num2 << " = " << num4 << endl;
cout << num1 << " * " << num2 << " = " << num5 << endl;
cout << num1 << " / " << num2 << " = " << num6 << endl;
cout << num1 << " < " << num2 << " ? " << (num1 < num2) << endl;
cout << num1 << " > " << num2 << " ? " << (num1 > num2) << endl;
cout << num1 << " <= " << num2 << " ? " << (num1 <= num2) << endl;
cout << num1 << " >= " << num2 << " ? " << (num1 >= num2) << endl;
cout << num1 << " == " << num2 << " ? " << (num1 == num2) << endl;
cout << num1 << " != " << num2 << " ? " << (num1 != num2) << endl;
return 0;
}
输入与输出样例 1:
请输入第一个分数的分子与分母:70 90
(7/9)
请输入第二个分数的分子与分母:45 60
(3/4)
请输入第三个分数的分子与分母:13 44
(13/44)
测试所有功能:
(7/9) + (3/4) = (55/36)
(7/9) - (3/4) = (1/36)
(7/9) * (3/4) = (7/12)
(7/9) / (3/4) = (28/27)
(7/9) < (3/4) ? 0
(7/9) > (3/4) ? 1
(7/9) <= (3/4) ? 0
(7/9) >= (3/4) ? 1
(7/9) == (3/4) ? 0
(7/9) != (3/4) ? 1
测试赋值功能:
(7/9) + (13/44) = (425/396)
(7/9) - (13/44) = (191/396)
(7/9) * (13/44) = (91/396)
(7/9) / (13/44) = (308/117)
(7/9) < (13/44) ? 0
(7/9) > (13/44) ? 1
(7/9) <= (13/44) ? 0
(7/9) >= (13/44) ? 1
(7/9) == (13/44) ? 0
(7/9) != (13/44) ? 1
输入与输出样例 2:
请输入第一个分数的分子与分母:56 0
NaN
请输入第二个分数的分子与分母:789 44
(789/44)
请输入第三个分数的分子与分母:55 900
(11/180)
测试所有功能:
NaN + (789/44) = NaN
NaN - (789/44) = NaN
NaN * (789/44) = NaN
NaN / (789/44) = NaN
NaN < (789/44) ? 0
NaN > (789/44) ? 0
NaN <= (789/44) ? 0
NaN >= (789/44) ? 0
NaN == (789/44) ? 0
NaN != (789/44) ? 0
测试赋值功能:
NaN + (11/180) = NaN
NaN - (11/180) = NaN
NaN * (11/180) = NaN
NaN / (11/180) = NaN
NaN < (11/180) ? 0
NaN > (11/180) ? 0
NaN <= (11/180) ? 0
NaN >= (11/180) ? 0
NaN == (11/180) ? 0
NaN != (11/180) ? 0
输入与输出样例 3:
请输入第一个分数的分子与分母:970 1200
(97/120)
请输入第二个分数的分子与分母:0 66
0
请输入第三个分数的分子与分母:240 5000
(6/125)
测试所有功能:
(97/120) + 0 = (97/120)
(97/120) - 0 = (97/120)
(97/120) * 0 = 0
(97/120) / 0 = NaN
(97/120) < 0 ? 0
(97/120) > 0 ? 1
(97/120) <= 0 ? 0
(97/120) >= 0 ? 1
(97/120) == 0 ? 0
(97/120) != 0 ? 1
测试赋值功能:
(97/120) + (6/125) = (2569/3000)
(97/120) - (6/125) = (2281/3000)
(97/120) * (6/125) = (97/2500)
(97/120) / (6/125) = (2425/144)
(97/120) < (6/125) ? 0
(97/120) > (6/125) ? 1
(97/120) <= (6/125) ? 0
(97/120) >= (6/125) ? 1
(97/120) == (6/125) ? 0
(97/120) != (6/125) ? 1