-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
3209 lines (2860 loc) · 139 KB
/
main.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
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************************************************************
* main.cpp
* editor: amo
*************************************************************************************************************************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <screen.h>
#include <text_view.h>
#include <view.h>
#include <math.h>
#include <space.h>
#include <node.h>
#include <linked_queue.h>
#include <array_queue.h>
#include <time.h>
#include <list.h>
#include <list_node.h>
#include <stack.h>
#include <sort.h>
#include <number.h>
#include <move.h>
#include <char.h>
#include <queen.h>
#include <maze.h>
#include <cell.h>
#include <bin_tree.h>
#include <bin_node.h>
#include <huffman_tree.h>
#include <bst.h>
#include <graph.h>
#include <entry.h>
#include <dict.h>
#include <entry.h>
#include <cmath>
#include <bmp.h>
using namespace std;
using namespace amo;
using namespace mystd::myfstream;
using amo::List;
using amo::Node;
using amo::Char;
using mystd::myfstream::ifstream;
using mystd::myfstream::ofstream;
/* -------------------------------------------------------------------- */
/* my define macro */
/* -------------------------------------------------------------------- */
typedef struct myframe {
int x;
int y;
void show_location(){
std::cout << "x:" << x << std::endl;
std::cout << "y:" << y << std::endl;
}
} myframe_t;
#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 */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
/* -------------------------------------------------------------------- */
/* global variables */
/* -------------------------------------------------------------------- */
char data[128];
int mTheme = 0;
int mXPad = 10;
int mYPad = 20;
/* -------------------------------------------------------------------- */
/* implements */
/* -------------------------------------------------------------------- */
class MyFrame{
int positionX;
int positionY;
public:
void setX(int x);
void setY(int y);
void showLocation();
MyFrame(int x, int y);
//MyFrame(MyFrame &frame);
MyFrame(int value);
~MyFrame();
MyFrame(){
positionX = 0;
positionY = 0;
}
};
/*
MyFrame::MyFrame(MyFrame &frame){
positionX = frame.positionX + 10;
positionY = frame.positionY + 10;
}
*/
MyFrame::MyFrame(int x, int y){
positionX = x;
positionY = y;
}
MyFrame::MyFrame(int value){
positionX = value;
positionY = -10;
}
MyFrame::~MyFrame(){
std::cout << "release at positionX:" << positionX << std::endl;
std::cout << "release at positionY:" << positionY << std::endl;
}
void MyFrame::showLocation(){
std::cout << "positionX:" << positionX << std::endl;
std::cout << "positionY:" << positionY << std::endl;
}
void MyFrame::setX(int x){
positionX = x;
}
void MyFrame::setY(int y){
positionY = y;
}
MyFrame* getMyFrameRef(int x, int y){
MyFrame *frame = new MyFrame(x, y);
return frame;
}
int layout(MyScreen screen){
int layoutX = screen.getPixelX() + mXPad;
int layoutY = screen.getPixelY() + mYPad;
switch(mTheme){
case 0:
std::cout << "### Standard Theme ###"<< std::endl;
std::cout << "... layoutX:" << layoutX << std::endl;
std::cout << "... layoutY:" << layoutY << std::endl;
break;
case 1:
std::cout << "### Festival Theme ###"<< std::endl;
std::cout << "... layoutX:" << layoutX << std::endl;
std::cout << "... layoutY:" << layoutY << std::endl;
break;
default:
std::cout << "### Unknown Theme ###"<< std::endl;
std::cout << "... layoutX:" << layoutX << std::endl;
std::cout << "... layoutY:" << layoutY << std::endl;
break;
}
}
void updateView(MyView *view, int changed, int l, int r, int t, int b){
std::cout << "[main]: >>> updateView()" << std::endl;
view->onLayout(changed, l, r, t, b);
}
/**
* Other than the restrictions above, the language puts no other constraints on what the overloaded operators do
* , or on the return type (it does not participate in overload resolution)
*
int operator+(MyMath &math1, MyMath &math2){
std::cout << "[main]: >>> operator+()" << std::endl;
int x = math1.getNumber();
int y = math2.getNumber();
math1.setNumber(x+y);
return 0;
}
void operator+(MyMath &math1, MyMath &math2){
std::cout << "[main]: >>> operator+()" << std::endl;
int x = math1.getNumber();
int y = math2.getNumber();
math1.setNumber(x+y);
}
*/
int operator-(MyMath& m1, MyMath& m2) {
std::cout << "[main::operator-(MyMath&, MyMath)]: >>>" << std::endl;
return m1.getNumber() - m2.getNumber();
}
void operator++(MyMath &math) {
std::cout << "[main::operator++(MyMath&)]: >>>" << std::endl;
++math.number;
}
namespace mystd::myfstream {
std::istream &operator>>(std::istream &stream, mystd::myfstream::ifstream &fis) {
std::cout << "[main::operator>>(std::istream&, mystd::myfstream::ifstream&)]: >>> enter numbers..." << std::endl;
stream >> fis.filebuf;
return stream;
}
std::ostream &operator<<(std::ostream &stream, mystd::myfstream::ifstream &fis) {
std::cout << "[main::operator<<(std::ostream&, mystd::myfstream::ifstream&)]: >>> content:" << std::endl;
for(int i=0; i<sizeof(fis.filebuf); i++) stream << "fis.filebuf[" << i << "]:" << fis.filebuf[i] << std::endl;
return stream;
}
std::istream &operator>>(std::istream &stream, mystd::myfstream::ofstream &fos) {
std::cout << "[main::operator>>(std::istream&, mystd::myfstream::ifstream&)]: >>> enter numbers..." << std::endl;
stream >> fos.filebuf;
return stream;
}
std::ostream &operator<<(std::ostream &stream, mystd::myfstream::ofstream &fos) {
std::cout << "[main::operator<<(std::ostream&, mystd::myfstream::ifstream&)]: >>> content:" << std::endl;
for(int i=0; i<sizeof(fos.filebuf); i++) stream << "fos.filebuf[" << i << "[]:" << fos.filebuf[i] << std::endl;
return stream;
}
}
/* a binary predicate implement as a class */
struct PredicateMyMathEqual{
bool operator()(MyMath& math1, MyMath& math2){
bool ret = math1.getNumber()==math2.getNumber()?true:false;
if (ret) std::cout << "[PredicateMyMathEqual::operator()(MyMath&,MyMath&)]: returns true meaning equivalently " << &math1 << " == " << &math2 << std::endl;
else std::cout << "[PredicateMyMathEqual::operator()(MyMath&,MyMath&)]: returns false meaning equivalently " << &math1 << " != " << &math2 << std::endl;
return ret;
}
};
/* a binary predicate implement as a class */
struct PredicateMyMathLess{
bool operator()(MyMath& math1, MyMath& math2){
bool ret = math1.getNumber()<math2.getNumber()?true:false;
if (ret) std::cout << "[PredicateMyMathLess::operator()(MyMath&,MyMath&)]: returns true meaning equivalently " << &math1 << " < " << &math2 << std::endl;
else std::cout << "[PredicateMyMathLess::operator()(MyMath&,MyMath&)]: returns false meaning equivalently " << &math1 << " >= " << &math2 << std::endl;
return ret;
}
};
void input(vector<char>& input) {
char c;
#if 0
while (true) {
cin >> c;
if(c == '\n') {
std::cout << "c == \\n and break" << std::endl;
break;
}
if(c == '\0') {
std::cout << "c == \\0 and break" << std::endl;
break;
}
if(c == ' ') {
std::cout << "c == ' ' and break" << std::endl;
break;
}
std::cout << "going to push char:" << c << std::endl;
input.push_back(c);
}
#elif 1
/**
* char * fgets ( char * str, int num, FILE * stream );
* Get string from stream
* Reads characters from stream and stores them as a C string into str until (num-1) characters have been read
* or either a newline or the end-of-file is reached, whichever happens first.
*
* A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.
* A terminating null character is automatically appended after the characters copied to str.
*
* Notice that fgets is quite different from gets: not only fgets accepts a stream argument
* , but also allows to specify the maximum size of str and includes in the string any ending newline character.
*
* Parameters
* str: Pointer to an array of chars where the string read is copied.
* num: Maximum number of characters to be copied into str (including the terminating null-character).
* stream: Pointer to a FILE object that identifies an input stream.
* stdin can be used as argument to read from the standard input.
* Return value:
* On success, the function returns str.
* If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof).
* If this happens before any characters could be read, the pointer returned is a null pointer.
*/
char buf[32];
std::cout << "Please enter input..." << std::endl;
fgets(buf, sizeof(buf), stdin);
std::cout << "Your input..." << std::endl;
puts(buf);
int i = 0;
while ((c=buf[i++]) && i<=strlen(buf)) {
if(c == '\n') {
std::cout << "c == \\n and break" << std::endl;
break;
}
if(c == '\0') {
std::cout << "c == \\0 and break" << std::endl;
break;
}
if(c == ' ') {
std::cout << "c == ' ' and break" << std::endl;
break;
}
std::cout << "going to push char:" << c << std::endl;
input.push_back(c);
}
#else
while (true) {
cin.get(c);
if(c == '\n') {
std::cout << "c == \\n and break" << std::endl;
break;
}
if(c == '\0') {
std::cout << "c == \\0 and break" << std::endl;
break;
}
if(c == ' ') {
std::cout << "c == ' ' and break" << std::endl;
break;
}
std::cout << "going to push char:" << c << std::endl;
input.push_back(c);
}
#endif
}
void output(vector<char>& output) {
for (std::vector<char>::iterator it=output.begin(); it!=output.end(); it++) cout << *it;
cout << endl;
}
int main(int argc, char *argv[])
{
int num = 10;
int* pnum = #
std::cout << std::endl;
std::cout << GREEN << "****************** argc:" << argc << " ******************"<< WHITE << std::endl;
for(int i=0; i<argc; i++) std::cout << GREEN << "****************** arg[" << i << "]: " << argv[i] << " ******************"<< WHITE << std::endl;
#if 0
std::cout << "Enter your name:" << std::endl;
std::cin >> data;
std::cout << "hello c++ to " << data << std::endl;
std::cout << std::setw(1) << "dec: " << std::dec << num << std::endl;
std::cout << std::setw(1) << "dec: " << std::dec << num << std::endl;
std::cout << "hex: 0x" << std::hex << num << std::endl;
return 0;
#endif
#if 0
std::cout << "Enter a number:" << std::endl;
std::cin >> num;
sprintf(data, "%d", num);
std::cout << "num: " << data << std::endl;
std::cout << "... going to handle pointer" << std::endl;
*pnum = 100;
sprintf(data, "%d", num);
std::cout << "num: " << data << std::endl;
#endif
#if 0
//A dynamic 2D array is basically an array of pointers to arrays.
int image[3][3] = {
{11, 12, 13},
{21, 22, 23},
{31, 32, 33},
};
int* pimage = image[0];
std::cout << "image : " << std::hex << image << std::endl;
std::cout << "image[0] : " << std::hex << image[0] << std::endl;
std::cout << "pimage : " << std::hex << pimage << std::endl;
std::cout << "image[1] : " << std::hex << image[1] << std::endl;
std::cout << "(image+1) : " << std::hex << (image+1) << std::endl;
std::cout << "*(image+1) : " << std::hex << *(image+1) << std::endl;
std::cout << "**(image+1) : " << std::dec << **(image+1) << std::endl;
std::cout << "*(image+1)+1 : " << std::hex << *(image+1)+1 << std::endl;
std::cout << "*(*(image+1)+1): " << std::dec << *(*(image+1)+1) << std::endl;
std::cout << "image[2] : " << std::hex << image[2] << std::endl;
std::cout << "(image+2) : " << std::hex << (image+2) << std::endl;
std::cout << "*(image+2) : " << std::hex << *(image+2) << std::endl;
std::cout << "**(image+2) : " << std::dec << **(image+2) << std::endl;
std::cout << "*(image+2)+1 : " << std::hex << *(image+2)+1 << std::endl;
std::cout << "*(*(image+2)+1): " << std::dec << *(*(image+2)+1) << std::endl;
std::cout << "*(*(image+2)+2): " << std::dec << *(*(image+2)+2) << std::endl;
return 0;
#endif
#if 0
myframe_t frame = {3, 3, };
frame.show_location();
#endif
#if 0
MyFrame mFrame;
//mFrame.x = 9;
//mFrame.y = 8;
mFrame.showLocation();
MyFrame mFrameList[3] = {MyFrame(1,1), MyFrame(2,2), MyFrame(3,3)};
mFrameList[0].showLocation();
mFrameList[1].showLocation();
mFrameList[2].showLocation();
MyFrame mFrameDup = MyFrame(mFrameList[0]);
mFrameDup.showLocation();
MyFrame* mFrameRef = getMyFrameRef(31, 89);
if(mFrameRef) mFrameRef->showLocation();
if(mFrameRef) delete mFrameRef;
#endif
#if 0
MyScreen mScreen(300, 300);
mScreen.showSize();
layout(mScreen);
#endif
#if 0
std::cout << "****************** instantiate ******************\n" << std::endl;
std::cout << "[main]: ... going to MyTextView tv1;" << std::endl;
MyTextView tv1;
std::cout << "[main]: ... going to MyTextView tv2();" << std::endl;
MyTextView tv2();
std::cout << "[main]: ... going to MyTextView tv3(\"main\", 1, 2, 3, 4);" << std::endl;
MyTextView tv3("main", 1, 2, 3, 4);
std::cout << "\n****************** main return ******************" << std::endl;
return 0;
#endif
#if 0
std::cout << "****************** polymorphism ******************\n" << std::endl;
char my_text[16] = "amo";
MyTextView tv(my_text, 0, 2, 0, 4);
//tv.showSuperField();
//tv.left = 111; //error due to access the protected member of superclass inherited private
tv.arg = 222; //fine to access the public member of superclass inherited public
std::cout << "[main]: ... going to tv.showPosition()" << std::endl;
tv.showPosition();
MyTextView* ptv = &tv;
std::cout << "[main]: ... going to ptv->showPosition()" << std::endl;
ptv->showPosition();
MyView* pv = &tv;
std::cout << "[main]: ... going to pv->showPosition()" << std::endl;
pv->showPosition();
std::cout << "****************** static override ******************\n" << std::endl;
//tv.displayLayout(10, 50, 30, 80);
tv.onLayout(1, 10, 50, 30, 80);
std::cout << "****************** dynamic override ******************\n" << std::endl;
MyView *view;
view = &tv;
if(view) view->onLayout(1, 10, 50, 30, 80);
//updateView(view, 1, 22, 33, 44, 55);
std::cout << "\n****************** main return ******************" << std::endl;
return 0;
#endif
#if 0
std::cout << "****************** about C++ reference ******************\n" << std::endl;
//std::cout << "... going to create mathA" << std::endl;
//MyMath mathA(11);
//std::cout << "... going to create mathB" << std::endl;
//MyMath mathB(mathA);
std::cout << "\ngoing to MyMath mathC(33)\n" << std::endl;
MyMath mathC(33);
int data[6] = {1, 2, 3, 4, 5, 6};
std::cout << "sizeof(data):" << sizeof(data) << " and sizeof(data[0]):" << sizeof(data[0]) << std::endl;
mathC.setData(data, sizeof(data)/sizeof(data[0]));
std::cout << "object mathC:" << &mathC << std::endl;
#if 0
std::cout << "\ngoing to MyMath& mathD = mathC\n" << std::endl;
MyMath& mathD = mathC;
std::cout << "reference mathD:" << &mathD << std::endl;
std::cout << "\ngoing to MyMath& mathE1 = mathC.decodeWithRef(mathC)\n" << std::endl;
MyMath& mathE1 = mathC.decodeWithRef(mathC); //no calling to copy constructor
std::cout << "reference mathE1:" << &mathE1 << std::endl;
std::cout << "\ngoing to MyMath& mathE2 = mathC.decode(mathC)\n" << std::endl;
MyMath& mathE2 = mathC.decode(mathC); //invokes ONE calling to copy constructor for parameter duplicating
std::cout << "reference mathE2:" << &mathE2 << std::endl;
std::cout << "\ngoing to MyMath mathF1 = mathC.decodeWithRef(mathC)\n" << std::endl;
//mathC.decodeWithRef(mathC); //no calling to copy constructor
MyMath mathF1 = mathC.decodeWithRef(mathC);//invokes ONE calling to copy constructor for object creating
std::cout << "... object mathF1:" << &mathF1 << std::endl;
std::cout << "\ngoing to MyMath mathF2 = mathC.decode(mathC)\n" << std::endl;
MyMath mathF2 = mathC.decode(mathC);//invokes TWO callings to copy constructor for object creating and parameter duplicating
std::cout << "object mathF2:" << &mathF2 << std::endl;
std::cout << "mathF2:" << mathF2;
#endif
std::cout << GREEN << "\ngoing to 'MyMath mathRVO = mathC.decodeWithRefRVO(mathC)'\n" << WHITE <<std::endl;
MyMath mathRVO = mathC.decodeWithRefRVO(mathC);
std::cout << "mathRVO:" << mathRVO;
std::cout << GREEN << "\ngoing to 'MyMath mathRef& = mathC.decodeWithRefReturnRef(mathC)'\n" << WHITE <<std::endl;
MyMath& mathRef = mathC.decodeWithRefReturnRef(mathC);
std::cout << "mathRef:" << mathRef;
std::cout << "\n****************** main return ******************" << std::endl;
return 0;
#endif
#if 0
std::cout << "****************** operator override ******************\n" << std::endl;
#if 0
std::cout << "\n****************** class override ******************\n" << std::endl;
MyMath mMath1(11);
MyMath mMath2(22);
//std::cout << "[main]: ... going to ref1 = mMath1.operator+(mMath2)" << std::endl;
//MyMath& ref1 = mMath1.operator+(mMath2);
//std::cout << "[main]: ... now ref1.getNumber():" << ref1.getNumber() << std::endl;
std::cout << "[main]: ... going to ref2 = = mMath1 + mMath2" << std::endl;
MyMath& ref2 = mMath1 + mMath2;
std::cout << "[main]: ... now ref2.getNumber():" << ref2.getNumber() << std::endl;
std::cout << "[main]: ... now mMath1.getNumber():" << mMath1.getNumber() << std::endl;
std::cout << "[main]: ... going to MyMath& m4 = mMath1++" << std::endl;
MyMath& m4 = mMath1++;
std::cout << "[main]: ... now m4.getNumber():" << m4.getNumber() << std::endl;
std::cout << "[main]: ... now mMath1.getNumber():" << mMath1.getNumber() << std::endl;
//std::cout << "[main]: ... going to mResult = mResult->operator++()" << std::endl;
//mResult = mResult->operator++();
//std::cout << "[main]: ... now mResult->operator++():" << mResult->getNumber() << std::endl;
//std::cout << "[main]: ... going to mResult = mResult->operator--(0)" << std::endl;
//mResult = mResult->operator--(0);
//std::cout << "[main]: ... now mResult->getNumber():" << mResult->getNumber() << std::endl;
//if(mResult) delete mResult;
#endif
#if 0
std::cout << "\n****************** friend override ******************\n" << std::endl;
int ret_sub = mMath1 - mMath2;
std::cout << "[main]: ... mMath1 - mMath2:" << ret_sub << std::endl;
operator++(mMath1);
std::cout << "[main]: ... now mMath1.getNumber():" << mMath1.getNumber() << std::endl;
#endif
#if 0
std::cout << "****************** = override ******************\n" << std::endl;
std::cout << "[main]: ... going to MyMath model(100)" << std::endl;
MyMath model(100);
int data[6] = {1, 2, 3, 4, 5, 6};
int *test;
model.setData(data, sizeof(data)/sizeof(data[0]));
model.getData(test);
#if 0
std::cout << "[main]: ... going to MyMath tmp(444)" << std::endl;
MyMath tmp(444);
std::cout << "[main]: ... going to MyMath &mock = tmp.operator=(model)" << std::endl;
MyMath &mock = tmp.operator=(model); //not refer to model for override operator =
std::cout << "[main]: ... now mock:" << &mock << ", tmp:" << &tmp << " and model:" << &model << std::endl;
std::cout << "[main]: ... now mock.getNumber():" << mock.getNumber() << ", tmp.getNumber():" << tmp.getNumber() << " and model.getNumber():" << model.getNumber() << std::endl;
#elif 1
std::cout << "[main]: ... going to MyMath tmp(555)" << std::endl;
MyMath tmp(555);
std::cout << "[main]: ... going to tmp = model" << std::endl;
tmp = model;
std::cout << "[main]: ... now tmp:" << &tmp << " and model:" << &model << std::endl;
std::cout << "[main]: ... now tmp.getNumber():" << tmp.getNumber() << " and model.getNumber():" << model.getNumber() << std::endl;
#elif 0
std::cout << "[main]: ... going to MyMath &mock = model;" << std::endl;
MyMath &mock = model;
std::cout << "[main]: ... now mock:" << &mock << " and model:" << &model << std::endl;
std::cout << "[main]: ... now mock.getNumber():" << mock.getNumber() << " and model.getNumber():" << model.getNumber() << std::endl;
#else
std::cout << "[main]: ... going to MyMath mock = model" << std::endl;
MyMath mock = model;
std::cout << "[main]: ... now mock:" << &mock << " and model:" << &model << std::endl;
std::cout << "[main]: ... now mock.getNumber():" << mock.getNumber() << " and model.getNumber():" << model.getNumber() << std::endl;
mock.getData(test);
std::cout << "[main]: ... now test[0] :" << test[0] << std::endl;
std::cout << "[main]: ... now test[1] :" << test[1] << std::endl;
std::cout << "[main]: ... now test[2]):" << test[2] << std::endl;
#endif
std::cout << "\n****************** main return ******************" << std::endl;
return 0;
#endif
#endif
#if 0
#if 0
std::cout << "****************** stream: get() ******************" << std::endl;
//std::cout << myostream << std::cin.get() << std::endl;
std::cout << "std::cin.get():" << std::cin.get() << std::endl;
/**
* The cin object in C++ is an object of class istream.
* It is used to accept the input from the standard input device i.e. keyboard.
*/
std::cin.get(); //discard '\n' left in stream buffer
/**
* Strings are actually one-dimensional array of characters terminated by a null character '\0'.
* Thus a null-terminated string contains the characters that comprise the string followed by a null.
*
* The following declaration and initialization create a string consisting of the word "Hello".
* To hold the null character at the end of the array, the size of the character array containing
* the string is one more than the number of characters in the word "Hello."
*/
char str1[4];
std::cout << "****************** stream: get(char &) *** not more than " << sizeof(str1)-1 << " words ***************" << std::endl;
for(int i=0; i<sizeof(str1); i++){
std::cin.get(str1[i]);
if(str1[i] == '\n') {
std::cout << "str1[" << i << "] == \\n and set as \\0" << std::endl;
str1[i] = '\0';
break;
}
if(str1[i] == '\0') {
std::cout << "str1[" << i << "] == \\0 and set as \\0" << std::endl;
str1[i] = '\0';
break;
}
if (i == sizeof(str1)-1) {
std::cout << "" << i << " is the last index and set as \\0" << std::endl;
str1[i] = '\0';
break;
}
}
std::cout << "str1:" << str1 << std::endl;
char str2[8];
std::cout << "****************** stream: get(char*, int, char) *** not more than " << sizeof(str2)-1 << " words ***************" << std::endl;
std::cin.get(str2, 8); //maximum number of characters to write to str2 (including the terminating null character).
std::cout << "str2:" << str2 << std::endl;
#if 1
std::cin.get(); //discard '\n' left in stream buffer
#else
char tmp;
std::cin.get(tmp); //pick up '\n' left in stream buffer
if(tmp == '\n') std::cout << "tmp == \\n:" << tmp << std::endl;
if(tmp == '\0') std::cout << "tmp == \\0:" << tmp << std::endl;
#endif
std::cout << "****************** stream: getline(char*, int, char) ******************" << std::endl;
char str3[8] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'};
std::cin.getline(str3, 8); //getline() doesn't leave '\n' in stream buffer
std::cout << str3 << std::endl;;
#if 0
std::cin.get(); //discard '\n' left in stream buffer
#else
char tmp;
std::cin.get(tmp); //pick up '\n' left in stream buffer
if(tmp == '\n') std::cout << "tmp == \\n:" << tmp << std::endl;
else if(tmp == '\0') std::cout << "tmp == \\0:" << tmp << std::endl;
else std::cout << "tmp:" << tmp << std::endl;
#endif
#endif
#if 0
std::vector<char> v;
cout << GREEN << "going to input" << WHITE << endl;
input(v);
cout << GREEN << "going to output:" << v.size() << WHITE << endl;
output(v);
#endif
#if 0
std::cout << "****************** i/o operator overload ******************" << std::endl;
std::cout << "going to MyMath desc(999)" << std::endl;
MyMath desc(999);
int data_desc[6] = {10, 20, 30, 40, 50, 60};
desc.setData(data_desc, sizeof(data_desc)/sizeof(data_desc[0]));
std::cout << "going to operator<<(std::cout, desc)" << std::endl;
operator<<(std::cout, desc);
std::cout << "going to std::cout << desc;" << std::endl;
std::cout << desc;
std::cout << "going to std::cin >> desc;" << std::endl;
std::cin >> desc;
std::cout << "going to std::cout << desc;" << std::endl;
std::cout << desc;
#endif
std::cout << "\n****************** main return ******************" << std::endl;
return 0;
#endif
#if 0
std::cout << "\n****************** iofstream ******************\n" << std::endl;
/**
* write to fos
*/
#if 1
int data_fos[6] = {10, 20, 30, 40, 50, 60};
//std::cout << "sizeof(data_fos):" << sizeof(data_fos) << " and sizeof(data_fos[0]):" << sizeof(data_fos[0]) << std::endl;
std::cout << "going to std::ofstream fos(test.log)" << std::endl;
std::ofstream fos("test.log");
for(int i=0; i<sizeof(data_fos)/sizeof(data_fos[0]); i++) {
std::cout << "going to fos << std::dec << " << data_fos[i] << std::endl;
fos << std::dec << data_fos[i] << ' ';
}
std::cout << "going to fos << amo_test" << std::endl;
fos << "amo_test";
std::cout << "going to fos.close()" << std::endl;
fos.close();
#else
std::cout << "going to std::ofstream fos(temp.log)" << std::endl;
std::ofstream fos("temp.log");
if (!fos) {
std::cout << "open output file failed" << std::endl;
std::cout << "\n****************** main return ******************" << std::endl;
return 0;
}
fos << "amo enter some text";
std::cout << "going to fos.close()" << std::endl;
fos.close();
#endif
/**
* read from fis
*/
#if 1
std::cout << "going to std::ifstream fis" << std::endl;
std::ifstream fis;
std::cout << "going to fis.open(test.log, std::ifstream::in)" << std::endl;
fis.open("test.log", std::ifstream::in);
if(!fis.fail()) std::cout << "open file succeeded" << std::endl;
else std::cout << "open file failed:" << fis.fail() << std::endl;
int buf[6];
char str_buf[256];
for(int i=0; i<sizeof(buf)/sizeof(buf[0]); i++) {
std::cout << "going to fis >> buf[" << i << "]" << std::endl;
fis >> buf[i];
}
std::cout << "going to fis.get(str_buf, 16)" << std::endl;
fis.get(str_buf, 16);
//fis.getline(str_buf, 16);
#else
std::cout << "going to std::ifstream fis(temp.log)" << std::endl;
std::ifstream fis("temp.log");
if (!fis) {
std::cout << "open input file failed" << std::endl;
std::cout << "\n****************** main return ******************" << std::endl;
return 0;
}
char str_buf[16] = {'b','b','b','b'};
//std::cout << "going to fis >> str_buf" << std::endl;
//fis >> str_buf;
//std::cout << "going to fis.get(str_buf, " << sizeof(str_buf)*sizeof(str_buf[0]) << ")" << std::endl;
//fis.get(str_buf, sizeof(str_buf)*sizeof(str_buf[0]));
std::cout << "going to fis.get(str_buf[0])" << std::endl;
fis.get(str_buf[0]);
#endif
/**
* display
*/
#if 1
for(int i=0; i<sizeof(buf)/sizeof(buf[0]); i++) std::cout << "buf[" << i << "]:" << buf[i] << std::endl;
std::cout << "str_buf:" << str_buf << std::endl;
#else
std::cout << "str_buf:" << str_buf << std::endl;
#endif
std::cout << "going to fis.close()" << std::endl;
fis.close();
//std::cout << "\n****************** main return ******************" << std::endl;
//return 0;
#endif
#if 0
std::cout << "\n****************** iofstream with get()/put() ******************\n" << std::endl;
#if 0
if(argc != 3) {
std::cout << "not supported copy file command" << std::endl;
return 0;
}
std::cout << "src file :" << argv[1] << std::endl;
std::cout << "dest file:" << argv[2] << std::endl;
std::ifstream mFin(argv[1], std::ios::binary|std::ios::in);
std::ofstream mFout(argv[2], std::ios::binary|std::ios::out);
#else
std::cout << "going to mFin(in.log)" << std::endl;
std::ifstream mFin("in.log", std::ios::binary|std::ios::in);
std::cout << "going to mFout(out.log)" << std::endl;
std::ofstream mFout("out.log", std::ios::binary|std::ios::out);
#endif
std::streampos pos;
/**
* std::istream::tellg returns the position of the current character in the input stream.
*/
pos = mFin.tellg();
std::cout << "beginning pos of mFin :" << pos << std::endl;
/**
* std::ostream::tellp() returns the position of the current character in the output stream
*/
pos = mFout.tellp();
std::cout << "beginning pos of mFout:" << pos << std::endl;
char tmp;
int mFinSize;
while(0){
std::cout << "going to mFin.get(tmp)" << std::endl;
mFin.get(tmp);
if(mFin.eof()) {
std::cout << "mFin.eof() returned true" << std::endl;
break; //std::istream::get() set fail bit flag when returns EOF
}
std::cout << "going to mFout.put(tmp)" << std::endl;
mFout.put(tmp);
}
/**
* std::istream::get() returns the first signature that the character read
* , or the end-of-file value (EOF) if no characters are available in the stream
* All other signatures always return *this.
* Note that this return value can be checked for the state of the stream
*(see CASTING a stream to bool for more info: Returns whether an error flag is set (either failbit or badbit)).
*/
while(mFin.get(tmp)){
std::cout << "going to mFout.put(tmp):" << tmp << " and now pos of input stream:" << mFin.tellg() << std::endl;
mFout.put(tmp);
}
/**
* std::ios::clear(iostate state = goodbit) sets a new value for the stream's internal error state flags.
* If state is goodbit (which is zero) all error flags are cleared.
*/
mFin.clear();
mFout.clear();
std::cout << "going to mFin.seekg(0, mFin.end)" << std::endl;
/**
*std::istream::seekg() sets the position of the next character to be extracted from the input stream.
*/
mFin.seekg(0, mFin.end);
/**
* std::istream::tellg() gets position in input sequence, returns the position of the current character in the input stream
* Internally, the function accesses the input sequence by first constructing a sentry object without evaluating it.
* Then, if member fail returns true, the function returns -1.
*/
mFinSize = mFin.tellg();
std::cout << "now pos of input stream:" << mFin.tellg() << std::endl;
std::cout << "going to mFin.seekg(4, mFin.beg)" << std::endl;
mFin.seekg(4, mFin.beg);//offset to filebuf[4]
std::cout << "now pos of input stream:" << mFin.tellg() << std::endl;
std::cout << "\n****************** iofstream with read()/write() ******************\n" << std::endl;
mFin.seekg(0, mFin.beg);
mFout.seekp(0, mFout.beg);
/**
* std::istream::read() extracts n characters from the stream and stores them in the array pointed to by s.
* This function simply copies a block of data, without checking its contents nor appending a null character at the end.
* and returns the istream object (*this).
*/
char content[mFinSize*4];
memset(content, 0, sizeof(content));
int length_fis = mFin.seekg(0, mFin.end).tellg();
mFin.seekg(0, mFin.beg);
std::cout << "created a char content["<< sizeof(content) << "]" << std::endl;
std::cout << "after mFin.read(content, " << length_fis << "), pos of input stream:" << mFin.read(content, length_fis).tellg() << std::endl;
char appends[32];
strcpy(appends, "\nappend_end\0");
std::cout << "now content:" << content << std::endl;
strncat(content, appends, strlen(appends));
std::cout << "now content:" << content << std::endl;
/**
* std::ostream::write() simply copies a block of data, without checking its contents:
* The array may contain null characters, which are also copied without stopping the copying process.
* and returns the ostream object (*this).
*/
std::cout << "after mFout.write(content, " << strlen(content) << "), pos of output stream:" << mFout.write(content, strlen(content)).tellp() << std::endl;
mFin.close();
mFout.close();
std::cout << "\n****************** main return ******************" << std::endl;
return 0;
#endif
#if 0
std::cout << "\n****************** namespace ******************\n" << std::endl;
char path1[32] = "myfis.log";
char path2[32] = "myfos.log";
mystd::myfstream::ifstream fis(path1);
mystd::myfstream::ofstream fos(path2);
//mystd::myfstream::ifstream::setUser(10);
//std::cout << "input file stream user:" << mystd::myfstream::ifstream::getUser() << std::endl;
fis.setUser(100);
std::cout << "input file stream user:" << fis.getUser() << std::endl;
//mystd::myfstream::ofstream::setUser(10);
//std::cout << "output file stream user:" << mystd::myfstream::ofstream::getUser() << std::endl;
fos.setUser(100);
std::cout << "input file stream user:" << fos.getUser() << std::endl;
char content[16] = "my_namespace";
fos.seekp(0, 0);
fos.write(content, strlen(content)+1);
fis.seekg(30, 0);
fis.read(content, strlen(content)+1);
std::cout << "going to std::cout << fis" << std::endl;
std::cout << fis;
std::cout << "going to std::cout << fos" << std::endl;
std::cout << fos;
std::cout << "going to std::cin >> fis" << std::endl;
std::cin >> fis;
std::cout << "going to std::cout << fis"<< std::endl;
std::cout << fis;
/*
fis.seekg(30, 0);
fis.read(content, strlen(content)+1);
fos.seekp(30, 0);
fos.write(content, strlen(content)+1);
*/
std::cout << "\n****************** main return ******************" << std::endl;
return 0;
#endif
#if 0
std::cout << "\n****************** new/delete ******************\n" << std::endl;
int (*p)[2]; //address shift is 2 of times of sizeof(int) between p and p+1
p = new int[3][2] {{11,12}, {21,22}, {31,32}};
//int data[3][2] {{11,12}, {21,22}, {31,32}};
//int (*p)[2] = data; //address shift is 2 of times of sizeof(int) between p and p+1
std::cout << "... beginning p :" << p << std::endl;
std::cout << "... beginning p+1:" << p+1 << std::endl;
std::cout << "... beginning p+2:" << p+2 << std::endl;
for (int i=0;i<3;i++) {
for (int j=0;j<2;j++) {
std::cout << "*(p+" << i << ")+" << j << ":" << *(p+i)+j << std::endl;
std::cout << "p[" << i << "]+" << j << " :" << p[i]+j << std::endl;
std::cout << "p[" << i << "]" << "[" << j << "]:" << (p[i][j]) << std::endl;
}
}
std::cout << "going to MyMath *m = new MyMath(999) that would invoke constructor" << std::endl;
MyMath *m = new MyMath(999);
std::cout << "going to delete m that would invoke deconstructor" << std::endl;
delete m;
std::cout << "\n****************** main return ******************" << std::endl;
return 0;
#endif
#if 0
std::cout << "****************** static laboratory ******************" << std::endl;
MyMath::user = 99;
MyMath::setUser(99);
std::cout << "math user:" << MyMath::getUser() << std::endl;
#endif
#if 0
std::cout << "****************** STL laboratory ******************/n" << std::endl;
#endif
#if 0
/**
* template < class T, class Alloc = allocator<T> > class vector; // generic template
*