-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fraction.h
89 lines (75 loc) · 1.67 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Fraction.h
*
* Created on: Feb 24, 2013
* Author: Zack
*/
#ifndef FRACTION_H_
#define FRACTION_H_
#include <iostream>
class Fraction
{
public:
//constructors
inline Fraction();
inline Fraction(int num);
inline Fraction(int num, int den);
inline Fraction(Fraction const &src);
//setters and getters
inline void set(int n, int d);
inline int get_num();
inline int get_den();
//mathmatical operations
Fraction add(const Fraction &other);
Fraction mult(const Fraction &other);
Fraction sub(const Fraction &other);
Fraction div(const Fraction &other);
//operators
//TODO these are inefficient it would be better to pass by reference
Fraction operator+(Fraction &other);
Fraction operator-(Fraction &other);
Fraction operator*(Fraction &other);
Fraction operator/(Fraction &other);
bool operator==(Fraction &other);
friend std::ostream &operator<<(std::ostream &os, Fraction &fract);
private:
int num, den;
void normalize();
int gcf(int a, int b);
int lcm(int a, int b);
};
Fraction::Fraction()
{
set(0, 1);
}
Fraction::Fraction(int num)
{
set(num, 1);
}
Fraction::Fraction(int num, int den)
{
set(num, den);
}
Fraction::Fraction(Fraction const &src)
{
num = src.num;
den = src.den;
}
void
Fraction::set(int n, int d)
{
num=n;
den=d;
normalize();
}
int
Fraction::get_num()
{
return num;
}
int
Fraction::get_den()
{
return den;
}
#endif /* FRACTION_H_ */