IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++:完成一个分数(fraction)类 -> 正文阅读

[C++知识库]C++:完成一个分数(fraction)类

前言:

关于运算符重载的知识点: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;  //double的精度,在main函数中,可由按需调整

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);  //用两个int型赋值
	fraction(const fraction& f);                       //用另一个fraction类赋值

	/*转换构造函数*/
	fraction(const double& a);   //实现DtoF功能:输入是double型转成分数
	fraction(const char str[]);  //实现StoF功能:输入是字符串型分数表示转成数值型分数

	/*类型转换函数*/
	operator double();           //将分数转换成double型

	/*double转为fraction类的显示调用*/
	friend fraction DtoF(const double& a);
	/*fraction类转double的显示调用*/
	friend double FtoD(const fraction& t);

	/*单目运算*/
	fraction Inverse();     //取负运算
	fraction Reciprocal();  //求倒数
	fraction Reduction();   //约分

	/*双目运算符:算术运算:*/
	// '+'
	friend fraction operator+(fraction& a, fraction& b);      //两个fraction型相加
	friend fraction operator+(const double& a, fraction& b);  //double + fraction
	friend fraction operator+(fraction& a,const double& b);   //fraction + double
	// '-'
	friend fraction operator-(fraction& a, fraction& b);      //两个fraction型相减
	friend fraction operator-(const double& a, fraction& b);  //double - fraction
	friend fraction operator-(fraction& a, const double& b);  //fraction - double
	// '*'
	friend fraction operator*(fraction& a, fraction& b);      //两个fraction型相乘
	friend fraction operator*(const double& a, fraction& b);  //double * fraction
	friend fraction operator*(fraction& a, const double& b);  //fraction * double
	// '/'
	friend fraction operator/(fraction& a, fraction& b);      //两个fraction型相除
	friend fraction operator/(const double& a, fraction& b);  //double / fraction
	friend fraction operator/(fraction& a, const double& b);  //fraction / double

	//  '+='
	friend fraction& operator+=(fraction& a, fraction& b);     //fraction += fraction
	friend double& operator+=(double& a, fraction& b);         //double += fraction
	friend fraction& operator+=(fraction& a, const double& b); //fraction += double
	// '-='
	friend fraction& operator-=(fraction& a, fraction& b);     //fraction -= fraction
	friend double& operator-=(double& a, fraction& b);         //double -= fraction
	friend fraction& operator-=(fraction& a, const double& b); //fraction -= double
	// '*='
	friend fraction& operator*=(fraction& a, fraction& b);     //fraction *= fraction
	friend double& operator*=(double& a, fraction& b);         //double *= fraction
	friend fraction& operator*=(fraction& a, const double& b); //fraction *= double
	// '/='
	friend fraction& operator/=(fraction& a, fraction& b);     //fraction /= fraction
	friend double& operator/=(double& a, fraction& b);         //double /= fraction
	friend fraction& operator/=(fraction& a, const double& b); //fraction /= double

	/*双目运算符:关系运算:*/
	// '=='
	friend bool operator==(const fraction& a, const fraction& b);  //两个fraction型作比较
	friend bool operator==(const double& a, const fraction& b);    //double与fraction作比较
	friend bool operator==(const fraction& a, const double& b);    //fraction与double作比较
	// '!='
	friend bool operator!=(const fraction& a, const fraction& b);  //两个fraction型作比较
	friend bool operator!=(const double& a, const fraction& b);    //double与fraction作比较
	friend bool operator!=(const fraction& a, const double& b);    //fraction与double作比较
	// '>'
	friend bool operator>(const fraction& a, const fraction& b);  //两个fraction型作比较
	friend bool operator>(const double& a, const fraction& b);    //double与fraction作比较
	friend bool operator>(const fraction& a, const double& b);    //fraction与double作比较
	// '<'
	friend bool operator<(const fraction& a, const fraction& b);  //两个fraction型作比较
	friend bool operator<(const double& a, const fraction& b);    //double与fraction作比较
	friend bool operator<(const fraction& a, const double& b);    //fraction与double作比较
	// '>='
	friend bool operator>=(const fraction& a, const fraction& b);  //两个fraction型作比较
	friend bool operator>=(const double& a, const fraction& b);    //double与fraction作比较
	friend bool operator>=(const fraction& a, const double& b);    //fraction与double作比较
	// '<='
	friend bool operator<=(const fraction& a, const fraction& b);  //两个fraction型作比较
	friend bool operator<=(const double& a, const fraction& b);    //double与fraction作比较
	friend bool operator<=(const fraction& a, const double& b);    //fraction与double作比较

	/*重载流输出运算符实现显示输出*/
	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 构造函数

