-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalue.h
73 lines (60 loc) · 1.58 KB
/
value.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
/*
value.h
Author: Jingyun Yang
Date Created: 10/2/16
Description: Header file for arithmatic type
Value that stores numbers represented in either
fraction or decimal format.
*/
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
#ifndef VALUE_H
#define VALUE_H
struct Fraction {
int up, down;
Fraction() : up(0), down(1) {}
Fraction(int u, int d) : up(u), down(d) {
// Ensure that GCD(up,down)=1
for(int k=2;k<=min(abs(up),abs(down));k++){
while(abs(up)%k==0 && abs(down)%k==0){up/=k;down/=k;}
}
}
};
class Value {
public:
// Constructors
Value();
Value(Fraction fv);
Value(double dv);
Value(string str);
// Getters
bool getDecimal() const;
Fraction getFracValue() const;
double getDecValue() const;
bool getCalculability() const;
// Print the value of the object
string printValue() const;
// Operator overload
Value& operator+=(const Value z);
Value& operator-=(const Value z);
Value& operator*=(const Value z);
Value& operator/=(const Value z);
Value& powv(const Value z);
friend Value operator+(Value a, const Value b) { return a += b; }
friend Value operator-(Value a, const Value b) { return a -= b; }
friend Value operator*(Value a, const Value b) { return a *= b; }
friend Value operator/(Value a, const Value b) { return a /= b; }
friend Value operator-(Value a) { return a*=-1; }
friend Value powv(Value a, Value b) { return a.powv(b); }
friend ostream &operator<<(ostream &out, const Value &m) {
return out << m.printValue();
}
private:
bool isDecimal;
Fraction fracValue;
double decValue;
bool calculability;
};
#endif