-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigReal.cpp
More file actions
74 lines (61 loc) · 2.09 KB
/
Copy pathBigReal.cpp
File metadata and controls
74 lines (61 loc) · 2.09 KB
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
#include "BigReal.h"
#include <sstream>
BigReal::BigReal() : wholePart(0), fractionalPart(0) {
// Initialize your data members here.
}
BigReal::BigReal(const std::string& realNumber) {
parseRealNumber(realNumber);
}
BigReal::BigReal(const BigReal& other) : wholePart(other.wholePart), fractionalPart(other.fractionalPart) {
// Copy the data members from the other object.
}
BigReal& BigReal::operator=(const BigReal& other) {
if (this != &other) {
wholePart = other.wholePart;
fractionalPart = other.fractionalPart;
}
return *this;
}
BigReal BigReal::operator+(const BigReal& other) const {
BigReal result;
result.wholePart = wholePart + other.wholePart;
result.fractionalPart = fractionalPart + other.fractionalPart;
// Adjust the whole part when the fractional part exceeds or equals 1,000,000
if (result.fractionalPart >= 1000000) {
result.wholePart += 1;
result.fractionalPart -= 1000000;
}
return result;
}
BigReal BigReal::operator-(const BigReal& other) const {
BigReal result;
result.wholePart = wholePart - other.wholePart;
result.fractionalPart = fractionalPart - other.fractionalPart;
// Adjust the whole part and fractional part for borrowing
while (result.fractionalPart < 0) {
result.fractionalPart += 1000000;
result.wholePart--;
}
return result;
}
std::string BigReal::toString() const {
std::stringstream ss;
ss << wholePart << ".";
if (fractionalPart < 100000) {
ss << "0"; // Add a leading zero for fractional parts less than 100000
}
ss.fill('0');
ss.width(5);
ss << fractionalPart;
return ss.str();
}
void BigReal::parseRealNumber(const std::string& realNumber) {
size_t dotPos = realNumber.find('.');
if (dotPos != std::string::npos) {
wholePart = std::stoi(realNumber.substr(0, dotPos));
fractionalPart = std::stoll(realNumber.substr(dotPos + 1));
} else {
wholePart = std::stoi(realNumber);
fractionalPart = 0;
}
}