-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbigdata.cpp
More file actions
693 lines (677 loc) · 14.3 KB
/
bigdata.cpp
File metadata and controls
693 lines (677 loc) · 14.3 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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#include "big_int.h"
#include "list_node.h"
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <sstream>
#include <vector>
#define VERSION "2.0.0"
#define LAST_DATA "$MMR"
#define LAST_DATA_IN_MMR_MAP "MMR"
#define MOD_DATA "$MOD"
#define MAXPOWERS "4001"
#define MAXPOWERI 4001
#define FL_OPT_MODULO_CASE_ON "$modulo_switch on"
#define FL_OPT_MODULO_CASE_OFF "$modulo_switch off"
using namespace std;
typedef map<string, list_node *> M_MMR;
static M_MMR m_mmr;
static list_node * LAST_DATA_MMR;
typedef enum
{
cal,
fl,
ver,
h,
q,
mod,
mmr
} eALL_INSTR;
typedef enum
{
finished,
unfinished,
unknown_instr,
error,
exits
} eRTN_INSTR;
typedef enum
{
rm,
touch,
echo
} eMMR_INSTR; //never used yet
typedef struct
{
list_node *base;
int pow;
list_node *modulus;
} modulus_t;
modulus_t system_modulus;
typedef struct
{
vector<string> decompose_instr;
bool valid; //if vector.size == 0, valid = false. else valid = true
} ANALYSIS_PACK_s;
static eALL_INSTR status = q; //cal:cal ; fl:fl ; h:h ; mmr:mmr ; q:normal
bool is_number(string &str)
{
bool is_positive = false;
if (str == "")
return false;
for (unsigned i = 0; i < str.length(); i++)
{
if (str[i] == ' ')
continue;
if ((str[i] >= '0') && (str[i] <= '9'))
is_positive = true;
else
return false;
}
if (is_positive)
return true;
else
return false;
}
list_node *analyze2list(string str)
{
list_node *num;
M_MMR::iterator iter;
string s_temp = "";
if (str[0] == '$')
{
s_temp = str.substr(1, str.length());
iter = m_mmr.find(s_temp);
if (iter != m_mmr.end())
num = create_list(iter->second); //bug: why createlist. send the original list.
else
return NULL;
}
else
{
if (is_number(str))
num = create_list(str);
else
return NULL;
}
return num;
}
int analyze2int(list_node *list)
{
list_node *nobigger = create_list(MAXPOWERS);
if (compare(list, nobigger) == 1)
{
cout << "Too large Operation.\n";
return 0;
}
int count = 0;
int rtn = 0;
list_node *ptr = list->get_prev();
while (ptr->get_data() != -1)
{
rtn += ptr->get_data() * (int)pow(10, count);
count++;
ptr = ptr->get_prev();
}
return rtn;
}
string analyze2str(int n)
{
stringstream ss;
string str;
ss << n;
ss >> str;
return str;
}
eALL_INSTR gets_status(string str)
{
if (str == "cal")
return cal;
else if (str == "fl")
return fl;
else if (str == "h")
return h;
else if (str == "mmr")
return mmr;
else
return q;
}
bool save_log(string &outputlog)
{
ofstream logfile;
logfile.open("log.log", ios_base::app);
if (logfile.is_open())
{
logfile << outputlog << endl;
logfile.close();
}
else
return false;
return true;
}
ANALYSIS_PACK_s analyze(string str)
{
int ptr = 0;
ANALYSIS_PACK_s rtnpack;
string temp;
unsigned i;
for (i = 0; i <= str.length(); i++)
{
if (str[i] != ' ')
continue;
temp = str.substr(ptr, i - ptr);
ptr = i + 1;
if (temp != "")
rtnpack.decompose_instr.push_back(temp);
}
temp = str.substr(ptr, i - ptr);
if (temp != "")
rtnpack.decompose_instr.push_back(temp);
if (rtnpack.decompose_instr.size() == 0)
rtnpack.valid = false;
else
rtnpack.valid = true;
return rtnpack;
}
eRTN_INSTR open_log(string filename)
{
ifstream file;
string str = "";
file.open(filename, ios_base::in);
while (file.good())
{
getline(file, str);
cout << " " << str << endl;
}
return finished;
}
eRTN_INSTR handle_cal(vector<string> hdle)
{
if ((hdle.size() == 1) && (hdle.at(0) == "--help"))
{
open_log("io/helplog.cal");
return finished;
}
if (hdle.size() != 3)
return error;
list_node *num1, *num2;
num1 = analyze2list(hdle.at(0));
num2 = analyze2list(hdle.at(2));
if ((num1 == NULL) || (num2 == NULL))
{
cout << " No variable found. Do you mean $[variable]?\n";
return error;
}
string operators = hdle.at(1);
string result_str = "";
if (operators == "+")
{
list_node *temp = addition(num1, num2, system_modulus.modulus);
temp->replaceGuardian(LAST_DATA_MMR);
result_str = printl(LAST_DATA_MMR);
delete (temp);
}
else if (operators == "-")
{
pair<bool, list_node *> temp = subtracion(num1, num2, system_modulus.modulus);
//*LAST_DATA_MMR = *temp.second;
temp.second->replaceGuardian(LAST_DATA_MMR);
result_str = printl(LAST_DATA_MMR);
delete (temp.second);
}
else if (operators == "*")
{
list_node *temp = multiplication(num1, num2, system_modulus.modulus);
temp->replaceGuardian(LAST_DATA_MMR);
result_str = printl(LAST_DATA_MMR);
delete (temp);
}
else if (operators == "/")
{
list_node *temp = division(num1, num2, system_modulus.modulus);
temp->replaceGuardian(LAST_DATA_MMR);
result_str = printl(LAST_DATA_MMR);
delete (temp);
}
else if (operators == "^")
{
list_node *temp = power(num1, num2, system_modulus.modulus);
temp->replaceGuardian(LAST_DATA_MMR);
result_str = printl(LAST_DATA_MMR);
delete (temp);
}
else
{
cout << " Unknown Operator.\n";
delete (num1->destructor());
delete (num2->destructor());
return error;
}
cout << "result = " << result_str << endl
<< " 10^(x):x=" << rtn_length(LAST_DATA_MMR) - 1 << endl;
string print = "";
string pow_str = analyze2str(system_modulus.pow);
if (modulo_switch)
print = "( " + printl(num1) + " " + operators + " " + printl(num2) + " ) % ( " + printl(system_modulus.base) + " ^ " + pow_str + " ) = " + result_str;
else
print = "( " + printl(num1) + " " + operators + " " + printl(num2) + " ) = " + result_str;
if (!save_log(print))
cout << " cannot record on logfile.\n";
delete (num1->destructor());
delete (num2->destructor());
return finished;
}
eRTN_INSTR handle_mmr(vector<string> hdle)
{
if ((hdle.size() == 1) && (hdle.at(0) == "--help"))
{
open_log("io/helplog.mmr");
return finished;
}
string instr = hdle.at(0);
if ((hdle.size() == 1) && (instr == "--help"))
{
cout << "show help mmr log\n";
return finished;
}
if (instr == "rm")
{
if (hdle.size() != 2)
{
cout << " parameter error. Too much or too little.\n";
return error;
}
string del_temp = hdle.at(1);
M_MMR::iterator iter = m_mmr.find(del_temp);
if (iter != m_mmr.end())
{
//num = create_list(iter->second);
iter->second->destructor();
m_mmr.erase(iter);
}
else
{
cout << " Variable " << del_temp << " not found.\n";
return error;
}
}
else if (instr == "touch")
{
if (hdle.size() != 3)
{
cout << " parameter error. Too much or too little.\n";
return error;
}
string touch_temp = hdle.at(1);
if (hdle.at(2) == LAST_DATA)
{
m_mmr[touch_temp] = create_list(LAST_DATA_MMR);
return finished;
}
if (hdle.at(2) == MOD_DATA)
{
m_mmr[touch_temp] = create_list(system_modulus.modulus);
return finished;
}
if (is_number(hdle.at(2)))
{
m_mmr[touch_temp] = create_list(hdle.at(2));
return finished;
}
else
{
cout << " Unknown number.\n";
return error;
}
}
else if (instr == "echo")
{
if (hdle.size() != 1)
{
cout << " parameter error. Too much or too little.\n";
return error;
}
if (m_mmr.size() == 0)
cout << "no variable storaged.\n";
else
{
M_MMR::iterator iter = m_mmr.begin();
for (iter = m_mmr.begin(); iter != m_mmr.end(); iter++)
{
cout << iter->first << " : ";
cout << printl(iter->second);
cout << endl;
}
}
}
else
{
cout << " No such instruction for mmr.\n";
return error;
}
return finished;
}
eRTN_INSTR handle_mod()
{
cout << " the modulus is: ";
cout << printl(system_modulus.base);
cout << " ^ " << system_modulus.pow << endl;
cout << " please input the index and the base:\n base: ";
string base_temp;
getline(cin, base_temp, '\n');
if (!is_number(base_temp))
{
cout << " incorrect input of number BASE.\n";
return error;
}
int temp = 0;
cout << " index: ";
cin >> temp;
if ((temp <= 0) || (temp >= MAXPOWERI))
{
cout << " incorrect input of number index.\n";
return error;
}
*system_modulus.base = base_temp;
system_modulus.pow = temp;
system_modulus.modulus = *system_modulus.base ^ system_modulus.pow;
TIME_BREAK = 0.02;
return finished;
}
eRTN_INSTR handle_fl(string filename)
{
ifstream file;
file.open(filename, ios_base::in);
if (file.is_open())
{
string str;
int succ_count = 0, err_count = 0;
clock_t start, end;
start = clock();
while (file.good())
{
getline(file, str);
cout << " " << str << endl;
if (str == FL_OPT_MODULO_CASE_ON)
modulo_switch = 1;
if (str == FL_OPT_MODULO_CASE_OFF)
modulo_switch = 0;
ANALYSIS_PACK_s temp_pack;
eRTN_INSTR fl_status;
temp_pack = analyze(str);
if (!temp_pack.valid)
{
//err_count++;
continue;
}
fl_status = handle_cal(temp_pack.decompose_instr);
if (fl_status == finished)
succ_count++;
else
{
cout << " Syntax error: instruction '" << str << "' could not been analyzed.\n";
err_count++;
}
}
end = clock();
cout << "\n Total: " << succ_count + err_count << " instruction(s) ,\n Successful analyzed: " << succ_count << " instruction(s) ,\n Syntax error: " << err_count << " instruction(s) .\n";
cout << "\n Total Time: " << (double)(end - start) / CLOCKS_PER_SEC << "s\n";
return finished;
}
else
{
cout << " cannot open file: " << filename << " .\n";
return error;
}
}
int welcome()
{
ifstream in;
char str;
string filename = "io/welcome.wcm";
in.open(filename);
if (!in)
{
return -1;
}
while (!in.eof())
{
in.read(&str, 1);
printf("%c", str);
}
in.close();
ofstream logfile;
logfile.open("log.log");
if (!logfile.is_open())
return -1;
logfile << "This is a logfile.\n";
logfile.close();
modulo_switch = true;
system_modulus.base = create_list("2");
system_modulus.pow = 50;
system_modulus.modulus = (*system_modulus.base ^ system_modulus.pow);
LAST_DATA_MMR = create_list("0");
TIME_BREAK = 0.02;
m_mmr[LAST_DATA_IN_MMR_MAP] = LAST_DATA_MMR;
return 0;
}
eRTN_INSTR execute(ANALYSIS_PACK_s &pack)
{
string temp_instr = pack.decompose_instr.at(0);
if (temp_instr == "q")
{
if ((pack.decompose_instr.size() == 2) && (pack.decompose_instr.at(1) == "--help"))
{
open_log("io/helplog.q");
return finished;
}
if (status == q)
{
if (pack.decompose_instr.size() == 2)
rename("log.log", pack.decompose_instr.at(1).c_str());
else if (pack.decompose_instr.size() == 1)
{
cout << " You do not rename the logfile before exit. Do you want to quit anyway?(y/n) > ";
char yon = ' ';
cin >> yon;
if ((yon == 'y') || (yon == 'Y'))
return exits;
else if ((yon == 'n') || (yon == 'N'))
return finished;
else
return error;
}
else
return error;
return exits;
}
else
status = q;
return finished;
}
if (status == q)
{
if (temp_instr == "cal")
{
vector<string> temp;
for (unsigned i = 1; i < pack.decompose_instr.size(); ++i)
temp.push_back(pack.decompose_instr.at(i));
if (temp.size() == 0)
{
status = gets_status(temp_instr);
return unfinished;
}
return handle_cal(temp);
}
else if (temp_instr == "fl")
{
if (pack.decompose_instr.size() == 1)
{
status = fl;
return unfinished;
}
else if (pack.decompose_instr.size() == 2)
{
if (pack.decompose_instr.at(1) == "--help")
{
open_log("io/helplog.fl");
return finished;
}
return handle_fl(pack.decompose_instr.at(1));
}
else
{
cout << " parameter too more or too less.\n";
return error;
}
}
else if (temp_instr == "ver")
{
cout << " VERSION: " << VERSION << endl;
return finished;
}
else if (temp_instr == "h")
{
open_log("io/helplog.help");
return finished;
}
else if (temp_instr == "mod")
{
if ((pack.decompose_instr.size() == 2) && (pack.decompose_instr.at(1) == "--help"))
{
open_log("io/helplog.modulus");
return finished;
}
else if (pack.decompose_instr.size() == 1)
return handle_mod();
else
return error;
}
else if (temp_instr == "sys")
{
cout << " modulus: ";
cout << printl(system_modulus.modulus);
cout << "\n MMR: ";
cout << printl(LAST_DATA_MMR);
cout << "\n storaged variables: " << m_mmr.size();
cout << "\n Break_time: " << TIME_BREAK << " s";
cout << "\n MAX_POW: " << MAXPOWERS;
cout << "\n Modulo Switch: " << modulo_switch << endl;
return finished;
}
else if (temp_instr == "mmr")
{
vector<string> temp;
for (unsigned i = 1; i < pack.decompose_instr.size(); ++i)
temp.push_back(pack.decompose_instr.at(i));
if (temp.size() == 0)
{
status = gets_status(temp_instr);
return unfinished;
}
return handle_mmr(temp);
}
else
{
return unknown_instr;
}
} //if (status = q)
else
{ //status != q
if (status == cal)
return handle_cal(pack.decompose_instr);
else if (status == mmr)
return handle_mmr(pack.decompose_instr);
else if (status == fl)
{
if (pack.decompose_instr.size() == 1)
{
if (pack.decompose_instr.at(0) == "--help")
{
open_log("io/helplog.fl");
return finished;
}
return handle_fl(pack.decompose_instr.at(0));
}
else
{
cout << " parameter too more or too less.\n";
return error;
}
}
else
return unknown_instr;
}
}
int main()
{
//bool already_print = false;
if (welcome() != 0)
{
return -1;
}
while (1)
{
if (true/*!already_print*/)
{
switch (status)
{
case q:
cout << "Big_Data > ";
break;
case h:
cout << "help > ";
break;
case cal:
cout << "cal > ";
break;
case fl:
cout << "fl > ";
break;
case mmr:
cout << "mmr > ";
break;
default:
break;
}
}
//already_print = true;
string instruction;
ANALYSIS_PACK_s temp;
fflush(stdin);
cin.clear();
getline(cin, instruction, '\n');
if (instruction == "modulo_switch off")
{
modulo_switch = false;
//already_print = false;
continue;
}
else if (instruction == "modulo_switch on")
{
modulo_switch = true;
//already_print = false;
continue;
}
temp = analyze(instruction);
if (!temp.valid)
continue;
switch (execute(temp))
{
case finished:
break;
case unfinished:
break;
case unknown_instr:
cout << " Unknown instruction: " << temp.decompose_instr[0] << " \n Type 'h' for getting more help.\n";
break;
case error:
cout << " There is something error in your inputing. Probably be syntax error.\n Type '--help' for getting more help.\n";
break;
case exits:
return 0;
}
//already_print = false;
}
}