- #ifndef FRACTION_H_INCLUDED
- #define FRACTION_H_INCLUDED
- #include <iostream>
- class CFraction
- {
- //整数和分数的四则运算
- friend CFraction operator+(int ifrac, CFraction& ffrac);
- friend CFraction operator-(int ifrac, CFraction& ffrac);
- friend CFraction operator*(int ifrac, CFraction& ffrac);
- friend CFraction operator/(int ifrac, CFraction& ffrac);
- friend std::istream& operator>>(std::istream& is, CFraction& obj);
- friend std::ostream& operator<<(std::ostream& os, CFraction& obj);
- public:
- CFraction(int up = 0,int down = 1):nume(up),deno(down)
- {
- if(down == 0)
- std::cerr << "Deno Error" << std::endl;
- correct();
- }
- CFraction(const CFraction &fraction):nume(fraction.nume),deno(fraction.deno){}//拷贝构造函数
- //分数的四则运算
- CFraction operator+(CFraction& frac);
- CFraction operator-(CFraction& frac);
- CFraction operator*(const CFraction& frac);
- CFraction operator/(const CFraction& frac);
- //分数和整数的四则运算
- CFraction operator+(int frac);
- CFraction operator-(int frac);
- CFraction operator*(const int frac);
- CFraction operator/(const int frac);
- //一目运算符
- CFraction operator+();//取正
- CFraction operator-();//取反
- CFraction operator~();//取倒数
- private:
- void correct();//保持负号在分子上
- void same(CFraction& num);//通分
- void reduction();//化简自己
- int greatestCommonDivisor(int, int);//最大公约数
- int nume;//分子
- int deno;//分母
- };
- #endif // FRACTION_H_INCLUDED
Fraction.cpp
- #include "Fraction.h"
- //输入输出运算符的重载放在头文件会出错
- std::istream& operator>>(std::istream& is, CFraction& obj)
- {
- char temp;
- is >> obj.nume >> temp >> obj.deno;
- return is;
- }
- std::ostream& operator<<(std::ostream& os, CFraction& obj)
- {
- //如果分母是1
- if(obj.deno == 1)
- os << obj.nume;
- //如果分子是0
- else if(obj.nume == 0)
- os << obj.nume;
- else
- os << obj.nume << "/" << obj.deno;
- return os;
- }
- //通分是两个分数对象,化简是一个分数
- int CFraction::greatestCommonDivisor(int num1, int num2)//最大公约数
- {
- int temp;
- num1 = num1 > 0 ? num1:-num1;
- num2 = num2 > 0 ? num2:-num2;
- while(num1 % num2)
- {
- temp = num1;
- num1 = num2;
- num2 = temp%num2;
- }
- return num2;
- }
- void CFraction::reduction()//化简自己
- {
- int re = greatestCommonDivisor(nume,deno);
- nume /= re;
- deno /= re;
- }
- void CFraction::same(CFraction& num)//通分
- {
- int re = greatestCommonDivisor(this->deno,num.deno);
- re = (this->deno * num.deno) / re;//最小公倍数
- int mul = re / this->deno;
- this->deno *= mul;
- this->nume *= mul;
- mul = re / num.deno;
- num.deno *= mul;
- num.nume *= mul;
- }
- void CFraction::correct()
- {
- if((deno < 0 && nume < 0) ||(deno < 0 && nume > 0))
- {
- deno = -deno;
- nume = -nume;
- }
- }
- //分数和之间的四则元算
- CFraction CFraction::operator*(const CFraction& frac)
- {
- CFraction temp;
- temp.nume = this->nume * frac.nume;
- temp.deno = this->deno * frac.deno;
- temp.reduction();
- temp.correct();
- return temp;
- }
- CFraction CFraction::operator/(const CFraction& frac)
- {
- CFraction temp;
- temp.nume = this->nume * frac.deno;
- temp.deno = this->deno * frac.nume;
- temp.reduction();
- temp.correct();
- return temp;
- }
- CFraction CFraction::operator+(CFraction& frac)
- {
- CFraction temp;
- if(this->deno != frac.deno)
- (*this).same(frac);
- temp.nume = this->nume + frac.nume;
- temp.deno = this->deno;
- temp.reduction();
- this->reduction();
- frac.reduction();
- return temp;
- }
- CFraction CFraction::operator-(CFraction& frac)
- {
- CFraction temp;
- if(this->deno != frac.deno)
- (*this).same(frac);
- temp.nume = this->nume - frac.nume;
- temp.deno = this->deno;
- temp.reduction();
- this->reduction();
- frac.reduction();
- return temp;
- }
- //分数和整数的四则运算
- CFraction CFraction::operator*(int frac)
- {
- CFraction temp;
- temp.nume = this->nume * frac;
- temp.deno = this->deno;
- temp.reduction();
- temp.correct();
- return temp;
- }
- CFraction CFraction::operator/(int frac)
- {
- CFraction temp;
- temp.deno = this->deno * frac;
- temp.nume = this->nume;
- temp.reduction();
- temp.correct();
- return temp;
- }
- CFraction CFraction::operator+(int frac)
- {
- CFraction temp;
- frac *= this->deno;
- temp.nume = this->nume + frac;
- temp.deno = this->deno;
- temp.reduction();
- return temp;
- }
- CFraction CFraction::operator-(int frac)
- {
- CFraction temp;
- frac *= this->deno;
- temp.nume = this->nume - frac;
- temp.deno = this->deno;
- temp.reduction();
- return temp;
- }
- //整数和分数的四则运算
- CFraction operator*(int ifrac, CFraction& ffrac)
- {
- CFraction temp;
- temp.nume = ifrac * ffrac.nume;
- temp.deno = ffrac.deno;
- temp.reduction();
- temp.correct();
- return temp;
- }
- CFraction operator/(int ifrac, CFraction& ffrac)
- {
- CFraction temp;
- temp.nume = ifrac * ffrac.deno;
- temp.deno = ffrac.nume;
- temp.reduction();
- temp.correct();
- return temp;
- }
- CFraction operator+(int ifrac, CFraction& ffrac)
- {
- CFraction temp;
- ifrac *= ffrac.deno;
- temp.nume = ffrac.nume + ifrac;
- temp.deno = ffrac.deno;
- temp.reduction();
- return temp;
- }
- CFraction operator-(int ifrac, CFraction& ffrac)
- {
- CFraction temp;
- ifrac *= ffrac.deno;
- temp.nume = ifrac - ffrac.nume;
- temp.deno = ffrac.deno;
- temp.reduction();
- return temp;
- }
- //一目运算符
- CFraction CFraction::operator+()
- {
- return *this;
- }
- CFraction CFraction::operator-()
- {
- CFraction temp;
- temp.nume = -this->nume;
- temp.deno = this->deno;
- return temp;
- }
- CFraction CFraction::operator~()
- {
- CFraction temp;
- temp.nume = this->deno;
- temp.deno = this->nume;
- temp.correct();
- return temp;
- }
main.cpp
- #include "Fraction.h"
- using namespace std;
- int main()
- {
- CFraction n1(1,-2),n2(1,2);
- CFraction n3 = n2 * n1;
- cout << n3 << endl;
- n3 = n1 / n2;
- cout << n3 << endl;
- n3 = n1 + n2;
- cout << n3 << endl;
- n3 = n1 - n2;
- cout << n3 << endl;
- n3 = 1 + n1;
- cout << n3 << endl;
- n3 = 3 * n2;
- cout << n3 << endl;
- n3 = 1 / n1;
- cout << n3 << endl;
- n3 = 1 - n1;
- cout << n3 << endl;
- return 0;
- }