-
Notifications
You must be signed in to change notification settings - Fork 0
/
fraction.h
45 lines (44 loc) · 1.15 KB
/
fraction.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef _FRACTION_
#define _FRACTION_
#include <iostream>
#include <cstdlib>
using namespace std;
class Fraction
{
private:
long numerator, denominator;
public:
Fraction(long n = 0, long d = 1);
Fraction operator-() const
{
return Fraction(-numerator, denominator);
}
Fraction &operator+=(const Fraction &a)
{
numerator = a.numerator * denominator + numerator * a.denominator;
denominator *= a.denominator;
return *this;
}
Fraction &operator-=(const Fraction &a)
{
*this += (-a);
return *this;
}
Fraction &operator++()
{
numerator += denominator;
return *this;
}
Fraction &operator--()
{
numerator -= denominator;
return *this;
}
friend Fraction operator+(const Fraction &, const Fraction &);
friend Fraction operator-(const Fraction &, const Fraction &);
friend Fraction operator*(const Fraction &, const Fraction &);
friend Fraction operator/(const Fraction &, const Fraction &);
friend ostream &operator<<(ostream &os, const Fraction &a);
friend istream &operator>>(istream &is, Fraction &a);
};
#endif