/*构造函数:用两个int型赋值(带默认参数)*/
fraction::fraction(const int& num, const int& den)
{
	numerator = num;
	denominator = den;
}

/*构造函数:用另一个fraction类赋值*/
fraction::fraction(const fraction& f)
{
	numerator = f.numerator;
	denominator = f.denominator;
}

2.3 转换构造函数

/*转换构造函数:
 *实现DtoF功能:输入是double型转成分数
*/
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();
	}
}

/*转换构造函数:
 *实现StoF功能:输入是字符串型分数表示转成数值型分数
*/
fraction::fraction(const char str[])
{
	string s(&str[0], &str[strlen(str)]);  //将字符数组转换为string类

	/*初始化*/
	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));
	}//end of if
	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));
	}//end of else 

	/*进行约分*/
	*this = this->Reduction();
}

2.4 类型转换函数

/*类型转换函数:将分数转换成double型*/
fraction::operator double()
{
	return numerator * 1.0 / denominator;
}

2.5 转换函数的显示调用

帮助后面各种运算符的实现

/*double转为fraction类的显示调用*/
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;
}

/*fraction类转double的显示调用*/
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型相加*/
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;
}

/*双目运算符:double + fraction*/
fraction operator+(const double& a, fraction& b)
{
	fraction c = DtoF(a);  //double转为fraction
	return (c + b);
}

/*双目运算符:fraction + double*/
fraction operator+(fraction& a, const double& b)
{
	fraction c = DtoF(b);  //double转为fraction
	return (a + c);
}

/*双目运算符:两个fraction型相减*/
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;
}

/*双目运算符:double - fraction*/
fraction operator-(const double& a, fraction& b)
{
	fraction c = DtoF(a);  //double转为fraction
	return (c - b);
}

/*双目运算符:fraction - double*/
fraction operator-(fraction& a, const double& b)
{
	fraction c = DtoF(b);  //double转为fraction
	return (a - c);
}

/*双目运算符:两个fraction型相乘*/
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;
}

/*双目运算符:double * fraction*/
fraction operator*(const double& a, fraction& b)
{
	fraction c = DtoF(a);  //double转为fraction
	return (c * b);
}

/*双目运算符:fraction * double*/
fraction operator*(fraction& a, const double& b)
{
	fraction c = DtoF(b);  //double转为fraction
	return (a * c);
}

/*双目运算符:两个fraction型相除*/
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;
}

/*双目运算符:double / fraction*/
fraction operator/(const double& a, fraction& b)
{
	fraction c = DtoF(a);  //double转为fraction
	return (c / b);
}

/*双目运算符:fraction / double*/
fraction operator/(fraction& a, const double& b)
{
	fraction c = DtoF(b);  //double转为fraction
	return (a / c);
}

/*fraction += fraction*/
fraction& operator+=(fraction& a, fraction& b)
{
	a = a + b;
	return a;
}

/*double += fraction*/
double& operator+=(double& a, fraction& b)
{
	double c = FtoD(b);  //fraction转为double
	a = a + c;
	return a;
}

/* fraction += double*/
fraction& operator+=(fraction& a, const double& b)
{
	fraction c = DtoF(b);  //double转为fraction
	a = a + c;
	return a;
}

/*fraction -= fraction*/
fraction& operator-=(fraction& a, fraction& b)
{
	a = a - b;
	return a;
}

/*double -= fraction*/
double& operator-=(double& a, fraction& b)
{
	double c = FtoD(b);  //fraction转为double
	a = a - c;
	return a;
}

/* fraction -= double*/
fraction& operator-=(fraction& a, const double& b)
{
	fraction c = DtoF(b);  //double转为fraction
	a = a - c;
	return a;
}

/*fraction *= fraction*/
fraction& operator*=(fraction& a, fraction& b)
{
	a = a * b;
	return a;
}

/*double *= fraction*/
double& operator*=(double& a, fraction& b)
{
	double c = FtoD(b);  //fraction转为double
	a = a * c;
	return a;
}

/* fraction *= double*/
fraction& operator*=(fraction& a, const double& b)
{
	fraction c = DtoF(b);  //double转为fraction
	a = a * c;
	return a;
}

/*fraction /= fraction*/
fraction& operator/=(fraction& a, fraction& b)
{
	a = a / b;
	return a;
}

/*double /= fraction*/
double& operator/=(double& a, fraction& b)
{
	double c = FtoD(b);  //fraction转为double
	a = a / c;
	return a;
}

/* fraction /= double*/
fraction& operator/=(fraction& a, const double& b)
{
	fraction c = DtoF(b);  //double转为fraction
	a = a / c;
	return a;
}

