forked from AllenCompSci/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm-string.h
More file actions
313 lines (299 loc) · 7.51 KB
/
Copy pathalgorithm-string.h
File metadata and controls
313 lines (299 loc) · 7.51 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "prototype.h"
void trimStr(std::string& x){
std:: cpy = "";
// left trim
bool val = true;
for(int i = 0; i < (int)x.length(); i++){
if(val){
if(x[i] != ' '){
val = true;
cpy = x[i];
}
}
else{
cpy += x[i];
}
}
x = cpy;
// right trim
val = true;
for(int i = x.length() - 1; i >= 0; i--){
if(val){
if(x[i] != ' '){
val = true;
cpy = x[i];
}
}
else{
cpy += x[i];
}
}
x = cpy;
bool neg = isNeg(x);
if(neg)
sign(x);
else
trimAbsStr(x);
if(neg && x!= "0")
x = '-' + x;
}
void trimStr(std::string& x, std::string& y){
trimStr(x);
trimStr(y);
}
void trimAbsStr(std::string& x){
while((int)x.length() > 0 && x[0] == '0')
x = x.substr(1);
}
void sign(std::string& x){
if(isNeg(x)){
x = abs(x);
trimAbsStr();
}
else{
trimAbsStr();
x = "-" + x;
}
}
void placeHandler(std::string& x, std::string& y){
trimStr(x, y);
bool x_isNeg = isNeg(x);
bool y_isNeg = isNeg(y);
if(x_isNeg)
sign(x);
if(y_isNeg)
sign(y);
while(cmprLen(x,y) != 0){
if(cmprLen(x,y) < 1){
x = "0" + x;
}
else{
y = "0" + y;
}
}
if(x_isNeg)
sign(x);
if(y_isNeg)
sign(y);
}
void placeHandler(std::vector<std::string>& x){
int x_Size = (int)x.size();
std::vector<bool> isNegVec;
int maxSize = 0;
for(int i = 0; i < x_Size; i++){
trimStr(x[i]);
bool x_isNeg = isNeg(x[i]);
if(x_isNeg)
sign(x[i]);
isNegVec.push_back(x_isNeg);
maxSize = maxSize > (int)x[i].length() ? maxSize : (int)x[i].length();
}
for(int i = 0; i < x_Size; i++){
while((int)x[i].length() < maxSize){
x[i] = "0" + x[i];
}
if(isNegVec[i]){
sign(x[i]);
}
}
}
int cmprLen(std::string x, std::string y){
return ((int)x.length() != (int) y.length()) ? ((int)x.length() - (int)y.length()) / abs((int)x.length() - (int)y.length()) : 0;
}
std::string abs(std::string x){
return isNeg(x) ? x.substr(1) : x;
}
std::string ADD(std::vector<std::string> NUMS){ // ADD for Multiply when vectors are created
int size_NUMS = (int) NUMS.size();
std::vector<std::string> POS;
std::vector<std::string> NEG;
std::vector<std::string> tmp;
for(int i = 0; i < size_NUMS; i++){
if(isNeg(NUMS[i]))
NEG.push_back(NUMS[i]);
else
POS.push_back(NUMS[i]);
}
placeHandler(POS);
placeHandler(NEG);
tmp.clear();
/// Stopping point USE tmp to hold the poped values and clear between pos and neg. stop when tmp.clear() is empty and pos/neg size = 1
/* DONT WANT TO DO... THIS IS CURRENT IMPLEMENTATION
do{
do{
tmp.push_back(ADD(POS.pop(), POS.pop()));
}while((int)(POS.size())>2);
do{
POS.push_back(tmp.pop());
}while(!tmp.empty());
placeHandler(POS);
}while((int)(POS.size()) == 1);
INSTEAD
*/
std::string temp = "0";
int carry = 0;
if((int)POS.size()>0){
for(int j = POS[0].length() - 1; j >= 0; j--){ // j index of string
int HOLD = 0;
for(int i = 0; i < (int)POS.size(); i++){ // i index of vector
HOLD += POS[i][j] - 48;
}
HOLD += carry; // ADDS PREVIOUS CARRY
temp = (char)((HOLD % 10) + 48) + temp;
carry = HOLD / 10; // Integer Division
}
while(carry != 0){
temp = (char)((carry % 10) + 48) + temp;
carry /= 10;
}
}
else{
temp = "0";
}
POS.clear();
POS.push_back(temp);
temp = "";
if((int)NEG.size()>0){
for(int j = NEG[0].length() - 1; j >= 0; j--){ // j index of string
int NEG = 0;
for(int i = 0; i < (int)NEG.size(); i++){ // i index of vector
HOLD += NEG[i][j] - 48;
}
HOLD += carry; // ADDS PREVIOUS CARRY
temp = (char)((HOLD % 10) + 48) + temp;
carry = HOLD / 10; // Integer Division
}
while(carry != 0){
temp = (char)((carry % 10) + 48) + temp;
carry /= 10;
}
}
else{
temp = "0";
}
return SUB(POS.pop(), temp);
}
std::string ADD(std::string x, std::string y){
if(isNeg(x) && isNeg(y)){
sign(x);
sign(y);
return "-" + ADD(x,y);
}
else if(isNeg(x)){
sign(x);
return SUB(y,x);
}
else if(isNeg(y)){
sign(y);
return SUB(x,y);
}
placeHandler(x,y);
std::result = "";
int carry = 0;
for(int i = (int)x.length() - 1; i >= 0; i--){
char value = x[i] /* char value is 48 larger than numeric */+ y[i] /* same */ - 48 /* we have to subtract 48 2 times then add it back once */ + carry;
if(value > '9'){
carry = 1;
value -= 10;
}
else
carry = 0;
result = (char)value + result;
}
if(carry > 0)
result = (char)(carry+48) + result;
trimStr(result);
return result;
}
std::string SUB(std::string x, std::string y){ // THIS IS X - Y
if(isNeg(x) && isNeg(y)){
// - (x - y) Need to check if y is larger than x. AFTER NEG
sign(x);
sign(y);
return SUB(y,x);
}
else if(isNeg(x)){
sign(x);
return "-" + ADD(x, y);
}
else if(isNeg(y)){
sign(y);
return ADD(x,y);
}
// Decimal Future
if(isLessThan(x,y){
return "-" + SUB(y,x);
}
placeHandler(x,y);
std::string result = "";
// This X will Always be greater than Y
for(int i = (int)x.length()-1; i>= 0; i--){
int val = x[i] - y[i] - 48;
if(val < 48){
val += 10;
x[i-1]--;
}
result = (char)(val + 48) + result;
}
return result;
}
std::string DIVIDE(std::string, std::string){
}
std::string INTDIV(std::string x, std::string y){
std::string temp = DIVIDE(x,y);
if(temp.find(' ')>0){
temp = temp.substr(0,temp.find(' '));
}
trimStr(temp);
return temp;
}
std::string MULTI(std::string, std::string){
}
std::string MOD(std::string, std::string){ /// Do Mod Last... Neeed Add. Sub Mult. Divide. IntDiv
}
std::string gcd(std::string x, std::string y){
trimStr(x,y);
if(isEqualTo(y, "0")){
return x;
}
else
return gcd(y, MOD(x,y));
}
bool isLessThan(std::string x, std::string y){
if(isNeg(x)){
if(!isNeg(y)){
return true;
}
}
else if (isNeg(y)){
return false;
}
if(cmprLen(x,y) == 0){
for(int i = 0; i < (int)x.length(); i++)
if(x[i] < y[i]){
return true;
}
else if(x[i] != y[i]{
return false;
}
}
else if(cmprLen(x,y)<1){
return true;
}
return false;
}
bool isEqualTo(std::string x, std::string y){
return x == y;
}
bool isGreaterThan(std::string, std::string){
return !isEqualtTo(x,y) && !isLessThan(x,y);
}
bool isNeg(std::string x){
return ((int)x.length() > 0) && x[0] == '-';
}
bool isEven(std::string x){
return x[x.length() - 1] % 2 == 0;
}
bool isOdd(std::string x){
return !isEven(x);
}