概要
通过运算符重载,完成了分式类,可以进行加减乘除的简单计算,运算逻辑完全采用直觉计算,支持将精度不高的小数转为分式。
类的头文件OptiCalculate.h
#pragma once
#include<iostream>
using namespace std;
class Fractional //分式类
{
public:
Fractional(); //构造默认为0/1,即有意义0
Fractional(long long a, long long b);//分式型构造
Fractional(double d); //通过小数构造分式,简易版构造,并不十分精确
~Fractional();
//判断合法 is valid -> 1
bool is_valid() const;
//输出显示
void show() const;
friend ostream& operator<<(ostream& out, Fractional F);
//化简分式->通过gcd算法
void simplify();
//相加重载
void operator+=(Fractional F);
void operator+=(long num);
void operator+=(double num);
Fractional operator+(Fractional F);
Fractional operator+(long num);
Fractional operator+(double num);
//相减重载
void operator-=(Fractional F);
void operator-=(long num);
void operator-=(double num);
Fractional operator-(Fractional F);
Fractional operator-(long num);
Fractional operator-(double num);
//相乘重载
void operator*=(Fractional F);
void operator*=(long num);
void operator*=(double num);
Fractional operator*(Fractional F);
Fractional operator*(long num);
Fractional operator*(double num);
//相除重载
void operator/=(Fractional F);
void operator/=(long num);
void operator/=(double num);
Fractional operator/(Fractional F);
Fractional operator/(long num);
Fractional operator/(double num);
private:
long long molecule;
long long denominator;
};
类的实现OptiCalculate.cpp
构造函数与析构函数
#include "OptiCalculate.h"
Fractional::Fractional():molecule(0),denominator(1) { }
Fractional::Fractional(long long a, long long b):molecule(a), denominator(b){ }
Fractional::Fractional(double d){
d *= 1E12;
this->denominator = 1E12;
this->molecule = d;
this->simplify();
}
Fractional::~Fractional(){}
有意义的判断和输出重载,化简
bool Fractional::is_valid() const{
if (denominator != 0)return 1;
else return 0;
}
void Fractional::show() const{
if (denominator == 0) {
cout << "invalid!" << endl;
}
else {
if (denominator == 1) {
cout << "[" << molecule << "]" << endl;
return;
}
else {
cout <<"["<< molecule<< "/" << denominator << "]" << endl;
}
}
}
ostream& operator<<(ostream& out, Fractional F) {
F.show();
return out;
}
//化简
void Fractional::simplify(){
if (molecule == 0 || denominator == 1)return;
//最大公约数
long long r = 0;
long long a = molecule;
long long b = denominator;
while (b) {
r = a % b;
a = b;
b = r;
}
molecule /= a;
denominator /= a;
if (denominator < 0 && molecule>0) {
denominator = -denominator;
molecule = -molecule;
}
}
加法部分
//加
void Fractional::operator+=(Fractional F){
if (F.is_valid() && this->is_valid()) {
molecule *= F.denominator;
F.molecule *= denominator;
molecule += F.molecule;
denominator *= F.denominator;
}
this->simplify();
}
void Fractional::operator+=(long num){
molecule += denominator * num;
}
void Fractional::operator+=(double num){
Fractional N = num;
*this += N;
this->simplify();
N.~Fractional();
}
Fractional Fractional::operator+(Fractional F){
Fractional N = *this;
N += F;
N.simplify();
return N;
}
Fractional Fractional::operator+(long num){
Fractional N = *this;
N += num;
N.simplify();
return N;
}
Fractional Fractional::operator+(double num){
Fractional N = *this;
N += num;
N.simplify();
return N;
}
减法部分
//减
void Fractional::operator-=(Fractional F){
if (F.is_valid() && this->is_valid()) {
molecule *= F.denominator;
F.molecule *= denominator;
molecule -= F.molecule;
denominator *= F.denominator;
}
this->simplify();
}
void Fractional::operator-=(long num){
molecule -= denominator * num;
}
void Fractional::operator-=(double num){
Fractional N = num;
*this -= N;
this->simplify();
N.~Fractional();
}
Fractional Fractional::operator-(Fractional F){
Fractional N = *this;
N -= F;
N.simplify();
return N;
}
Fractional Fractional::operator-(long num){
Fractional N = *this;
N -= num;
N.simplify();
return N;
}
Fractional Fractional::operator-(double num){
Fractional N = *this;
N -= num;
N.simplify();
return N;
}
乘法部分
//乘
void Fractional::operator*=(Fractional F){
if (F.is_valid()&&this->is_valid()) {
molecule *= F.molecule;
denominator *= F.denominator;
}
this->simplify();
}
void Fractional::operator*=(long num){
molecule *= num;
this->simplify();
}
void Fractional::operator*=(double num){
Fractional N = num;
*this += N;
this->simplify();
N.~Fractional();
}
Fractional Fractional::operator*(Fractional F){
Fractional N = *this;
N *= F;
N.simplify();
return N;
}
Fractional Fractional::operator*(long num){
Fractional N = *this;
N *= num;
N.simplify();
return N;
}
Fractional Fractional::operator*(double num){
Fractional N = *this;
N *= num;
N.simplify();
return N;
}
除法部分
//除
void Fractional::operator/=(Fractional F){
if (F.is_valid() && this->is_valid()) {
molecule *= F.denominator;
denominator *= F.molecule;
}
this->simplify();
}
void Fractional::operator/=(long num){
denominator *= num;
this->simplify();
}
void Fractional::operator/=(double num){
Fractional N = num;
*this /= N;
this->simplify();
N.~Fractional();
}
Fractional Fractional::operator/(Fractional F){
Fractional N = *this;
N /= F;
N.simplify();
return N;
}
Fractional Fractional::operator/(long num){
Fractional N = *this;
N /= num;
N.simplify();
return N;
}
Fractional Fractional::operator/(double num){
Fractional N = *this;
N /= num;
N.simplify();
return N;
}
结果展示
测试代码输入如下:
#include"OptiCalculate.h"
int main() {
Fractional f1(2, 5);
Fractional f2 = 0.625;
Fractional f3(4);
cout << f1 << f2 << f3 << endl;
f1 += 0.5;
f2 -= 0.5;
f3 *= 0.5;
Fractional f4;
f4 = f3 / 5L;
cout << f1 << f2 << f3 << f4 << endl;
f1 = f2 + f3;
f2 = f1 - f3;
f3 = f1 * f2;
f4 = f3 / f1;
cout << f1 << f2 << f3 << f4 << endl;
return 0;
}
输出如下:
结语
控制台打印分式看起来就是比小数更有感觉,不是吗?