/*双目运算符:== (两个fraction型作比较)*/
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;
}

/*双目运算符:== (double与fraction作比较)*/
bool operator==(const double& a, const fraction& b)
{
	fraction c = DtoF(a);  //double转为fraction
	if (b == c)
		return 1;
	else
		return 0;
}

/*双目运算符:== (fraction与double作比较)*/
bool operator==(const fraction& a, const double& b)
{
	fraction c = DtoF(b);  //double转为fraction
	if (a == c)
		return 1;
	else
		return 0;
}

/*双目运算符:!= (两个fraction型作比较)*/
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;
}

/*双目运算符:!= (double与fraction作比较)*/
bool operator!=(const double& a, const fraction& b)
{
	fraction c = DtoF(a);  //double转为fraction
	if (b == c)
		return 0;
	else
		return 1;
}

/*双目运算符:!= (fraction与double作比较)*/
bool operator!=(const fraction& a, const double& b)
{
	fraction c = DtoF(b);  //double转为fraction
	if (a == c)
		return 0;
	else
		return 1;
}

/*双目运算符:> (两个fraction型作比较)*/
bool operator>(const fraction& a, const fraction& b)
{
	/*fraction转为double型*/
	double c = FtoD(a);
	double d = FtoD(b);

	if (c - d > eps)
		return 1;
	else
		return 0;
}

/*双目运算符:> (double与fraction作比较)*/
bool operator>(const double& a, const fraction& b)
{
	double c = FtoD(b);  //fraction转为double
	if (a - c > eps)
		return 1;
	else
		return 0;
}

/*双目运算符:> (fraction与double作比较)*/
bool operator>(const fraction& a, const double& b)
{
	double c = FtoD(a);  //fraction转为double
	if (c - b > eps)
		return 1;
	else
		return 0;
}

/*双目运算符:< (两个fraction型作比较)*/
bool operator<(const fraction& a, const fraction& b)
{
	/*fraction转为double型*/
	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;
}

/*双目运算符:< (double与fraction作比较)*/
bool operator<(const double& a, const fraction& b)
{
	double c = FtoD(b);  //fraction转为double
	if (fabs(a - c) < eps)  //相等
		return 0;
	else if (a - c < eps)
		return 1;
	else
		return 0;
}

/*双目运算符:< (fraction与double作比较)*/
bool operator<(const fraction& a, const double& b)
{
	double c = FtoD(a);  //fraction转为double
	if (fabs(c - b) < eps)  //相等
		return 0;
	else if (c - b < eps)
		return 1;
	else
		return 0;
}

/*双目运算符:>= (两个fraction型作比较)*/
bool operator>=(const fraction& a, const fraction& b)
{
	/*fraction转为double型*/
	double c = FtoD(a);
	double d = FtoD(b);

	if (c - d > eps || fabs(c - d) < eps)
		return 1;
	else
		return 0;
}

/*双目运算符:>= (double与fraction作比较)*/
bool operator>=(const double& a, const fraction& b)
{
	double c = FtoD(b);  //fraction转为double
	if (a - c > eps || fabs(a - c) < eps)
		return 1;
	else
		return 0;
}

/*双目运算符:>= (fraction与double作比较)*/
bool operator>=(const fraction& a, const double& b)
{
	double c = FtoD(a);  //fraction转为double
	if (c - b > eps || fabs(c - b) < eps)
		return 1;
	else
		return 0;
}

/*双目运算符:<= (两个fraction型作比较)*/
bool operator<=(const fraction& a, const fraction& b)
{
	/*fraction转为double型*/
	double c = FtoD(a);
	double d = FtoD(b);

	if (c - d < eps)
		return 1;
	else
		return 0;
}

/*双目运算符:<= (double与fraction作比较)*/
bool operator<=(const double& a, const fraction& b)
{
	double c = FtoD(b);  //fraction转为double
	if (a - c < eps)
		return 1;
	else
		return 0;
}

/*双目运算符:<= (fraction与double作比较)*/
bool operator<=(const fraction& a, const double& b)
{
	double c = FtoD(a);  //fraction转为double
	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)  //值为0或为整数
		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;  //double的精度

void wait_for_enter(const char* prompt = "")
{
	if (prompt == NULL || prompt[0] == 0) //代替strlen(prompt)==0的作用,目的是为了防止后面不小心使用系统函数而特地不加<cstring>头文件
		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为变量名
	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类中可以添加符号、值等数据,可能在后续的各种函数实现中会更加方便
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-22 13:57:27  更:2021-07-22 13:57:58 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/2 3:27:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码