├── .gitmodules ├── Makefile ├── README.md ├── day1 ├── 1.1 ├── 1.1.cc ├── 1.1.i ├── 1.1.s ├── 1.png ├── 2.png ├── 20190722_homework.txt ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── a1.txt ├── in.txt ├── mooc │ ├── .vscode │ │ ├── settings.json │ │ └── tasks.json │ ├── Complex │ ├── Complex.cc │ ├── in.txt │ ├── in2.txt │ ├── out.txt │ ├── out2.txt │ ├── stduentManage │ ├── stduentManage.cxx │ ├── stduentManage.dSYM │ │ └── Contents │ │ │ ├── Info.plist │ │ │ └── Resources │ │ │ └── DWARF │ │ │ └── stduentManage │ ├── weirdClass │ ├── weirdClass.cc │ ├── whatRet │ ├── whatRet.cc │ ├── wherePrt │ └── wherePrt.cc ├── readme.md ├── res.txt ├── res2.txt └── result.txt ├── day10 ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── Circle ├── Cylinder ├── Makefile ├── WOWII ├── day10_homework.txt ├── include │ └── Circle.hh ├── person ├── readme.md └── src │ ├── Circle.cc │ ├── Cylinder.cc │ ├── WOWII.cc │ └── person.cc ├── day11 ├── 1.png ├── 2.png ├── 3.png ├── Makefile ├── SizeofVCls ├── day11_homework.txt ├── dynamicBingding ├── pureVirtual ├── readme.md └── src │ ├── SizeofVCls.cc │ ├── dynamicBingding.cc │ └── pureVirtual.cc ├── day12 ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── Makefile ├── day12_homework.txt ├── log4cpp │ ├── Makefile │ ├── config │ │ └── log4cpp.conf │ ├── include │ │ └── log4cpp_head.hh │ ├── log4cpp │ └── src │ │ └── main.cc ├── queue │ ├── Makefile │ ├── include │ │ ├── method.tcc │ │ └── queueHead.hh │ ├── queue │ └── src │ │ └── queue.cc ├── readme.md └── templateSingleton │ ├── Makefile │ ├── include │ ├── Computer.hh │ ├── Point.hh │ ├── singletonMethod.tcc │ └── singletonTemp.hh │ ├── readme.md │ ├── singleton │ └── src │ └── main.cc ├── day13 ├── 1.png ├── 2.png ├── CntWords ├── CntWordsUnordered ├── CntWordsVector ├── HeapSort ├── LRU ├── Makefile ├── china_daily.txt ├── config │ └── log4cpp.conf ├── day13_homework.txt ├── include │ └── log4cpp_head.hh ├── readme.md ├── src │ ├── CntWords.cc │ ├── CntWordsUnordered.cc │ ├── CntWordsVector.cc │ ├── HeapSort.cc │ ├── LRU.cc │ └── textQuery.cc └── textQuery ├── day14 ├── 1.png ├── Makefile ├── againWordLadder ├── day14_homework.docx ├── mooc │ ├── Makefile │ ├── SimpleSumArray │ ├── againSort │ ├── filter │ ├── istream_iterator │ ├── notDiffTemplate │ ├── readme.md │ ├── simpleForeach │ ├── sortAgain │ ├── src │ │ ├── SimpleSumArray.cc │ │ ├── againSort.cc │ │ ├── filter.cc │ │ ├── istream_iterator.cc │ │ ├── notDiffTemplate.cc │ │ ├── simpleForeach.cc │ │ ├── sortAgain.cc │ │ └── whyTrue.cc │ └── whyTrue ├── readme.md ├── src │ ├── againWordLadder.cc │ └── validParentheses.cc ├── textQuery │ └── Makefile └── validParentheses ├── day15 ├── 1.png ├── 10.png ├── 11.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── 7.png ├── 8.png ├── 9.png ├── Makefile ├── day15_homework.txt ├── mooc │ ├── Cmplt │ ├── CmpltMyString │ ├── Makefile │ ├── MyString │ ├── PointOut │ ├── bigInt │ ├── inheritMyString │ ├── overLoadOp │ ├── readme.md │ └── src │ │ ├── Cmplt.cc │ │ ├── CmpltMyString.cc │ │ ├── MyString.cc │ │ ├── PointOut.cc │ │ ├── bigInt.cc │ │ ├── inheritMyString.cc │ │ └── overLoadOp.cc ├── readme.md ├── sgi_stl │ ├── 1config11 │ ├── 1config3 │ ├── 1config5 │ ├── Makefile │ ├── readme.md │ ├── src │ │ ├── 1config11.cc │ │ ├── 1config3.cc │ │ ├── 1config5.cc │ │ └── test.cc │ └── test ├── src │ └── vector.cc └── vector ├── day2 ├── 1.png ├── 2.png ├── Computer5.cc ├── day2_homework.txt ├── mooc │ ├── CntAnimalNum │ ├── CntAnimalNum.cc │ ├── WOW.cc │ ├── bigAndBase │ ├── bigAndBase.cc │ ├── harderSwap │ ├── harderSwap.cc │ ├── mythfulIniArr │ ├── mythfulIniArr.cc │ ├── pointerWhere │ ├── pointerWhere.cc │ ├── simpleSwap │ ├── simpleSwap.cc │ ├── test │ ├── test.cc │ ├── weirdRet │ ├── weirdRet.cc │ ├── whatRet │ └── whatRet.cc ├── myStr ├── myStr.cc └── readme.md ├── day3 ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── day3_homework.txt ├── mutex │ ├── Makefile │ ├── include │ │ └── mutex.h │ ├── mutex │ └── src │ │ ├── methodCond.cc │ │ ├── methodMutex.cc │ │ └── mutex.cc ├── queue │ ├── Makefile │ ├── include │ │ └── queueHead.h │ ├── queue │ └── src │ │ ├── main.cc │ │ └── method.cc ├── readme.md ├── singleTon ├── singleTon.cc ├── stack └── stack.cc ├── day4 ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── Makefile ├── The_Holy_Bible.txt ├── day4_homework.txt ├── dictionary │ ├── Makefile │ ├── The_Holy_Bible.txt │ ├── dictionary │ ├── dictionary.txt │ ├── incl │ │ └── head.h │ └── src │ │ └── main.cc ├── include │ └── vector.h ├── main ├── readme.md ├── src │ ├── 1.stackOnly.cc │ ├── 2.heapOnly.cc │ └── 3.vector.cc └── vector ├── day5 ├── 1.png ├── 2.png ├── 3.png ├── ConfigTest │ ├── 1.1.confTest │ ├── Makefile │ ├── config │ │ └── test.conf │ ├── include │ │ └── log4cpp_head.hh │ ├── log4cpp │ └── src │ │ └── 1.1.confTest.cc ├── befConfig │ ├── 1.test │ ├── 1.test.cc │ └── include │ │ └── log4cpp_head.hh ├── day5_homework.txt ├── log4cppPKG │ ├── Makefile │ ├── config │ │ └── log4cpp.conf │ ├── include │ │ └── log4cpp_head.hh │ ├── log4cpp │ └── src │ │ └── main.cc └── readme.md ├── day6 ├── Makefile ├── day6.md └── readme.md ├── day8 ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── FbCowString │ ├── Makefile │ ├── fbstr │ ├── include │ │ └── FbStrCOW.hxx │ └── src │ │ ├── CharProxyCOW.cc │ │ ├── FbStringCOW.cc │ │ └── main.cc ├── Makefile ├── SingletonAutoRelease │ ├── Makefile │ ├── include │ │ └── singleton.hh │ ├── singleton │ └── src │ │ ├── main.cc │ │ └── singleton.cc ├── day8_homework.txt ├── log4cpp ├── pimpl │ ├── Makefile │ ├── include │ │ ├── pimpl.hxx │ │ └── student.hxx │ ├── pimpl │ └── src │ │ ├── main.cc │ │ ├── pimplMethod.cc │ │ └── student.cc └── readme.md └── day9 ├── Makefile ├── day9_homework.txt ├── readme.md ├── res.txt ├── resources ├── bomtest.xml ├── coolshell.xml ├── dream.xml ├── empty.xml ├── out │ ├── compact.xml │ ├── dreamout.xml │ ├── pretty.xml │ ├── printer.xml │ ├── readme.txt │ ├── test7.xml │ ├── textfile.txt │ └── utf8testout.xml ├── utf8test.xml ├── utf8testverify.xml ├── xmltest-4636783552757760.xml ├── xmltest-5330.xml ├── xmltest-5662204197076992.xml └── xmltest-5720541257269248.xml ├── src ├── coolshell.cc └── tinyxml2.cc ├── text.html ├── xml └── xmltest.cc /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "day7"] 2 | path = day7 3 | url = https://github.com/stjimreal/CPP_DAY7 4 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | OBJS := ${patsubst ./src/%.cc, ./bin/%.o, $(SRCS)} 3 | ELFS:= queue 4 | CFLAGS:= -std=c++17 -Wall 5 | CXX:= g++ 6 | 7 | $(ELFS):$(OBJS) 8 | $(CXX) $(CFLAGS) $^ -o $@ 9 | 10 | ./bin/%.o:./src/%.cc 11 | $(CXX) $(CFLAGS) -c $< -o $@ 12 | 13 | .PHONY: clean rebuild 14 | clean: 15 | rm -rf $(ELFS) $(OBJS) 16 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C++ 作业题目汇总 2 | 3 | ## DAY1 4 | 5 | [作业文档](./day1/20190722_homework.txt) 6 | [提交答案](./day1/readme.md) 7 | 8 | ## DAY2 9 | 10 | [作业文档](./day2/day2_homework.txt) 11 | [提交答案](./day2/readme.md) 12 | 13 | ## DAY3 14 | 15 | [作业文档](./day3/day3_homework.txt) 16 | [提交答案](./day3/readme.md) 17 | 18 | ## DAY4 19 | 20 | [作业文档](./day4/day4_homework.txt) 21 | [提交答案](./day4/readme.md) 22 | 23 | ## DAY5 24 | 25 | [作业文档](./day5/day5_homework.txt) 26 | [提交答案](./day5/readme.md) 27 | 28 | ## DAY6 29 | 30 | [提交答案](./day6/readme.md) 31 | 32 | ## DAY7 33 | 34 | [作业文档](https://github.com/stjimreal/CPP_DAY7/blob/3a07cd330bfc97ef38d42a92a4391f8360acf760/day7_homework.txt) 35 | [提交答案](https://github.com/stjimreal/CPP_DAY7/blob/3a07cd330bfc97ef38d42a92a4391f8360acf760/README.md) 36 | 37 | ## DAY8 38 | 39 | [作业文档](./day8/day8_homework.txt) 40 | [提交答案](./day8/readme.md) 41 | 42 | ## DAY10 43 | 44 | [作业文档](./day10/day10_homework.txt) 45 | [提交答案](./day10/readme.md) 46 | 47 | ## DAY11 48 | 49 | [作业文档](./day11/day11_homework.txt) 50 | [提交答案](./day11/readme.md) 51 | 52 | ## DAY12 53 | 54 | [作业文档](./day12/day12_homework.txt) 55 | [提交答案](./day12/readme.md) 56 | 57 | ## DAY13 58 | 59 | [作业文档](./day13/day13_homework.txt) 60 | [提交答案](./day13/readme.md) 61 | 62 | ## DAY14 63 | 64 | [作业文档](./day14/day14_homework.txt) 65 | [提交答案](./day14/readme.md) 66 | -------------------------------------------------------------------------------- /day1/1.1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day1/1.1 -------------------------------------------------------------------------------- /day1/1.1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | #define DEM "ax" 7 | const int AX = 10; 8 | int AY; 9 | int main(const int argc, const char **argv) 10 | { 11 | const int x = 5; 12 | int y = 10; 13 | int * p = (int *)&x; 14 | *p = 11; 15 | cout<<"address:"<<&x<<" value = "< 2 | #include 3 | #include 4 | using namespace std; 5 | class Complex { 6 | private: 7 | double r,i; 8 | public: 9 | void Print() { 10 | cout << r << "+" << i << "i" << endl; 11 | } 12 | Complex(){r = 0; i = 0;} 13 | Complex(const char* s) 14 | { 15 | string str = s; 16 | auto pos1 = str.find('+'); 17 | auto pos2 = str.find_last_not_of('i'); 18 | r = atof(str.c_str()); 19 | i = atof(str.substr(pos1 + 1, pos2 - pos1).c_str()); 20 | } 21 | }; 22 | int main() { 23 | Complex a; 24 | a = "3.67245+4.45i"; a.Print(); 25 | a = "5+6i"; a.Print(); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /day1/mooc/in.txt: -------------------------------------------------------------------------------- 1 | Wang Hao,21,1300012703,91,96,94,89 2 | -------------------------------------------------------------------------------- /day1/mooc/in2.txt: -------------------------------------------------------------------------------- 1 | Leonardo DiCaprio,18,138,59,59,59,63 2 | -------------------------------------------------------------------------------- /day1/mooc/out.txt: -------------------------------------------------------------------------------- 1 | Wang Hao,21,1300012703,92.5 2 | -------------------------------------------------------------------------------- /day1/mooc/out2.txt: -------------------------------------------------------------------------------- 1 | Wang Hao,21,1300012703,92.5 2 | -------------------------------------------------------------------------------- /day1/mooc/stduentManage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day1/mooc/stduentManage -------------------------------------------------------------------------------- /day1/mooc/stduentManage.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | class Student { 10 | // 姓名,年龄,学号,第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,第四学年平均成绩。 11 | char name[24]; 12 | unsigned age, sno, sumGrade; 13 | double avgGrade; 14 | public: 15 | void input() 16 | { 17 | char ch; 18 | char word[48]; 19 | int t; 20 | t = 0; 21 | bzero(name, 24); 22 | age = 0; sno = 0; sumGrade = 0; 23 | while((ch = getchar())!= ',') 24 | { 25 | name[t++] = ch; 26 | } 27 | t = 0;bzero(word, 48); 28 | while((ch = getchar())!= ',') 29 | word[t++] = ch; 30 | age = atoi(word); 31 | t = 0;bzero(word, 48); 32 | while((ch = getchar())!= ',') 33 | word[t++] = ch; 34 | sno = atoi(word); 35 | t = 0;bzero(word, 48); 36 | while((ch = getchar())!= EOF) 37 | { 38 | if(ch == ',') 39 | { 40 | sumGrade += atoi(word); 41 | t = 0;bzero(word, 48); 42 | continue; 43 | } 44 | word[t++] = ch; 45 | } 46 | sumGrade += atoi(word); 47 | } 48 | void calculate() 49 | { 50 | avgGrade = double(sumGrade) / 4.0; 51 | } 52 | void output() 53 | { 54 | cout< 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.stduentManage 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /day1/mooc/stduentManage.dSYM/Contents/Resources/DWARF/stduentManage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day1/mooc/stduentManage.dSYM/Contents/Resources/DWARF/stduentManage -------------------------------------------------------------------------------- /day1/mooc/weirdClass: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day1/mooc/weirdClass -------------------------------------------------------------------------------- /day1/mooc/weirdClass.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Sample { 4 | public: 5 | int v; 6 | // 在此处补充你的代码 7 | Sample(){} 8 | Sample(int a):v(a){} 9 | // 复制构造函数可以访问私有成员 10 | Sample(const Sample &a):v(a.v + 2){} 11 | 12 | }; 13 | // 9、22、5 14 | void PrintAndDouble(Sample o) 15 | { 16 | cout << o.v; 17 | cout << endl; 18 | } 19 | int main() 20 | { 21 | Sample a(5); 22 | Sample b = a; 23 | PrintAndDouble(b); 24 | Sample c = 20; 25 | PrintAndDouble(c); 26 | Sample d; 27 | d = a; 28 | cout << d.v; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /day1/mooc/whatRet: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day1/mooc/whatRet -------------------------------------------------------------------------------- /day1/mooc/whatRet.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A { 4 | public: 5 | int val; 6 | 7 | A(int 8 | // 在此处补充你的代码 9 | v): val(v){} 10 | A():val(123){} 11 | A& GetObj() 12 | { 13 | return *this; 14 | } 15 | }; 16 | int main() 17 | { 18 | int m,n; 19 | A a; 20 | cout << a.val << endl; 21 | while(cin >> m >> n) { 22 | a.GetObj() = m; 23 | cout << a.val << endl; 24 | a.GetObj() = A(n); 25 | cout << a.val<< endl; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /day1/mooc/wherePrt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day1/mooc/wherePrt -------------------------------------------------------------------------------- /day1/mooc/wherePrt.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A { 4 | public: 5 | int i; 6 | A(int x) { i = x; } 7 | ~A(){printf("%d\n", i);} 8 | }; 9 | int main() 10 | { 11 | A a(1); 12 | A * pa = new A(2); 13 | delete pa; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /day1/readme.md: -------------------------------------------------------------------------------- 1 | # C++ day1 2 | 3 | ## 1.const关键字与宏定义的区别是什么 4 | 5 | > 区别在于宏定义属于立即数,`const`关键字在全局中与立即数位于相同的地址空间,是无法访问的代码区,如果强制转换,则会造成程序运行崩溃。 6 | 7 | ```cpp 8 | #include 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | #define DEM "ax" 13 | const int AX = 10; 14 | int AY; 15 | int main(const int argc, const char **argv) 16 | { 17 | const int x = 5; 18 | int y = 10; 19 | const int * p = &AX; 20 | int * ptr = (int *)p; 21 | *ptr = 11; 22 | cout<<"address:"<<&AX<<" value = "<<*p; 23 | cout<<"address of global const = "<<&AX<<" address of function const = "<<&x< 2 | 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::string; 7 | 8 | class person 9 | { 10 | public: 11 | person() 12 | : _name(), _age(-1){} 13 | person(string nm, int age) 14 | : _name(nm), _age(age){} 15 | virtual void display() 16 | { 17 | cout<<_name<<' '<<_age<<' '; 18 | } 19 | private: 20 | string _name; 21 | int _age; 22 | }; 23 | 24 | class employee: public person 25 | { 26 | public: 27 | employee() 28 | : person() 29 | , _department(), _salary(-1) 30 | {} 31 | employee(string nm, int age, string depart, double salary) 32 | : person(nm, age) 33 | , _department(depart) 34 | , _salary(salary) 35 | {} 36 | void display() 37 | { 38 | person::display(); 39 | cout<<_department<<' '<<_salary<
98 | 99 | ![picture](1.png) 100 | 对象空间 101 | ![picture](2.png) 102 | 指针内容 103 | 104 | 105 | ### 2. 纯虚函数 106 | 107 | ```cpp 108 | #include 109 | #include 110 | 111 | using std::cout; 112 | using std::cin; 113 | using std::endl; 114 | using std::string; 115 | 116 | class student 117 | { 118 | public: 119 | virtual void info()const =0; 120 | student(const string&nm, long int sno) 121 | : _nm(nm), _sno(sno) 122 | {} 123 | 124 | friend std::ostream& operator<<(std::ostream& os, const student &stu) 125 | { 126 | os<<"学生信息是"<
191 | 192 | ![picture](3.png) 193 | -------------------------------------------------------------------------------- /day11/src/SizeofVCls.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | 7 | class vbase 8 | { 9 | public: 10 | virtual void display() 11 | { 12 | cout<<"I am virtual base\n"; 13 | } 14 | long int _vvb = 8; 15 | }; 16 | 17 | class base 18 | { 19 | public: 20 | base() = default; 21 | virtual void display() 22 | { 23 | cout<<"I am base\n"; 24 | } 25 | private: 26 | int _bv = 16; 27 | }; 28 | 29 | class child:virtual public vbase, public base 30 | { 31 | public: 32 | virtual void display() 33 | { 34 | cout<<"I am child\n"; 35 | } 36 | virtual void show(){} 37 | private: 38 | int _cv = 32; 39 | }; 40 | 41 | int main() 42 | { 43 | int j = 0, k = 0; 44 | child me; 45 | base& h1 = me; 46 | vbase& h2 = me; 47 | int i = 6; 48 | me.display(); 49 | h1.display(); 50 | h2.display(); 51 | printf("Sizeof me = %ld\n", sizeof(me)); 52 | } -------------------------------------------------------------------------------- /day11/src/dynamicBingding.cc: -------------------------------------------------------------------------------- 1 | //在类内的成员函数中访问该类层次中的虚函数,要使用this指针 2 | #include 3 | using namespace std; 4 | 5 | class base //基类定义 6 | { 7 | public: 8 | virtual void disp() //虚函数disp 9 | { 10 | cout<<"hello,base"<disp(); //或直接用disp() 16 | } 17 | 18 | void call_base_2() 19 | { 20 | base::disp(); //去掉bass::再试试 21 | } 22 | }; 23 | 24 | class child:public base //派生类定义 25 | { 26 | public: 27 | void disp() //对虚函数进行了覆盖定义 28 | { 29 | cout<<"hello,child"<disp(); 35 | } 36 | 37 | // 函数call_base_1()在child类中虽然没有直接写出来, 38 | // 但还是继承过来了,默认和base类的代码是一样的 39 | // void call_base_1() 40 | // { 41 | // this->disp(); 42 | // } 43 | 44 | // 函数call_base_2()在child类中虽然没有直接写出来, 45 | // 但还是继承过来了,默认和base类的代码是一样的 46 | // void call_base_2() 47 | // { 48 | // base::disp(); 49 | // } 50 | }; 51 | 52 | int main() 53 | { 54 | base obj_Base; //声明一个基类对象 55 | child obj_Child; //声明一个派生类对象 56 | obj_Base.call_base_1(); //基类对象调用非虚函数call_base_1() 57 | obj_Child.call_child_1(); //派生类对象调用非虚函数call_child_1() 58 | 59 | cout << endl; 60 | 61 | base* pBase = &obj_Base; //声明一基类指针,并用基类对象地址为其初始化 62 | pBase->call_base_1(); //使用基类指针在成员函数内调用虚函数 63 | pBase->call_base_2(); 64 | 65 | cout << endl; 66 | 67 | pBase = &obj_Child; //用派生类对象地址为基类指针初始化 68 | pBase->call_base_1(); //使用基类指针在成员函数内调用虚函数 69 | pBase->call_base_2(); 70 | 71 | return 0; 72 | } 73 | 74 | -------------------------------------------------------------------------------- /day11/src/pureVirtual.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | 9 | class student 10 | { 11 | public: 12 | virtual void info()const =0; 13 | student(const string&nm, long int sno) 14 | : _nm(nm), _sno(sno) 15 | {} 16 | 17 | friend std::ostream& operator<<(std::ostream& os, const student &stu) 18 | { 19 | os<<"学生信息是"<::getInstance("Xiaomi", 6666); 19 | Computer * p2 = Singleton::getInstance("Xiaomi", 6666); 20 | 21 | Point * p3 = Singleton::getInstance(1, 2); 22 | Point * p4 = Singleton::getInstance(1, 2); 23 | 24 | return 0; 25 | } 26 | 27 | 3. 实现log4cpp的封装,使其可以像printf一样使用,测试用例如下: 28 | 29 | void test() { 30 | int number = 100; 31 | const char * pstr = "hello, log4cpp"; 32 | LogInfo("this is an info message. number = %d, str = %s\n", number, pstr); 33 | } 34 | 35 | 36 | 4. STL是由哪些组件构成的?它们各自具备什么样的特点? 37 | 38 | 5. 序列式容器都有哪些异同? -------------------------------------------------------------------------------- /day12/log4cpp/Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | OBJS := ${patsubst ./src/%.cc, ./bin/%.o, $(SRCS)} 3 | ELFS:= log4cpp 4 | CFLAGS:= -std=c++17 -Wall 5 | CXX:= g++ 6 | $(ELFS):$(OBJS) 7 | $(CXX) $(CFLAGS) $^ -o $@ -llog4cpp -lpthread 8 | ./bin/%.o:./src/%.cc 9 | $(CXX) $(CFLAGS) -c $< -o $@ 10 | 11 | .PHONY: clean 12 | clean: 13 | rm -rf $(ELFS) $(OBJS) 14 | .PHONY: rebuild 15 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /day12/log4cpp/config/log4cpp.conf: -------------------------------------------------------------------------------- 1 | # 定义了3个category fatal, warn, debug 2 | # 其中warn和debug设置了additivity属性为false;fatal的additivity属性默认为true 3 | 4 | rootCategory=DEBUG, rootAppender, A3 5 | 6 | category.fatal=WARN,A1 7 | 8 | category.warn=NOTICE, A2 9 | 10 | # category.debug=DEBUG, 11 | 12 | # 定义rootAppender类型和layout属性,这里在bash打印与log中相同的内容 13 | appender.rootAppender=org.apache.log4cpp.ConsoleAppender 14 | appender.rootAppender.layout=org.apache.log4cpp.PatternLayout 15 | appender.rootAppender.layout.ConversionPattern=[%p]"%m"%d{%Y-%m-%d %H:%M:%S} [%c]%n 16 | 17 | #定义fatal记录、warn记录、debug记录的属性 18 | appender.A1=org.apache.log4cpp.RollingFileAppender 19 | appender.A1.fileName=./log/error.log 20 | appender.A1.maxFileSize=102400 21 | appender.A1.maxBackupIndex=3 22 | appender.A1.backupPattern=%Y-%m-%d 23 | appender.A1.layout=org.apache.log4cpp.PatternLayout 24 | appender.A1.layout.ConversionPattern=[%p]"%m"%d{%Y-%m-%d %H:%M:%S} [%c]%n 25 | 26 | appender.A2=org.apache.log4cpp.RollingFileAppender 27 | appender.A2.fileName=./log/warn.log 28 | appender.A2.maxFileSize=102400 29 | appender.A2.maxBackupIndex=3 30 | appender.A2.backupPattern=%Y-%m-%d 31 | appender.A2.layout=org.apache.log4cpp.PatternLayout 32 | appender.A2.layout.ConversionPattern=[%p]"%m"%d{%Y-%m-%d %H:%M:%S} [%c]%n 33 | 34 | appender.A3=org.apache.log4cpp.RollingFileAppender 35 | appender.A3.fileName=./log/debug.log 36 | appender.A3.maxFileSize=102400 37 | appender.A3.maxBackupIndex=3 38 | appender.A3.backupPattern=%Y-%m-%d 39 | appender.A3.layout=org.apache.log4cpp.PatternLayout 40 | appender.A3.layout.ConversionPattern=[%p]"%m"%d{%Y-%m-%d %H:%M:%S} [%c]%n -------------------------------------------------------------------------------- /day12/log4cpp/include/log4cpp_head.hh: -------------------------------------------------------------------------------- 1 | #ifndef _LOG4CPP_HEAD_HH 2 | #define _LOG4CPP_HEAD_HH 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace log4cpp; 17 | 18 | #define log4cpp_prt(func, string) func("%s --%s--%s--%d--", x.c_str(), __FILE__, __FUNCTION__, __LINE__) 19 | using std::cout; 20 | using std::endl; 21 | 22 | class Log 23 | { 24 | public: 25 | template static void LogInfo(const char *str, const T&...args) 26 | { 27 | _restore->debug.info(str, args...); 28 | } 29 | static Log* genObj(); 30 | static void destroy(); 31 | 32 | log4cpp::Category& fatal; 33 | log4cpp::Category& warn; 34 | log4cpp::Category& debug; 35 | 36 | private: 37 | static void init(); 38 | Log(); 39 | ~Log(); 40 | log4cpp::Category& _root; 41 | static Log* _restore; 42 | static pthread_once_t _auto; 43 | }; 44 | 45 | Log* Log::_restore = nullptr; 46 | pthread_once_t Log::_auto = PTHREAD_ONCE_INIT; 47 | 48 | Log::Log(): 49 | fatal(log4cpp::Category::getInstance(std::string("fatal"))), 50 | warn(log4cpp::Category::getInstance(std::string("warn"))), 51 | debug(log4cpp::Category::getInstance(std::string("debug"))), 52 | _root(log4cpp::Category::getRoot()) 53 | {} 54 | 55 | Log::~Log(){} 56 | 57 | Log* Log::genObj() 58 | { 59 | if(_restore) 60 | return _restore; 61 | pthread_once(&_auto, init); 62 | return _restore; 63 | } 64 | 65 | void Log::init() 66 | { 67 | try 68 | { 69 | PropertyConfigurator::configure("./config/log4cpp.conf"); 70 | } 71 | catch(ConfigureFailure& f) 72 | { 73 | std::cerr << f.what() << '\n'; 74 | exit(-1); 75 | } 76 | _restore = new Log; 77 | ::atexit(destroy); 78 | } 79 | 80 | void Log::destroy() 81 | { 82 | if(_restore ==nullptr) 83 | return; 84 | Category::shutdown(); 85 | delete _restore; 86 | } 87 | #endif -------------------------------------------------------------------------------- /day12/log4cpp/log4cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day12/log4cpp/log4cpp -------------------------------------------------------------------------------- /day12/log4cpp/src/main.cc: -------------------------------------------------------------------------------- 1 | #include "../include/log4cpp_head.hh" 2 | 3 | 4 | 5 | 6 | void test() { 7 | int number = 100; 8 | const char * pstr = "hello, log4cpp"; 9 | Log::LogInfo("this is an info message. number = %d, str = %s\n", number, pstr); 10 | } 11 | 12 | int main() 13 | { 14 | Log::genObj(); 15 | test(); 16 | return 0; 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /day12/queue/Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | OBJS := ${patsubst ./src/%.cc, ./bin/%.o, $(SRCS)} 3 | ELFS:= queue 4 | CFLAGS:= -std=c++17 -Wall 5 | CXX:= g++ 6 | 7 | $(ELFS):$(OBJS) 8 | $(CXX) $(CFLAGS) $^ -o $@ 9 | 10 | ./bin/%.o:./src/%.cc 11 | $(CXX) $(CFLAGS) -c $< -o $@ 12 | 13 | .PHONY: clean rebuild 14 | clean: 15 | rm -rf $(ELFS) $(OBJS) 16 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /day12/queue/include/method.tcc: -------------------------------------------------------------------------------- 1 | #define __QUEUE_METHOD_TCC 2 | #include "queueHead.hh" 3 | 4 | template void queue::push(T x) 5 | { 6 | linkNode* tmp = new linkNode(x); 7 | _tail->next = tmp; 8 | _tail = _tail->next; 9 | printf("value "); 10 | std::cout<<_tail->value; 11 | printf(" is pushed. Now you have %d members\n", ++_size); 12 | } 13 | 14 | template void queue::pop() 15 | { 16 | if(empty()) return; 17 | linkNode* tmp = _head -> next; 18 | 19 | printf("value "); 20 | std::cout<value; 21 | printf(" is poped. Now you have %d members\n", --_size); 22 | _head -> next = _head -> next -> next; 23 | delete tmp; 24 | } 25 | 26 | template T queue::front() 27 | { 28 | T ret; 29 | if(empty()) 30 | { 31 | printf("get value failed\n"); 32 | return T(); 33 | } 34 | ret = _head -> next -> value; 35 | return ret; 36 | } 37 | 38 | template T queue::back() 39 | { 40 | T ret; 41 | if(empty()) 42 | { 43 | printf("get value failed\n"); 44 | return T(); 45 | } 46 | ret = _tail -> value; 47 | return ret; 48 | } 49 | 50 | template bool queue::empty() 51 | { 52 | return _size == 0; 53 | } 54 | 55 | template bool queue::full() 56 | { 57 | return false; 58 | } 59 | 60 | template queue::~queue() 61 | { 62 | printf("%s function called\n", __FUNCTION__); 63 | while(1) 64 | { 65 | if(empty()) 66 | { 67 | delete _head; 68 | break; 69 | } 70 | pop(); 71 | } 72 | } -------------------------------------------------------------------------------- /day12/queue/include/queueHead.hh: -------------------------------------------------------------------------------- 1 | #ifndef __QUEUE_HEAD_H 2 | #define __QUEUE_HEAD_H 3 | #include 4 | 5 | template 6 | class queue 7 | { 8 | struct linkNode 9 | { 10 | T value; 11 | linkNode* next; 12 | linkNode():value(T()), next(nullptr){} 13 | linkNode(T x):value(x), next(nullptr){} 14 | }; 15 | linkNode* _head, *_tail; 16 | int _size; 17 | public: 18 | queue(){_tail = _head = new linkNode;_size = 0;} 19 | queue(const queue &x):_head(x._head), _tail(x._tail){} 20 | 21 | /* 元素入队 */ 22 | void push(T); 23 | /* 元素出队 */ 24 | void pop(); 25 | /* 读取队头元素 */ 26 | T front(); 27 | /* 读取队尾元素 */ 28 | T back(); 29 | /* 判断队列是否为空 */ 30 | bool empty(); 31 | /* 判断队列是否已满 */ 32 | bool full(); 33 | 34 | ~queue(); 35 | }; 36 | #endif 37 | 38 | #ifndef __QUEUE_METHOD_TCC 39 | #define __QUEUE_METHOD_TCC 40 | #include "./method.tcc" 41 | #endif -------------------------------------------------------------------------------- /day12/queue/queue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day12/queue/queue -------------------------------------------------------------------------------- /day12/queue/src/queue.cc: -------------------------------------------------------------------------------- 1 | #include "../include/queueHead.hh" 2 | 3 | int main() 4 | { 5 | queue sq2; 6 | std::string a; 7 | a = "10";sq2.push(a); 8 | a = "12";sq2.push(a); 9 | a = "14";sq2.push(a); 10 | std::string front = sq2.front(); 11 | std::cout<<"front = "< 6 | template T* Singleton::getInstance(const Args&... args) 7 | { 8 | printf("%s\n", __FUNCTION__); 9 | if(_ptr) 10 | return &_ptr->_value; 11 | ::atexit(Singleton::destroy); 12 | pthread_once(&_auto, init); 13 | _ptr->_value = T(args...); 14 | return &_ptr->_value; 15 | } 16 | 17 | template 18 | void Singleton::init() 19 | { 20 | printf("%s\n", __FUNCTION__); 21 | _ptr = new Singleton; 22 | } 23 | 24 | template 25 | void Singleton::destroy() 26 | { 27 | printf("%s\n", __FUNCTION__); 28 | if(_ptr) 29 | delete _ptr; 30 | } 31 | 32 | template Singleton* Singleton::_ptr = nullptr; 33 | template pthread_once_t Singleton::_auto = PTHREAD_ONCE_INIT; 34 | -------------------------------------------------------------------------------- /day12/templateSingleton/include/singletonTemp.hh: -------------------------------------------------------------------------------- 1 | #ifndef __SINGLETON_TEMPLATE_HH 2 | #define __SINGLETON_TEMPLATE_HH 3 | 4 | #include 5 | #include 6 | 7 | template 8 | class Singleton 9 | { 10 | public: 11 | template static T* getInstance(const Args&... args); 12 | 13 | private: 14 | static void destroy(); 15 | static void init(); 16 | Singleton(): _value(T()){printf("%s\n", __FUNCTION__);} 17 | Singleton(const Singleton&){} 18 | ~Singleton(){printf("%s\n", __FUNCTION__);} 19 | 20 | T _value; 21 | static Singleton* _ptr; 22 | static pthread_once_t _auto; 23 | }; 24 | 25 | #endif 26 | 27 | #ifndef __SINGLETON_METHOD_CC 28 | #define __SINGLETON_METHOD_CC 29 | #include "singletonMethod.tcc" 30 | #endif 31 | -------------------------------------------------------------------------------- /day12/templateSingleton/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day12/templateSingleton/readme.md -------------------------------------------------------------------------------- /day12/templateSingleton/singleton: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day12/templateSingleton/singleton -------------------------------------------------------------------------------- /day12/templateSingleton/src/main.cc: -------------------------------------------------------------------------------- 1 | #include "../include/singletonTemp.hh" 2 | #include "../include/Computer.hh" 3 | #include "../include/Point.hh" 4 | 5 | Point pt(1, 2); 6 | Point pt2(3, 4); 7 | 8 | Computer com("Mac", 8888); 9 | 10 | int main(void) 11 | { 12 | Computer * p1 = Singleton::getInstance("Xiaomi", 6666); 13 | Computer * p2 = Singleton::getInstance("Xiaomi", 6666); 14 | printf("p1 address = %p, p2 address = %p\n", p1, p2); 15 | 16 | Point * p3 = Singleton::getInstance(1, 2); 17 | Point * p4 = Singleton::getInstance(1, 2); 18 | printf("p3 address = %p, p4 address = %p\n", p3, p4); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /day13/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day13/1.png -------------------------------------------------------------------------------- /day13/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day13/2.png -------------------------------------------------------------------------------- /day13/CntWords: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day13/CntWords -------------------------------------------------------------------------------- /day13/CntWordsUnordered: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day13/CntWordsUnordered -------------------------------------------------------------------------------- /day13/CntWordsVector: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day13/CntWordsVector -------------------------------------------------------------------------------- /day13/HeapSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day13/HeapSort -------------------------------------------------------------------------------- /day13/LRU: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day13/LRU -------------------------------------------------------------------------------- /day13/Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | OBJS := ${patsubst ./src/%.cc, ./bin/%.o, $(SRCS)} 3 | ELFS:= ${patsubst ./src/%.cc, %, $(SRCS)} 4 | CFLAGS:= -std=c++17 -Wall 5 | CXX:= g++ 6 | all:$(ELFS) 7 | 8 | %:./src/%.cc 9 | $(CXX) $(CFLAGS) $< -o $@ -llog4cpp 10 | 11 | .PHONY: clean rebuild 12 | clean: 13 | rm -rf $(ELFS) $(OBJS) 14 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /day13/china_daily.txt: -------------------------------------------------------------------------------- 1 | 1.Shenzhen-HK stock link 'likely in second quarter': UBS 2 | 3 | The Chinese mainland will likely announce the launch of the much-anticipated 4 | stock trading link between the Shenzhen and Hong Kong exchanges in the second 5 | quarter of the year, further opening the mainland's stock market to overseas 6 | investors, a senior analyst at UBS Securities said on Wednesday. 7 | 8 | If the link is launched this year, it would mean that about 70 percent of market 9 | capitalization of the A shares will be available to overseas investors who already 10 | gained access to the mainland market through the existing Shanghai-Hong Kong 11 | Stock Connect, said Gao Ting, chief China equities strategist at UBS Securities. 12 | 13 | The link will broaden investors' stock selection in the mainland as the Shenzhen 14 | market, unlike Shanghai, is home to many high-tech, innovative companies and 15 | private firms with high growth potential, Gao said. 16 | 17 | "The trading link will increase the investment opportunities for overseas investors 18 | in sectors including healthcare, consumption, material and information technology," 19 | he said at a news conference in Beijing. 20 | 21 | Premier Li Keqiang said in March that the central government will seek to launch the 22 | Shenzhen-Hong Kong Stock Connect this year. 23 | 24 | Bloomberg reported earlier citing people familiar with the matter that the launch 25 | of the trading link may be announced before July. It quoted a spokesman for the 26 | Hong Kong Exchanges & Clearing Ltd as saying that a preparatory period of three to 27 | four months will be needed following the announcement of regulatory approval. 28 | 29 | While overseas investors can already invest in the mainland's stock market under the 30 | Qualified Foreign Institutional Investors program, the launch of the Shenzhen-Hong 31 | Kong Stock Connect still holds symbolic significance as it will signal a further 32 | opening of the capital market, analysts said. 33 | 34 | Zhao Wenli, an analyst at China Merchants Securities (HK) Co Ltd, said in a 35 | research note that good quality and reasonably priced small and mid-cap stocks 36 | under the trading link will attract greater attention from foreign investors. 37 | 38 | 39 | 40 | 2.Going abroad? Don't forget your instant noodles 41 | 42 | Chinese travelers like to take instant noodles with them while travelling abroad, 43 | according to a report by Hong Kong Economic Times. 44 | And the favorite items travelers like to bring with them in their luggage differed 45 | greatly depending on their nationalities, according to a survey conducted by travel 46 | website Lowcost Holidays, which sampled 7,500 travelers from 29 countries and regions. 47 | 48 | Most of the items were edible, partially because travelers want a taste of home 49 | wherever they go, such as kimchi for 55 percent of South Koreans, cheese for 53 50 | percent of French, coffee for 64 percent of Italians, tea bags for 48 percent of 51 | British, and miso soup (a very popular soup) for 56 percent of Japanese. 52 | 53 | Others choices were somewhat surprising. Sixty-six percent of Belgians brought bath 54 | towels for scrubbing their body, 32 percent of Americans brought toilet paper, 55 | 37 percent of Russians packed cards, and 30 percent of Swiss, their Swiss knives. 56 | 57 | 58 | 3.Popular eatery's duck tastes flight online 59 | 60 | Quanjude, China's iconic restaurant chain for original Peking roast duck with a 61 | history since 1864, has embraced the nation's "Internet Plus" strategy, with a new 62 | online platform that features duck rolls and about 30 other dishes. 63 | "Internet Plus" has sparked integration of the Internet with traditional industries, 64 | and the food and beverage business has been no exception. In the catering and 65 | dining industry, the past year has witnessed a speedy market increase in online 66 | ordering, delivery and payment. 67 | 68 | "Chinese people say life is all about clothing, eating, housing and traffic. 69 | The Internet has changed all the other three industries before it started to change 70 | dining and catering industry very recently, and Quanjude has been adapting into the new environment to make most out of it," says Xu Jia, chief accounting officer 71 | with Quanjude and chairman of a new joint-venture company that is pursuing the online takeout and e-commerce market. 72 | 73 | Recently, the State-owned restaurant chain announced in Beijing that it established 74 | the company, Yage Technology Inc, in October 2015 with Chongqing Kuangcao Technology 75 | Inc, an online-service company based in Chongqing. 76 | 77 | "We believe with our time-honored brand image, experienced artisan cooking skills, 78 | detail-oriented service and superb supply chain, we will succeed in this new sector, 79 | " Xu says, "because even in the era of the Internet, what matters most in the 80 | industry is still the food and service." 81 | 82 | After more than a year's research and development, the new company has developed patented 83 | techniques to make high-quality Peking roast duck rolls available for takeout diners, 84 | testing the product in a six-month pilot project in Chongqing. 85 | 86 | Under the brand name Xiaoyage, literally "little duck brother", the online-ordered 87 | duck rolls are made in Quanjude restaurants, with the same recipe and ingredients 88 | as rolls served in Quanjude restaurants. 89 | 90 | At home or work, diners can reheat the duck rolls to 65 C in six minutes, using 91 | a special bag that generates steam when water is poured on it. 92 | 93 | The takeout duck rolls taste almost the same as those served in a Quanjude restaurant, 94 | according to Yang Aixiang, general manager with Yage Technology. 95 | 96 | The package costs 200 yuan ($31) each, and apart from the duck dish, there are 97 | also more than 30 signature dishes of Quanjude available through online ordering 98 | channels, such as mustard duck feet and spicy sliced duck wings. All can be ordered 99 | through the official WeChat account (xiaoyage222) and the Baidu Takeaway platform. 100 | 101 | Authorities in several municipalities have posed new regulations on speed and route 102 | limits for delivery vehicles, including Beijing, the first and most important market for Quanjude Takeout and e-commerce. 103 | 104 | Yang Xun, a publicist with Baidu Takeout, which handles delivery service of 105 | Quanjude's takeout delicacies, says all their deliverymen will obey laws and 106 | regulations to ensure best service for diners, including adjusting routes to 107 | avoid barred roads for delivery vehicles. -------------------------------------------------------------------------------- /day13/config/log4cpp.conf: -------------------------------------------------------------------------------- 1 | # 定义了3个category fatal, warn, debug 2 | # 其中warn和debug设置了additivity属性为false;fatal的additivity属性默认为true 3 | 4 | rootCategory=DEBUG, rootAppender, A3 5 | 6 | category.fatal=WARN,A1 7 | 8 | category.warn=NOTICE, A2 9 | 10 | # category.debug=DEBUG, 11 | 12 | # 定义rootAppender类型和layout属性,这里在bash打印与log中相同的内容 13 | appender.rootAppender=org.apache.log4cpp.ConsoleAppender 14 | appender.rootAppender.layout=org.apache.log4cpp.PatternLayout 15 | appender.rootAppender.layout.ConversionPattern=[%p]"%m"%d{%Y-%m-%d %H:%M:%S} [%c]%n 16 | 17 | #定义fatal记录、warn记录、debug记录的属性 18 | appender.A1=org.apache.log4cpp.RollingFileAppender 19 | appender.A1.fileName=./log/error.log 20 | appender.A1.maxFileSize=102400 21 | appender.A1.maxBackupIndex=3 22 | appender.A1.backupPattern=%Y-%m-%d 23 | appender.A1.layout=org.apache.log4cpp.PatternLayout 24 | appender.A1.layout.ConversionPattern=[%p]"%m"%d{%Y-%m-%d %H:%M:%S} [%c]%n 25 | 26 | appender.A2=org.apache.log4cpp.RollingFileAppender 27 | appender.A2.fileName=./log/warn.log 28 | appender.A2.maxFileSize=102400 29 | appender.A2.maxBackupIndex=3 30 | appender.A2.backupPattern=%Y-%m-%d 31 | appender.A2.layout=org.apache.log4cpp.PatternLayout 32 | appender.A2.layout.ConversionPattern=[%p]"%m"%d{%Y-%m-%d %H:%M:%S} [%c]%n 33 | 34 | appender.A3=org.apache.log4cpp.RollingFileAppender 35 | appender.A3.fileName=./log/debug.log 36 | appender.A3.maxFileSize=102400 37 | appender.A3.maxBackupIndex=3 38 | appender.A3.backupPattern=%Y-%m-%d 39 | appender.A3.layout=org.apache.log4cpp.PatternLayout 40 | appender.A3.layout.ConversionPattern=[%p]"%m"%d{%Y-%m-%d %H:%M:%S} [%c]%n -------------------------------------------------------------------------------- /day13/day13_homework.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 1. 词频统计的作业再用map/unordered_map容器去实现一次,体验一下使用vector/map/unordered_map时程序执行的速度 4 | 5 | 2. 文本查询作业 6 | 该程序将读取用户指定的任意文本文件【当前目录下的china_daily.txt】, 7 | 然后允许用户从该文件中查找单词。查询的结果是该单词出现的次数,并列 8 | 出每次出现所在的行。如果某单词在同一行中多次出现,程序将只显示该行 9 | 一次。行号按升序显示。 10 | 11 | 要求: 12 | a. 它必须允许用户指明要处理的文件名字。 13 | 14 | b. 程序将存储该文件的内容,以便输出每个单词所在的原始行。 15 | vector lines;//O(1) 完美hash 16 | 或 map lines;//O(logN) 17 | 或 unorderedmap lines;//O(1) 18 | 19 | c. 它必须将每一行分解为各个单词,并记录每个单词所在的所有行。 20 | 在输出行号时,应保证以升序输出,并且不重复。 21 | 22 | map > word2Line; 23 | map dict; 24 | 25 | d. 对特定单词的查询将返回出现该单词的所有行的行号。 26 | 27 | e. 输出某单词所在的行文本时,程序必须能根据给定的行号从输入 28 | 文件中获取相应的行。 29 | 30 | 示例: 31 | 使用提供的文件内容,然后查找单词 "element"。输出的前几行为: 32 | --------------------------------------------- 33 | element occurs 125 times. 34 | (line 62) element with a given key. 35 | (line 64) second element with the same key. 36 | (line 153) element |==| operator. 37 | (line 250) the element type. 38 | (line 398) corresponding element. 39 | --------------------------------------------- 40 | 41 | 程序接口[可选]: 42 | class TextQuery 43 | { 44 | public: 45 | void readFile(const string filename); 46 | void query(const string & word); 47 | private: 48 | //...... 49 | }; 50 | 51 | //程序测试用例 52 | int main(int argc, char *argv[]) 53 | { 54 | string queryWord("hello"); 55 | 56 | TextQuery tq; 57 | tq.readFile("test.dat"); 58 | tq.query(queryWord); 59 | return 0; 60 | } 61 | 62 | 3. Leetcode 146 LRU [题目在当前目录下] 63 | 64 | 4. 实现一个堆排序算法 65 | 66 | 可选择使用模板或非模板的实现: 67 | 68 | 使用模板的框架如下: 69 | template > 70 | class HeapSort 71 | { 72 | public: 73 | void heapAdjust(); 74 | void sort(); 75 | private: 76 | //... 77 | }; -------------------------------------------------------------------------------- /day13/include/log4cpp_head.hh: -------------------------------------------------------------------------------- 1 | #ifndef _LOG4CPP_HEAD_HH 2 | #define _LOG4CPP_HEAD_HH 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace log4cpp; 17 | 18 | #define log4cpp_prt(func, string) func("%s --%s--%s--%d--", x.c_str(), __FILE__, __FUNCTION__, __LINE__) 19 | using std::cout; 20 | using std::endl; 21 | 22 | class Log 23 | { 24 | public: 25 | template static void LogInfo(const char *str, const T&...args) 26 | { 27 | _restore->debug.info(str, args...); 28 | } 29 | static Log* genObj(); 30 | static void destroy(); 31 | 32 | log4cpp::Category& fatal; 33 | log4cpp::Category& warn; 34 | log4cpp::Category& debug; 35 | 36 | private: 37 | static void init(); 38 | Log(); 39 | ~Log(); 40 | log4cpp::Category& _root; 41 | static Log* _restore; 42 | static pthread_once_t _auto; 43 | }; 44 | 45 | Log* Log::_restore = nullptr; 46 | pthread_once_t Log::_auto = PTHREAD_ONCE_INIT; 47 | 48 | Log::Log(): 49 | fatal(log4cpp::Category::getInstance(std::string("fatal"))), 50 | warn(log4cpp::Category::getInstance(std::string("warn"))), 51 | debug(log4cpp::Category::getInstance(std::string("debug"))), 52 | _root(log4cpp::Category::getRoot()) 53 | {} 54 | 55 | Log::~Log(){} 56 | 57 | Log* Log::genObj() 58 | { 59 | if(_restore) 60 | return _restore; 61 | pthread_once(&_auto, init); 62 | return _restore; 63 | } 64 | 65 | void Log::init() 66 | { 67 | try 68 | { 69 | PropertyConfigurator::configure("./config/log4cpp.conf"); 70 | } 71 | catch(ConfigureFailure& f) 72 | { 73 | std::cerr << f.what() << '\n'; 74 | exit(-1); 75 | } 76 | _restore = new Log; 77 | ::atexit(destroy); 78 | } 79 | 80 | void Log::destroy() 81 | { 82 | if(_restore ==nullptr) 83 | return; 84 | Category::shutdown(); 85 | delete _restore; 86 | } 87 | #endif -------------------------------------------------------------------------------- /day13/src/CntWords.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../include/log4cpp_head.hh" 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | string getWord(std::istream &file) 13 | { 14 | string str; 15 | char ch; 16 | do 17 | { 18 | file.get(ch); 19 | }while(isalpha(ch) == 0 && !file.eof()); 20 | while(isalpha(ch) && !file.eof()) 21 | { 22 | str.push_back(ch); 23 | file.get(ch); 24 | } 25 | return str; 26 | } 27 | 28 | int main(int argc, const char * argv[]) 29 | { 30 | map dictionary; 31 | ifstream file(argv[1], ios_base::in); 32 | string word; 33 | timeval begin, end; 34 | gettimeofday(&begin, NULL); 35 | while(!file.eof()) 36 | { 37 | word = getWord(file); 38 | ++dictionary[word]; 39 | } 40 | map::iterator it = dictionary.begin(); 41 | if(it->first.empty()) 42 | ++it; 43 | gettimeofday(&end, NULL); 44 | Log::genObj(); 45 | Log::LogInfo("used time = %ld ---%s\n", (end.tv_sec - begin.tv_sec) * 1000000 + end.tv_usec - begin.tv_usec, __FILE__); 46 | for(; it != dictionary.end(); ++it) 47 | { 48 | printf("%-20s %d\n", it->first.c_str(), it->second); 49 | } 50 | file.close(); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /day13/src/CntWordsUnordered.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../include/log4cpp_head.hh" 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | string getWord(ifstream &file) 13 | { 14 | string str; 15 | char ch; 16 | do 17 | { 18 | file.get(ch); 19 | }while(isalpha(ch) == 0 && !file.eof()); 20 | while(isalpha(ch) && !file.eof()) 21 | { 22 | str.push_back(ch); 23 | file.get(ch); 24 | } 25 | return str; 26 | } 27 | 28 | int main(int argc, const char * argv[]) 29 | { 30 | unordered_map dictionary; 31 | ifstream file(argv[1], ios_base::in); 32 | string word; 33 | timeval begin, end; 34 | gettimeofday(&begin, NULL); 35 | while(!file.eof()) 36 | { 37 | word = getWord(file); 38 | ++dictionary[word]; 39 | } 40 | unordered_map::iterator it = dictionary.begin(); 41 | if(it->first.empty()) 42 | ++it; 43 | gettimeofday(&end, NULL); 44 | Log::genObj(); 45 | Log::LogInfo("used time = %ld ---%s\n", (end.tv_sec - begin.tv_sec) * 1000000 + end.tv_usec - begin.tv_usec, __FILE__); 46 | for(; it != dictionary.end(); ++it) 47 | { 48 | printf("%-20s %d\n", it->first.c_str(), it->second); 49 | } 50 | file.close(); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /day13/src/CntWordsVector.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../include/log4cpp_head.hh" 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | string getWord(ifstream &file) 13 | { 14 | string str; 15 | char ch; 16 | do 17 | { 18 | file.get(ch); 19 | }while(isalpha(ch) == 0 && !file.eof()); 20 | while(isalpha(ch) && !file.eof()) 21 | { 22 | str.push_back(ch); 23 | file.get(ch); 24 | } 25 | return str; 26 | } 27 | 28 | int main(int argc, const char * argv[]) 29 | { 30 | vector> dictionary; 31 | ifstream file(argv[1], ios_base::in); 32 | string word; 33 | timeval begin, end; 34 | gettimeofday(&begin, NULL); 35 | int cnt = 0; 36 | while(!file.eof()) 37 | { 38 | word = getWord(file); 39 | for (int i = 0; i < cnt; ++i) 40 | { 41 | if(dictionary[i].first == word) 42 | ++dictionary[i].second; 43 | } 44 | dictionary.push_back(pair(word, 1)); 45 | ++cnt; 46 | } 47 | vector>::iterator it = dictionary.begin(); 48 | if(it->first.empty()) 49 | ++it; 50 | gettimeofday(&end, NULL); 51 | Log::genObj(); 52 | Log::LogInfo("used time = %ld ---%s\n", (end.tv_sec - begin.tv_sec) * 1000000 + end.tv_usec - begin.tv_usec, __FILE__); 53 | for(; it != dictionary.end(); ++it) 54 | { 55 | printf("%-20s %d\n", it->first.c_str(), it->second); 56 | } 57 | file.close(); 58 | return 0; 59 | } -------------------------------------------------------------------------------- /day13/src/HeapSort.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | template > 8 | class HeapSort 9 | { 10 | public: 11 | void operator()(T* beg, T* end) 12 | { 13 | _it = beg; 14 | _size = end - beg; 15 | heapAdjust(); 16 | sort(); 17 | } 18 | void heapAdjust(); 19 | void sort(); 20 | private: 21 | T* _it; 22 | int _size; 23 | priority_queue, Compare> _heap; 24 | }; 25 | 26 | template 27 | void HeapSort::heapAdjust() 28 | { 29 | for (int i = 0; i < _size; ++i) 30 | { 31 | _heap.push(*(_it + i)); 32 | } 33 | } 34 | 35 | template 36 | void HeapSort::sort() 37 | { 38 | for (int i = 0; i < _size; ++i) 39 | { 40 | *(_it + i) = _heap.top(); 41 | _heap.pop(); 42 | } 43 | } 44 | 45 | int main() 46 | { 47 | std::array a = {4, 3 ,4, 5, 2}; 48 | HeapSort x; 49 | x(a.begin(), a.end()); 50 | for (auto i = a.begin(); i != a.end(); i++) 51 | { 52 | printf("%d\n", *i); 53 | } 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /day13/src/LRU.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::pair; 6 | using std::unordered_map; 7 | using std::list; 8 | 9 | class LRUCache { 10 | public: 11 | LRUCache(int capacity) 12 | :_size(capacity), _cur(0) 13 | {} 14 | int get(int key) { 15 | auto it = _dict.find(key); 16 | if(it == _dict.end()) 17 | return -1; 18 | int value = it->second->second; 19 | _list.erase(it->second); 20 | _list.push_front(std::make_pair(key, value)); 21 | _dict[key] = _list.begin(); 22 | return value; 23 | } 24 | 25 | void put(int key, int value) { 26 | auto it = _dict.find(key); 27 | if(it != _dict.end()) 28 | { 29 | _list.erase(it->second);--_cur; 30 | } 31 | _list.push_front(std::make_pair(key, value));++_cur; 32 | _dict[key] = _list.begin(); 33 | if(_cur > _size) 34 | { 35 | int key = _list.back().first; 36 | _dict.erase(key);--_cur; 37 | _list.pop_back(); 38 | } 39 | } 40 | private: 41 | unordered_map >::iterator >_dict; 42 | list > _list; 43 | int _size; 44 | int _cur; 45 | }; 46 | 47 | int main() 48 | { 49 | LRUCache cache(2); 50 | cache.put(1, 1); 51 | cache.put(2, 2); 52 | std::cout<get(key); 65 | * obj->put(key,value); 66 | */ -------------------------------------------------------------------------------- /day13/src/textQuery.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using std::endl; 12 | using std::cout; 13 | using std::cin; 14 | using std::string; 15 | using std::map; 16 | using std::vector; 17 | using std::pair; 18 | using std::set; 19 | 20 | class TextQuery 21 | { 22 | public: 23 | void readFile(const string &filename); 24 | void query(const string & word); 25 | TextQuery() 26 | : _lines(), _word2Line(), _dict() 27 | {} 28 | ~TextQuery() 29 | { 30 | 31 | } 32 | private: 33 | vector _lines; 34 | map> _word2Line; 35 | map _dict; 36 | 37 | }; 38 | 39 | string getWord(std::istream &file); 40 | 41 | void TextQuery::readFile(const string &filename) 42 | { 43 | std::ifstream file(filename, std::ios_base::in); 44 | string line, word; 45 | char ch; 46 | size_t i = 0; 47 | while(file.get(ch) && !file.eof()) 48 | { 49 | if(ch == '\n') 50 | { 51 | ++i; 52 | std::istringstream strStream(line); 53 | do 54 | { 55 | word = ::getWord(strStream); 56 | ++_dict[word]; 57 | _word2Line[word].insert(i); 58 | }while(word.empty() == false); 59 | _lines.push_back(line); 60 | line.clear(); 61 | } else { 62 | line.push_back(ch); 63 | } 64 | } 65 | file.close(); 66 | 67 | } 68 | 69 | void TextQuery::query(const string & word) 70 | { 71 | printf("%s occurs %d times\n", word.c_str(), _dict[word]); 72 | for(auto it = _word2Line[word].begin(); it != _word2Line[word].end(); ++it) 73 | { 74 | printf("\t(line %d) ", *it); 75 | cout<<_lines[*it - 1]<>route; 87 | tq.readFile(route); 88 | while(1) 89 | { 90 | string queryWord; 91 | 92 | cout<<"word"<>queryWord; 94 | tq.query(queryWord); 95 | } 96 | 97 | return 0; 98 | } 99 | 100 | string getWord(std::istream &file) 101 | { 102 | string str; 103 | char ch; 104 | do 105 | { 106 | file.get(ch); 107 | }while(isalpha(ch) == 0 && !file.eof()); 108 | while(isalpha(ch) && !file.eof()) 109 | { 110 | str.push_back(ch); 111 | file.get(ch); 112 | } 113 | return str; 114 | } -------------------------------------------------------------------------------- /day13/textQuery: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day13/textQuery -------------------------------------------------------------------------------- /day14/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day14/1.png -------------------------------------------------------------------------------- /day14/Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | OBJS := ${patsubst ./src/%.cc, ./bin/%.o, $(SRCS)} 3 | ELFS:= ${patsubst ./src/%.cc, %, $(SRCS)} 4 | CFLAGS:= -std=c++17 -Wall 5 | CXX:= g++ 6 | 7 | all:$(ELFS) 8 | 9 | %:./src/%.cc 10 | $(CXX) $(CFLAGS) $< -o $@ 11 | 12 | .PHONY: clean rebuild 13 | clean: 14 | rm -rf $(ELFS) $(OBJS) 15 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /day14/againWordLadder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day14/againWordLadder -------------------------------------------------------------------------------- /day14/day14_homework.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day14/day14_homework.docx -------------------------------------------------------------------------------- /day14/mooc/Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | OBJS := ${patsubst ./src/%.cc, ./bin/%.o, $(SRCS)} 3 | ELFS:= ${patsubst ./src/%.cc, %, $(SRCS)} 4 | CFLAGS:= -std=c++17 -Wall 5 | CXX:= g++ 6 | 7 | all:$(ELFS) 8 | 9 | %:./src/%.cc 10 | $(CXX) $(CFLAGS) $< -o $@ 11 | 12 | .PHONY: clean rebuild 13 | clean: 14 | rm -rf $(ELFS) $(OBJS) 15 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /day14/mooc/SimpleSumArray: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day14/mooc/SimpleSumArray -------------------------------------------------------------------------------- /day14/mooc/againSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day14/mooc/againSort -------------------------------------------------------------------------------- /day14/mooc/filter: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day14/mooc/filter -------------------------------------------------------------------------------- /day14/mooc/istream_iterator: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day14/mooc/istream_iterator -------------------------------------------------------------------------------- /day14/mooc/notDiffTemplate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day14/mooc/notDiffTemplate -------------------------------------------------------------------------------- /day14/mooc/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day14/mooc/readme.md -------------------------------------------------------------------------------- /day14/mooc/simpleForeach: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day14/mooc/simpleForeach -------------------------------------------------------------------------------- /day14/mooc/sortAgain: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day14/mooc/sortAgain -------------------------------------------------------------------------------- /day14/mooc/src/SimpleSumArray.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | template 5 | T SumArray( 6 | // 在此处补充你的代码 7 | T* beg, T* end) 8 | { 9 | T sum = *beg; 10 | for(auto iter = beg + 1; iter != end; ++iter) 11 | { 12 | sum += *iter; 13 | } 14 | return sum; 15 | } 16 | int main() { 17 | string array[4] = { "Tom","Jack","Mary","John"}; 18 | cout << SumArray(array,array+4) << endl; 19 | int a[4] = { 1, 2, 3, 4}; //提示:1+2+3+4 = 10 20 | cout << SumArray(a,a+4) << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /day14/mooc/src/againSort.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool Greater2(int n1,int n2) 5 | { 6 | return n1 > n2; 7 | } 8 | bool Greater1(int n1,int n2) 9 | { 10 | return n1 < n2; 11 | } 12 | bool Greater3(double d1,double d2) 13 | { 14 | return d1 < d2; 15 | } 16 | 17 | template 18 | void mysort( 19 | // 在此处补充你的代码 20 | T1 *beg, T1 *end, bool(*myGreater)(T2, T2)) 21 | { 22 | for(auto iter1 = beg; iter1 < end; ++iter1) 23 | { 24 | for(auto iter2 = iter1 + 1; iter2 < end; ++iter2) 25 | { 26 | if(myGreater(*iter2, *iter1)) //如果小就交换 27 | { 28 | T1 tmp = *iter1; 29 | *iter1 = *iter2; 30 | *iter2 = tmp; 31 | } 32 | } 33 | } 34 | } 35 | 36 | 37 | 38 | #define NUM 5 39 | int main() 40 | { 41 | int an[NUM] = { 8,123,11,10,4 }; 42 | mysort(an,an+NUM,Greater1); //从小到大排序 43 | for( int i = 0;i < NUM; i ++ ) 44 | cout << an[i] << ","; 45 | mysort(an,an+NUM,Greater2); //从大到小排序 46 | cout << endl; 47 | for( int i = 0;i < NUM; i ++ ) 48 | cout << an[i] << ","; 49 | cout << endl; 50 | double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1}; 51 | mysort(d+1,d+5,Greater3); //将数组从下标1到下标4从小到大排序 52 | for( int i = 0;i < 6; i ++ ) 53 | cout << d[i] << ","; 54 | return 0; 55 | } -------------------------------------------------------------------------------- /day14/mooc/src/filter.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | // 在此处补充你的代码 5 | template 6 | T* Filter(T* beg, T* end, T* obj, bool(* func)(T)) 7 | { 8 | auto iter = beg, addIter = obj; 9 | for(; iter != end; ++iter) 10 | { 11 | if(func(*iter)) 12 | *addIter++ = *iter; 13 | } 14 | return addIter; 15 | } 16 | 17 | bool LargerThan2(int n) 18 | { 19 | return n > 2; 20 | } 21 | bool LongerThan3(string s) 22 | { 23 | return s.length() > 3; 24 | } 25 | 26 | string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"}; 27 | string as2[5]; 28 | int a1[5] = { 1,2,3,4,5}; 29 | int a2[5]; 30 | int main() { 31 | string * p = Filter(as1,as1+5,as2,LongerThan3); 32 | for(int i = 0;i < p - as2; ++i) 33 | cout << as2[i]; 34 | cout << endl; 35 | int * p2 = Filter(a1,a1+5,a2,LargerThan2); 36 | for(int i = 0;i < p2-a2; ++i) 37 | cout << a2[i] << ","; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /day14/mooc/src/istream_iterator.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | template 6 | class CMyistream_iterator 7 | { 8 | // 在此处补充你的代码 9 | public: 10 | CMyistream_iterator(istream& is) 11 | : buffer(), inBit(false) 12 | {} 13 | T& operator*() 14 | { 15 | if(inBit) 16 | return buffer; 17 | cin>>buffer; 18 | inBit = true; 19 | return buffer; 20 | } 21 | CMyistream_iterator& operator++(int x) 22 | { 23 | inBit = false; 24 | return *this; 25 | } 26 | 27 | 28 | private: 29 | T buffer; 30 | bool inBit; 31 | }; 32 | 33 | 34 | 35 | int main() 36 | { 37 | int t; 38 | cin >> t; 39 | while( t -- ) { 40 | CMyistream_iterator inputInt(cin); 41 | int n1,n2,n3; 42 | n1 = * inputInt; //读入 n1 43 | int tmp = * inputInt; 44 | cout << tmp << endl; 45 | inputInt ++; 46 | n2 = * inputInt; //读入 n2 47 | inputInt ++; 48 | n3 = * inputInt; //读入 n3 49 | cout << n1 << " " << n2<< " " << n3 << " "; 50 | CMyistream_iterator inputStr(cin); 51 | string s1,s2; 52 | s1 = * inputStr; 53 | inputStr ++; 54 | s2 = * inputStr; 55 | cout << s1 << " " << s2 << endl; 56 | } 57 | return 0; 58 | } -------------------------------------------------------------------------------- /day14/mooc/src/notDiffTemplate.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | template 6 | class myclass { 7 | // 在此处补充你的代码 8 | private: 9 | T* p; 10 | size_t size; 11 | public: 12 | myclass(const T* buf, const size_t sz) 13 | : p(new T[sz]), size(sz) 14 | { 15 | memcpy(p, buf, size * sizeof(T)); 16 | } 17 | ~myclass( ) { 18 | delete [] p; 19 | } 20 | void Show() 21 | { 22 | for( int i = 0;i < size;i ++ ) { 23 | cout << p[i] << ","; 24 | } 25 | cout << endl; 26 | } 27 | }; 28 | int a[100]; 29 | int main() { 30 | char line[100]; 31 | while( cin >> line ) { 32 | myclass obj(line,strlen(line));; 33 | obj.Show(); 34 | int n; 35 | cin >> n; 36 | for(int i = 0;i < n; ++i) 37 | cin >> a[i]; 38 | myclass obj2(a,n); 39 | obj2.Show(); 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /day14/mooc/src/simpleForeach.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | // 在此处补充你的代码 5 | template 6 | void MyForeach(T* beg, T* end, void(* func)(T&)) 7 | { 8 | auto iter = beg; 9 | for(;iter != end; ++iter) 10 | { 11 | func(*iter); 12 | } 13 | } 14 | 15 | template 16 | void MyForeach(T* beg, T* end, void(* func)(T)) 17 | { 18 | auto iter = beg; 19 | for(;iter != end; ++iter) 20 | { 21 | func(*iter); 22 | } 23 | } 24 | 25 | void Print(string s) 26 | { 27 | cout << s; 28 | } 29 | void Inc(int & n) 30 | { 31 | ++ n; 32 | } 33 | string arr[100]; 34 | int a[100]; 35 | int main() { 36 | int m,n; 37 | while(cin >> m >> n) { 38 | for(int i = 0;i < m; ++i) 39 | cin >> arr[i]; 40 | for(int j = 0; j < n; ++j) 41 | cin >> a[j]; 42 | MyForeach(arr,arr+m,Print); 43 | cout << endl; 44 | MyForeach(a,a+n,Inc); 45 | for(int i = 0;i < n; ++i) 46 | cout << a[i] << ","; 47 | cout << endl; 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /day14/mooc/src/sortAgain.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool Greater2(int n1,int n2) 5 | { 6 | return n1 > n2; 7 | } 8 | bool Greater1(int n1,int n2) 9 | { 10 | return n1 < n2; 11 | } 12 | bool Greater3(double d1,double d2) 13 | { 14 | return d1 < d2; 15 | } 16 | 17 | template 18 | T1* mypartition(T1 *beg, T1 *end, bool(*myGreater)(T2, T2)) 19 | { 20 | T1 pivot = *beg; 21 | while(beg < end - 1) 22 | { 23 | while(beg < end - 1 && myGreater(pivot, *(end - 1))) --end; 24 | *beg = *(end - 1); 25 | while(beg < end - 1 && myGreater(*beg, pivot)) ++beg; 26 | *(end - 1) = *beg; 27 | } 28 | *beg = pivot; 29 | return beg; 30 | } 31 | 32 | template 33 | void mysort( 34 | // 在此处补充你的代码 35 | T1 *beg, T1 *end, bool(*myGreater)(T2, T2)) 36 | { 37 | if(beg >= end - 1) 38 | return ; 39 | T1* parti = mypartition(beg, end, myGreater); 40 | mysort(beg, parti, myGreater); 41 | mysort(parti + 1, end, myGreater); 42 | } 43 | 44 | 45 | 46 | #define NUM 5 47 | int main() 48 | { 49 | int an[NUM] = { 8,123,11,10,4 }; 50 | mysort(an,an+NUM,Greater1); //从小到大排序 51 | for( int i = 0;i < NUM; i ++ ) 52 | cout << an[i] << ","; 53 | mysort(an,an+NUM,Greater2); //从大到小排序 54 | cout << endl; 55 | for( int i = 0;i < NUM; i ++ ) 56 | cout << an[i] << ","; 57 | cout << endl; 58 | double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1}; 59 | mysort(d+1,d+5,Greater3); //将数组从下标1到下标4从小到大排序 60 | for( int i = 0;i < 6; i ++ ) 61 | cout << d[i] << ","; 62 | return 0; 63 | } -------------------------------------------------------------------------------- /day14/mooc/src/whyTrue.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class MyCin 4 | { 5 | // 在此处补充你的代码 6 | public: 7 | MyCin(): 8 | _badbit(false) 9 | {} 10 | operator bool() 11 | { 12 | return !_badbit; 13 | } 14 | template 15 | friend MyCin& operator>>(MyCin& m, T& n) 16 | { 17 | if(m._badbit) 18 | return m; 19 | cin>>n; 20 | if(n == -1) 21 | m._badbit = true; 22 | return m; 23 | } 24 | private: 25 | bool _badbit; 26 | 27 | }; 28 | int main() 29 | { 30 | MyCin m; 31 | int n1,n2; 32 | while( m >> n1 >> n2) 33 | cout << n1 << " " << n2 << endl; 34 | return 0; 35 | } 36 | /* 37 | 12 44 38 | 344 555 39 | -1 40 | 2 3 41 | */ -------------------------------------------------------------------------------- /day14/mooc/whyTrue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day14/mooc/whyTrue -------------------------------------------------------------------------------- /day14/src/againWordLadder.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using std::cout; 12 | using std::vector; 13 | using std::cin; 14 | using std::list; 15 | using std::unordered_map; 16 | using std::queue; 17 | using std::unordered_set; 18 | using std::endl; 19 | using std::string; 20 | 21 | class Solution { 22 | public: 23 | Solution() 24 | : _wordList(), _targetList() 25 | {} 26 | int ladderLength(const string& begWord, const string& endWord, const vector &set) 27 | { 28 | if(std::find(set.begin(), set.end(), endWord) == set.end()) 29 | return 0; 30 | _wordList.clear(); 31 | for(auto &mem: set) 32 | { 33 | string word; 34 | auto len = mem.size(); 35 | for(int i = 0; i < len; ++i) 36 | { 37 | word = mem; 38 | word[i] = '*'; 39 | _wordList[word].push_back(mem); 40 | } 41 | } 42 | #ifndef NDEBUG 43 | cout<<"_wordHashList:"< 2 | #include 3 | #include 4 | #include 5 | 6 | using std::stack; 7 | using std::cin; 8 | using std::cout; 9 | using std::endl; 10 | using std::string; 11 | 12 | int main() 13 | { 14 | stack > stack; 15 | char ch, check; 16 | while(!cin.eof()) 17 | { 18 | cin.get(ch); 19 | if(ch == ']' || ch == '}' || ch == ')') 20 | { 21 | check = stack.top(); 22 | if(stack.empty()||(ch == ']' && check != '[')|| (ch == '}' && check != '{') || (ch == ')' && check != '(')) 23 | { 24 | cout<<"false"< std::bind的实现原理阅读材料 11 | > http://www.cnblogs.com/xusd-null/p/3698969.html 12 | 13 | > https://www.tuicool.com/articles/iMZ7ba 14 | 15 | > function + bind的救赎 16 | > https://blog.csdn.net/myan/article/details/5928531 17 | 18 | 5. 了解std::allocator的用法之后,实现自定义的Vector类 19 | 20 | 接口形式: 21 | template 22 | class Vector 23 | { 24 | public: 25 | Vector(); 26 | ~Vector(); 27 | 28 | void push_back(const T &); 29 | void pop_back(); 30 | 31 | int size(); 32 | int capacity(); 33 | private: 34 | void reallocate();//重新分配内存,动态扩容要用的 35 | private: 36 | static std::allocator _alloc; 37 | 38 | T * _start; //指向数组中的第一个元素 39 | T * _finish; //指向最后一个实际元素之后的那个元素 40 | T * _end_of_storage; //指向数组本身之后的位置 41 | }; 42 | 43 | Vector模型 44 | ______________________________ 45 | |_|_|_|_|_|____________________| 46 | ↑ ↑ ↑ 47 | _start _finish _end_of_storage 48 | 49 | 6. 结合STL源码阅读《STL源码剖析》第二章内容理解std::allocator的实现 -------------------------------------------------------------------------------- /day15/mooc/Cmplt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day15/mooc/Cmplt -------------------------------------------------------------------------------- /day15/mooc/CmpltMyString: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day15/mooc/CmpltMyString -------------------------------------------------------------------------------- /day15/mooc/Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | ELFS:= ${patsubst ./src/%.cc, %, $(SRCS)} 3 | CFLAGS:= -std=c++17 -Wall 4 | CXX:= g++ 5 | 6 | all:$(ELFS) 7 | 8 | %:./src/%.cc 9 | $(CXX) $(CFLAGS) $< -o $@ 10 | 11 | .PHONY: clean rebuild 12 | clean: 13 | rm -rf $(ELFS) $(OBJS) 14 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /day15/mooc/MyString: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day15/mooc/MyString -------------------------------------------------------------------------------- /day15/mooc/PointOut: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day15/mooc/PointOut -------------------------------------------------------------------------------- /day15/mooc/bigInt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day15/mooc/bigInt -------------------------------------------------------------------------------- /day15/mooc/inheritMyString: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day15/mooc/inheritMyString -------------------------------------------------------------------------------- /day15/mooc/overLoadOp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day15/mooc/overLoadOp -------------------------------------------------------------------------------- /day15/mooc/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day15/mooc/readme.md -------------------------------------------------------------------------------- /day15/mooc/src/Cmplt.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class Array2 { 8 | // 在此处补充你的代码 9 | /* 10 | 0,1,2,3, 11 | 12 | 4,5,6,7, 13 | 14 | 8,9,10,11, 15 | 16 | next 17 | 18 | 0,1,2,3, 19 | 20 | 4,5,6,7, 21 | 22 | 8,9,10,11, 23 | */ 24 | class array 25 | { 26 | int * _arr; 27 | public: 28 | array(int column) 29 | : _arr(new int[column]) 30 | {} 31 | array() 32 | : _arr(nullptr) 33 | {} 34 | ~array() 35 | { 36 | if(_arr) 37 | delete []_arr; 38 | _arr = nullptr; 39 | } 40 | void assign1(int x) 41 | { 42 | _arr = new int[x]; 43 | } 44 | 45 | int& operator[](int index) 46 | { 47 | return _arr[index]; 48 | } 49 | }; 50 | public: 51 | Array2() 52 | : _arr2(nullptr) 53 | {} 54 | Array2(int row, int column) 55 | { 56 | _arr2 = new array[row + 1] + 1; 57 | *(long *)(_arr2 - 1) = 1; 58 | for (int i = 0; i < row; ++i) 59 | { 60 | _arr2[i].assign1(column); 61 | } 62 | } 63 | array& operator[](int index) 64 | { 65 | return _arr2[index]; 66 | } 67 | 68 | int & operator()(int x, int y) 69 | { 70 | return _arr2[x][y]; 71 | } 72 | 73 | Array2& operator=(const Array2& args2) 74 | { 75 | if(&args2 == this) 76 | return *this; 77 | if(this->_arr2) 78 | delete []_arr2; 79 | _arr2 = args2._arr2; 80 | ++*(long *)(_arr2 - 1); 81 | return *this; 82 | } 83 | 84 | ~Array2() 85 | { 86 | if(--*(long *)(_arr2 - 1) == 0) 87 | { 88 | delete [](_arr2 - 1); 89 | } 90 | _arr2 = nullptr; 91 | } 92 | 93 | private: 94 | array* _arr2; 95 | }; 96 | 97 | int main() { 98 | Array2 a(3,4); 99 | int i,j; 100 | for( i = 0;i < 3; ++i ) 101 | for( j = 0; j < 4; j ++ ) 102 | { 103 | a[i][j] = i * 4 + j; 104 | } 105 | 106 | for( i = 0;i < 3; ++i ) { 107 | for( j = 0; j < 4; j ++ ) { 108 | cout << a(i,j) << ","; 109 | } 110 | cout << endl; 111 | } 112 | cout << "next" << endl; 113 | Array2 b; b = a; 114 | for( i = 0;i < 3; ++i ) { 115 | for( j = 0; j < 4; j ++ ) { 116 | cout << b[i][j] << ","; 117 | } 118 | cout << endl; 119 | } 120 | return 0; 121 | } -------------------------------------------------------------------------------- /day15/mooc/src/CmpltMyString.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | // int strlen(const char * s) 5 | // { int i = 0; 6 | // for(; s[i]; ++i); 7 | // return i; 8 | // } 9 | // void strcpy(char * d,const char * s) 10 | // { 11 | // int i = 0; 12 | // for( i = 0; s[i]; ++i) 13 | // d[i] = s[i]; 14 | // d[i] = 0; 15 | 16 | // } 17 | int strcmp(const char * s1,const char * s2) 18 | { 19 | for(int i = 0; s1[i] && s2[i] ; ++i) { 20 | if( s1[i] < s2[i] ) 21 | return -1; 22 | else if( s1[i] > s2[i]) 23 | return 1; 24 | } 25 | return 0; 26 | } 27 | // void strcat(char * d,const char * s) 28 | // { 29 | // int len = strlen(d); 30 | // strcpy(d+len,s); 31 | // } 32 | class MyString 33 | { 34 | // 在此处补充你的代码 35 | public: 36 | MyString(const void * s) 37 | { 38 | if(s) { 39 | _size = strlen((const char*) s); 40 | _str = new char[_size + 1]; 41 | strcpy(_str,(const char *)s); 42 | } 43 | else{ 44 | _str = nullptr; 45 | _size = 0; 46 | } 47 | } 48 | 49 | MyString() 50 | : _str(nullptr), _size(0) 51 | {} 52 | 53 | MyString(const MyString& str) 54 | { 55 | _str = new char[str._size + 1]; 56 | strcpy(_str, str._str); 57 | _size = str._size; 58 | } 59 | 60 | ~MyString() 61 | { 62 | if(_str) 63 | delete []_str; 64 | _str = nullptr; 65 | } 66 | 67 | bool operator<(const MyString& args)const 68 | { 69 | return strcmp(_str, args._str)<0; 70 | } 71 | bool operator==(const MyString& args)const 72 | { 73 | return strcmp(_str, args._str) == 0; 74 | } 75 | bool operator>(const MyString& args)const 76 | { 77 | return strcmp(_str, args._str)>0; 78 | } 79 | 80 | friend ostream& operator<<(ostream& os, const MyString& str) 81 | { 82 | if(str._str) 83 | os< *s2 ) 153 | return 1; 154 | } 155 | int main() 156 | { 157 | MyString s1("abcd-"),s2,s3("efgh-"); 158 | MyString s4(s1); 159 | MyString SArray[4] = {"big","me","about","take"}; 160 | cout << "1. " << s1 << s2 << s3<< s4<< endl; 161 | s4 = s3; 162 | s3 = s1 + s3; 163 | cout << "2. " << s1 << endl; 164 | cout << "3. " << s2 << endl; 165 | cout << "4. " << s3 << endl; 166 | cout << "5. " << s4 << endl; 167 | cout << "6. " << s1[2] << endl; 168 | s2 = s1; 169 | s1 = "ijkl-"; 170 | s1[2] = 'A' ; 171 | cout << "7. " << s2 << endl; 172 | cout << "8. " << s1 << endl; 173 | s1 += "mnop"; 174 | cout << "9. " << s1 << endl; 175 | s4 = "qrst-" + s2; 176 | cout << "10. " << s4 << endl; 177 | s1 = s2 + s4 + " uvw " + "xyz"; 178 | cout << "11. " << s1 << endl; 179 | qsort(SArray,4,sizeof(MyString),CompareString); 180 | for( int i = 0;i < 4;i ++ ) 181 | cout << SArray[i] << endl; 182 | //s1的从下标0开始长度为4的子串 183 | cout << s1(0,4) << endl; 184 | //s1的从下标5开始长度为10的子串 185 | cout << s1(5,10) << endl; 186 | return 0; 187 | } -------------------------------------------------------------------------------- /day15/mooc/src/MyString.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | class MyString { 6 | char * p; 7 | public: 8 | MyString(const char * s) { 9 | if( s) { 10 | p = new char[strlen(s) + 1]; 11 | strcpy(p,s); 12 | } 13 | else 14 | p = NULL; 15 | 16 | } 17 | ~MyString() { if(p) delete [] p; } 18 | 19 | void Copy(const MyString& str) 20 | { 21 | if(this->p){ 22 | delete []p; 23 | p = nullptr; 24 | 25 | } 26 | if(str.p == this->p) 27 | { 28 | return; 29 | } 30 | if(str.p) 31 | { 32 | p = new char[strlen(str.p) + 1]; 33 | strcpy(p, str.p); 34 | } else { 35 | p = nullptr; 36 | } 37 | } 38 | MyString(const MyString&str) 39 | { 40 | Copy(str); 41 | } 42 | MyString & operator=(const MyString &str) 43 | { 44 | Copy(str); 45 | return *this; 46 | } 47 | friend ostream& operator<<(ostream& os, const MyString& str) 48 | { 49 | return cout<> w1 >> w2) { 56 | MyString s1(w1),s2 = s1; 57 | MyString s3(NULL); 58 | s3.Copy(w1); 59 | cout << s1 << "," << s2 << "," << s3 << endl; 60 | 61 | s2 = w2; 62 | s3 = s2; 63 | s1 = s3; 64 | cout << s1 << "," << s2 << "," << s3 << endl; 65 | } 66 | } -------------------------------------------------------------------------------- /day15/mooc/src/PointOut.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Point { 4 | private: 5 | int x; 6 | int y; 7 | public: 8 | Point() { }; 9 | // 在此处补充你的代码 10 | friend istream& operator>>(istream& is, Point& p) 11 | { 12 | return is>>p.x>>p.y; 13 | } 14 | friend ostream& operator<<(ostream& os, const Point& p) 15 | { 16 | return os<> p) { 23 | cout << p << endl; 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /day15/mooc/src/bigInt.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | /* 7 | 99999999999999999999999999888888888888888812345678901234567789 12 8 | 6 6 9 | */ 10 | /* 11 | 99999999999999999999999999888888888888888812345678901234567801 12 | 99999999999999999999999999888888888888888812345678901234567801 13 | 99999999999999999999999999888888888888888812345678901234567801 14 | 25 15 | 25 16 | 26 17 | 12 18 | 12 19 | 12 20 | 13 21 | 13 22 | 14 23 | */ 24 | const int MAX = 110; 25 | class CHugeInt { 26 | public: 27 | CHugeInt(int n) 28 | { 29 | bzero(_hugeInt, sizeof(_hugeInt)); 30 | int i = 0; 31 | while(n) 32 | { 33 | _hugeInt[i++] = n % 10 + '0'; 34 | n /= 10; 35 | } 36 | _size = i; 37 | } 38 | CHugeInt(const char *s) 39 | { 40 | bzero(_hugeInt, sizeof(_hugeInt)); 41 | const char *i = s; 42 | _size = 0; 43 | while(*i != '\0') 44 | { 45 | ++i; 46 | ++_size; 47 | } 48 | 49 | for (int j = 0; j < _size; ++j) 50 | { 51 | _hugeInt[j] = *--i; 52 | } 53 | } 54 | CHugeInt(int size, char value) 55 | : _size(size) 56 | { 57 | memset(_hugeInt, value, sizeof(_hugeInt)); 58 | } 59 | friend CHugeInt operator+(const CHugeInt& x, const CHugeInt& y) 60 | { 61 | int size = x._size > y._size? x._size: y._size; 62 | const char* largePtr = x._size > y._size?x._hugeInt: y._hugeInt; 63 | CHugeInt rtn(size, '\0'); 64 | int rised = 0; 65 | int cnt = x._size < y._size? x._size: y._size; 66 | int i; 67 | for (i = 0; i < cnt; ++i) 68 | { 69 | if(x._hugeInt[i] - '0' + y._hugeInt[i] - '0' + rised >= 10) 70 | { 71 | rtn._hugeInt[i] = rised + x._hugeInt[i] + y._hugeInt[i] - '0' - 10; 72 | rised = 1; 73 | } else { 74 | rtn._hugeInt[i] = rised + x._hugeInt[i] + y._hugeInt[i] - '0'; 75 | rised = 0; 76 | } 77 | } 78 | if(rised) 79 | { 80 | while(largePtr[i] == '9') 81 | { 82 | rtn._hugeInt[i++] = '0'; 83 | } 84 | if(largePtr[i] >= '0'){ 85 | rtn._hugeInt[i] = largePtr[i] + rised; 86 | ++i; 87 | rised = 0; 88 | } else { 89 | rtn._hugeInt[i] = '1'; 90 | rised = 0; 91 | } 92 | } 93 | while(largePtr[i] >= '0' && largePtr[i] <= '9') 94 | { 95 | rtn._hugeInt[i] = largePtr[i]; 96 | ++i; 97 | } 98 | return rtn; 99 | } 100 | 101 | CHugeInt& operator+=(int n) 102 | { 103 | CHugeInt tmp = *this + n; 104 | strcpy(_hugeInt, tmp._hugeInt); 105 | _size = tmp._size; 106 | return *this; 107 | } 108 | 109 | CHugeInt& operator++() 110 | { 111 | int i = 0; 112 | while(_hugeInt[i] == '9') 113 | { 114 | _hugeInt[i++] = '0'; 115 | } 116 | if(_hugeInt[i] >= '0'){ 117 | _hugeInt[i] = _hugeInt[i] + 1; 118 | ++i; 119 | } else { 120 | ++_size; 121 | _hugeInt[i] = '1'; 122 | } 123 | return *this; 124 | } 125 | CHugeInt operator++(int x) 126 | { 127 | CHugeInt tmp = *this; 128 | this->operator++(); 129 | return tmp; 130 | } 131 | friend ostream& operator<<(ostream& os, const CHugeInt& x) 132 | { 133 | int i = 0; 134 | while(x._hugeInt[i] != '\0') 135 | ++i; 136 | while(i > 0) 137 | { 138 | printf("%c", x._hugeInt[--i]); 139 | } 140 | return os; 141 | } 142 | 143 | private: 144 | char _hugeInt[2 * MAX]; 145 | int _size; 146 | }; 147 | int main() 148 | { 149 | char s[210]; 150 | int n; 151 | 152 | while (cin >> s >> n) { 153 | CHugeInt a(s); 154 | CHugeInt b(n); 155 | 156 | cout << a + b << endl; 157 | cout << n + a << endl; 158 | cout << a + n << endl; 159 | b += n; 160 | cout << ++ b << endl; 161 | cout << b++ << endl; 162 | cout << b << endl; 163 | } 164 | return 0; 165 | } 166 | -------------------------------------------------------------------------------- /day15/mooc/src/inheritMyString.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | class MyString:public string 7 | { 8 | public: 9 | MyString() 10 | : string() 11 | {} 12 | MyString(const void *s) 13 | : string((const char *)s) 14 | {} 15 | MyString(const string &s) 16 | : string(s) 17 | {} 18 | MyString(const MyString&s) 19 | : string(s) 20 | {} 21 | 22 | MyString operator()(int pos, int len) 23 | { 24 | return (*this).substr(pos, len); 25 | } 26 | }; 27 | 28 | 29 | int main() 30 | { 31 | MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); 32 | MyString SArray[4] = {"big","me","about","take"}; 33 | cout << "1. " << s1 << s2 << s3<< s4<< endl; 34 | s4 = s3; 35 | s3 = s1 + s3; 36 | cout << "2. " << s1 << endl; 37 | cout << "3. " << s2 << endl; 38 | cout << "4. " << s3 << endl; 39 | cout << "5. " << s4 << endl; 40 | cout << "6. " << s1[2] << endl; 41 | s2 = s1; 42 | s1 = "ijkl-"; 43 | s1[2] = 'A' ; 44 | cout << "7. " << s2 << endl; 45 | cout << "8. " << s1 << endl; 46 | s1 += "mnop"; 47 | cout << "9. " << s1 << endl; 48 | s4 = "qrst-" + s2; 49 | cout << "10. " << s4 << endl; 50 | s1 = s2 + s4 + " uvw " + "xyz"; 51 | cout << "11. " << s1 << endl; 52 | sort(SArray,SArray+4); 53 | for( int i = 0;i < 4;i ++ ) 54 | cout << SArray[i] << endl; 55 | //s1的从下标0开始长度为4的子串 56 | cout << s1(0,4) << endl; 57 | //s1的从下标5开始长度为10的子串 58 | cout << s1(5,10) << endl; 59 | return 0; 60 | } -------------------------------------------------------------------------------- /day15/mooc/src/overLoadOp.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class MyInt 4 | { 5 | int nVal; 6 | public: 7 | MyInt( int n) { nVal = n ;} 8 | // 在此处补充你的代码 9 | MyInt& operator-(int arg) 10 | { 11 | nVal -= arg; 12 | return *this; 13 | } 14 | operator int() 15 | { 16 | return nVal; 17 | } 18 | }; 19 | int Inc(int n) { 20 | return n + 1; 21 | } 22 | int main () { 23 | int n; 24 | while(cin >>n) { 25 | MyInt objInt(n); 26 | objInt-2-1-3; 27 | cout << Inc(objInt); 28 | cout <<","; 29 | objInt-2-1; 30 | cout << Inc(objInt) << endl; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /day15/sgi_stl/1config11: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day15/sgi_stl/1config11 -------------------------------------------------------------------------------- /day15/sgi_stl/1config3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day15/sgi_stl/1config3 -------------------------------------------------------------------------------- /day15/sgi_stl/1config5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day15/sgi_stl/1config5 -------------------------------------------------------------------------------- /day15/sgi_stl/Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | ELFS:= ${patsubst ./src/%.cc, %, $(SRCS)} 3 | CFLAGS:= -std=c++17 -Wall 4 | CXX:= g++ 5 | 6 | all:$(ELFS) 7 | 8 | %:./src/%.cc 9 | $(CXX) $(CFLAGS) $< -o $@ 10 | 11 | .PHONY: clean rebuild 12 | clean: 13 | rm -rf $(ELFS) $(OBJS) 14 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /day15/sgi_stl/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day15/sgi_stl/readme.md -------------------------------------------------------------------------------- /day15/sgi_stl/src/1config11.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class alloc{ 8 | 9 | }; 10 | 11 | inline size_t __deque_buf_size(size_t n, size_t sz) 12 | { 13 | return n != 0 ? n : (sz < 512 ? size_t(512 / sz) : size_t(1)); 14 | } 15 | 16 | template 17 | struct __deque_iterator { 18 | typedef __deque_iterator iterator; 19 | typedef __deque_iterator const_iterator; 20 | 21 | static size_t buffer_size(){ 22 | return __deque_buf_size(BufSiz, sizeof(T)); 23 | } 24 | }; 25 | 26 | template 27 | class deque{ 28 | public: 29 | typedef __deque_iterator iterator; 30 | }; 31 | 32 | int main() 33 | { 34 | cout<< deque::iterator::buffer_size()<::iterator::buffer_size()< 2 | 3 | using namespace std; 4 | 5 | template 6 | class testClass{ 7 | public: 8 | static int _data; 9 | }; 10 | 11 | template 12 | int testClass::_data = 1; 13 | 14 | int main() 15 | { 16 | cout<< testClass::_data< obji1, obji2; 18 | testClass objj1; 19 | cout< 2 | 3 | using namespace std; 4 | 5 | template 6 | struct testCls 7 | { 8 | testCls() 9 | { 10 | cout<<"I, O"< 15 | struct testCls 16 | { 17 | testCls() 18 | { 19 | cout<<"T*, T*"< 24 | struct testCls 25 | { 26 | testCls() 27 | { 28 | cout<<"const T*, T*"< obj1; 35 | testCls obj2; 36 | testCls obj3; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /day15/sgi_stl/src/test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | # if defined(__sgi) 9 | cout<<"__sgi"< 2 | #include 3 | #include 4 | // #include 5 | 6 | using std::allocator; 7 | using std::cout; 8 | using std::endl; 9 | using std::cin; 10 | using std::string; 11 | 12 | template > 13 | class vector 14 | { 15 | public: 16 | 17 | typedef Tp value_type; 18 | typedef value_type* pointer; 19 | typedef value_type* iterator; 20 | typedef value_type& reference; 21 | typedef ptrdiff_t difference_type; 22 | typedef const value_type* const_iterator; 23 | typedef const value_type& const_reference; 24 | 25 | const_iterator cbegin() const { return _first;} 26 | const_iterator cend() const { return _last;} 27 | iterator begin() { return _first;} 28 | iterator end() { return _last;} 29 | difference_type capacity() { return _end - _first;} 30 | difference_type size() { return _last - _first;} 31 | reference operator[](size_t index) 32 | { 33 | return *(_first + index); 34 | } 35 | const_reference operator[](size_t index) const 36 | { 37 | return *(_first + index); 38 | } 39 | 40 | vector() 41 | : _first(nullptr), _last(nullptr), _end(nullptr) 42 | {} 43 | 44 | vector(size_t size, value_type val) 45 | { 46 | _last =_first = _alloc.allocate(size); 47 | for(int i = 0; i < size; ++i) 48 | { 49 | _alloc.construct(_last++, val); 50 | } 51 | _end = _last; 52 | } 53 | 54 | vector(size_t size) 55 | { 56 | _last =_first = _alloc.allocate(size); 57 | for(int i = 0; i < size; ++i) 58 | { 59 | _alloc.construct(_last++); 60 | } 61 | _end = _last; 62 | } 63 | 64 | iterator push_back(const value_type& val) 65 | { 66 | auto sz = capacity(); 67 | if(_end == _last){ 68 | reallocate(sz > 0 ? (sz << 1) : 1); 69 | printf("%s\n", __FUNCTION__); 70 | for(auto iter = cbegin(); iter != cend(); ++iter) 71 | { 72 | std::cout<<*iter<<' '; 73 | } 74 | cout<<"capacity = "<print(); 124 | 125 | delete pc4; 126 | 127 | return 0; 128 | } 129 | 130 | void test1() 131 | { 132 | Computer pc("Thinkpad", 8888); 133 | cout << "pc = "; 134 | pc.print(); 135 | 136 | Computer pc2 = pc;//调用复制(拷贝)构造函数 137 | //等价于Computer pc3(pc); 138 | cout << "pc2 = "; 139 | pc2.print(); 140 | 141 | //pc2._brand 142 | 143 | } 144 | 145 | Computer getComputer() 146 | { 147 | Computer pc3("Xiaomi", 7500); 148 | cout << "pc3 = "; 149 | pc3.print(); 150 | return pc3; 151 | } 152 | 153 | void test2() 154 | { 155 | getComputer();//临时对象, 非持久化对象, 没有写回到内存中的 156 | //右值, 无法取地址 157 | int number = 1; 158 | &number; 159 | 160 | const int & ref = 1;//字面值, 右值 161 | //只有const引用能够绑定到右值 162 | cout << "ref = " << ref << endl; 163 | 164 | Computer pc4 = getComputer(); 165 | cout << "pc4 = "; 166 | pc4.print(); 167 | } 168 | 169 | void test3() 170 | { 171 | Computer pc1("Thinkpad", 8888); 172 | Computer pc2("Xiaomi", 7500); 173 | 174 | pc2 = pc1; 175 | //pc2.operator=(pc1); 176 | cout << "执行赋值语句之后:" << endl; 177 | cout << "pc1 = "; 178 | pc1.print(); 179 | cout << "pc2 = "; 180 | pc2.print(); 181 | 182 | int a = 3, b = 4; 183 | a = (a = b);//自复制 184 | 185 | } 186 | 187 | int main(void) 188 | { 189 | //test0(); 190 | //test1(); 191 | //test2(); 192 | test3(); 193 | 194 | return 0; 195 | } 196 | -------------------------------------------------------------------------------- /day2/day2_homework.txt: -------------------------------------------------------------------------------- 1 | 2 | [C++语法规则很多,要落实下来,得通过多敲代码来理解, 3 | 看N遍不如写一次;在写代码的过程中,会碰到其它你不曾碰到过的 4 | 编译问题,切记程序是调试出来的;再就是通过练习,把敲代码的 5 | 速度提升上来,熟悉键盘,培养写代码的感觉] 6 | 7 | 1. C++内存布局分为几个区域,各自具备什么特点? 8 | 9 | 2. 当定义类时,编译器会为类自动生成哪些函数? 10 | 这些函数各自都有什么特点? 11 | 12 | 3. 什么是浅拷贝, 什么是深拷贝? 13 | 14 | 4. 实现一个自定义的String类,保证main函数对正确执行 15 | class String 16 | { 17 | public: 18 | String(); 19 | String(const char *pstr); 20 | String(const String & rhs); 21 | String & operator=(const String & rhs); 22 | ~String(); 23 | 24 | void print(); 25 | 26 | private: 27 | char * _pstr; 28 | }; 29 | 30 | int main(void) 31 | { 32 | String str1; 33 | str1.print(); 34 | 35 | String str2 = "Hello,world"; 36 | String str3("wangdao"); 37 | 38 | str2.print(); 39 | str3.print(); 40 | 41 | String str4 = str3; 42 | str4.print(); 43 | 44 | str4 = str2; 45 | str4.print(); 46 | 47 | return 0; 48 | } 49 | 50 | 5. 当天代码实现一遍, 多测试 -------------------------------------------------------------------------------- /day2/mooc/CntAnimalNum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day2/mooc/CntAnimalNum -------------------------------------------------------------------------------- /day2/mooc/CntAnimalNum.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Animal 5 | { 6 | public: 7 | static int number; 8 | Animal(){++number;} 9 | virtual ~Animal(); 10 | }; 11 | 12 | struct Dog: public Animal 13 | { 14 | public: 15 | static int number; 16 | Dog(){++number;} 17 | ~Dog(){--number;} 18 | }; 19 | 20 | struct Cat: public Animal 21 | { 22 | public: 23 | static int number; 24 | Cat(){++number;} 25 | ~Cat(){--number;} 26 | }; 27 | 28 | Animal::~Animal() 29 | { 30 | --number; 31 | } 32 | 33 | int Animal::number = 0; 34 | int Dog::number = 0; 35 | int Cat::number = 0; 36 | 37 | void print() { 38 | cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl; 39 | } 40 | 41 | int main() { 42 | print(); 43 | Dog d1, d2; 44 | Cat c1; 45 | print(); 46 | Dog* d3 = new Dog(); 47 | Animal* c2 = new Cat; 48 | Cat* c3 = new Cat; 49 | print(); 50 | delete c3; 51 | print(); 52 | delete c2; 53 | delete d3; 54 | print(); 55 | 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /day2/mooc/WOW.cc: -------------------------------------------------------------------------------- 1 | /* 总时间限制: 1000ms 内存限制: 65536kB 2 | 描述 3 | 魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。 4 | 红司令部,City 1,City 2,……,City n,蓝司令部 5 | 6 | 两军的司令部都会制造武士。武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值、攻击力这三种属性。 7 | 8 | 双方的武士编号都是从1开始计算。红方制造出来的第n个武士,编号就是n。同样,蓝方制造出来的第n个武士,编号也是n。 9 | 10 | 武士在刚降生的时候有一个生命值。 11 | 12 | 在每个整点,双方的司令部中各有一个武士降生。 13 | 14 | 红方司令部按照iceman、lion、wolf、ninja、dragon的顺序循环制造武士。 15 | 16 | 蓝方司令部按照lion、dragon、ninja、iceman、wolf的顺序循环制造武士。 17 | 18 | 制造武士需要生命元。 19 | 20 | 制造一个初始生命值为m的武士,司令部中的生命元就要减少m个。 21 | 22 | 如果司令部中的生命元不足以制造某个按顺序应该制造的武士,那么司令部就试图制造下一个。如果所有武士都不能制造了,则司令部停止制造武士。 23 | 24 | 给定一个时间,和双方司令部的初始生命元数目,要求你将从0点0分开始到双方司令部停止制造武士为止的所有事件按顺序输出。 25 | 一共有两种事件,其对应的输出样例如下: 26 | 27 | 1) 武士降生 28 | 输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter 29 | 表示在4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。 30 | 31 | 2) 司令部停止制造武士 32 | 输出样例: 010 red headquarter stops making warriors 33 | 表示在10点整,红方司令部停止制造武士 34 | 35 | 输出事件时: 36 | 37 | 首先按时间顺序输出; 38 | 39 | 同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。 40 | 41 | 输入 42 | 第一行是一个整数,代表测试数据组数。 43 | 44 | 每组测试数据共两行。 45 | 46 | 第一行:一个整数M。其含义为, 每个司令部一开始都有M个生命元( 1 <= M <= 10000)。 47 | 48 | 第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于10000。 49 | 输出 50 | 对每组测试数据,要求输出从0时0分开始,到双方司令部都停止制造武士为止的所有事件。 51 | 对每组测试数据,首先输出"Case:n" n是测试数据的编号,从1开始 。 52 | 接下来按恰当的顺序和格式输出所有事件。每个事件都以事件发生的时间开头,时间以小时为单位,有三位。 53 | 54 | 样例输入 55 | 1 56 | 20 57 | 3 4 5 6 7 58 | 样例输出 59 | Case:1 60 | 000 red iceman 1 born with strength 5,1 iceman in red headquarter 61 | 000 blue lion 1 born with strength 6,1 lion in blue headquarter 62 | 001 red lion 2 born with strength 6,1 lion in red headquarter 63 | 001 blue dragon 2 born with strength 3,1 dragon in blue headquarter 64 | 002 red wolf 3 born with strength 7,1 wolf in red headquarter 65 | 002 blue ninja 3 born with strength 4,1 ninja in blue headquarter 66 | 003 red headquarter stops making warriors 67 | 003 blue iceman 4 born with strength 5,1 iceman in blue headquarter 68 | 004 blue headquarter stops making warriors 69 | */ 70 | #include 71 | #include 72 | class Warriors 73 | { 74 | int strength; 75 | int number; 76 | int attack; 77 | Warriors(int n, int s, int a = 0): strength(s), number(n), attack(a){} 78 | public: 79 | static Warriors* genWarriors(int n, int strength) 80 | { 81 | return new Warriors(n, strength); 82 | } 83 | }; 84 | class headquarter 85 | { 86 | Warriors* dragonQue, ninjaQue, icemanQue, lionQue, wolfQue; 87 | 88 | }; 89 | 90 | -------------------------------------------------------------------------------- /day2/mooc/bigAndBase: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day2/mooc/bigAndBase -------------------------------------------------------------------------------- /day2/mooc/bigAndBase.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class Base { 5 | public: 6 | int k; 7 | Base(int n):k(n) { } 8 | }; 9 | class Big 10 | { 11 | public: 12 | int v; 13 | Base b; 14 | // 在此处补充你的代码 15 | Big(int x): 16 | v(x), b(x){} 17 | }; 18 | int main() 19 | { 20 | int n; 21 | while(cin >>n) { 22 | Big a1(n); 23 | Big a2 = a1; 24 | cout << a1.v << "," << a1.b.k << endl; 25 | cout << a2.v << "," << a2.b.k << endl; 26 | } 27 | } -------------------------------------------------------------------------------- /day2/mooc/harderSwap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day2/mooc/harderSwap -------------------------------------------------------------------------------- /day2/mooc/harderSwap.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap( 5 | // 在此处补充你的代码 6 | int * &a, int * &b 7 | ) 8 | { 9 | int * tmp = a; 10 | a = b; 11 | b = tmp; 12 | } 13 | int main() 14 | { 15 | int a = 3,b = 5; 16 | int * pa = & a; 17 | int * pb = & b; 18 | swap(pa,pb); 19 | cout << *pa << "," << * pb; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /day2/mooc/mythfulIniArr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day2/mooc/mythfulIniArr -------------------------------------------------------------------------------- /day2/mooc/mythfulIniArr.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int * a[] = { 7 | // 在此处补充你的代码 8 | NULL,NULL,new int[3],new int[5] 9 | }; 10 | 11 | *a[2] = 123; 12 | a[3][5] = 456; 13 | if(! a[0] ) { 14 | cout << * a[2] << "," << a[3][5]; 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /day2/mooc/pointerWhere: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day2/mooc/pointerWhere -------------------------------------------------------------------------------- /day2/mooc/pointerWhere.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct A 5 | { 6 | int v; 7 | A(int vv):v(vv) { } 8 | // 在此处补充你的代码 9 | const A* getPointer() const 10 | { 11 | return this; 12 | } 13 | }; 14 | 15 | int main() 16 | { 17 | const A a(10); 18 | const A * p = a.getPointer(); 19 | cout << p->v << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /day2/mooc/simpleSwap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day2/mooc/simpleSwap -------------------------------------------------------------------------------- /day2/mooc/simpleSwap.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A 4 | { 5 | public: 6 | int x; 7 | int getX() { return x; } 8 | }; 9 | void swap( 10 | // 在此处补充你的代码 11 | A &a, A &b 12 | ) 13 | { 14 | int tmp = a.x; 15 | a.x = b.x; 16 | b.x = tmp; 17 | } 18 | int main() 19 | { 20 | A a,b; 21 | a.x = 3; 22 | b.x = 5; 23 | swap(a,b); 24 | cout << a.getX() << "," << b.getX(); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /day2/mooc/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day2/mooc/test -------------------------------------------------------------------------------- /day2/mooc/test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class A{ 6 | int _ix, _iy; 7 | public: 8 | A(int x = 0, int y = 0) 9 | { 10 | this->_ix = x + 1; 11 | this->_iy = y + 2; 12 | } 13 | ~A() 14 | { 15 | cout<<"destructor called\n"; 16 | } 17 | }; 18 | 19 | int main() 20 | { 21 | A a = 2; 22 | a = 4; 23 | cout<<"main end"< 2 | using namespace std; 3 | // 在此处补充你的代码 4 | int & 5 | getElement(int * a, int i) 6 | { 7 | return a[i]; 8 | } 9 | int main() 10 | { 11 | int a[] = {1,2,3}; 12 | getElement(a,1) = 10; 13 | cout << a[1] ; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /day2/mooc/whatRet: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day2/mooc/whatRet -------------------------------------------------------------------------------- /day2/mooc/whatRet.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A { 4 | public: 5 | int val; 6 | 7 | A(int 8 | // 在此处补充你的代码 9 | x):val(x){} 10 | A(){ 11 | val = 123; 12 | } 13 | A& GetObj() 14 | { 15 | return *this; 16 | } 17 | }; 18 | int main() 19 | { 20 | int m,n; 21 | A a; 22 | cout << a.val << endl; 23 | while(cin >> m >> n) { 24 | a.GetObj() = m; 25 | cout << a.val << endl; 26 | a.GetObj() = A(n); 27 | cout << a.val<< endl; 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /day2/myStr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day2/myStr -------------------------------------------------------------------------------- /day2/myStr.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | using std::cout; 7 | using std::cin; 8 | using std::endl; 9 | class String 10 | { 11 | public: 12 | String(); 13 | String(const char *pstr); 14 | String(const String & rhs); 15 | String & operator=(const String & rhs); 16 | ~String(); 17 | void print(); 18 | 19 | private: 20 | char * _pstr; 21 | }; 22 | 23 | String::String() 24 | { 25 | _pstr = NULL; 26 | } 27 | 28 | String::String(const char *pstr) 29 | { 30 | _pstr = new char[strlen(pstr) + 1]; 31 | strcpy(_pstr, pstr); 32 | } 33 | 34 | String::String(const String & rhs) 35 | { 36 | this->_pstr = new char[strlen(rhs._pstr) + 1]; 37 | strcpy(this->_pstr, rhs._pstr); 38 | } 39 | 40 | String::~String() 41 | { 42 | if(_pstr) 43 | delete []_pstr; 44 | _pstr = NULL; 45 | } 46 | 47 | String & String::operator=(const String & rhs) 48 | { 49 | if(&rhs == this) return *this; 50 | if(_pstr) 51 | delete []this->_pstr; 52 | this->_pstr = new char[strlen(rhs._pstr) + 1]; 53 | 54 | return *this; 55 | } 56 | 57 | void String::print() 58 | { 59 | printf("%s", this->_pstr); 60 | } 61 | 62 | int main(void) 63 | { 64 | String str1; 65 | str1.print(); 66 | 67 | String str2 = "Hello,world"; 68 | String str3("wangdao"); 69 | 70 | str2.print(); 71 | str3.print(); 72 | 73 | String str4 = str3; 74 | str4.print(); 75 | 76 | str4 = str2; 77 | str4.print(); 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /day2/readme.md: -------------------------------------------------------------------------------- 1 | # C++: day2 2 | 3 | ## 1. C++内存布局分为几个区域,各自具备什么特点 4 | 5 | 1. 内存从下到上共有`4 GB`虚拟内存空间,从`0x0`到大约`0x08048`为系统环境空间,从`0x80000`大约`3 GB`到`0xfffff`为内核代码空间。 6 | 2. 自`3 GB`往低地址增长的为栈空间,栈空间下面往上增长的为内存映射区,包括`mmap`、写时复制、动态库等映射技术的空间。 7 | 3. 自内存映射区下的位置往上增长的为堆空间,堆空间是进程运行时的动态空间。 8 | 4. 从系统环境空间往上的为代码区(`.text`),代码区而上为只读字符区(`.data`),只读字符区而上为`.bss`区,包括初始化和未初始化的静态变量、全局变量。 9 | 5. 全局变量而上为堆空间底部。 10 | 11 | ![picture](./1.png) 12 | 13 | ## 2. 当定义类时,编译器会为类自动生成哪些函数?这些函数各自都有什么特点 14 | 15 | 构造函数(`constructor`)和析构函数(`destructor`),构造函数和析构函数都没有返回类型且与类重名,而析构函数在前面添加`~`。 16 | 17 | 1. 构造函数可以重载,析构函数不可重载。 18 | 2. 构造函数在初始化、赋值、传参、类型转换时会调用。析构函数在函数消亡的时候自动地调用。 19 | 3. 构造函数和析构函数都可以灵活地定义为私有、公有成员。 20 | 21 | ## 3. 什么是浅拷贝, 什么是深拷贝 22 | 23 | 1. 拷贝是定义的由拷贝构造函数(`_class_A_(const _class_A_& x)`) 24 | 2. 浅拷贝是类中的指针对象的值直接赋值为传入实体的地址,在调用析构函数时会`delete`两次,造成程序崩溃 25 | 3. 深拷贝不是简单的指针定位,而是开辟相同大小的空间,使得指针对象指向这一空间,并且复制传入的实体的值 26 | 27 | ## 4. 实现一个自定义的String类,保证main函数对正确执行 28 | 29 | ```cpp 30 | class String 31 | { 32 | public: 33 | String(); 34 | String(const char *pstr); 35 | String(const String & rhs); 36 | String & operator=(const String & rhs); 37 | ~String(); 38 | 39 | void print(); 40 | 41 | private: 42 | char * _pstr; 43 | }; 44 | 45 | int main(void) 46 | { 47 | String str1; 48 | str1.print(); 49 | 50 | String str2 = "Hello,world"; 51 | String str3("wangdao"); 52 | 53 | str2.print(); 54 | str3.print(); 55 | 56 | String str4 = str3; 57 | str4.print(); 58 | 59 | str4 = str2; 60 | str4.print(); 61 | 62 | return 0; 63 | } 64 | ``` 65 | 66 | 填充代码如下: 67 | 68 | ```cpp 69 | String::String() 70 | { 71 | _pstr = NULL; 72 | } 73 | 74 | String::String(const char *pstr) 75 | { 76 | _pstr = new char[strlen(pstr) + 1]; 77 | strcpy(_pstr, pstr); 78 | } 79 | 80 | String::String(const String & rhs) 81 | { 82 | this->_pstr = new char[strlen(rhs._pstr) + 1]; 83 | strcpy(this->_pstr, rhs._pstr); 84 | } 85 | 86 | String::~String() 87 | { 88 | if(_pstr) 89 | delete []_pstr; 90 | _pstr = NULL; 91 | } 92 | 93 | String & String::operator=(const String & rhs) 94 | { 95 | if(&rhs == this) return *this; 96 | if(_pstr) 97 | delete []this->_pstr; 98 | this->_pstr = new char[strlen(rhs._pstr) + 1]; 99 | 100 | return *this; 101 | } 102 | 103 | void String::print() 104 | { 105 | printf("%s", this->_pstr); 106 | } 107 | ``` 108 | 109 | 编译后,运行结果如下: 110 | ![picture](./2.png) 111 | -------------------------------------------------------------------------------- /day3/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day3/1.png -------------------------------------------------------------------------------- /day3/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day3/2.png -------------------------------------------------------------------------------- /day3/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day3/3.png -------------------------------------------------------------------------------- /day3/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day3/4.png -------------------------------------------------------------------------------- /day3/day3_homework.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day3/day3_homework.txt -------------------------------------------------------------------------------- /day3/mutex/Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | OBJS := ${patsubst ./src/%.cc, ./bin/%.o, $(SRCS)} 3 | ELFS:= mutex 4 | CFLAGS:= -Wall 5 | CXX:= g++ 6 | $(ELFS):$(OBJS) 7 | $(CXX) $(CFLAGS) $^ -o $@ 8 | ./bin/%.o:./src/%.cc 9 | $(CXX) $(CFLAGS) -c $< -o $@ 10 | 11 | .PHONY: clean 12 | clean: 13 | rm -rf $(ELFS) $(OBJS) 14 | .PHONY: rebuild 15 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /day3/mutex/include/mutex.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | class MutexLock 6 | { 7 | public: 8 | //... 9 | void lock(); 10 | void unlock(); 11 | pthread_mutex_t* getLock(); 12 | MutexLock(); 13 | ~MutexLock(); 14 | private: 15 | //... 16 | pthread_mutex_t _lock; 17 | }; 18 | 19 | class Condition 20 | { 21 | public: 22 | //... 23 | void wait(); 24 | void notify(); 25 | void notifyall(); 26 | Condition(MutexLock&); 27 | ~Condition(); 28 | private: 29 | //... 30 | MutexLock &_condLock; 31 | pthread_cond_t _cond; 32 | }; -------------------------------------------------------------------------------- /day3/mutex/mutex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day3/mutex/mutex -------------------------------------------------------------------------------- /day3/mutex/src/methodCond.cc: -------------------------------------------------------------------------------- 1 | #include "../include/mutex.h" 2 | 3 | Condition::Condition(MutexLock &x):_condLock(x) 4 | { 5 | 6 | pthread_cond_init(&_cond, NULL); 7 | } 8 | 9 | Condition::~Condition() 10 | { 11 | pthread_cond_destroy(&_cond); 12 | } 13 | 14 | void Condition::notify() 15 | { 16 | pthread_cond_signal(&_cond); 17 | } 18 | 19 | void Condition::notifyall() 20 | { 21 | pthread_cond_broadcast(&_cond); 22 | } 23 | 24 | void Condition::wait() 25 | { 26 | pthread_cond_wait(&_cond, _condLock.getLock()); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /day3/mutex/src/methodMutex.cc: -------------------------------------------------------------------------------- 1 | #include "../include/mutex.h" 2 | 3 | MutexLock::MutexLock() 4 | { 5 | pthread_mutex_init(&_lock, NULL); 6 | } 7 | 8 | MutexLock::~MutexLock() 9 | { 10 | pthread_mutex_destroy(&_lock); 11 | } 12 | 13 | void MutexLock::lock() 14 | { 15 | pthread_mutex_lock(&_lock); 16 | } 17 | 18 | void MutexLock::unlock() 19 | { 20 | pthread_mutex_unlock(&_lock); 21 | } 22 | 23 | pthread_mutex_t* MutexLock::getLock() 24 | { 25 | return &_lock; 26 | } -------------------------------------------------------------------------------- /day3/mutex/src/mutex.cc: -------------------------------------------------------------------------------- 1 | #include "../include/mutex.h" 2 | #define N 40000000 3 | #include 4 | MutexLock mutex; 5 | Condition cond(mutex); 6 | 7 | void *threadFunc(void *p) 8 | { 9 | int &sum = *(int *)p; 10 | int x = 0; 11 | while(N > sum) 12 | { 13 | mutex.lock(); 14 | ++sum; 15 | mutex.unlock(); 16 | ++x; 17 | } 18 | printf("subThread got %d calculated into sum.\n", x); 19 | pthread_exit(NULL); 20 | } 21 | 22 | void clean(void *p) 23 | { 24 | MutexLock &mutex = *(MutexLock *)p; 25 | mutex.unlock(); 26 | } 27 | 28 | void *threadEat(void *p) 29 | { 30 | int &apple = *(int *)p; 31 | while(1) 32 | { 33 | sleep(1); 34 | mutex.lock(); 35 | pthread_cleanup_push(clean, &mutex); 36 | if(0 == apple) 37 | cond.wait(); 38 | --apple; 39 | printf("Ate an apple. Now %d apple left.\n", apple); 40 | pthread_cleanup_pop(0); 41 | mutex.unlock(); 42 | } 43 | } 44 | 45 | int main() 46 | { 47 | int sum = 0; 48 | pthread_t thid, thid2; 49 | pthread_create(&thid, NULL, threadFunc, (void *)&sum); 50 | int x = 0; 51 | while(N > sum) 52 | { 53 | mutex.lock(); 54 | ++sum; 55 | mutex.unlock(); 56 | ++x; 57 | } 58 | printf("mainThread got %d calculated into sum.\n", x); 59 | pthread_join(thid, nullptr); 60 | 61 | int apple = 10; 62 | pthread_create(&thid, NULL, threadEat, (void *)&apple); 63 | pthread_create(&thid2, NULL, threadEat, (void *)&apple); 64 | 65 | int cnt = 0; 66 | while(1) 67 | { 68 | sleep(3); 69 | if(0 >= apple) 70 | { 71 | apple += 10; 72 | cond.notify(); 73 | } 74 | ++cnt; 75 | if(cnt >=2) 76 | { 77 | cond.notifyall(); 78 | } 79 | } 80 | 81 | return 0; 82 | } -------------------------------------------------------------------------------- /day3/queue/Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | OBJS := ${patsubst ./src/%.cc, ./bin/%.o, $(SRCS)} 3 | ELFS:= queue 4 | CFLAGS:= -Wall 5 | CXX:= g++-9 6 | $(ELFS):$(OBJS) 7 | $(CXX) $(CFLAGS) $^ -o $@ 8 | ./bin/%.o:./src/%.cc 9 | $(CXX) $(CFLAGS) -c $< -o $@ 10 | 11 | .PHONY: clean 12 | clean: 13 | rm -rf $(ELFS) $(OBJS) 14 | .PHONY: rebuild 15 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /day3/queue/include/queueHead.h: -------------------------------------------------------------------------------- 1 | #ifndef _QUEUE_HEAD_H 2 | #define _QUEUE_HEAD_H 3 | #include 4 | struct linkNode 5 | { 6 | int value; 7 | linkNode* next; 8 | linkNode(int x = 0):value(x), next(nullptr){} 9 | }; 10 | 11 | class queue 12 | { 13 | linkNode* _head, *_tail; 14 | 15 | public: 16 | queue(){_tail = _head = new linkNode;} 17 | queue(const queue &x):_head(x._head), _tail(x._tail){} 18 | 19 | /* 元素入队 */ 20 | void push(int); 21 | /* 元素出队 */ 22 | void pop(); 23 | /* 读取队头元素 */ 24 | int front(); 25 | /* 读取队尾元素 */ 26 | int back(); 27 | /* 判断队列是否为空 */ 28 | bool empty(); 29 | /* 判断队列是否已满 */ 30 | bool full(); 31 | 32 | ~queue(); 33 | }; 34 | 35 | #endif -------------------------------------------------------------------------------- /day3/queue/queue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day3/queue/queue -------------------------------------------------------------------------------- /day3/queue/src/main.cc: -------------------------------------------------------------------------------- 1 | // #include 2 | #include "../include/queueHead.h" 3 | #include 4 | int main() 5 | { 6 | queue sq2; 7 | int a; 8 | a = 10;sq2.push(a); 9 | a = 12;sq2.push(a); 10 | a = 14;sq2.push(a); 11 | int front = sq2.front(); 12 | std::cout<<"front = "<next = tmp; 7 | _tail = _tail->next; 8 | printf("value %d is pushed. Now you have %d members\n", _tail->value, ++_head->value); 9 | } 10 | 11 | void queue::pop() 12 | { 13 | linkNode* tmp = _head -> next; 14 | if(empty()) return; 15 | 16 | printf("value %d is popped. Now you have %d members\n", tmp->value, --_head->value); 17 | _head -> next = _head -> next -> next; 18 | delete tmp; 19 | } 20 | 21 | int queue::front() 22 | { 23 | int ret = -1; 24 | if(empty()) 25 | { 26 | printf("get value failed\n"); 27 | return ret; 28 | } 29 | ret = _head -> next -> value; 30 | return ret; 31 | } 32 | 33 | int queue::back() 34 | { 35 | int ret = -1; 36 | if(empty()) 37 | { 38 | printf("get value failed\n"); 39 | return ret; 40 | } 41 | ret = _tail -> value; 42 | return ret; 43 | } 44 | 45 | bool queue::empty() 46 | { 47 | return _head->value == 0 || _head == _tail; 48 | } 49 | 50 | bool queue::full() 51 | { 52 | return false; 53 | } 54 | 55 | queue::~queue() 56 | { 57 | printf("%s function called\n", __FUNCTION__); 58 | while(1) 59 | { 60 | if(empty()) 61 | { 62 | delete _head; 63 | break; 64 | } 65 | pop(); 66 | } 67 | } -------------------------------------------------------------------------------- /day3/singleTon: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day3/singleTon -------------------------------------------------------------------------------- /day3/singleTon.cc: -------------------------------------------------------------------------------- 1 | #include 2 | /* singleton */ 3 | class soloA 4 | { 5 | int _apple; 6 | static soloA* _savePtr; 7 | soloA(){std::cout<<"Constructor Called\n";} 8 | soloA(const soloA&x){} 9 | ~soloA(){std::cout<<"Destructor Called\n";} 10 | public: 11 | static soloA* getObj() 12 | { 13 | if (_savePtr) 14 | { 15 | return _savePtr; 16 | } else { 17 | _savePtr = new soloA(); 18 | return _savePtr; 19 | } 20 | } 21 | static void destroy() 22 | { 23 | if(_savePtr) 24 | { 25 | delete _savePtr; 26 | _savePtr = nullptr; 27 | } 28 | } 29 | void print() 30 | { 31 | std::cout<<"Apple is "<<_apple++<print(); 47 | p3->print(); 48 | p2->print(); 49 | p1->print(); 50 | std::cout<<"p1 = "<< p1<value); 37 | ++_top->value; 38 | print(); 39 | } 40 | int pop() 41 | { 42 | if(0 >= _top->value || nullptr == _top->next) 43 | { 44 | printf("pop failure\n"); 45 | return -1; 46 | } 47 | --_top->value; 48 | auto tmp = _top->next; 49 | int x = -1; 50 | if(_top->next) 51 | { 52 | x = tmp->value; 53 | _top->next = _top->next->next; 54 | delete tmp; 55 | printf("value %d popped\n", x); 56 | } 57 | print(); 58 | return x; 59 | } 60 | int top() 61 | { 62 | if(0 < _top->value && _top->next) 63 | { 64 | return _top->next->value; 65 | } else { 66 | printf("top failure\n"); 67 | return -1; 68 | } 69 | } 70 | void print() 71 | { 72 | linkNode *ptr = _top; 73 | printf("%d: ", ptr->value); 74 | ptr = ptr->next; 75 | while(ptr) 76 | { 77 | printf("%d ", ptr->value); 78 | if(ptr -> next) 79 | printf("-> "); 80 | ptr = ptr->next; 81 | } 82 | printf("\n"); 83 | } 84 | ~stack() 85 | { 86 | printf("%s function called\n", __FUNCTION__); 87 | while(_top->next) 88 | pop(); 89 | delete _top; 90 | } 91 | }; 92 | 93 | int main() 94 | { 95 | stack ss1; 96 | int a; 97 | a = 10;ss1.push(a); 98 | a = 12;ss1.push(a); 99 | a = 14;ss1.push(a); 100 | int top = ss1.top(); 101 | std::cout<<"top = "< dictionary; 23 | ifstream file(argv[1], std::ios_base::in); 24 | string word; 25 | while(!file.eof()) 26 | { 27 | word = getWord(file); 28 | ++dictionary[word]; 29 | } 30 | map::iterator it = dictionary.begin(); 31 | if(it->first.empty()) 32 | ++it; 33 | for(; it != dictionary.end(); ++it) 34 | { 35 | printf("%-20s %d\n", it->first.c_str(), it->second); 36 | } 37 | file.close(); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /day4/include/vector.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template class vector 4 | { 5 | T* _beg; 6 | T* _fin; 7 | T* _end_of_storage; 8 | public: 9 | vector(); 10 | vector(const size_t n, const T &value) 11 | : _beg(new T[n]) 12 | { 13 | printf("===%s===\n", __FUNCTION__); 14 | _end_of_storage = _fin = _beg + n; 15 | for(T *iterator = _beg;iterator != _fin; ++iterator) 16 | *iterator = value; 17 | } 18 | void push_back(const T& value) 19 | { 20 | if(_fin == _end_of_storage) 21 | { 22 | size_t size = _end_of_storage - _beg; 23 | T *newPtr = new T[size * 2]; 24 | memcpy(newPtr, _beg, size * sizeof(T)); 25 | delete []_beg; 26 | _beg = newPtr; 27 | _fin = newPtr + size; 28 | _end_of_storage = newPtr + size * 2; 29 | } 30 | *_fin++ = value; 31 | } 32 | T pop() 33 | { 34 | if(_beg == _fin) 35 | return 0; 36 | return *(--_fin); 37 | } 38 | size_t capacity() 39 | { 40 | return _end_of_storage - _beg; 41 | } 42 | size_t size() 43 | { 44 | return _fin - _beg; 45 | } 46 | ~vector() 47 | { 48 | printf("===%s===\n", __FUNCTION__); 49 | delete []_beg; 50 | _beg = _fin = _end_of_storage = nullptr; 51 | } 52 | friend std::ostream& operator <<(std::ostream& out, const vector &v) 53 | { 54 | for(T* iterator = v._beg;iterator != v._fin; ++iterator) 55 | { 56 | out<<*iterator<<" "; 57 | } 58 | return out; 59 | } 60 | }; 61 | 62 | template vector::vector() {_beg = _fin = _end_of_storage = nullptr;} -------------------------------------------------------------------------------- /day4/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day4/main -------------------------------------------------------------------------------- /day4/src/1.stackOnly.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class on_stack 4 | { 5 | void * operator new(size_t); 6 | void operator delete(void *); 7 | int _value; 8 | public: 9 | on_stack(): _value(-1){} 10 | on_stack(int x): _value(x){} 11 | ~on_stack(){} 12 | friend std::ostream & operator<<(std::ostream &out, const on_stack& x) 13 | { 14 | out< 2 | class on_stack 3 | { 4 | void * operator new(size_t); 5 | void operator delete(void *); 6 | int _value; 7 | public: 8 | on_stack(): _value(-1){} 9 | on_stack(int x): _value(x){} 10 | ~on_stack(){} 11 | friend std::ostream & operator<<(std::ostream &out, const on_stack& x) 12 | { 13 | out< v(5, 1); 6 | std::cout<<"value: "< 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace log4cpp; 14 | 15 | #endif -------------------------------------------------------------------------------- /day5/ConfigTest/log4cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day5/ConfigTest/log4cpp -------------------------------------------------------------------------------- /day5/ConfigTest/src/1.1.confTest.cc: -------------------------------------------------------------------------------- 1 | #include "../include/log4cpp_head.hh" 2 | #include 3 | #include 4 | 5 | using std::string; 6 | using std::cout; 7 | using std::queue; 8 | using std::endl; 9 | 10 | int main() 11 | { 12 | try 13 | { 14 | PropertyConfigurator::configure("./config/test.conf"); 15 | } 16 | catch(ConfigureFailure& f) 17 | { 18 | std::cerr << f.what() << '\n'; 19 | return -1; 20 | } 21 | 22 | log4cpp::Category& root = log4cpp::Category::getRoot(); 23 | log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1")); 24 | log4cpp::Category& sub2 = log4cpp::Category::getInstance(std::string("sub2")); 25 | log4cpp::Category& sub3 = log4cpp::Category::getInstance(std::string("sub3")); 26 | // log4cpp::Category& sub4 = log4cpp::Category::getInstance(std::string("sub4")); 27 | 28 | // 正常使用这些category对象进行日志处理。 29 | root.fatal("root's log"); 30 | 31 | // sub1 has appender A1 and rootappender. since the additivity property is set true by default 32 | sub1.info("sub1's log"); 33 | 34 | // sub2 has appender A2 appender. since the additivity property is set to false 35 | sub2.alert("sub2's log"); 36 | 37 | // sub3 only has A3 appender. since the additivity property is set to false 38 | sub3.debug("sub3's log"); 39 | sub3.alert("sub3's log"); 40 | 41 | // // sub4 can not be found in the config file, so the root category's appender and layout are used 42 | // sub4.warn("sub4's log"); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /day5/befConfig/1.test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day5/befConfig/1.test -------------------------------------------------------------------------------- /day5/befConfig/1.test.cc: -------------------------------------------------------------------------------- 1 | #include "./include/log4cpp_head.hh" 2 | #include 3 | #include 4 | 5 | /* 6 | typedef enum { 7 | EMERG = 0, 8 | FATAL = 0, 9 | ALERT = 100, 10 | CRIT = 200, 11 | ERROR = 300, 12 | WARN = 400, 13 | NOTICE = 500, 14 | INFO = 600, 15 | DEBUG = 700, 16 | NOTSET = 800 17 | } PriorityLevel; 18 | */ 19 | 20 | 21 | using std::cout; 22 | using std::endl; 23 | using std::cin; 24 | using std::queue; 25 | using std::string; 26 | 27 | /* 封装设计输出格式 */ 28 | #define setLayout(append) { PatternLayout* pLayout = new PatternLayout(); \ 29 | pLayout->setConversionPattern("\"%m\" %c [%p] %x %d{%d %m %Y %H:%M:%S}%n"); \ 30 | append->setLayout(pLayout);} 31 | 32 | int main() 33 | { 34 | /* Appender绑定输出途径,常用的有IO流,string队列,log文件 */ 35 | StringQueueAppender* strApp = new StringQueueAppender("strApp"); 36 | OstreamAppender* osAppend = new OstreamAppender("osAppend", &cout); 37 | RollingFileAppender* rfApp = new RollingFileAppender("rfApp", "verbose.log", 1024, 5); 38 | 39 | /* 设置输出格式 */ 40 | setLayout(strApp); 41 | setLayout(osAppend); 42 | setLayout(rfApp); 43 | 44 | /* Category设置Category记录的从属,流绑定以及信息分类 */ 45 | Category& rt = Category::getRoot(); 46 | Category& infoCat1 = rt.getInstance("infoCat1"); 47 | 48 | infoCat1.addAppender(osAppend); 49 | infoCat1.addAppender(rfApp); 50 | infoCat1.setPriority(Priority::DEBUG); 51 | 52 | rt.addAppender(strApp); 53 | rt.setPriority(Priority::EMERG); 54 | 55 | infoCat1.info("Minimum error! ===%s==%d===", __FUNCTION__, __LINE__); 56 | infoCat1.debug("No gateway"); 57 | rt.warn("SYSTEM FATAL ERROR!"); 58 | rt.emerg("No way!"); 59 | // infoCat1.removeAppender(osAppend); 60 | 61 | /* 获取信息队列,防止在多线程中多次中断开启I/O系统调用 */ 62 | cout<<"Get msg from Memory Queue!"<& Str = strApp->getQueue(); 65 | while(!Str.empty()) 66 | { 67 | cout< 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace log4cpp; 14 | 15 | #endif -------------------------------------------------------------------------------- /day5/day5_homework.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 1. 熟悉log4cpp的用法,动手敲代码 4 | 5 | http://blog.csdn.net/liuhong135541/article/category/1496383 6 | 7 | 2. 封装log4cpp,使用起来比较方便,如下所示。 8 | 9 |  输出的日志信息能同时输出到终端和文件 10 | 11 | int main(void) 12 | { 13 | cout << "hello,world"<warn("hello"); 40 | 41 | -------------------------------------------------------------------------------- /day5/log4cppPKG/Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | OBJS := ${patsubst ./src/%.cc, ./bin/%.o, $(SRCS)} 3 | ELFS:= log4cpp 4 | CFLAGS:= -Wall 5 | CXX:= g++ 6 | $(ELFS):$(OBJS) 7 | $(CXX) $(CFLAGS) $^ -o $@ -llog4cpp -lpthread 8 | ./bin/%.o:./src/%.cc 9 | $(CXX) $(CFLAGS) -c $< -o $@ 10 | 11 | .PHONY: clean 12 | clean: 13 | rm -rf $(ELFS) $(OBJS) 14 | .PHONY: rebuild 15 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /day5/log4cppPKG/config/log4cpp.conf: -------------------------------------------------------------------------------- 1 | # 定义了3个category fatal, warn, debug 2 | # 其中warn和debug设置了additivity属性为false;fatal的additivity属性默认为true 3 | 4 | rootCategory=DEBUG, rootAppender, A3 5 | 6 | category.fatal=WARN,A1 7 | 8 | category.warn=NOTICE, A2 9 | 10 | # category.debug=DEBUG, 11 | 12 | # 定义rootAppender类型和layout属性,这里在bash打印与log中相同的内容 13 | appender.rootAppender=org.apache.log4cpp.ConsoleAppender 14 | appender.rootAppender.layout=org.apache.log4cpp.PatternLayout 15 | appender.rootAppender.layout.ConversionPattern=[%p]"%m"%d{%Y-%m-%d %H:%M:%S} [%c]%n 16 | 17 | #定义fatal记录、warn记录、debug记录的属性 18 | appender.A1=org.apache.log4cpp.RollingFileAppender 19 | appender.A1.fileName=./log/error.log 20 | appender.A1.maxFileSize=102400 21 | appender.A1.maxBackupIndex=3 22 | appender.A1.backupPattern=%Y-%m-%d 23 | appender.A1.layout=org.apache.log4cpp.PatternLayout 24 | appender.A1.layout.ConversionPattern=[%p]"%m"%d{%Y-%m-%d %H:%M:%S} [%c]%n 25 | 26 | appender.A2=org.apache.log4cpp.RollingFileAppender 27 | appender.A2.fileName=./log/warn.log 28 | appender.A2.maxFileSize=102400 29 | appender.A2.maxBackupIndex=3 30 | appender.A2.backupPattern=%Y-%m-%d 31 | appender.A2.layout=org.apache.log4cpp.PatternLayout 32 | appender.A2.layout.ConversionPattern=[%p]"%m"%d{%Y-%m-%d %H:%M:%S} [%c]%n 33 | 34 | appender.A3=org.apache.log4cpp.RollingFileAppender 35 | appender.A3.fileName=./log/debug.log 36 | appender.A3.maxFileSize=102400 37 | appender.A3.maxBackupIndex=3 38 | appender.A3.backupPattern=%Y-%m-%d 39 | appender.A3.layout=org.apache.log4cpp.PatternLayout 40 | appender.A3.layout.ConversionPattern=[%p]"%m"%d{%Y-%m-%d %H:%M:%S} [%c]%n -------------------------------------------------------------------------------- /day5/log4cppPKG/include/log4cpp_head.hh: -------------------------------------------------------------------------------- 1 | #ifndef _LOG4CPP_HEAD_HH 2 | #define _LOG4CPP_HEAD_HH 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace log4cpp; 14 | 15 | #endif -------------------------------------------------------------------------------- /day5/log4cppPKG/log4cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day5/log4cppPKG/log4cpp -------------------------------------------------------------------------------- /day5/log4cppPKG/src/main.cc: -------------------------------------------------------------------------------- 1 | #include "../include/log4cpp_head.hh" 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | class Log 7 | { 8 | public: 9 | 10 | static Log* genObj(); 11 | static void destroy(); 12 | log4cpp::Category& _root; 13 | log4cpp::Category& fatal; 14 | log4cpp::Category& warn; 15 | log4cpp::Category& debug; 16 | friend inline void prt(void(*f)(const char* Format, ...), const char * str); 17 | 18 | private: 19 | Log(); 20 | ~Log(); 21 | 22 | static Log* _restore; 23 | }; 24 | 25 | Log* Log::_restore = nullptr; 26 | 27 | Log::Log(): 28 | _root(log4cpp::Category::getRoot()), 29 | fatal(log4cpp::Category::getInstance(std::string("fatal"))), 30 | warn(log4cpp::Category::getInstance(std::string("warn"))), 31 | debug(log4cpp::Category::getInstance(std::string("debug"))) 32 | {} 33 | 34 | Log::~Log(){} 35 | 36 | Log* Log::genObj() 37 | { 38 | if(_restore) 39 | return _restore; 40 | try 41 | { 42 | PropertyConfigurator::configure("./config/log4cpp.conf"); 43 | } 44 | catch(ConfigureFailure& f) 45 | { 46 | std::cerr << f.what() << '\n'; 47 | return (Log *)-1; 48 | } 49 | _restore = new Log; 50 | return _restore; 51 | } 52 | 53 | void Log::destroy() 54 | { 55 | if(_restore ==nullptr) return; 56 | delete _restore; 57 | Category::shutdown(); 58 | _restore = nullptr; 59 | } 60 | 61 | inline void prt(void(*f)(const char* Format, ...), const char * str) 62 | { 63 | f("%s --%s--%s--%d--", str, __FILE__, __FUNCTION__, __LINE__); 64 | } 65 | 66 | #define prt(func, string) func("%s --%s--%s--%d--", x.c_str(), __FILE__, __FUNCTION__, __LINE__) 67 | 68 | int main() 69 | { 70 | Log* pLog = Log::genObj(); 71 | std::string x = "Hello"; 72 | x += " No Way"; 73 | prt(pLog->debug.info, x.c_str()); 74 | prt(pLog->debug.debug, x.c_str()); 75 | prt(pLog->debug.emerg, x.c_str()); 76 | prt(pLog->fatal.error, x.c_str()); 77 | prt(pLog->fatal.fatal, x.c_str()); 78 | Log::destroy(); 79 | return 0; 80 | } -------------------------------------------------------------------------------- /day6/Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | OBJS := ${patsubst ./src/%.cc, ./bin/%.o, $(SRCS)} 3 | ELFS:= log4cpp 4 | CFLAGS:= -Wall 5 | CXX:= g++ 6 | $(ELFS):$(OBJS) 7 | $(CXX) $(CFLAGS) $^ -o $@ -llog4cpp -lpthread 8 | ./bin/%.o:./src/%.cc 9 | $(CXX) $(CFLAGS) -c $< -o $@ 10 | 11 | .PHONY: clean 12 | clean: 13 | rm -rf $(ELFS) $(OBJS) 14 | .PHONY: rebuild 15 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /day6/day6.md: -------------------------------------------------------------------------------- 1 | # Github 的使用 2 | 3 | ## 1、创建Git 库:git init 4 | 5 | ## 2、Git 库添加文件: git add [文件名](加到本地暂存区); git commit -m "评论" 提交到版本库 6 | 7 | ## 3、添加到远程库:git remote origin git@github.com:stjimreal/(库名字); git push -u origin master 8 | 9 | ## 4、版本回退:git log; git reset --hard [sha1 码] 10 | -------------------------------------------------------------------------------- /day6/readme.md: -------------------------------------------------------------------------------- 1 | # Github 的使用 2 | 3 | ## 1、创建Git 库:git init 4 | 5 | ## 2、Git 库添加文件: git add [文件名](加到本地暂存区); git commit -m "评论" 提交到版本库 6 | 7 | ## 3、添加到远程库:git remote origin git@github.com:stjimreal/(库名字); git push -u origin master 8 | 9 | ## 4、版本回退:git log; git reset --hard [sha1 码] 10 | -------------------------------------------------------------------------------- /day8/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day8/1.png -------------------------------------------------------------------------------- /day8/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day8/2.png -------------------------------------------------------------------------------- /day8/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day8/3.png -------------------------------------------------------------------------------- /day8/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day8/4.png -------------------------------------------------------------------------------- /day8/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day8/5.png -------------------------------------------------------------------------------- /day8/FbCowString/Makefile: -------------------------------------------------------------------------------- 1 | SRCS:=$(wildcard ./src/*.cc) 2 | OBJS := ${patsubst ./src/%.cc, ./bin/%.o, $(SRCS)} 3 | ELFS:= fbstr 4 | CFLAGS:= -Wall 5 | CXX:= g++-9 6 | $(ELFS):$(OBJS) 7 | $(CXX) $(CFLAGS) $^ -o $@ 8 | ./bin/%.o:./src/%.cc 9 | $(CXX) $(CFLAGS) -c $< -o $@ 10 | 11 | .PHONY: clean 12 | clean: 13 | rm -rf $(ELFS) $(OBJS) 14 | .PHONY: rebuild 15 | rebuild:clean $(ELFS) -------------------------------------------------------------------------------- /day8/FbCowString/fbstr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day8/FbCowString/fbstr -------------------------------------------------------------------------------- /day8/FbCowString/include/FbStrCOW.hxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace fbstr{ 5 | class String 6 | { 7 | class charProxy 8 | { 9 | public: 10 | operator char(); 11 | char& operator=(const char &); 12 | charProxy(String &self, std::size_t index) 13 | :_self(self), _idx(index) 14 | {} 15 | 16 | private: 17 | String& _self; 18 | std::size_t _idx; 19 | }; 20 | public: 21 | String(); 22 | String(const char *); 23 | String(const String&); 24 | ~String(); 25 | String &operator=(const String &); 26 | String &operator=(const char *); 27 | 28 | String &operator+=(const String &); 29 | String &operator+=(const char *); 30 | 31 | charProxy operator[](std::size_t index); 32 | const char &operator[](std::size_t index) const; 33 | 34 | std::size_t size() const; 35 | const char* c_str() const; 36 | 37 | // friend bool operator==(const String &, const String &); 38 | // friend bool operator!=(const String &, const String &); 39 | 40 | // friend bool operator<(const String &, const String &); 41 | // friend bool operator>(const String &, const String &); 42 | // friend bool operator<=(const String &, const String &); 43 | // friend bool operator>=(const String &, const String &); 44 | 45 | friend std::ostream &operator<<(std::ostream &os, const String &s); 46 | friend std::istream &operator>>(std::istream &is, String &s); 47 | 48 | private: 49 | inline void freeProcess(); 50 | inline size_t getLen(const uint8_t type) const; 51 | inline char * getStr(String &, const uint8_t type); 52 | inline const char * getStr(const String &, const uint8_t type) const; 53 | inline uint8_t getType(const String &str) const; 54 | inline void setSize(const uint8_t type, const size_t length); 55 | union dataType{ 56 | struct { 57 | uint8_t _size; 58 | char _buffer[23]; 59 | }_short; 60 | struct{ 61 | size_t _size; 62 | size_t _capacity; 63 | char * _pstr; 64 | }_long; 65 | }; 66 | dataType _dat; 67 | }; 68 | 69 | String operator+(const String &, const String &); 70 | String operator+(const String &, const char *); 71 | String operator+(const char *, const String &); 72 | 73 | const int BLOCK_SIZE = 24; 74 | 75 | inline size_t String::getLen(const uint8_t type) const 76 | { 77 | return type > 0 ? (_dat._long._size >> 2) : (_dat._short._size >> 2); 78 | } 79 | 80 | inline char* String::getStr(String &str, const uint8_t type) 81 | { 82 | return type > 0 ? str._dat._long._pstr : str._dat._short._buffer; 83 | } 84 | 85 | inline const char* String::getStr(const String &str, const uint8_t type) const 86 | { 87 | return type > 0 ? str._dat._long._pstr : str._dat._short._buffer; 88 | } 89 | 90 | inline uint8_t String::getType(const String &str) const 91 | { 92 | return *(uint8_t *)&str._dat & 3; 93 | } 94 | 95 | inline void String::setSize(const uint8_t type, const size_t length) 96 | { 97 | if(type > 0) 98 | *(size_t *)&_dat = type | length << 2; 99 | else 100 | *(uint8_t *)&_dat = type | length << 2; 101 | } 102 | 103 | /* 基于三种类型的堆空间释放 */ 104 | inline void String::freeProcess() 105 | { 106 | const uint8_t type = getType(*this); 107 | switch (type) 108 | { 109 | case 1: 110 | if(_dat._long._pstr && --*(int*)(_dat._long._pstr - 4) <= 0){ 111 | delete [](_dat._long._pstr - 4); 112 | #ifndef NDEBUG 113 | printf("assignment1 freed %p\n", _dat._long._pstr); 114 | #endif 115 | } 116 | _dat._long._pstr = nullptr; 117 | break; 118 | case 2: 119 | if(_dat._long._pstr) 120 | delete []_dat._long._pstr; 121 | #ifndef NDEBUG 122 | printf("assignment2 freed %p\n", _dat._long._pstr); 123 | #endif 124 | _dat._long._pstr = nullptr; 125 | break; 126 | default: 127 | break; 128 | } 129 | } 130 | } // namespace str 131 | 132 | -------------------------------------------------------------------------------- /day8/FbCowString/src/CharProxyCOW.cc: -------------------------------------------------------------------------------- 1 | #include "../include/FbStrCOW.hxx" 2 | 3 | namespace fbstr 4 | { 5 | String::charProxy::operator char() 6 | { 7 | return _self._dat._long._pstr[_idx]; 8 | } 9 | 10 | char& String::charProxy::operator=(const char &ch) 11 | { 12 | uint8_t type = _self.getType(_self); 13 | char* str = _self.getStr(_self, type); 14 | if(type != 1 || *(uint32_t *)(str - 4) <= 1) 15 | { 16 | str[_idx] = ch; 17 | return str[_idx]; 18 | } 19 | 20 | /* 开始进行引用计数处理 */ 21 | --*(uint32_t *)(str - 4); 22 | printf("引用计数为 %u\n", *(uint32_t *)(str - 4)); 23 | _self._dat._long._pstr = new char[_self._dat._long._capacity + 5] + 4; 24 | *(uint32_t *)(_self._dat._long._pstr - 4) = 1; 25 | strcpy(_self._dat._long._pstr, str); 26 | 27 | _self._dat._long._pstr[_idx] = ch; 28 | return _self._dat._long._pstr[_idx]; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /day8/FbCowString/src/main.cc: -------------------------------------------------------------------------------- 1 | #include "../include/FbStrCOW.hxx" 2 | 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using namespace fbstr; 7 | 8 | int main() 9 | { 10 | const char *p = "Hello"; 11 | String s1 = p, s3; 12 | String s2 = "World// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const StrddffdsWorld// const Strddffds"; 13 | s3 = "World// const Strddffd"; 14 | cout<<"s2 ====== size = "< 2 | #include "../include/singleton.hh" 3 | 4 | Singleton1st::autoRelease::~autoRelease() 5 | { 6 | std::cout<<__FUNCTION__< 5 | #include 6 | 7 | class student 8 | { 9 | public: 10 | student(const std::string& name, const int sno, const int age, const double *arr, const int num); 11 | ~student(); 12 | friend std::ostream& operator<<(std::ostream& os, const student& stu); 13 | 14 | private: 15 | std::string _name; 16 | int _sno, _age; 17 | double _avgGrade; 18 | class pimplMethod; 19 | pimplMethod* _stu_pimpl; 20 | }; 21 | #endif -------------------------------------------------------------------------------- /day8/pimpl/pimpl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day8/pimpl/pimpl -------------------------------------------------------------------------------- /day8/pimpl/src/main.cc: -------------------------------------------------------------------------------- 1 | #include "../include/pimpl.hxx" 2 | #include "../include/student.hxx" 3 | 4 | int main() 5 | { 6 | double arr[] = {83.4, 82.32, 83.45, 84.55}; 7 | student jim("jimlau", 14208145, 22, arr, 4); 8 | std::cout<avgCnt(arr, num); 8 | } 9 | 10 | student::~student(){delete _stu_pimpl;} 11 | 12 | std::ostream& operator<<(std::ostream& os, const student &stu) 13 | { 14 | os< _rss; 28 | }; 29 | 30 | 要求:最后生成一个 pagelib.dat, 其格式: 31 | 32 | 33 | 1 34 | ... 35 | ... 36 | ... 37 | 38 | 39 | ... 40 | 41 | 42 | ... 43 | 44 | 45 | 46 | boost ==> 准标准库 47 | 48 | RSS文件解析作业思路: 49 | xml --> rss --> tinyxml2 --> boost::regex/std::regex -------------------------------------------------------------------------------- /day9/readme.md: -------------------------------------------------------------------------------- 1 | # C++ day9 2 | 3 | ## 1. 使用tinyXml2解析RSS文件,并生成一个网页库pagelib.dat 4 | 5 | [tinyXml2](https://github.com/leethomason/tinyxml2) 6 | [rss](http://www.runoob.com/rss/rss-tutorial.html) 7 | 8 | 正则表达式 进行过滤 `html`的标签 9 | 10 | 参考接口: 11 | 12 | ```cpp 13 | struct RssItem 14 | { 15 | string title; 16 | string link; 17 | string description; 18 | string content; 19 | }; 20 | 21 | class RssReader 22 | { 23 | public: 24 | RssReader(); 25 | 26 | void parseRss();//解析 27 | void dump(const string & filename);//输出 28 | private: 29 | vector _rss; 30 | }; 31 | ``` 32 | 33 | 要求:最后生成一个 `pagelib.dat`, 其格式: 34 | 35 | ```xml 36 | 37 | 1 38 | ... 39 | ... 40 | ... 41 | 42 | 43 | ... 44 | 45 | 46 | ... 47 | 48 | ``` 49 | 50 | > boost ==> 准标准库 51 | 52 | RSS文件解析作业思路: 53 | 54 | > xml --> rss --> tinyxml2 --> boost::regex/std::regex 55 | -------------------------------------------------------------------------------- /day9/resources/bomtest.xml: -------------------------------------------------------------------------------- 1 |  2 | -------------------------------------------------------------------------------- /day9/resources/empty.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day9/resources/empty.xml -------------------------------------------------------------------------------- /day9/resources/out/compact.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day9/resources/out/compact.xml -------------------------------------------------------------------------------- /day9/resources/out/pretty.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /day9/resources/out/printer.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /day9/resources/out/readme.txt: -------------------------------------------------------------------------------- 1 | This file is here to create this output directory when the source is pulled from git. -------------------------------------------------------------------------------- /day9/resources/out/test7.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day9/resources/out/textfile.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /day9/resources/out/utf8testout.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | The world has many languages 4 | Мир имеет много языков 5 | el mundo tiene muchos idiomas 6 | 世界有很多语言 7 | <Русский название="name" ценность="value"><имеет> 8 | <汉语 名字="name" 价值="value">世界有很多语言 9 | "Mëtæl!" 10 | <ä>Umlaut Element 11 | 12 | -------------------------------------------------------------------------------- /day9/resources/utf8test.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | The world has many languages 4 | Мир имеет много языков 5 | el mundo tiene muchos idiomas 6 | 世界有很多语言 7 | <Русский название="name" ценность="value"><имеет> 8 | <汉语 名字="name" 价值="value">世界有很多语言 9 | "Mëtæl!" 10 | <ä>Umlaut Element 11 | 12 | -------------------------------------------------------------------------------- /day9/resources/utf8testverify.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | The world has many languages 4 | Мир имеет много языков 5 | el mundo tiene muchos idiomas 6 | 世界有很多语言 7 | <Русский название="name" ценность="value"><имеет> 8 | <汉语 名字="name" 价值="value">世界有很多语言 9 | "Mëtæl!" 10 | <ä>Umlaut Element 11 | 12 | -------------------------------------------------------------------------------- /day9/resources/xmltest-4636783552757760.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day9/resources/xmltest-4636783552757760.xml -------------------------------------------------------------------------------- /day9/resources/xmltest-5330.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day9/resources/xmltest-5330.xml -------------------------------------------------------------------------------- /day9/resources/xmltest-5720541257269248.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day9/resources/xmltest-5720541257269248.xml -------------------------------------------------------------------------------- /day9/src/coolshell.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | inline void ERROR_CHECK(const int rtn, const int value, const char * funcName) 8 | { 9 | if(rtn != value) {perror(funcName);exit(-1);} 10 | } 11 | 12 | inline void ARGS_CHECK(const int rtn, const int value) 13 | { 14 | if(rtn != value) {printf("Wrong Args!\n");exit(-1);} 15 | } 16 | 17 | using namespace tinyxml2; 18 | using std::cin; 19 | using std::cout; 20 | using std::end; 21 | using std::string; 22 | using std::vector; 23 | using boost::regex; 24 | 25 | struct RssItem 26 | { 27 | string title; 28 | string link; 29 | string description; 30 | string content; 31 | }; 32 | 33 | class RssReader 34 | { 35 | public: 36 | RssReader(); 37 | 38 | void parseRss();//解析 39 | void dump(const string & filename);//输出 40 | private: 41 | vector _rss; 42 | }; 43 | 44 | 45 | int main(const int argc, const char ** argv) 46 | { 47 | ARGS_CHECK(argc, 2); 48 | XMLDocument doc; 49 | doc.LoadFile( argv[1] ); 50 | ERROR_CHECK(doc.ErrorID(), XML_SUCCESS, "LoadFile"); 51 | printf("title = %s\n", doc.RootElement()->FirstChildElement("channel")->FirstChildElement("title")->GetText()); 52 | 53 | XMLElement* item = nullptr; 54 | item = doc.RootElement()->FirstChildElement("channel")->FirstChildElement("item"); 55 | int cnt = 0; 56 | while(item) 57 | { 58 | printf("NO = %d\n", ++cnt); 59 | XMLElement* titleEle = item->FirstChildElement("title"); 60 | printf("title = %s\n", titleEle->GetText()); 61 | XMLElement* linkEle = item->FirstChildElement("link"); 62 | printf("link = %s\n", linkEle->GetText()); 63 | XMLElement* descriEle = item -> FirstChildElement("description"); 64 | printf("description =%s\n", descriEle->GetText()); 65 | // XMLElement* contentEle = item -> FirstChildElement("content:encoded"); 66 | // printf("content =%s\n", contentEle->GetText()); //用boost库匹配正则表达式得到干净的纯文本 67 | item = item->NextSiblingElement(); 68 | } 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /day9/xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stjimreal/Wangdao_CPP_master/6645160f15b1c7851ffea5de47905dff965ebb43/day9/xml --------------------------------------------------------------------------------