-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigDecimalInt.cpp
188 lines (166 loc) · 4.33 KB
/
BigDecimalInt.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
/// Course: CS213 - Programming II - 2018
/// Title: Assignment 2 - Task 3
/// Program: CS213-2018-A1-T3
/// Author: Dr. Mohammad El-Ramly
/// Date: 10 August 2018
/* programmers: Ahmed Sayed Mansour 20170022
Eslam Saleh Mohamed 20170046
Eslam Mohamed Mahmoud 20170049
*/
/// last edit : 26/10/2018
/// Version: 1.0
#include "BigDecimalInt.h"
//A function to fill all elements of the another smaller string with zeros
void BigDecimalInt::fillZeros(int maxSize, string& str1, string& str2)
{
int diff;
if (str1.length() > str2.length())
{
diff = maxSize - str2.length();
for (int i = 0; i < diff; i++)
str2 = "0" + str2;
}
else if (str1.length() < str2.length())
{
diff = maxSize - str1.length();
for (int i = 0; i < diff; i++)
str1 = "0" + str1;
}
}
//Constractor of adding a string value
BigDecimalInt::BigDecimalInt(string num)
{
str = num;
}
//Constractor of adding an integer value and converting to a string
BigDecimalInt::BigDecimalInt(int num = 0)
{
stringstream convert;
convert << num;
str = convert.str();
}
//Operator + to get the sum of numbers
BigDecimalInt BigDecimalInt::operator+ (BigDecimalInt other)
{
string first = str;
string second = other.str;
bool bothNegative = false;
if (first[0] == '-' && second[0] == '-') //if both are negative, then add and append -ve sign to the result
{
bothNegative = true;
first = first.substr(1);
second = second.substr(1);
}
else if (first[0] == '-')
{
BigDecimalInt negative(first.substr(1)); // removing the -ve sign;
return other - negative;
}
else if (second[0] == '-')
{
BigDecimalInt negative(second.substr(1)); // removing the -ve sign;
BigDecimalInt thisStr(first);
return thisStr - negative;
}
int maxSize = (first.length() > second.length()) ? first.length() : second.length();
fillZeros(maxSize, first, second);
string result = "";
int temp;
bool carry = false;
for (int i = maxSize - 1; i >= 0; i--)
{
temp = (first[i] + second[i] - 48);
if (carry)
temp = (first[i] + second[i] - 48) + 1;
if ((temp - 48) > 9)
{
temp -= 10;
carry = 1;
}
else
carry = 0;
result = (char)(temp)+result;
}
if (carry)
result = "1" + result;
if (bothNegative)
result = "-" + result;
BigDecimalInt sum(result);
return sum;
}
//Operator - to get the value of number - another number
BigDecimalInt BigDecimalInt::operator- (BigDecimalInt other)
{
string first = str;
string second = other.str;
if (first[0] == '-' && second[0] == '-')
//if both are negative, then other becomes +ve ; -123- (-200) = -123 + 200 = 200-123
{
first = first.substr(1);
second = second.substr(1);
// swap the two numbers
string temp = first;
first = second;
second = temp;
}
else if (first[0] == '-')
{
//(if the first is negative, then call + and add -ve sign to the result (-123 - 200) = -(123+200)
first = first.substr(1);
BigDecimalInt temp(first);
BigDecimalInt sum = temp + other;
sum.str = "-" + sum.str;
return sum;
}
else if (second[0] == '-')
{
//(if the second is negative, then call Add (100 - (-100) = 100+100)
BigDecimalInt negative(second.substr(1)); // removing the -ve sign;
BigDecimalInt thisStr(first);
return thisStr + negative;
}
int maxSize = (first.length() > second.length()) ? first.length() : second.length();
fillZeros(maxSize, first, second);
bool secondIsGreater = false;
if (second[0] > first[0])
{
// swap the two numbers and add -ve sign to the result
string temp = first;
first = second;
second = temp;
secondIsGreater = true;
}
string result = "";
int temp;
bool borrow = false;
for (int i = maxSize - 1; i >= 0; i--)
{
temp = first[i];
if (borrow)
{
temp = first[i] - 1;
borrow = false;
}
if (temp >= second[i])
{
result = (char)(temp - second[i] + 48) + result;
borrow = false;
}
else if (temp < second[i]) // borrow
{
temp += 10;
borrow = true;
result = (char)(temp - second[i] + 48) + result;
}
}
if (secondIsGreater)
result = "-" + result;
BigDecimalInt sub(result);
return sub;
}
//A friend Ostream function to print out the requeted value
ostream& operator << (ostream& out, BigDecimalInt printStr)
{
out << printStr.str;
return out;
}