-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnumber.cpp
429 lines (384 loc) · 12.8 KB
/
number.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*************************
* number.cpp
* editor: amo
*************************/
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bitset>
#include <cmath>
#include <vector>
#include <number.h>
#include <algorithm> // std::for_each
#include <stack>
using namespace amo;
/* -------------------------------------------------------------------- */
/* my define macro */
/* -------------------------------------------------------------------- */
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
/* -------------------------------------------------------------------- */
/* global variables */
/* -------------------------------------------------------------------- */
// Allocating and initializing Class's static data member
Number* Number::instance = 0;
/* -------------------------------------------------------------------- */
/* implements */
/* -------------------------------------------------------------------- */
amo::Number::Number() {
std::cout << "[Number()]:" << this << std::endl;
}
amo::Number::~Number() {
std::cout << "[~Number()]:" << this << std::endl;
}
Number& amo::Number::getInstance() {
if (instance == nullptr) {
std::cout << "[getInstance()]:going to 'instance = new Number()':" << &instance << std::endl;
instance = new Number();
}
std::cout << "[getInstance()]:returns:" << instance << std::endl;
return *instance;
}
int amo::Number::getBinCount(int num) {
int count = 0;
while (num > 0) {
if ((num&0x1)>0) count++;
std::cout << "[getBinCount(int)]: in while-loop, count:" << count << " and parameter:" << std::bitset<sizeof(int)*8>(num) << " (" << num << ")" << std::endl;
num >>= 1;
}
return count;
}
#if 0
long long amo::Number::power2(int n) {
if (n < 0) return 0;
long long pow = 1;
std::cout << "[power2(int)]: pow:" << pow << std::endl;
//while (0 < (--n)) {
while (0 < n--) {
pow <<= 1;
std::cout << "[power2(int)]: in while-loop, n:" << n << " and pow:" << pow << std::endl;
}
return pow;
}
#else
inline long long sqr(long long n) {return n * n;}
long long amo::Number::power2(int n) {
if (n < 0) return 0;
if (n == 0) {
std::cout << "[power2(int)]: n == 0 and returns 1" << std::endl;
return 1;
}
if (n & 1) std::cout << "[power2(int)]: n:" << n << " and going to sqr(power2(" << n/2 << ")) << 1" << std::endl; //odd
else std::cout << "[power2(int)]: n:" << n << " and going to sqr(power2(" << n/2 << "))" << std::endl; //even
return (n & 1) ? sqr(power2(n/2)) << 1 : sqr(power2(n/2));
}
#endif
long long amo::Number::power(int base, int n) {
if (n < 0) return 0;
long long pow = 1;
std::cout << "[power2(int)]: pow:" << pow << std::endl;
while (0 < n--) {
pow = pow*base;
std::cout << "[power2(int)]: in while-loop, n:" << n << " and pow:" << pow << std::endl;
}
return pow;
}
int amo::Number::akermann(int m, int n) {
if (m < 0 || n <0) {
std::cout << "[akermann(int, int)]: !!! m < 0 || n < 0 returns:" << -1 << std::endl;
return -1;
}
if (m == 0) {
std::cout << "[akermann(int, int)]: m == 0 returns:" << n+1 << std::endl;
return n+1;
}
if (n == 0) {
std::cout << "[akermann(int, int)]: m > 0 && n == 0 going to akermann(" << m-1 << ", " << 1 << "))" << std::endl;
return akermann(m-1, 1);
} else {
std::cout << "[akermann(int, int)]: m > 0 && n > 0 going to akermann(" << m-1 << ", akermann(" << m << ", " << n-1 << "))" << std::endl;
return akermann(m-1, akermann(m, n-1));
}
}
int amo::Number::hailstone(int n) {
int run = 1;
if (n < 1) {
std::cout << "[hailstone(int)]: !!! n < 1 returns:" << -1 << std::endl;
return 1;
}
if (n == 1) {
std::cout << "[hailstone(int)]: n == 1 && run = 0 returns:" << 0 << std::endl;
return 0;
}
if (n%2 == 0) {
std::cout << "[hailstone(int)]: even and going to hailstone(n/2:" << n/2 << ")" << std::endl;
run += hailstone(n/2);
} else {
std::cout << "[hailstone(int)]: odd and going to hailstone((3*n+1):" << 3*n+1 << ")" << std::endl;
run += hailstone(3*n+1);
}
std::cout << "[hailstone(int)]: returns:" << run << std::endl;
return run;
}
#if 0
int amo::Number::sum(std::vector<int>& vector) {
int back = 0;
int sum = 0;
if (vector.size() < 0) {
std::cout << "[sum(std::vector<int>&)]: !!! n < 0 returns:" << 0 << std::endl;
return 0;
}
if (vector.size() == 0) {
std::cout << "[sum(std::vector<int>&)]: size == 0 returns:" << 0 << std::endl;
return 0;
}
back = vector.back();
vector.pop_back();
int recursion = Number::sum(vector);
std::cout << "[sum(std::vector<int>&)]: going to sum = back:" << back << " + sum() recurrently:" << recursion << std::endl;
sum = back + recursion;
std::cout << "[sum(std::vector<int>&)]: returns:" << sum << std::endl;
return sum;
}
#else
struct functor {
void operator() (int element) {std::cout << "element:" << element << std::endl;}
} my_functor;
int amo::Number::sum(std::vector<int>& vector) {
if (vector.size() < 0) {
std::cout << "[sum(std::vector<int>&)]: !!! n < 0 returns:" << 0 << std::endl;
return 0;
}
if (vector.size() == 0) {
std::cout << "[sum(std::vector<int>&)]: size == 0 returns:" << 0 << std::endl;
return 0;
}
if (vector.size() == 1) {
std::cout << "[sum(std::vector<int>&)]: size == 0 returns:" << 0 << std::endl;
return vector.back();
}
int back = 0;
int sum = 0;
int mi = vector.size()/2;
std::vector<int> v1, v2;
v1.assign(vector.begin(), std::next(vector.begin(), mi));
v2.assign(std::next(vector.begin(), mi), vector.end());
std::cout << "[sum(std::vector<int>&)]: v1:" << std::endl;
for_each(v1.begin(), v1.end(), my_functor);
std::cout << "[sum(std::vector<int>&)]: v2:" << std::endl;
for_each(v2.begin(), v2.end(), my_functor);
sum = Number::sum(v1) + Number::sum(v2);
return sum;
}
#endif
int amo::Number::factory(int n) {
if (n == 1) return 1;
else return n*(factory(n-1));
}
#if 0
/**
* O(2^n)
*/
int amo::Number::fibonacci(int n) {
if (n < 2) {
std::cout << "[fibonacci(int)]: n < 2 returns:" << n << std::endl;
return n;
} else {
int fib = fibonacci(n-1) + fibonacci(n-2);
std::cout << "[fibonacci(int)]: n = " << n << " returns:" << fib << std::endl;
return fib;
}
}
#else
/**
* O(n)
*/
int amo::Number::fibonacci(int n) {
int g = 1; //fib(k)
int f = 0; //fib(k-1)
int tmp;
int k = n-1; //pattern starts from n = 1 not 0
while (0 < k--) {
tmp = g + f;
f = g;
g = tmp;
}
return g;
}
#endif
/**
* base case of recursion could be set any n with known return and prev
* O(n)
*/
int amo::Number::fibonacci(int n, int& pp) {
if (n < 0) {
int prevprev = 0;
int prev = 0;
pp = prev;
return 0;
}
else if (n == 1) {
int prevprev = 0;
int prev = 0;
pp = prev;
std::cout << "[fibonacci(int)]: Base case of recursion (n == 1) returns:" << (prev + prevprev) << " and pp:" << pp << std::endl;
return 1;
}
else if (n == 2) {
int prevprev = 0;
int prev = 1;
pp = prev;
std::cout << "[fibonacci(int)]: Base case of recursion (n == 2) returns:" << (prev + prevprev) << " and pp:" << pp << std::endl;
return 1;
}
else if (n == 3) {
int prevprev = 1;
int prev = 1;
pp = prev;
std::cout << "[fibonacci(int)]: Base case of recursion (n == 3) returns:" << (prev + prevprev) << " and pp:" << pp << std::endl;
return prev + prevprev;
} else {
int prevprev;
int prev;
prev = fibonacci(n-1, prevprev);
pp = prev;
std::cout << "[fibonacci(int)]: n:" << n << " returns:" << (prev + prevprev) << " and pp:" << pp << std::endl;
return prev + prevprev;
}
}
amo::Operator amo::Number::toOp(char op) {
switch (op) {
case '+' : return ADD;
case '-' : return SUB;
case '*' : return MUL;
case '/' : return DIV;
case '^' : return POW;
case '!' : return FAC;
case '(' : return L_P;
case ')' : return R_P;
case '\0': return EOE;
default : return ERROR;
}
}
#define N_OPTR 9
const char priority[N_OPTR][N_OPTR] = {
/* + - * / ^ ! ( ) \0 */
/* + */ '>', '>', '<', '<', '<', '<', '<', '>', '>',
/* - */ '>', '>', '<', '<', '<', '<', '<', '>', '>',
/* * */ '>', '>', '>', '>', '<', '<', '<', '>', '>',
/* / */ '>', '>', '>', '>', '<', '<', '<', '>', '>',
/* ^ */ '>', '>', '>', '>', '>', '<', '<', '>', '>',
/* ! */ '>', '>', '>', '>', '>', '>', ' ', '>', '>',
/* ( */ '<', '<', '<', '<', '<', '<', '<', '=', ' ',
/* ) */ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
/* \0 */ '<', '<', '<', '<', '<', '<', '<', ' ', '='
};
char amo::Number::orderOp(char op1, char op2) {
if (toOp(op1) == Operator::ERROR) return '$';
if (toOp(op2) == Operator::ERROR) return '$';
return priority[toOp(op1)][toOp(op2)];
}
bool amo::Number::isPrior(char op1, char op2) {
if (toOp(op1) == Operator::ERROR) return false;
if (toOp(op2) == Operator::ERROR) return false;
if (priority[toOp(op1)][toOp(op2)] == '>') return true;
else return false;
}
bool amo::Number::isCal(char op) {
if (toOp(op) == Operator::ERROR) return false;
if (toOp(op) <= Operator::FAC) return true;
else return false;
}
std::vector<char> amo::Number::rePolish(std::vector<char>& infix) {
std::vector<char> postfix;
amo::Stack<char> stack;
for (std::vector<char>::iterator it=infix.begin(); it!=infix.end(); it++) {
char c = *it;
std::cout << "[rePolish()]: for-loop, c:" << c << std::endl;
if (isdigit(c)) {
std::cout << "[rePolish()]: met a digit and pushes postfix char:" << c << std::endl;
postfix.push_back(c);
} else {
if (c == '(') {
std::cout << "[rePolish()]: met a '(' and pushes stack char:" << c << std::endl;
stack.push(c);
}
else if (isCal(c)) {
if (stack.empty()) {
std::cout << "[rePolish()]: met a cal, stack is empty and pushes stack char:" << c << std::endl;
stack.push(c);
} else if (stack.top() == '(') {
std::cout << "[rePolish()]: met a cal, stack top is '(' and pushes stack char:" << c << std::endl;
stack.push(c);
} else if (stack.top() != '(' && isPrior(c, stack.top())) {
std::cout << "[rePolish()]: met a cal, stack top is char:" << stack.top() << " which is lower prior and pushes stack char:" << c << std::endl;
stack.push(c);
} else if (!isPrior(c, stack.top())) {
std::cout << "[rePolish()]: met a cal, pops stack top:" << stack.top()
<< " which is higher prior and pushes postfix char:" << stack.top()
<< " then pushes stack char:" << c
<< std::endl;
postfix.push_back(stack.pop());
stack.push(c);
}
}
else if (c == ')') {
while (stack.top()!='(') {
std::cout << "[rePolish()]: met a ')', pops stack top:" << stack.top()
<< " and pushes postfix char:" << stack.top()
<< std::endl;
postfix.push_back(stack.pop());
}
stack.pop(); //pops '(' out from stack
}
else std::cout << "[rePolish()]: not a calculator or digit:" << c << std::endl;
}
}
while(!stack.empty()) {
std::cout << "[rePolish()]: eventually pushes postfix and pops stack top:" << stack.top() << std::endl;
postfix.push_back(stack.pop());
}
return postfix;
}
int amo::Number::calculate(int arg, char op) {
//int arg = atoi(&d);
std::cout << "[calculate()]: returns " << op << "" << arg << std::endl;
if (op=='!') return factory(arg);
else return -1;
}
int amo::Number::calculate(int arg1, int arg2, char op) {
//int arg1 = atoi(&d1);
//int arg2 = atoi(&d2);
std::cout << "[calculate()]: returns " << arg1 << " " << op << " " << arg2 << std::endl;
if (op=='+') return arg1 + arg2;
else if (op=='-') return arg1 - arg2;
else if (op=='*') return arg1 * arg2;
else if (op=='/') return arg1 / arg2;
else if (op=='^') return power(arg1, arg2);
else return -1;
}
int amo::Number::evaluate(char* rpn) {
amo::Stack<int> stack;
char c;
while ((c=*rpn++) != '\0') {
std::cout << "[evaluate()]: while-loop, c:" << c << std::endl;
if (isdigit(c)) stack.push(atoi(&c));
else {
if (c == '!')
stack.push(calculate(stack.pop(), c));
else
stack.push(calculate(stack.pop(), stack.pop(), c));
}
std::cout << CYAN << "[evaluate()]: operated " << c << ", and top:" << stack.top() << WHITE << std::endl;
}
return stack.pop();
}