前言:
关于运算符重载的知识点:C++ 运算符重载
一:题目要求
用C++语言完成一个分数类(fraction)的构建,分数类实现的功能(函数)要求如下:
1.单目运算:
Inverse:取负运算(例:+2/3 -> -2/3,或者 -2/3 -> +2/3)、
Reciprocal:求倒数(例:2/3 -> 3/2)、
Reduction:约分(例:6/9 -> 2/3)、
DtoF: 输入是double型转成分数(例:0.25 -> 1/4)、
StoF: 输入是字符串型分数表示转成数值型分数(例“1/4”-> 1/4)
2.双目运算
算术运算(加、减、乘、除、通分)、
关系运算(>、<、>=、<=、==、!=)
3.其他
显示输出
4.main函数
测试上述各项功能,并逐一输出结果到文件中保存。
二:实现过程
1.fraction.h的实现
- GCD函数:帮助进行约分的实现
- 两个构造函数:一个是用两个int型分别给分子、分母赋值,且带默认参数,分子默认为0,分母默认为1(分母不能为0);另一个则是用fraction赋值
- 两个转换构造函数:实现输入double、字符串型转换成分数类
- 类型转换函数:将分数转换为double型
- DtoF:double转为fraction的显示调用函数,帮助后面各种运算符的实现
- FtoD:fraction转为double的显示调用函数,帮助后面各种运算符的实现
- 单目运算符:写成类成员函数的形式
- 双目运算符:写成友员函数的形式(因为有左值不是fraction类的情况,如果左值是fraction类,则写作类成员函数/友元函数都可)
- 重载输出流:实现显示输出
#pragma once
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;
extern const double eps;
class fraction {
private:
int numerator;
int denominator;
int GCD(const int& m, const int& n) const;
public:
fraction(const int& num = 0, const int& den = 1);
fraction(const fraction& f);
fraction(const double& a);
fraction(const char str[]);
operator double();
friend fraction DtoF(const double& a);
friend double FtoD(const fraction& t);
fraction Inverse();
fraction Reciprocal();
fraction Reduction();
friend fraction operator+(fraction& a, fraction& b);
friend fraction operator+(const double& a, fraction& b);
friend fraction operator+(fraction& a,const double& b);
friend fraction operator-(fraction& a, fraction& b);
friend fraction operator-(const double& a, fraction& b);
friend fraction operator-(fraction& a, const double& b);
friend fraction operator*(fraction& a, fraction& b);
friend fraction operator*(const double& a, fraction& b);
friend fraction operator*(fraction& a, const double& b);
friend fraction operator/(fraction& a, fraction& b);
friend fraction operator/(const double& a, fraction& b);
friend fraction operator/(fraction& a, const double& b);
friend fraction& operator+=(fraction& a, fraction& b);
friend double& operator+=(double& a, fraction& b);
friend fraction& operator+=(fraction& a, const double& b);
friend fraction& operator-=(fraction& a, fraction& b);
friend double& operator-=(double& a, fraction& b);
friend fraction& operator-=(fraction& a, const double& b);
friend fraction& operator*=(fraction& a, fraction& b);
friend double& operator*=(double& a, fraction& b);
friend fraction& operator*=(fraction& a, const double& b);
friend fraction& operator/=(fraction& a, fraction& b);
friend double& operator/=(double& a, fraction& b);
friend fraction& operator/=(fraction& a, const double& b);
friend bool operator==(const fraction& a, const fraction& b);
friend bool operator==(const double& a, const fraction& b);
friend bool operator==(const fraction& a, const double& b);
friend bool operator!=(const fraction& a, const fraction& b);
friend bool operator!=(const double& a, const fraction& b);
friend bool operator!=(const fraction& a, const double& b);
friend bool operator>(const fraction& a, const fraction& b);
friend bool operator>(const double& a, const fraction& b);
friend bool operator>(const fraction& a, const double& b);
friend bool operator<(const fraction& a, const fraction& b);
friend bool operator<(const double& a, const fraction& b);
friend bool operator<(const fraction& a, const double& b);
friend bool operator>=(const fraction& a, const fraction& b);
friend bool operator>=(const double& a, const fraction& b);
friend bool operator>=(const fraction& a, const double& b);
friend bool operator<=(const fraction& a, const fraction& b);
friend bool operator<=(const double& a, const fraction& b);
friend bool operator<=(const fraction& a, const double& b);
friend ostream& operator<<(ostream& out, const fraction& a);
};
#endif
2.fraction.cpp的实现
2.1 GCD求最大公约数
因为后续的约分函数中是分子分母同时除以最大公约数,因此GCD缺省返回1
int fraction::GCD(const int& m, const int& n) const
{
if (m == 0 || n == 0)
return 1;
if (m % n == 0)
return n;
else if (n % m == 0)
return m;
if (abs(m) < abs(n))
return GCD(n % m, m);
else
return GCD(m % n, n);
}
2.2 构造函数
fraction::fraction(const int& num, const int& den)
{
numerator = num;
denominator = den;
}
fraction::fraction(const fraction& f)
{
numerator = f.numerator;
denominator = f.denominator;
}
2.3 转换构造函数
fraction::fraction(const double& a)
{
int den = 1;
double t = a;
if (fabs(a) < eps)
{
this->numerator = 0;
this->denominator = 1;
}
else
{
while (fabs(t - int(t)) > 1e-6)
{
t *= 10;
den *= 10;
}
this->numerator = int(t);
this->denominator = den;
*this = this->Reduction();
}
}
fraction::fraction(const char str[])
{
string s(&str[0], &str[strlen(str)]);
numerator = 0;
denominator = 1;
int i, n = s.find('/');
int minus = s.find('-');
int len = s.length();
string num, den;
if (n >= len)
{
if (minus == 0)
{
for (i = 1; i < len; i++)
numerator += int(s[i] - '0') * int(pow(10, len - i - 1));
numerator = -numerator;
}
else
for (i = 0; i < len; i++)
numerator += int(s[i] - '0') * int(pow(10, len - i - 1));
}
else
{
if (minus == 0)
{
num = s.substr(1, n);
for (i = 1; i < n; i++)
numerator += int(s[i] - '0') * int(pow(10, n - i - 1));
numerator = -numerator;
}
else
{
num = s.substr(0, n);
for (i = 0; i < n; i++)
numerator += int(s[i] - '0') * int(pow(10, n - i - 1));
}
denominator = 0;
den = s.substr(n + 1, len - n - 1);
for (i = n + 1; i < len; i++)
denominator += int(s[i] - '0') * int(pow(10, len - i - 1));
}
*this = this->Reduction();
}
2.4 类型转换函数
fraction::operator double()
{
return numerator * 1.0 / denominator;
}
2.5 转换函数的显示调用
帮助后面各种运算符的实现
fraction DtoF(const double& a)
{
fraction f;
int den = 1;
double t = a;
if (fabs(a) < eps)
{
f.numerator = 0;
f.denominator = 1;
}
else
{
while (fabs(t - int(t)) > 1e-6)
{
t *= 10;
den *= 10;
}
f.numerator = int(t);
f.denominator = den;
f = f.Reduction();
}
return f;
}
double FtoD(const fraction& t)
{
return t.numerator * 1.0 / t.denominator;
}
2.6 单目运算符
fraction fraction::Inverse()
{
fraction t;
t.numerator = -numerator;
t.denominator = denominator;
return t;
}
fraction fraction::Reciprocal()
{
fraction t;
t.numerator = denominator;
t.denominator = numerator;
return t;
}
fraction fraction::Reduction()
{
fraction t;
t.numerator = numerator / abs(this->GCD(numerator, denominator));
t.denominator = denominator / abs(this->GCD(numerator, denominator));
return t;
}
2.7 双目运算符
fraction operator+(fraction& a, fraction& b)
{
fraction c;
c.numerator = a.numerator * b.denominator + b.numerator * a.denominator;
c.denominator = a.denominator * b.denominator;
c = c.Reduction();
return c;
}
fraction operator+(const double& a, fraction& b)
{
fraction c = DtoF(a);
return (c + b);
}
fraction operator+(fraction& a, const double& b)
{
fraction c = DtoF(b);
return (a + c);
}
fraction operator-(fraction& a, fraction& b)
{
fraction c;
c.numerator = a.numerator * b.denominator - b.numerator * a.denominator;
c.denominator = a.denominator * b.denominator;
c = c.Reduction();
return c;
}
fraction operator-(const double& a, fraction& b)
{
fraction c = DtoF(a);
return (c - b);
}
fraction operator-(fraction& a, const double& b)
{
fraction c = DtoF(b);
return (a - c);
}
fraction operator*(fraction& a, fraction& b)
{
fraction c;
c.numerator = a.numerator * b.numerator;
c.denominator = a.denominator * b.denominator;
c = c.Reduction();
return c;
}
fraction operator*(const double& a, fraction& b)
{
fraction c = DtoF(a);
return (c * b);
}
fraction operator*(fraction& a, const double& b)
{
fraction c = DtoF(b);
return (a * c);
}
fraction operator/(fraction& a, fraction& b)
{
fraction c;
c.numerator = a.numerator * b.denominator;
c.denominator = a.denominator * b.numerator;
c = c.Reduction();
return c;
}
fraction operator/(const double& a, fraction& b)
{
fraction c = DtoF(a);
return (c / b);
}
fraction operator/(fraction& a, const double& b)
{
fraction c = DtoF(b);
return (a / c);
}
fraction& operator+=(fraction& a, fraction& b)
{
a = a + b;
return a;
}
double& operator+=(double& a, fraction& b)
{
double c = FtoD(b);
a = a + c;
return a;
}
fraction& operator+=(fraction& a, const double& b)
{
fraction c = DtoF(b);
a = a + c;
return a;
}
fraction& operator-=(fraction& a, fraction& b)
{
a = a - b;
return a;
}
double& operator-=(double& a, fraction& b)
{
double c = FtoD(b);
a = a - c;
return a;
}
fraction& operator-=(fraction& a, const double& b)
{
fraction c = DtoF(b);
a = a - c;
return a;
}
fraction& operator*=(fraction& a, fraction& b)
{
a = a * b;
return a;
}
double& operator*=(double& a, fraction& b)
{
double c = FtoD(b);
a = a * c;
return a;
}
fraction& operator*=(fraction& a, const double& b)
{
fraction c = DtoF(b);
a = a * c;
return a;
}
fraction& operator/=(fraction& a, fraction& b)
{
a = a / b;
return a;
}
double& operator/=(double& a, fraction& b)
{
double c = FtoD(b);
a = a / c;
return a;
}
fraction& operator/=(fraction& a, const double& b)
{
fraction c = DtoF(b);
a = a / c;
return a;
}
bool operator==(const fraction& a, const fraction& b)
{
if (a.numerator == 0 && b.numerator == 0)
return 1;
else if (a.numerator == b.numerator && a.denominator == b.denominator)
return 1;
else
return 0;
}
bool operator==(const double& a, const fraction& b)
{
fraction c = DtoF(a);
if (b == c)
return 1;
else
return 0;
}
bool operator==(const fraction& a, const double& b)
{
fraction c = DtoF(b);
if (a == c)
return 1;
else
return 0;
}
bool operator!=(const fraction& a, const fraction& b)
{
if (a.numerator == 0 && b.numerator == 0)
return 0;
else if (a.numerator == b.numerator && a.denominator == b.denominator)
return 0;
else
return 1;
}
bool operator!=(const double& a, const fraction& b)
{
fraction c = DtoF(a);
if (b == c)
return 0;
else
return 1;
}
bool operator!=(const fraction& a, const double& b)
{
fraction c = DtoF(b);
if (a == c)
return 0;
else
return 1;
}
bool operator>(const fraction& a, const fraction& b)
{
double c = FtoD(a);
double d = FtoD(b);
if (c - d > eps)
return 1;
else
return 0;
}
bool operator>(const double& a, const fraction& b)
{
double c = FtoD(b);
if (a - c > eps)
return 1;
else
return 0;
}
bool operator>(const fraction& a, const double& b)
{
double c = FtoD(a);
if (c - b > eps)
return 1;
else
return 0;
}
bool operator<(const fraction& a, const fraction& b)
{
double c = FtoD(a);
double d = FtoD(b);
if (fabs(c - d) < eps)
return 0;
else if (c - d < eps)
return 1;
else
return 0;
}
bool operator<(const double& a, const fraction& b)
{
double c = FtoD(b);
if (fabs(a - c) < eps)
return 0;
else if (a - c < eps)
return 1;
else
return 0;
}
bool operator<(const fraction& a, const double& b)
{
double c = FtoD(a);
if (fabs(c - b) < eps)
return 0;
else if (c - b < eps)
return 1;
else
return 0;
}
bool operator>=(const fraction& a, const fraction& b)
{
double c = FtoD(a);
double d = FtoD(b);
if (c - d > eps || fabs(c - d) < eps)
return 1;
else
return 0;
}
bool operator>=(const double& a, const fraction& b)
{
double c = FtoD(b);
if (a - c > eps || fabs(a - c) < eps)
return 1;
else
return 0;
}
bool operator>=(const fraction& a, const double& b)
{
double c = FtoD(a);
if (c - b > eps || fabs(c - b) < eps)
return 1;
else
return 0;
}
bool operator<=(const fraction& a, const fraction& b)
{
double c = FtoD(a);
double d = FtoD(b);
if (c - d < eps)
return 1;
else
return 0;
}
bool operator<=(const double& a, const fraction& b)
{
double c = FtoD(b);
if (a - c < eps)
return 1;
else
return 0;
}
bool operator<=(const fraction& a, const double& b)
{
double c = FtoD(a);
if (c - b < eps)
return 1;
else
return 0;
}
2.8 重载输出运算符
ostream& operator<<(ostream& out,const fraction& a)
{
if (a.numerator == 0 || a.denominator == 1)
out << a.numerator;
else
out << a.numerator << '/' << a.denominator;
return out;
}
3.fraction_main.cpp的实现
#include <iostream>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <conio.h>
#include "fraction.h"
using namespace std;
const double eps = 1e-6;
void wait_for_enter(const char* prompt = "")
{
if (prompt == NULL || prompt[0] == 0)
cout << endl << "按回车键继续";
else
cout << endl << prompt << ",按回车键继续";
while (_getch() != '\r')
;
cout << endl << endl;
}
int main()
{
if (1)
{
fraction f(2, 8);
cout << "一:单目运算符测试部分:" << endl;
cout << "1.用(2,8)给f赋值, f的值应该是 2/8, 实际是:" << f << endl;
cout << "2.取负运算后, f的值应该是-2/8, 实际是:" << f.Inverse() << endl;
cout << "3.求倒数运算后, f的值应该是 8/2, 实际是:" << f.Reciprocal() << endl;
cout << "4.约分运算后, f的值应该是 1/4, 实际是:" << f.Reduction() << endl;
f = 0.1;
cout << "5.DtoF运算,用0.1给a赋值, f的值应该是1/10, 实际是:" << f << endl;
f = "1/6";
cout << "6.StoF运算,用\"1/6\"给a赋值, f的值应该是 1/6, 实际是:" << f << endl;
f = "-1/6";
cout << "7.StoF运算,用\"-1/6\"给a赋值, f的值应该是-1/6, 实际是:" << f << endl;
wait_for_enter("单目运算符测试部分 测试完成");
}
if (1)
{
fraction f1(1, 4), f2(1, 2);
double d = 0.5;
cout << "二:双目运算符测试部分:" << endl;
cout << " 算术运算测试部分:" << endl;
cout << "赋值:fraction f1 = " << f1 << " " << "frection f2 = " << f2 << endl;
cout << " double d = " << d << endl;
cout << "1.f1 + f2, 值应该是 3/4, 实际是:" << f1 + f2 << endl;
cout << " f1 + d , 值应该是 3/4, 实际是:" << f1 + d << endl;
cout << " d + f2, 值应该是 1, 实际是:" << d + f2 << endl;
cout << "2.f1 - f2, 值应该是-1/4, 实际是:" << f1 - f2 << endl;
cout << " f1 - d , 值应该是-1/4, 实际是:" << f1 - d << endl;
cout << " d - f2, 值应该是 0, 实际是:" << d - f2 << endl;
cout << "3.f1 * f2, 值应该是 1/8, 实际是:" << f1 * f2 << endl;
cout << " f1 * d , 值应该是 1/8, 实际是:" << f1 * d << endl;
cout << " d * f2, 值应该是 1/4, 实际是:" << d * f2 << endl;
cout << "4.f1 / f2, 值应该是 1/2, 实际是:" << f1 / f2 << endl;
cout << " f1 / d , 值应该是 1/2, 实际是:" << f1 / d << endl;
cout << " d / f2, 值应该是 1, 实际是:" << d / f2 << endl;
wait_for_enter("双目运算符的算术运算测试部分 测试完成");
}
if (1)
{
fraction f1(1, 4), f2(1, 2);
double d = 0.5;
cout << "二:双目运算符测试部分:" << endl;
cout << " 关系运算测试部分:" << endl;
cout << "赋值:fraction f1 = " << f1 << " " << "frection f2 = " << f2 << endl;
cout << " double d = " << d << endl;
cout << "1.f1 > f2, 值应该是 0, 实际是:" << (f1 > f2) << endl;
cout << " f1 < f2 , 值应该是 1, 实际是:" << (f1 < f2) << endl;
cout << " f1 >= f2, 值应该是 0, 实际是:" << (f1 >= f2) << endl;
cout << " f1 <= f2, 值应该是 1, 实际是:" << (f1 <= f2) << endl;
cout << " f1 == f2, 值应该是 0, 实际是:" << (f1 == f2) << endl;
cout << " f1 != f2, 值应该是 1, 实际是:" << (f1 != f2) << endl;
cout << "2.f1 > d, 值应该是 0, 实际是:" << (f1 > d) << endl;
cout << " f1 < d, 值应该是 1, 实际是:" << (f1 < d) << endl;
cout << " f1 >= d, 值应该是 0, 实际是:" << (f1 >= d) << endl;
cout << " f1 <= d, 值应该是 1, 实际是:" << (f1 <= d) << endl;
cout << " f1 == d, 值应该是 0, 实际是:" << (f1 == d) << endl;
cout << " f1 != d, 值应该是 1, 实际是:" << (f1 != d) << endl;
cout << "3.d > f2, 值应该是 0, 实际是:" << (d > f2) << endl;
cout << " d < f2 , 值应该是 0, 实际是:" << (d < f2) << endl;
cout << " d >= f2, 值应该是 1, 实际是:" << (d >= f2) << endl;
cout << " d <= f2, 值应该是 1, 实际是:" << (d <= f2) << endl;
cout << " d == f2, 值应该是 1, 实际是:" << (d == f2) << endl;
cout << " d != f2, 值应该是 0, 实际是:" << (d != f2) << endl;
wait_for_enter("双目运算符的逻辑运算测试部分 测试完成");
}
if (1)
{
fraction f1(1, 4), f2(1, 2);
double d = 0.5;
cout << "三:题目要求之外的部分运算符测试部分:" << endl;
cout << "赋值:fraction f1 = " << f1 << " " << "frection f2 = " << f2 << endl;
cout << " double d = " << d << endl;
f1 += f2;
cout << "1.f1 += f2, f1值应该是 3/4, 实际是:" << f1 << endl;
f1 += d;
cout << " f1 += d , f1值应该是 5/4, 实际是:" << f1 << endl;
d += f2;
cout << " d += f2, d值应该是 1, 实际是:" << d << endl;
f1 -= f2;
cout << "2.f1 -= f2, f1值应该是 3/4, 实际是:" << f1 << endl;
f1 -= d;
cout << " f1 -= d , f1值应该是-1/4, 实际是:" << f1 << endl;
d -= f2;
cout << " d -= f2, d值应该是 0.5, 实际是:" << d << endl;
f1 *= f2;
cout << "3.f1 *= f2, f1值应该是-1/8, 实际是:" << f1 << endl;
f1 *= d;
cout << " f1 *= d , f1值应该是-1/16,实际是:" << f1 << endl;
d *= f2;
cout << " d *= f2, d值应该是0.25, 实际是:" << d << endl;
f1 /= f2;
cout << "4.f1 /= f2, f1值应该是-1/8, 实际是:" << f1 << endl;
f1 /= d;
cout << " f1 /= d , f1值应该是-1/4, 实际是:" << f1 << endl;
d /= f2;
cout << " d /= f2, d值应该是 0.5, 实际是:" << d << endl;
wait_for_enter("题目要求之外的部分运算符测试部分 测试完成");
}
ofstream fout;
fout.open("fraction_test_output.txt", ios::out);
if (!fout.is_open())
{
cout << "文件打开失败" << endl;
return -1;
}
if (1)
{
fraction f(2, 8);
fout << "一:单目运算符测试部分:" << endl;
fout << "1.用(2,8)给f赋值, f的值应该是 2/8, 实际是:" << f << endl;
fout << "2.取负运算后, f的值应该是-2/8, 实际是:" << f.Inverse() << endl;
fout << "3.求倒数运算后, f的值应该是 8/2, 实际是:" << f.Reciprocal() << endl;
fout << "4.约分运算后, f的值应该是 1/4, 实际是:" << f.Reduction() << endl;
f = 0.1;
fout << "5.DtoF运算,用0.1给a赋值, f的值应该是1/10, 实际是:" << f << endl;
f = "1/6";
fout << "6.StoF运算,用\"1/6\"给a赋值, f的值应该是 1/6, 实际是:" << f << endl;
f = "-1/6";
fout << "7.StoF运算,用\"-1/6\"给a赋值, f的值应该是-1/6, 实际是:" << f << endl;
fout << "单目运算符测试部分 测试完成" << endl << endl;
}
if (1)
{
fraction f1(1, 4), f2(1, 2);
double d = 0.5;
fout << "二:双目运算符测试部分:" << endl;
fout << " 算术运算测试部分:" << endl;
fout << "赋值:fraction f1 = " << f1 << " " << "frection f2 = " << f2 << endl;
fout << " double d = " << d << endl;
fout << "1.f1 + f2, 值应该是 3/4, 实际是:" << f1 + f2 << endl;
fout << " f1 + d , 值应该是 3/4, 实际是:" << f1 + d << endl;
fout << " d + f2, 值应该是 1, 实际是:" << d + f2 << endl;
fout << "2.f1 - f2, 值应该是-1/4, 实际是:" << f1 - f2 << endl;
fout << " f1 - d , 值应该是-1/4, 实际是:" << f1 - d << endl;
fout << " d - f2, 值应该是 0, 实际是:" << d - f2 << endl;
fout << "3.f1 * f2, 值应该是 1/8, 实际是:" << f1 * f2 << endl;
fout << " f1 * d , 值应该是 1/8, 实际是:" << f1 * d << endl;
fout << " d * f2, 值应该是 1/4, 实际是:" << d * f2 << endl;
fout << "4.f1 / f2, 值应该是 1/2, 实际是:" << f1 / f2 << endl;
fout << " f1 / d , 值应该是 1/2, 实际是:" << f1 / d << endl;
fout << " d / f2, 值应该是 1, 实际是:" << d / f2 << endl;
fout << "双目运算符的算术运算测试部分 测试完成" << endl << endl;
}
if (1)
{
fraction f1(1, 4), f2(1, 2);
double d = 0.5;
fout << "二:双目运算符测试部分:" << endl;
fout << " 关系运算测试部分:" << endl;
fout << "赋值:fraction f1 = " << f1 << " " << "frection f2 = " << f2 << endl;
fout << " double d = " << d << endl;
fout << "1.f1 > f2, 值应该是 0, 实际是:" << (f1 > f2) << endl;
fout << " f1 < f2 , 值应该是 1, 实际是:" << (f1 < f2) << endl;
fout << " f1 >= f2, 值应该是 0, 实际是:" << (f1 >= f2) << endl;
fout << " f1 <= f2, 值应该是 1, 实际是:" << (f1 <= f2) << endl;
fout << " f1 == f2, 值应该是 0, 实际是:" << (f1 == f2) << endl;
fout << " f1 != f2, 值应该是 1, 实际是:" << (f1 != f2) << endl;
fout << "2.f1 > d, 值应该是 0, 实际是:" << (f1 > d) << endl;
fout << " f1 < d, 值应该是 1, 实际是:" << (f1 < d) << endl;
fout << " f1 >= d, 值应该是 0, 实际是:" << (f1 >= d) << endl;
fout << " f1 <= d, 值应该是 1, 实际是:" << (f1 <= d) << endl;
fout << " f1 == d, 值应该是 0, 实际是:" << (f1 == d) << endl;
fout << " f1 != d, 值应该是 1, 实际是:" << (f1 != d) << endl;
fout << "3.d > f2, 值应该是 0, 实际是:" << (d > f2) << endl;
fout << " d < f2 , 值应该是 0, 实际是:" << (d < f2) << endl;
fout << " d >= f2, 值应该是 1, 实际是:" << (d >= f2) << endl;
fout << " d <= f2, 值应该是 1, 实际是:" << (d <= f2) << endl;
fout << " d == f2, 值应该是 1, 实际是:" << (d == f2) << endl;
fout << " d != f2, 值应该是 0, 实际是:" << (d != f2) << endl;
fout << "双目运算符的逻辑运算测试部分 测试完成" << endl << endl;
}
if (1)
{
fraction f1(1, 4), f2(1, 2);
double d = 0.5;
fout << "三:题目要求之外的部分运算符测试部分:" << endl;
fout << "赋值:fraction f1 = " << f1 << " " << "frection f2 = " << f2 << endl;
fout << " double d = " << d << endl;
f1 += f2;
fout << "1.f1 += f2, f1值应该是 3/4, 实际是:" << f1 << endl;
f1 += d;
fout << " f1 += d , f1值应该是 5/4, 实际是:" << f1 << endl;
d += f2;
fout << " d += f2, d值应该是 1, 实际是:" << d << endl;
f1 -= f2;
fout << "2.f1 -= f2, f1值应该是 3/4, 实际是:" << f1 << endl;
f1 -= d;
fout << " f1 -= d , f1值应该是-1/4, 实际是:" << f1 << endl;
d -= f2;
fout << " d -= f2, d值应该是 0.5, 实际是:" << d << endl;
f1 *= f2;
fout << "3.f1 *= f2, f1值应该是-1/8, 实际是:" << f1 << endl;
f1 *= d;
fout << " f1 *= d , f1值应该是-1/16,实际是:" << f1 << endl;
d *= f2;
fout << " d *= f2, d值应该是0.25, 实际是:" << d << endl;
f1 /= f2;
fout << "4.f1 /= f2, f1值应该是-1/8, 实际是:" << f1 << endl;
f1 /= d;
fout << " f1 /= d , f1值应该是-1/4, 实际是:" << f1 << endl;
d /= f2;
fout << " d /= f2, d值应该是 0.5, 实际是:" << d << endl;
fout << "题目要求之外的部分运算符测试部分 测试完成" << endl << endl;
fout.close();
}
return 0;
}
四:总结
1.需要注意的地方:
- 分母不能为0
- 为保护数据,参数最好写成const的形式
- double类型不能以传统类型比较,注意浮点数的精度,具体精度可以由用户设定,本程序中在main.cpp中设定了全局变量const double eps,可供用户修改
- 重载运算符时,根据是否需要改变自身的值来决定返回值是 fraction& 还是 fraction
- 输出到文件后记得关闭文件!!!
2.可改进之处:
- 单目运算符中的取负、求倒数、约分等由于题目要求的原因写成了函数形式,个人感觉写成重载运算符的方式更好一些
- main函数中的测试可以写的更多样一些,比如可以由用户输入数据、写一个分数计算器的形式等
- 错误处理还有许多方面需要完善
- 双目运算符中fraction除了与double进行各种算术运算,也可以添加其他各种数据类型,原理相差不大,可以自行添加
- fraction类中可以添加符号、值等数据,可能在后续的各种函数实现中会更加方便
|