-
Notifications
You must be signed in to change notification settings - Fork 0
/
frac.h
53 lines (42 loc) · 1.43 KB
/
frac.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
46
47
48
49
50
51
52
53
#ifndef FRAC_H
#define FRAC_H
#include <string>
struct Bad_frac{
std::string err_name;
Bad_frac(const char* n) : err_name{n} {}
Bad_frac(std::string n) : err_name{n} {}
};
inline void error(const char* err_name){ throw Bad_frac(err_name);}
class Fraction{
double a,b;
double gcd(double a, double b);
void simplify (double& a, double& b);
public:
Fraction (double a=0, double b=1);
Fraction reciprocal () const;
std::ostream& print(std::ostream& o) const;
Fraction& operator - ();
Fraction& operator += (const Fraction& rhs);
Fraction& operator *= (const Fraction& rhs);
Fraction& operator /= (const Fraction& rhs);
Fraction& operator -= (const Fraction& rhs);
bool operator == (const Fraction& rhs) const {
return (this->a * rhs.b) == (this->b * rhs.a);
}
bool operator != (const Fraction& rhs) const {
return (this->a * rhs.b) != (this->b * rhs.a);
}
bool operator < (const Fraction& rhs) const {
return (this->a * rhs.b) < (this->b * rhs.a);
}
bool operator > (const Fraction& rhs) const {
return (this->a * rhs.b) > (this->b * rhs.a);
}
double eval (){ return a / b; }
};
Fraction operator + (const Fraction& x, const Fraction& y);
Fraction operator - (const Fraction& x, const Fraction& y);
Fraction operator * (const Fraction& x, const Fraction& y);
Fraction operator / (const Fraction& x, const Fraction& y);
std::ostream& operator << (std::ostream& o, const Fraction& f);
#endif