-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalue.cpp
194 lines (175 loc) · 4.32 KB
/
value.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
value.cpp
Author: Jingyun Yang
Date Created: 10/2/16
Description: Implementation of Value Class.
*/
#include "value.h"
Value::Value()
: isDecimal(false), fracValue(Fraction()), decValue(0.0), calculability(false) {}
Value::Value(Fraction fv) {
if(fv.down == 0) {
cerr << "Arithmatic error: Denominator is zero! ";
Value();
return;
}
isDecimal = false;
fracValue = fv;
decValue = (double)fv.up / fv.down;
calculability = true;
}
Value::Value(double dv) {
Value newValue = Value(to_string(dv));
*this = newValue;
}
Value::Value(string str) {
Value newValue;
int found = str.find('.');
if(found != string::npos) {
string left = str.substr(0,found);
string right = str.substr(found + 1);
if(left.length() >= 10) {
cerr << "Arithmatic error: Number too large! ";
*this = Value();
return;
}
while(right.length() > 0 && right[right.length()-1] == '0') {
right = right.substr(0,right.length()-1);
}
if(right.length() == 0) {
newValue = Value(Fraction(stoi(left), 1));
*this = newValue;
return;
}
else if(right.length() <= 5){
if(right.find('.') != string::npos) {
cerr << "Arithmatic Error: More than one '.' in a number! ";
Value();
return;
}
int leftNumber = stoi(left), rightNumber = stoi(right);
int multiplier = pow(10.0,(floor)(log10(rightNumber)) + 1.0);
newValue = Value(Fraction(leftNumber*multiplier+rightNumber,multiplier));
*this = newValue;
return;
}
else {
isDecimal = true;
fracValue = Fraction();
decValue = stod(str);
calculability = true;
return;
}
}
newValue = Value(Fraction(stoi(str),1));
*this = newValue;
return;
}
bool Value::getDecimal() const {
return isDecimal;
}
Fraction Value::getFracValue() const {
return fracValue;
}
double Value::getDecValue() const {
return decValue;
}
bool Value::getCalculability() const {
return calculability;
}
string Value::printValue() const {
if(!calculability) return "";
if(isDecimal) {
return to_string(decValue);
}
else if(fracValue.down == 1 || fracValue.up == 0) {
return to_string(fracValue.up);
}
else {
return to_string(fracValue.up) + "/" + to_string(fracValue.down);
}
}
Value& Value::operator+=(const Value z) {
if(!calculability || !z.getCalculability()) {
*this = Value();
return *this;
}
if(isDecimal || z.getDecimal()) {
*this = Value(decValue+z.getDecValue());
}
else {
*this = Value(Fraction(fracValue.up*z.getFracValue().down
+fracValue.down*z.getFracValue().up
,z.getFracValue().down*fracValue.down));
}
return *this;
}
Value& Value::operator-=(const Value z) {
if(!calculability || !z.getCalculability()) {
*this = Value();
return *this;
}
if(isDecimal || z.getDecimal()) {
*this = Value(decValue-z.getDecValue());
}
else {
*this = Value(Fraction(fracValue.up*z.getFracValue().down
-fracValue.down*z.getFracValue().up
,z.getFracValue().down*fracValue.down));
}
return *this;
}
Value& Value::operator*=(const Value z) {
if(!calculability || !z.getCalculability()) {
*this = Value();
return *this;
}
if(isDecimal || z.getDecimal()) {
*this = Value(decValue*z.getDecValue());
}
else {
*this = Value(Fraction(z.getFracValue().up*fracValue.up
,z.getFracValue().down*fracValue.down));
}
return *this;
}
Value& Value::operator/=(const Value z) {
if(!calculability || !z.getCalculability()) {
*this = Value();
return *this;
}
if(isDecimal || z.getDecimal()) {
*this = Value(decValue/z.getDecValue());
}
else {
*this = Value(Fraction(z.getFracValue().down*fracValue.up
,z.getFracValue().up*fracValue.down));
}
return *this;
}
Value& Value::powv(const Value z) {
if(!calculability || !z.getCalculability()) {
*this = Value();
return *this;
}
if(decValue < 0 && z.getDecValue() != (double)(int(z.getDecValue()))) {
cerr << "Arithmatic error: Can't power a negative number by a non-integer! ";
*this = Value();
return *this;
}
if(!isDecimal && z.getDecValue() == (double)(int(z.getDecValue()))) {
fracValue.up = pow(fracValue.up,fabs(z.getDecValue()));
fracValue.down = pow(fracValue.down,fabs(z.getDecValue()));
if(z.getDecValue() < 0) {
swap(fracValue.up,fracValue.down);
if(fracValue.down < 0) {
fracValue.up *= -1;
fracValue.down *= -1;
}
}
}
else {
*this = Value(pow(decValue,z.getDecValue()));
}
return *this;
}