├── 8-1 Students I ├── README.md └── Student1 │ ├── ReadMe.txt │ ├── Student1.cpp │ ├── Student1.exe │ ├── Student1.vcxproj │ ├── Student1.vcxproj.filters │ ├── Text.txt │ ├── stdafx.cpp │ ├── stdafx.h │ └── targetver.h ├── 8-2 Students II ├── README.md └── Student2 │ ├── ReadMe.txt │ ├── Student2.cpp │ ├── Student2.exe │ ├── Student2.h │ ├── Student2.vcxproj │ ├── Student2.vcxproj.filters │ ├── Student2Test.cpp │ ├── Text.txt │ ├── stdafx.cpp │ ├── stdafx.h │ └── targetver.h ├── 8-3 Adventure ├── Adventure │ ├── Adventure.cpp │ ├── Adventure.exe │ ├── Adventure.vcxproj │ ├── Adventure.vcxproj.filters │ ├── MyCastle.txt │ ├── ReadMe.txt │ ├── castle.cpp │ ├── castle.h │ ├── stdafx.cpp │ ├── stdafx.h │ └── targetver.h └── README.md ├── 8-4 Personal Diary ├── Personal Diary │ ├── Diary.cpp │ ├── Diary.h │ ├── ReadMe.txt │ ├── diary.txt │ ├── pdadd.cpp │ ├── pdadd.exe │ ├── pdadd.vcxproj │ ├── pdadd.vcxproj.filters │ ├── pdadd_test.txt │ ├── pdlist.cpp │ ├── pdlist.exe │ ├── pdlist.vcxproj │ ├── pdlist.vcxproj.filters │ ├── pdlist_test.txt │ ├── pdremove.cpp │ ├── pdremove.exe │ ├── pdremove.vcxproj │ ├── pdremove.vcxproj.filters │ ├── pdremove_test.txt │ ├── pdshow.cpp │ ├── pdshow.exe │ ├── pdshow.vcxproj │ ├── pdshow.vcxproj.filters │ ├── pdshow_test.txt │ ├── stdafx.cpp │ ├── stdafx.h │ └── targetver.h ├── README.md └── 作业四 - 说明.pdf ├── 8-5 Fraction ├── README.md └── fraction │ ├── FractionTest.cpp │ ├── ReadMe.txt │ ├── fraction.cpp │ ├── fraction.exe │ ├── fraction.h │ ├── fraction.vcxproj │ ├── fraction.vcxproj.filters │ ├── stdafx.cpp │ ├── stdafx.h │ └── targetver.h ├── 8-6 Vector ├── README.md └── vector │ ├── ReadMe.txt │ ├── stdafx.cpp │ ├── stdafx.h │ ├── targetver.h │ ├── test.cpp │ ├── vector.cpp │ ├── vector.exe │ ├── vector.h │ ├── vector.vcxproj │ └── vector.vcxproj.filters ├── 8-7 STL allocator + memory pool (group work) ├── Figure1.png ├── MyAllocator │ ├── code │ │ ├── TestMyAllocator.cpp │ │ ├── TestWithFile.cpp │ │ ├── generate.cpp │ │ └── myallocator.h │ ├── executable file │ │ ├── TestMyAllocator │ │ │ └── MyAllocator.exe │ │ ├── TestWithFile │ │ │ └── MyAllocator.exe │ │ └── generate.exe │ └── test file │ │ ├── test0.txt │ │ ├── test1.txt │ │ ├── test10.txt │ │ ├── test2.txt │ │ ├── test3.txt │ │ ├── test4.txt │ │ ├── test5.txt │ │ ├── test6.txt │ │ ├── test7.txt │ │ ├── test8.txt │ │ └── test9.txt ├── README.md └── STL Allocator + Mem pool + 大作业要求.pptx └── README.md /8-1 Students I/README.md: -------------------------------------------------------------------------------- 1 | # 8-1 Students I (10 分) 2 | 3 | Write a program that asks you 10 records of students. Each record consists of a name (w/o space), and scores for three courses (in integer, 1 to 5). Output a list as following and calculate average score (in float) of each student and each course. Output the lowest and highest score for each course. 4 | 5 | ``` 6 | no name score1 score2 score3 average 7 | 1 K.Weng 5 5 5 5 8 | 2 T.Dixon 4 3 2 3 9 | 3 V.Chu 3 4 5 4 10 | 4 L.Tson 4 3 4 3.66667 11 | 5 L.Lee 3 4 3 3.33333 12 | 6 I.Young 4 2 5 3.66667 13 | 7 K.Hiro 4 2 1 2.33333 14 | 8 G.Ping 4 4 4 4 15 | 9 H.Gu 2 3 4 3 16 | 10 J.Jon 5 4 3 4 17 | average 3.8 3.4 3.6 18 | min 2 2 1 19 | max 5 5 5 20 | ``` 21 | 22 | **Evaluation standard** 23 | 24 | 1. result correctness 25 | 2. c++ code quality (compact and reasonable) 26 | 3. comments quality (clean and accurate) 27 | 4. c functions like `printf` and `scanf` are not allowed 28 | 29 | **Required Files**: source code, makefile (Mac, Linux), exe (Windows) -------------------------------------------------------------------------------- /8-1 Students I/Student1/ReadMe.txt: -------------------------------------------------------------------------------- 1 | ======================================================================== 2 | 控制台应用程序:Student1 项目概述 3 | ======================================================================== 4 | 5 | 应用程序向导已为您创建了此 Student1 应用程序。 6 | 7 | 本文件概要介绍组成 Student1 应用程序的每个文件的内容。 8 | 9 | 10 | Student1.vcxproj 11 | 这是使用应用程序向导生成的 VC++ 项目的主项目文件,其中包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。 12 | 13 | Student1.vcxproj.filters 14 | 这是使用“应用程序向导”生成的 VC++ 项目筛选器文件。它包含有关项目文件与筛选器之间的关联信息。在 IDE 中,通过这种关联,在特定节点下以分组形式显示具有相似扩展名的文件。例如,“.cpp”文件与“源文件”筛选器关联。 15 | 16 | Student1.cpp 17 | 这是主应用程序源文件。 18 | 19 | ///////////////////////////////////////////////////////////////////////////// 20 | 其他标准文件: 21 | 22 | StdAfx.h, StdAfx.cpp 23 | 这些文件用于生成名为 Student1.pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。 24 | 25 | ///////////////////////////////////////////////////////////////////////////// 26 | 其他注释: 27 | 28 | 应用程序向导使用“TODO:”注释来指示应添加或自定义的源代码部分。 29 | 30 | ///////////////////////////////////////////////////////////////////////////// 31 | -------------------------------------------------------------------------------- /8-1 Students I/Student1/Student1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-1 Students I/Student1/Student1.cpp -------------------------------------------------------------------------------- /8-1 Students I/Student1/Student1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-1 Students I/Student1/Student1.exe -------------------------------------------------------------------------------- /8-1 Students I/Student1/Student1.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {B2C5FC27-00E3-4DDD-BDD0-17E5060373B3} 23 | Win32Proj 24 | Student1 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Use 87 | Level3 88 | Disabled 89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 90 | true 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | Use 100 | Level3 101 | Disabled 102 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 103 | true 104 | 105 | 106 | Console 107 | true 108 | 109 | 110 | 111 | 112 | Level3 113 | Use 114 | MaxSpeed 115 | true 116 | true 117 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 118 | true 119 | 120 | 121 | Console 122 | true 123 | true 124 | true 125 | 126 | 127 | 128 | 129 | Level3 130 | Use 131 | MaxSpeed 132 | true 133 | true 134 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 135 | true 136 | 137 | 138 | Console 139 | true 140 | true 141 | true 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | Create 155 | Create 156 | Create 157 | Create 158 | 159 | 160 | 161 | 162 | 163 | 164 | -------------------------------------------------------------------------------- /8-1 Students I/Student1/Student1.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 资源文件 21 | 22 | 23 | 24 | 25 | 头文件 26 | 27 | 28 | 头文件 29 | 30 | 31 | 32 | 33 | 源文件 34 | 35 | 36 | 源文件 37 | 38 | 39 | -------------------------------------------------------------------------------- /8-1 Students I/Student1/Text.txt: -------------------------------------------------------------------------------- 1 | K.Weng 5 5 5 2 | T.Dixon 4 3 2 3 | V.Chu 3 4 5 4 | L.Tson 4 3 4 5 | L.Lee 3 4 3 6 | I.Young 4 2 5 7 | K.Hiro 4 2 1 8 | G.Ping 4 4 4 9 | H.Gu 2 3 4 10 | J.Jon 5 4 3 -------------------------------------------------------------------------------- /8-1 Students I/Student1/stdafx.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-1 Students I/Student1/stdafx.cpp -------------------------------------------------------------------------------- /8-1 Students I/Student1/stdafx.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-1 Students I/Student1/stdafx.h -------------------------------------------------------------------------------- /8-1 Students I/Student1/targetver.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-1 Students I/Student1/targetver.h -------------------------------------------------------------------------------- /8-2 Students II/README.md: -------------------------------------------------------------------------------- 1 | # 8-2 Students II (10 分) 2 | 3 | Write a CLI program that reads scores and names of students, and prints out a summary sheet (in the same format with the last homework "Students I"). The user can input as many students as possible. One student can have as many courses as possible. One course consists the name of the course and the scores the students got. Please note that a student won't choose every available course in general. 4 | 5 | **Evaluation standard** 6 | 7 | 1. c++ code quality (clean, compact and reasonable) 8 | 2. comments quality 9 | 10 | **Required Files** 11 | 12 | Please prepare a zip package including the following items: 13 | 14 | 1. the source code 15 | 2. a makefile (Mac, Linux) or exe (Windows), depending on your development environment 16 | 3. an external input file of your test data -------------------------------------------------------------------------------- /8-2 Students II/Student2/ReadMe.txt: -------------------------------------------------------------------------------- 1 | ======================================================================== 2 | 控制台应用程序:Student2 项目概述 3 | ======================================================================== 4 | 5 | 应用程序向导已为您创建了此 Student2 应用程序。 6 | 7 | 本文件概要介绍组成 Student2 应用程序的每个文件的内容。 8 | 9 | 10 | Student2.vcxproj 11 | 这是使用应用程序向导生成的 VC++ 项目的主项目文件,其中包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。 12 | 13 | Student2.vcxproj.filters 14 | 这是使用“应用程序向导”生成的 VC++ 项目筛选器文件。它包含有关项目文件与筛选器之间的关联信息。在 IDE 中,通过这种关联,在特定节点下以分组形式显示具有相似扩展名的文件。例如,“.cpp”文件与“源文件”筛选器关联。 15 | 16 | Student2.cpp 17 | 这是主应用程序源文件。 18 | 19 | ///////////////////////////////////////////////////////////////////////////// 20 | 其他标准文件: 21 | 22 | StdAfx.h, StdAfx.cpp 23 | 这些文件用于生成名为 Student2.pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。 24 | 25 | ///////////////////////////////////////////////////////////////////////////// 26 | 其他注释: 27 | 28 | 应用程序向导使用“TODO:”注释来指示应添加或自定义的源代码部分。 29 | 30 | ///////////////////////////////////////////////////////////////////////////// 31 | -------------------------------------------------------------------------------- /8-2 Students II/Student2/Student2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-2 Students II/Student2/Student2.cpp -------------------------------------------------------------------------------- /8-2 Students II/Student2/Student2.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-2 Students II/Student2/Student2.exe -------------------------------------------------------------------------------- /8-2 Students II/Student2/Student2.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifndef STUDENT2_H 4 | #define STUDENT2_H 5 | 6 | class Student { 7 | private: 8 | string name;//student name 9 | struct Course { 10 | string name; 11 | int score;//-1 means not taken 12 | };//name and score for each course 13 | vector courses;//all course chosen 14 | int coursenum;//number of courses taken 15 | double average;//average score of all courses 16 | 17 | public: 18 | vector read();//read in the data from the file 19 | void StuAverage(vector Stu);//calculate average score of a student 20 | void CourAMM(vector Stu);//calculate average,max,min score of a course 21 | void output(vector Stu);//output the table 22 | }; 23 | 24 | #endif -------------------------------------------------------------------------------- /8-2 Students II/Student2/Student2.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {61DD8121-334B-44BF-93AF-5E6E66271133} 23 | Win32Proj 24 | Student2 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Use 87 | Level3 88 | Disabled 89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 90 | true 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | Use 100 | Level3 101 | Disabled 102 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 103 | true 104 | 105 | 106 | Console 107 | true 108 | 109 | 110 | 111 | 112 | Level3 113 | Use 114 | MaxSpeed 115 | true 116 | true 117 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 118 | true 119 | 120 | 121 | Console 122 | true 123 | true 124 | true 125 | 126 | 127 | 128 | 129 | Level3 130 | Use 131 | MaxSpeed 132 | true 133 | true 134 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 135 | true 136 | 137 | 138 | Console 139 | true 140 | true 141 | true 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | Create 155 | Create 156 | Create 157 | Create 158 | 159 | 160 | 161 | 162 | 163 | 164 | -------------------------------------------------------------------------------- /8-2 Students II/Student2/Student2.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 资源文件 21 | 22 | 23 | 24 | 25 | 头文件 26 | 27 | 28 | 头文件 29 | 30 | 31 | 32 | 33 | 源文件 34 | 35 | 36 | 源文件 37 | 38 | 39 | -------------------------------------------------------------------------------- /8-2 Students II/Student2/Student2Test.cpp: -------------------------------------------------------------------------------- 1 | #include "Student2.h" 2 | #include "stdafx.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | 11 | int main() 12 | { 13 | vector Stu = Stu.read();//read in the data from the file 14 | Student.StuAverage(Stu);//calculate average score of a student 15 | CourAMM(Stu);//calculate average,max,min score of a course 16 | output(Stu);//output the table 17 | int i; 18 | cin >> i;//pause 19 | return 0; 20 | } -------------------------------------------------------------------------------- /8-2 Students II/Student2/Text.txt: -------------------------------------------------------------------------------- 1 | 7 2 | Amy Chinese 1 Math 3 English 1 Physics 5 Chemis 3 Biology 4 History 3 3 | Bob Chinese 4 Math 1 English 2 Physics 5 Chemis 3 Biology 2 History 3 4 | Charles Chinese 3 Math 5 English 5 Physics -1 Chemis 5 Biology 3 History 5 5 | Daisy Chinese 3 Math 5 English 2 Physics 4 Chemis 1 Biology 5 History 2 6 | Elize Chinese 5 Math 2 English 5 Physics 3 Chemis 3 Biology 3 History 4 7 | Peter Chinese 2 Math -1 English 3 Physics 2 Chemis 5 Biology 4 History 4 8 | Tom Chinese 1 Math 3 English 3 Physics 5 Chemis 4 Biology 2 History 5 9 | John Chinese 4 Math 3 English -1 Physics 4 Chemis 2 Biology 3 History 3 10 | Harry Chinese 3 Math 2 English 3 Physics 3 Chemis 5 Biology 5 History 1 -------------------------------------------------------------------------------- /8-2 Students II/Student2/stdafx.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-2 Students II/Student2/stdafx.cpp -------------------------------------------------------------------------------- /8-2 Students II/Student2/stdafx.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-2 Students II/Student2/stdafx.h -------------------------------------------------------------------------------- /8-2 Students II/Student2/targetver.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-2 Students II/Student2/targetver.h -------------------------------------------------------------------------------- /8-3 Adventure/Adventure/Adventure.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-3 Adventure/Adventure/Adventure.cpp -------------------------------------------------------------------------------- /8-3 Adventure/Adventure/Adventure.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-3 Adventure/Adventure/Adventure.exe -------------------------------------------------------------------------------- /8-3 Adventure/Adventure/Adventure.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {A6EDC7AA-D04A-4162-BA02-4D4CA61B4D32} 23 | Win32Proj 24 | Adventure 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Use 87 | Level3 88 | Disabled 89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 90 | true 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | Use 100 | Level3 101 | Disabled 102 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 103 | true 104 | 105 | 106 | Console 107 | true 108 | 109 | 110 | 111 | 112 | Level3 113 | Use 114 | MaxSpeed 115 | true 116 | true 117 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 118 | true 119 | 120 | 121 | Console 122 | true 123 | true 124 | true 125 | 126 | 127 | 128 | 129 | Level3 130 | Use 131 | MaxSpeed 132 | true 133 | true 134 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 135 | true 136 | 137 | 138 | Console 139 | true 140 | true 141 | true 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | Create 158 | Create 159 | Create 160 | Create 161 | 162 | 163 | 164 | 165 | 166 | -------------------------------------------------------------------------------- /8-3 Adventure/Adventure/Adventure.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 资源文件 21 | 22 | 23 | 24 | 25 | 头文件 26 | 27 | 28 | 头文件 29 | 30 | 31 | 头文件 32 | 33 | 34 | 35 | 36 | 源文件 37 | 38 | 39 | 源文件 40 | 41 | 42 | 源文件 43 | 44 | 45 | -------------------------------------------------------------------------------- /8-3 Adventure/Adventure/MyCastle.txt: -------------------------------------------------------------------------------- 1 | room0 2 east down 2 | room1 3 east west down 3 | room2 3 east west down 4 | room3 2 west down 5 | room4 2 east down 6 | room5 2 east west 7 | room6 2 west down 8 | room7 3 east up down 9 | room8 4 east west up down 10 | room9 2 west up 11 | room10 2 up down 12 | room11 2 up down 13 | room12 2 east down 14 | room13 2 west up 15 | room14 3 east up down 16 | room15 4 east west up down 17 | room16 2 east west 18 | room17 4 east west up down 19 | room18 3 east west up 20 | room19 4 east west up down 21 | room20 1 west 22 | room21 2 east up 23 | room22 4 east west up down 24 | room23 2 west down 25 | room24 3 east up down 26 | room25 2 east west 27 | room26 3 east west up 28 | room27 2 west down 29 | room28 2 east down 30 | room29 2 west up 31 | room30 3 east up down 32 | room31 3 east west up 33 | room32 2 west down 34 | room33 1 down 35 | room34 2 up down 36 | room35 2 east up 37 | room36 3 east west down 38 | room37 4 east west up down 39 | room38 2 west down 40 | room39 2 up down 41 | room40 3 east up down 42 | room41 2 west up 43 | room42 1 east 44 | room43 2 west up 45 | room44 2 east up 46 | lobby 3 east west up 47 | room46 3 east west up 48 | room47 3 east west up 49 | room48 1 west -------------------------------------------------------------------------------- /8-3 Adventure/Adventure/ReadMe.txt: -------------------------------------------------------------------------------- 1 | ======================================================================== 2 | 控制台应用程序:Adventure 项目概述 3 | ======================================================================== 4 | 5 | 应用程序向导已为您创建了此 Adventure 应用程序。 6 | 7 | 本文件概要介绍组成 Adventure 应用程序的每个文件的内容。 8 | 9 | 10 | Adventure.vcxproj 11 | 这是使用应用程序向导生成的 VC++ 项目的主项目文件,其中包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。 12 | 13 | Adventure.vcxproj.filters 14 | 这是使用“应用程序向导”生成的 VC++ 项目筛选器文件。它包含有关项目文件与筛选器之间的关联信息。在 IDE 中,通过这种关联,在特定节点下以分组形式显示具有相似扩展名的文件。例如,“.cpp”文件与“源文件”筛选器关联。 15 | 16 | Adventure.cpp 17 | 这是主应用程序源文件。 18 | 19 | ///////////////////////////////////////////////////////////////////////////// 20 | 其他标准文件: 21 | 22 | StdAfx.h, StdAfx.cpp 23 | 这些文件用于生成名为 Adventure.pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。 24 | 25 | ///////////////////////////////////////////////////////////////////////////// 26 | 其他注释: 27 | 28 | 应用程序向导使用“TODO:”注释来指示应添加或自定义的源代码部分。 29 | 30 | ///////////////////////////////////////////////////////////////////////////// 31 | -------------------------------------------------------------------------------- /8-3 Adventure/Adventure/castle.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "castle.h" 8 | using namespace std; 9 | 10 | void PrintInfo(Room *room) 11 | { 12 | cout << "Welcome come to the " << room->roomname << "."; 13 | if (room->ExitNum == 0) 14 | cout << "There is no exit.";//actually it is impossible 15 | else { 16 | cout << "There are " << room->ExitNum << " exits: "; 17 | string name[4]; 18 | int i = 0; 19 | if (room->EastExit) name[i++] = "east"; 20 | if (room->WestExit) name[i++] = "west"; 21 | if (room->UpExit) name[i++] = "up"; 22 | if (room->DownExit) name[i++] = "down"; 23 | for (i = 0; i < room->ExitNum; i++) 24 | { 25 | cout << name[i]; 26 | if (i == room->ExitNum - 2) cout << " and "; 27 | else if (i == room->ExitNum - 1) cout << "." << endl; 28 | else cout << ", "; 29 | } 30 | } 31 | cout << "Enter your command:"; 32 | } 33 | 34 | Castle::Castle() 35 | { 36 | 37 | } 38 | 39 | Castle::~Castle() { 40 | 41 | while (!castle.empty()) { 42 | vector::iterator it = castle.end(); 43 | castle.pop_back(); 44 | delete (*it); 45 | } 46 | } 47 | 48 | void Castle::GetMap() 49 | { 50 | string name; 51 | ifstream file("MyCastle.txt", ios::in);//data of a 9*9 castle map 52 | if (!file)//test if the file is opened successfully 53 | { 54 | cout << "Error opening file." << endl; 55 | return; 56 | } 57 | for (int i = 0; i < 49; i++) 58 | { 59 | Room *room = new Room; 60 | file >> room->roomname >> room->ExitNum; 61 | room->EastExit = room->WestExit = room->UpExit = room->DownExit = false; 62 | room->Monster = room->Princess = false;//initialize 63 | string direction; 64 | for (int i = 0; i < room->ExitNum; i++) 65 | { 66 | file >> direction; 67 | if (direction == "east") room->EastExit = true; 68 | if (direction == "west") room->WestExit = true; 69 | if (direction == "up") room->UpExit = true; 70 | if (direction == "down") room->DownExit = true; 71 | } 72 | castle.push_back(room); 73 | } 74 | file.close();//close the file 75 | } 76 | 77 | void Castle::RandPosition() 78 | { 79 | //a 7*7 castle 80 | int princess = rand() % 49; 81 | int monster = rand() % 49;//0-48 82 | while (princess == monster || princess == 45 || monster == 45) { 83 | //princess and monster not in the lobby and not the same room 84 | princess = rand() % 49; 85 | monster = rand() % 49; 86 | } 87 | castle[princess]->Princess = true; 88 | castle[monster]->Monster = true; 89 | } 90 | 91 | void Castle::Run() 92 | { 93 | //a 7*7 castle 94 | //initial 95 | Room *current = castle[45];//lobby:map[3][6] 96 | int position = 45; 97 | 98 | string go, direct; 99 | while (1) { 100 | PrintInfo(current); 101 | cin >> go >> direct; 102 | //move 103 | if (direct == "east") { 104 | if (!current->EastExit) { 105 | cout << "The exit doesn't exist!" << endl; 106 | continue; 107 | } 108 | else { 109 | position++; 110 | current = castle[position]; 111 | } 112 | } 113 | else if (direct == "west") { 114 | if (!current->WestExit) { 115 | cout << "The exit doesn't exist!" << endl; 116 | continue; 117 | } 118 | else { 119 | position--; 120 | current = castle[position]; 121 | } 122 | } 123 | else if (direct == "up") { 124 | if (!current->UpExit) { 125 | cout << "The exit doesn't exist!" << endl; 126 | continue; 127 | } 128 | else { 129 | position -= 7; 130 | current = castle[position]; 131 | } 132 | } 133 | else if (direct == "down") { 134 | if (!current->DownExit) { 135 | cout << "The exit doesn't exist!" << endl; 136 | continue; 137 | } 138 | else { 139 | position += 7; 140 | current = castle[position]; 141 | } 142 | } 143 | //judge 144 | if (current->Monster) { 145 | cout << "You are eaten by the monster! Game over!" << endl; 146 | return;//dead 147 | } 148 | if (current->Princess) { 149 | if (!getPrincess) 150 | getPrincess = true; 151 | cout << "You get the princess,run out of the castle quickly!" << endl; 152 | } 153 | if (getPrincess && position == 45) { 154 | cout << "Successfully save the princess! " << "You win!" << endl; 155 | return;//win 156 | } 157 | } 158 | } -------------------------------------------------------------------------------- /8-3 Adventure/Adventure/castle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef CASTLE_H 3 | #define CASTLE_H 4 | #include "stdafx.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | struct Room { 13 | string roomname; 14 | int ExitNum;//number of exits 15 | bool EastExit, WestExit, UpExit, DownExit;//exists 16 | bool Monster = false; 17 | bool Princess = false; 18 | }; 19 | void PrintInfo(Room *room); 20 | 21 | class Castle { 22 | private: 23 | vector castle; 24 | bool getPrincess = false; 25 | public: 26 | Castle(); 27 | ~Castle(); 28 | void GetMap(); 29 | void RandPosition(); 30 | void Run(); 31 | }; 32 | #endif // !CASTLE_H 33 | 34 | -------------------------------------------------------------------------------- /8-3 Adventure/Adventure/stdafx.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-3 Adventure/Adventure/stdafx.cpp -------------------------------------------------------------------------------- /8-3 Adventure/Adventure/stdafx.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-3 Adventure/Adventure/stdafx.h -------------------------------------------------------------------------------- /8-3 Adventure/Adventure/targetver.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-3 Adventure/Adventure/targetver.h -------------------------------------------------------------------------------- /8-3 Adventure/README.md: -------------------------------------------------------------------------------- 1 | # 8-3 Adventure (20 分) 2 | 3 | **Story** 4 | 5 | Adventure is a CLI game. The player has to explore in the castle with many levels and a lot of rooms. The task of the player is to find a room where the princess is prisoned and take her leave the castle. There are many types of rooms, and each type of room has different types of exits. Note that there's a monster in one of the rooms, which the exact location is not able to be aware. But once the player meets a monster, the game is over. 6 | 7 | When the game starts, the player is in the lobby of the castle. Then the program shows information about the lobby:name of the room, how many exits are there, and names of all exits (e.g.: "east", "south", "up"), like: 8 | 9 | ``` 10 | Welcome to the lobby. There are 3 exits: east, west and up. 11 | Enter your command: 12 | ``` 13 | 14 | The player then can input "go" followed by the name of one exit to enter the room connected to that door, like: 15 | 16 | ``` 17 | go east 18 | ``` 19 | 20 | The player goes into the room to the east. The program shows the information about that room, like what happened in the lobby just now. And the player may input command to choose another room. 21 | 22 | Once the player enters a room with a monster, the program shows a message and game over. Once the player enters the room of princess, the program shows a message about the princess, and the princess is going to leave with the player. The player then has to find their way out the castle. The only way to leave the castle is via the lobby. 23 | 24 | All printed messages and user input are in English to simplify the code. 25 | 26 | **Evaluation standard** 27 | 28 | 1. c++ code quality (clean, compact and reasonable) 29 | 2. comments quality 30 | 3. at least three different kinds of rooms 31 | 4. at least five rooms 32 | 5. the room with monster or princess is randomly set 33 | 34 | **Required Files** 35 | 36 | Please prepare a zip package including the following items: 37 | 38 | 1. the source code 39 | 2. a makefile (Mac, Linux) or exe (Windows), depending on your development environment 40 | 3. an external input file with your test data -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/Diary.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Diary.h" 3 | 4 | void Diary::Reset() 5 | { 6 | text.clear(); 7 | } 8 | 9 | void Diary::SetText() 10 | { 11 | string str; 12 | getline(cin, str); 13 | while (str != ".") 14 | { 15 | text.push_back(str); 16 | getline(cin, str); 17 | } 18 | } 19 | 20 | void Diary::SetDate(string d) 21 | { 22 | date = d; 23 | } 24 | 25 | void Diary::AddLine(string Line) 26 | { 27 | text.push_back(Line); 28 | } 29 | 30 | string Diary::GetText(VStringSize i) 31 | { 32 | return text[i]; 33 | } 34 | 35 | string Diary::GetDate() 36 | { 37 | return date; 38 | } 39 | 40 | VStringSize Diary::Size() 41 | { 42 | return text.size(); 43 | } 44 | 45 | //read diaries from file to memory 46 | void read_to_memory(string filename, vector& my_diary) 47 | { 48 | Diary diary; 49 | string str; 50 | FILE *stream; 51 | freopen_s(&stream, filename.c_str(), "r", stdin); 52 | while (getline(cin, str)) 53 | { 54 | diary.SetDate(str); 55 | diary.Reset(); 56 | getline(cin, str); 57 | while (str != ".") 58 | { 59 | diary.AddLine(str); 60 | getline(cin, str); 61 | } 62 | my_diary.push_back(diary); 63 | } 64 | fclose(stdin); 65 | cin.clear(); 66 | } 67 | 68 | //store diaries from memory to file 69 | void write_to_file(string filename, vector& my_diary) 70 | { 71 | sort(my_diary.begin(), my_diary.end(), date_sort); 72 | FILE *stream; 73 | freopen_s(&stream, filename.c_str(), "w", stdout); 74 | for (int i = 0; i < my_diary.size(); i++) 75 | { 76 | cout << my_diary[i].GetDate() << endl; 77 | for (int j = 0; j < my_diary[i].Size(); j++) 78 | { 79 | cout << my_diary[i].GetText(j) << endl; 80 | } 81 | cout << "." << endl; 82 | } 83 | fclose(stdout); 84 | } 85 | 86 | bool date_sort(const Diary &a1, const Diary &a2) 87 | { 88 | Diary a = a1, b = a2; 89 | string date1 = a.GetDate(); 90 | string date2 = b.GetDate(); 91 | if(date1.compare(date2) >= 0)//date1>=date2 92 | return false; 93 | else return true; 94 | } -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/Diary.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef DIARY_H 3 | #define DIARY_H 4 | 5 | #include "stdafx.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | typedef vector::size_type VStringSize; 16 | class Diary { 17 | private: 18 | string date; 19 | vector text; 20 | public: 21 | void Reset(); 22 | void SetText(); 23 | void SetDate(string); 24 | void AddLine(string); 25 | string GetText(VStringSize); 26 | string GetDate(); 27 | VStringSize Size(); 28 | }; 29 | void read_to_memory(string filename, vector& my_diary); 30 | void write_to_file(string filename, vector& my_diary); 31 | bool date_sort(const Diary &a1, const Diary &a2); 32 | #endif // !DIARY_H -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/ReadMe.txt: -------------------------------------------------------------------------------- 1 | pdadd 2 | add an entity given in the file "pdadd_test.txt" 3 | to the diary for the date in the file "diary.txt" 4 | 5 | pdlist [ ] 6 | lists all entities in the diary ordered by date in the file "pdlist_test.txt" 7 | If the start and the end date are provided in the file "pdlist_test.txt", 8 | it lists entities between the start and the end only. 9 | 10 | pdshow 11 | prints the content of the entity 12 | specified by the date given in the file "pdshow_test.txt" 13 | to the file "pdshow_test.txt" 14 | 15 | pdremove 16 | removes one entity of the date given in the file "pdremove_test.txt" 17 | from the file "diary.txt". -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/diary.txt: -------------------------------------------------------------------------------- 1 | 2020-02-13 2 | Today is sunny. I feel happy. 3 | I get a full mark in the exam! 4 | . 5 | 2020-03-07 6 | Today is raining. 7 | I have a terrible toothache and it makes me feel awful. 8 | . 9 | -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdadd.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Diary.h" 3 | 4 | int main() 5 | { 6 | vector my_diary; 7 | Diary diary; 8 | string date; 9 | string str; 10 | 11 | //read from file to memory 12 | read_to_memory("diary.txt", my_diary); 13 | 14 | //read in the new diary 15 | FILE *stream; 16 | freopen_s(&stream, "pdadd_test.txt", "r", stdin); 17 | getline(cin, date); 18 | if (date.empty()) 19 | { 20 | printf("The new diary is empty!"); 21 | return 0; 22 | } 23 | diary.SetDate(date); 24 | diary.Reset(); 25 | getline(cin, str); 26 | while (str != ".") 27 | { 28 | diary.AddLine(str); 29 | getline(cin, str); 30 | } 31 | 32 | int i; 33 | for (i = 0; i < my_diary.size(); i++) 34 | if (my_diary[i].GetDate() == date) break; 35 | if (i == my_diary.size())//no entity of the same date 36 | my_diary.push_back(diary); 37 | else 38 | my_diary[i] = diary; 39 | 40 | fclose(stdin); 41 | cin.clear(); 42 | 43 | write_to_file("diary.txt", my_diary); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdadd.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-4 Personal Diary/Personal Diary/pdadd.exe -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdadd.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {C32F5498-94F7-42A7-983B-FCA804DF9081} 23 | Win32Proj 24 | pdadd 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Use 87 | Level3 88 | Disabled 89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 90 | true 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | Use 100 | Level3 101 | Disabled 102 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 103 | true 104 | 105 | 106 | Console 107 | true 108 | 109 | 110 | 111 | 112 | Level3 113 | Use 114 | MaxSpeed 115 | true 116 | true 117 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 118 | true 119 | 120 | 121 | Console 122 | true 123 | true 124 | true 125 | 126 | 127 | 128 | 129 | Level3 130 | Use 131 | MaxSpeed 132 | true 133 | true 134 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 135 | true 136 | 137 | 138 | Console 139 | true 140 | true 141 | true 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | Create 159 | Create 160 | Create 161 | Create 162 | 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdadd.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 资源文件 21 | 22 | 23 | 资源文件 24 | 25 | 26 | 27 | 28 | 头文件 29 | 30 | 31 | 头文件 32 | 33 | 34 | 头文件 35 | 36 | 37 | 38 | 39 | 源文件 40 | 41 | 42 | 源文件 43 | 44 | 45 | 源文件 46 | 47 | 48 | -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdadd_test.txt: -------------------------------------------------------------------------------- 1 | 2020-02-20 2 | Today is cloudy. 3 | I meet my math teacher in my highschool when going out. 4 | . -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdlist.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Diary.h" 3 | int between(string date, string begin_date, string end_date) 4 | { 5 | if (date.compare(end_date) <= 0 && date.compare(begin_date) >= 0) 6 | return 1; 7 | else 8 | return 0; 9 | } 10 | int main() 11 | { 12 | vector my_diary; 13 | string begin_date, end_date; 14 | 15 | //read from file to memory 16 | read_to_memory("diary.txt", my_diary); 17 | //sort and write back 18 | write_to_file("diary.txt", my_diary); 19 | 20 | FILE *stream; 21 | freopen_s(&stream, "pdlist_test.txt", "r", stdin); 22 | getline(cin, begin_date); 23 | getline(cin, end_date); 24 | fclose(stdin); 25 | cin.clear(); 26 | if (begin_date.empty() && end_date.empty()) 27 | { 28 | write_to_file("pdlist_test.txt", my_diary); 29 | return 0; 30 | } 31 | freopen_s(&stream, "pdlist_test.txt", "w", stdout); 32 | cout << "Diaries from " << begin_date << " to " << end_date << endl; 33 | int count = 0; 34 | for (int i = 0; i < my_diary.size(); i++) 35 | { 36 | if (between(my_diary[i].GetDate(), begin_date, end_date)) 37 | { 38 | cout << my_diary[i].GetDate() << endl; 39 | for (int j = 0; j < my_diary[i].Size(); j++) 40 | { 41 | cout << my_diary[i].GetText(j) << endl; 42 | } 43 | cout << "." << endl; 44 | count++; 45 | } 46 | } 47 | if (count == 0) 48 | cout << "None."; 49 | fclose(stdout); 50 | 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdlist.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-4 Personal Diary/Personal Diary/pdlist.exe -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdlist.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {07885144-E916-497E-B6AF-13FCB3E69064} 23 | Win32Proj 24 | pdlist 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Use 87 | Level3 88 | Disabled 89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 90 | true 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | Use 100 | Level3 101 | Disabled 102 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 103 | true 104 | 105 | 106 | Console 107 | true 108 | 109 | 110 | 111 | 112 | Level3 113 | Use 114 | MaxSpeed 115 | true 116 | true 117 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 118 | true 119 | 120 | 121 | Console 122 | true 123 | true 124 | true 125 | 126 | 127 | 128 | 129 | Level3 130 | Use 131 | MaxSpeed 132 | true 133 | true 134 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 135 | true 136 | 137 | 138 | Console 139 | true 140 | true 141 | true 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | Create 159 | Create 160 | Create 161 | Create 162 | 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdlist.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 资源文件 21 | 22 | 23 | 资源文件 24 | 25 | 26 | 27 | 28 | 头文件 29 | 30 | 31 | 头文件 32 | 33 | 34 | 头文件 35 | 36 | 37 | 38 | 39 | 源文件 40 | 41 | 42 | 源文件 43 | 44 | 45 | 源文件 46 | 47 | 48 | -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdlist_test.txt: -------------------------------------------------------------------------------- 1 | 2020-03-01 2 | 2020-03-25 -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdremove.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Diary.h" 3 | 4 | int main() 5 | { 6 | vector my_diary; 7 | string date; 8 | 9 | //read from file to memory 10 | read_to_memory("diary.txt", my_diary); 11 | 12 | FILE *stream; 13 | freopen_s(&stream, "pdremove_test.txt", "r", stdin); 14 | getline(cin, date); 15 | fclose(stdin); 16 | cin.clear(); 17 | freopen_s(&stream, "pdremove_test.txt", "w", stdout); 18 | int flag = 0; 19 | for (int i = 0; i < my_diary.size(); i++) 20 | { 21 | if (my_diary[i].GetDate() == date)//remove it 22 | { 23 | for (int j = i; j < my_diary.size() - 1; j++) 24 | my_diary[j] = my_diary[j + 1]; 25 | my_diary.pop_back(); 26 | 27 | flag = 1; 28 | cout << "Success!";//success 29 | fclose(stdout); 30 | write_to_file("diary.txt", my_diary); 31 | } 32 | } 33 | if (flag == 0) 34 | { 35 | cout << "Fail! There is no diary of " << date << "." << endl;//fail 36 | fclose(stdout); 37 | return -1; 38 | } 39 | 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdremove.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-4 Personal Diary/Personal Diary/pdremove.exe -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdremove.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {270AEB86-600E-4C55-8F05-B3A39E493961} 23 | Win32Proj 24 | pdremove 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Use 87 | Level3 88 | Disabled 89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 90 | true 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | Use 100 | Level3 101 | Disabled 102 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 103 | true 104 | 105 | 106 | Console 107 | true 108 | 109 | 110 | 111 | 112 | Level3 113 | Use 114 | MaxSpeed 115 | true 116 | true 117 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 118 | true 119 | 120 | 121 | Console 122 | true 123 | true 124 | true 125 | 126 | 127 | 128 | 129 | Level3 130 | Use 131 | MaxSpeed 132 | true 133 | true 134 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 135 | true 136 | 137 | 138 | Console 139 | true 140 | true 141 | true 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | Create 159 | Create 160 | Create 161 | Create 162 | 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdremove.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 资源文件 21 | 22 | 23 | 资源文件 24 | 25 | 26 | 27 | 28 | 头文件 29 | 30 | 31 | 头文件 32 | 33 | 34 | 头文件 35 | 36 | 37 | 38 | 39 | 源文件 40 | 41 | 42 | 源文件 43 | 44 | 45 | 源文件 46 | 47 | 48 | -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdremove_test.txt: -------------------------------------------------------------------------------- 1 | 2020-03-07 -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdshow.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "Diary.h" 3 | 4 | int main() 5 | { 6 | vector my_diary; 7 | string date; 8 | 9 | //read from file to memory 10 | read_to_memory("diary.txt", my_diary); 11 | sort(my_diary.begin(), my_diary.end(), date_sort); 12 | 13 | FILE *stream; 14 | freopen_s(&stream, "pdshow_test.txt", "r", stdin); 15 | getline(cin, date); 16 | fclose(stdin); 17 | cin.clear(); 18 | freopen_s(&stream, "pdshow_test.txt", "w", stdout); 19 | int flag = 0; 20 | for (int i = 0; i < my_diary.size(); i++) 21 | { 22 | if (my_diary[i].GetDate() == date)//show the diary 23 | { 24 | cout << my_diary[i].GetDate() << endl; 25 | for (int j = 0; j < my_diary[i].Size(); j++) 26 | { 27 | cout << my_diary[i].GetText(j) << endl; 28 | } 29 | cout << "." << endl; 30 | flag = 1; 31 | } 32 | } 33 | if (flag == 0) 34 | { 35 | cout << "Fail! There is no diary of " << date << "." << endl; 36 | } 37 | fclose(stdout); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdshow.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-4 Personal Diary/Personal Diary/pdshow.exe -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdshow.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {6C5E028A-FA43-47FA-84EE-2D57B8D8E5D9} 23 | Win32Proj 24 | pdshow 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Use 87 | Level3 88 | Disabled 89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 90 | true 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | Use 100 | Level3 101 | Disabled 102 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 103 | true 104 | 105 | 106 | Console 107 | true 108 | 109 | 110 | 111 | 112 | Level3 113 | Use 114 | MaxSpeed 115 | true 116 | true 117 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 118 | true 119 | 120 | 121 | Console 122 | true 123 | true 124 | true 125 | 126 | 127 | 128 | 129 | Level3 130 | Use 131 | MaxSpeed 132 | true 133 | true 134 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 135 | true 136 | 137 | 138 | Console 139 | true 140 | true 141 | true 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | Create 159 | Create 160 | Create 161 | Create 162 | 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdshow.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 资源文件 21 | 22 | 23 | 资源文件 24 | 25 | 26 | 27 | 28 | 头文件 29 | 30 | 31 | 头文件 32 | 33 | 34 | 头文件 35 | 36 | 37 | 38 | 39 | 源文件 40 | 41 | 42 | 源文件 43 | 44 | 45 | 源文件 46 | 47 | 48 | -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/pdshow_test.txt: -------------------------------------------------------------------------------- 1 | 2020-03-07 2 | -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/stdafx.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-4 Personal Diary/Personal Diary/stdafx.cpp -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/stdafx.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-4 Personal Diary/Personal Diary/stdafx.h -------------------------------------------------------------------------------- /8-4 Personal Diary/Personal Diary/targetver.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-4 Personal Diary/Personal Diary/targetver.h -------------------------------------------------------------------------------- /8-4 Personal Diary/README.md: -------------------------------------------------------------------------------- 1 | # 8-4 Personal Diary (20 分) 2 | 3 | **Requirements** 4 | 5 | The Personal Diary is a CLI (Command Line Interface) software, consists of four programs: 6 | 7 | ``` 8 | pdadd 9 | pdlist [ ] 10 | pdshow 11 | pdremove 12 | ``` 13 | 14 | - **`pdadd`** is used to add an entity to the diary for the date. If an entity of the same date is in the diary, the existing one will be replaced. pdadd reads lines of the diary from the stdin, line by line, until a line with a single `'.'` character or the `EOF` character (`Ctrl-D` in Unix and `Ctrl-Z` in Windows). 15 | - **`pdlist`** lists all entities in the diary ordered by date. If the `start` and the `end` date are provided through command line parameters, it lists entities between the start and the end only. This program lists to the stdout. 16 | - **`pdshow`** prints the content of the entity specified by the date to the stdout. 17 | - **`pdremove`** removes one entity of the date. It returns 0 on success and -1 on failure. 18 | 19 | The software stores the diary in one data file, and reads it to the memory at the beginning of each program, and stores it back to the file at the end of the process. 20 | 21 | **Evaluation standard** 22 | 23 | 1. c++ code quality (clean, compact and reasonable) 24 | 2. comments quality 25 | 3. common classes and functions should be shared between programs 26 | 4. these programs are physically independent so direct interaction is not permitted 27 | 5. these programs are able to work together by means of redirection 28 | 6. these programs are able to be used in a shell/batch script 29 | 30 | **Files to submit** 31 | 32 | Please prepare a .zip package including the following items: 33 | 34 | 1. the source code 35 | 2. makefile (for Mac or Linux users) or .exes (for Windows users) 36 | 3. a shell/batch script with some test uses for your software (cover all programs) -------------------------------------------------------------------------------- /8-4 Personal Diary/作业四 - 说明.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-4 Personal Diary/作业四 - 说明.pdf -------------------------------------------------------------------------------- /8-5 Fraction/README.md: -------------------------------------------------------------------------------- 1 | # 8-5 Fraction (10 分) 2 | 3 | **Requirements** 4 | 5 | Write a class that represents a fraction number like `2/3`. 6 | 7 | Functions below have to be implemented for this class: 8 | 9 | - default ctor 10 | - ctor takes two integers as parameters 11 | - copy ctor 12 | - arithmetical operators: `+ - * /` 13 | - relational operators: `< <= == != >= >` 14 | - type cast to double 15 | - to string 16 | - inserter and extractor for streams 17 | - conversion from a finite decimal string like: `1.414` 18 | 19 | **Evaluation standard** 20 | 21 | 1. c++ code quality (clean, compact and reasonable) 22 | 2. comments quality 23 | 24 | **Files to submit** 25 | 26 | Please prepare a .zip package including the following items: 27 | 28 | 1. the source code (including the Fraction class, and some typical usages for test) 29 | 2. makefile (for Mac or Linux users) or .exes (for Windows users) or CMakeLists.txt -------------------------------------------------------------------------------- /8-5 Fraction/fraction/FractionTest.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "fraction.h" 3 | 4 | int main() 5 | { 6 | //default ctor 7 | Fraction a; 8 | cout << "-------default ctor-------" << endl; 9 | cout << "a = " << a << endl; 10 | //ctor takes two integers as parameters 11 | cout << "-------ctor takes two integers as parameters-------" << endl; 12 | cout << "Input two integers as parameters:"; 13 | int m, n; 14 | cin >> m >> n; 15 | Fraction b(m, n); 16 | cout << "b = " << b << endl; 17 | //copy ctor 18 | cout << "-------copy ctor b to c-------" << endl; 19 | Fraction c(b); 20 | cout << "c = " << c << endl; 21 | 22 | //inserter and extractor for streams 23 | cout << "-------inserter and extractor for streams-------" << endl; 24 | Fraction x, y; 25 | cout << "Input two fractions x, y (example:1/3):" << endl; 26 | cout << "Nonminimalist fractions will be automatically simplified." << endl; 27 | cin >> x >> y; 28 | cout << "x = " << x << endl; 29 | cout << "y = " << y << endl; 30 | 31 | //arithmetical operators: + - * / 32 | cout << "-------arithmetical operators-------" << endl; 33 | cout << "x + y = " << x + y << endl; 34 | cout << "x - y = " << x - y << endl; 35 | cout << "x * y = " << x * y << endl; 36 | cout << "x / y = " << x / y << endl; 37 | 38 | //relational operators : < <= == != >= > 39 | cout << "-------relational operators-------" << endl; 40 | cout << "x < y is " << (x < y) << endl; 41 | cout << "x <= y is " << (x <= y) << endl; 42 | cout << "x == y is " << (x == y) << endl; 43 | cout << "x != y is " << (x != y) << endl; 44 | cout << "x >= y is " << (x >= y) << endl; 45 | cout << "x > y is " << (x > y) << endl; 46 | 47 | //type cast to double 48 | cout << "-------type cast to double-------" << endl; 49 | double p = x.to_double(); 50 | cout << "x type cast to double p." << endl << "p = " << p << endl; 51 | //type cast to string 52 | cout << "-------type cast to string-------" << endl; 53 | string q = x.to_string(); 54 | cout << "x type cast to string q." << endl << "q = " << q << endl; 55 | 56 | //conversion from a finite decimal string like : 1.414 57 | cout << "-------conversion from a finite decimal string-------" << endl; 58 | cout << "Input a finite decimal string like 1.414:" << endl; 59 | string str; 60 | cin >> str; 61 | Fraction d((char*)str.c_str()); 62 | cout << "d = " << d << endl; 63 | 64 | system("pause"); 65 | return 0; 66 | } -------------------------------------------------------------------------------- /8-5 Fraction/fraction/ReadMe.txt: -------------------------------------------------------------------------------- 1 | ======================================================================== 2 | 控制台应用程序:fraction 项目概述 3 | ======================================================================== 4 | 5 | 应用程序向导已为您创建了此 fraction 应用程序。 6 | 7 | 本文件概要介绍组成 fraction 应用程序的每个文件的内容。 8 | 9 | 10 | fraction.vcxproj 11 | 这是使用应用程序向导生成的 VC++ 项目的主项目文件,其中包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。 12 | 13 | fraction.vcxproj.filters 14 | 这是使用“应用程序向导”生成的 VC++ 项目筛选器文件。它包含有关项目文件与筛选器之间的关联信息。在 IDE 中,通过这种关联,在特定节点下以分组形式显示具有相似扩展名的文件。例如,“.cpp”文件与“源文件”筛选器关联。 15 | 16 | fraction.cpp 17 | 这是主应用程序源文件。 18 | 19 | ///////////////////////////////////////////////////////////////////////////// 20 | 其他标准文件: 21 | 22 | StdAfx.h, StdAfx.cpp 23 | 这些文件用于生成名为 fraction.pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。 24 | 25 | ///////////////////////////////////////////////////////////////////////////// 26 | 其他注释: 27 | 28 | 应用程序向导使用“TODO:”注释来指示应添加或自定义的源代码部分。 29 | 30 | ///////////////////////////////////////////////////////////////////////////// 31 | -------------------------------------------------------------------------------- /8-5 Fraction/fraction/fraction.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-5 Fraction/fraction/fraction.cpp -------------------------------------------------------------------------------- /8-5 Fraction/fraction/fraction.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-5 Fraction/fraction/fraction.exe -------------------------------------------------------------------------------- /8-5 Fraction/fraction/fraction.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef FRACTION_H 3 | #define FRACTION_H 4 | 5 | #include "stdafx.h" 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | class Fraction 13 | { 14 | private: 15 | int numerator;//fenzi 16 | int denominator;//fenmu 17 | public: 18 | Fraction(void) :numerator(0), denominator(1) {}; //default ctor 19 | Fraction(int a, int b) :numerator(a), denominator(b) //ctor takes two integers as parameters 20 | { 21 | if (denominator == 0) 22 | { 23 | throw exception("Denominator should not be zero"); 24 | } 25 | Simplify(); 26 | } 27 | static int gcd(int a, int b); //greatest common divisor 28 | void Simplify(); 29 | Fraction(const Fraction &p); //copy ctor 30 | ~Fraction() {}; 31 | 32 | //arithmetical operators : + - * / 33 | friend const Fraction operator+(const Fraction &x, const Fraction &y); 34 | friend const Fraction operator-(const Fraction &x, const Fraction &y); 35 | friend const Fraction operator*(const Fraction &x, const Fraction &y); 36 | friend const Fraction operator/(const Fraction &x, const Fraction &y); 37 | 38 | //relational operators : < <= == != >= > 39 | friend bool operator<(const Fraction &x, const Fraction &y); 40 | friend bool operator<=(const Fraction &x, const Fraction &y); 41 | friend bool operator==(const Fraction &x, const Fraction &y); 42 | friend bool operator!=(const Fraction &x, const Fraction &y); 43 | friend bool operator>=(const Fraction &x, const Fraction &y); 44 | friend bool operator>(const Fraction &x, const Fraction &y); 45 | 46 | //type cast to string 47 | string to_string() const; 48 | //type cast to double 49 | double to_double() const; 50 | 51 | //inserter and extractor for streams 52 | friend istream& operator >> (istream& is, Fraction& obj); 53 | friend ostream& operator<<(ostream& os, const Fraction& obj); 54 | 55 | //conversion from a finite decimal string like : 1.414 56 | Fraction(char *str); 57 | bool strFrac(char *str); 58 | char getFirstChar(char *str); //return the first non-numeric character in the string 59 | void getStrBeforeChar(char *str, char c, char *substr); //get the string before the decimal point 60 | void getStrAfterChar(char *str, char c, char *substr); //get the string after the decimal point 61 | bool checkErr(char *str); //check if the string contains non-numeric characters 62 | }; 63 | 64 | #endif // !FRACTION_H 65 | 66 | -------------------------------------------------------------------------------- /8-5 Fraction/fraction/fraction.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {40108FAC-281B-43BA-87D1-E76A620198BB} 23 | Win32Proj 24 | fraction 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Use 87 | Level3 88 | Disabled 89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 90 | true 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | Use 100 | Level3 101 | Disabled 102 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 103 | true 104 | 105 | 106 | Console 107 | true 108 | 109 | 110 | 111 | 112 | Level3 113 | Use 114 | MaxSpeed 115 | true 116 | true 117 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 118 | true 119 | 120 | 121 | Console 122 | true 123 | true 124 | true 125 | 126 | 127 | 128 | 129 | Level3 130 | Use 131 | MaxSpeed 132 | true 133 | true 134 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 135 | true 136 | 137 | 138 | Console 139 | true 140 | true 141 | true 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | Create 157 | Create 158 | Create 159 | Create 160 | 161 | 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /8-5 Fraction/fraction/fraction.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 头文件 23 | 24 | 25 | 头文件 26 | 27 | 28 | 头文件 29 | 30 | 31 | 32 | 33 | 源文件 34 | 35 | 36 | 源文件 37 | 38 | 39 | 源文件 40 | 41 | 42 | -------------------------------------------------------------------------------- /8-5 Fraction/fraction/stdafx.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-5 Fraction/fraction/stdafx.cpp -------------------------------------------------------------------------------- /8-5 Fraction/fraction/stdafx.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-5 Fraction/fraction/stdafx.h -------------------------------------------------------------------------------- /8-5 Fraction/fraction/targetver.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-5 Fraction/fraction/targetver.h -------------------------------------------------------------------------------- /8-6 Vector/README.md: -------------------------------------------------------------------------------- 1 | # 8-6 Vector (20 分) 2 | 3 | **Requirements** 4 | 5 | Given the declaration of a class template `Vector` below, write the bodies of the functions, and a main() to test all the facilities `Vector` provides. 6 | 7 | ```c++ 8 | template 9 | class Vector { 10 | public: 11 | Vector(); // creates an empty vector 12 | Vector(int size); // creates a vector for holding 'size' elements 13 | Vector(const Vector& r); // the copy ctor 14 | ~Vector(); // destructs the vector 15 | T& operator[](int index); // accesses the specified element without bounds checking 16 | T& at(int index); // accesses the specified element, throws an exception of type 'std::out_of_range' when index <0 or >=m_nSize 17 | int size() const; // return the size of the container 18 | void push_back(const T& x); // adds an element to the end 19 | void clear(); // clears the contents 20 | bool empty() const; // checks whether the container is empty 21 | private: 22 | void inflate(); // expand the storage of the container to a new capacity, e.g. 2*m_nCapacity 23 | T *m_pElements; // pointer to the dynamically allocated storage 24 | int m_nSize; // the number of elements in the container 25 | int m_nCapacity; // the number of elements that can be held in currently allocated storage 26 | }; 27 | ``` 28 | 29 | **Evaluation standard** 30 | 31 | 1. c++ code quality (clean, compact and reasonable) 32 | 2. comments quality 33 | 3. test coverage 34 | 35 | **Files to submit** 36 | 37 | Please prepare a .zip package including the following items: 38 | 39 | 1. the source code (including the Vector class, and various test cases for its usage) 40 | 2. makefile (for Mac or Linux users) or .exes (for Windows users) or CMakeLists.txt -------------------------------------------------------------------------------- /8-6 Vector/vector/ReadMe.txt: -------------------------------------------------------------------------------- 1 | ======================================================================== 2 | 控制台应用程序:vector 项目概述 3 | ======================================================================== 4 | 5 | 应用程序向导已为您创建了此 vector 应用程序。 6 | 7 | 本文件概要介绍组成 vector 应用程序的每个文件的内容。 8 | 9 | 10 | vector.vcxproj 11 | 这是使用应用程序向导生成的 VC++ 项目的主项目文件,其中包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。 12 | 13 | vector.vcxproj.filters 14 | 这是使用“应用程序向导”生成的 VC++ 项目筛选器文件。它包含有关项目文件与筛选器之间的关联信息。在 IDE 中,通过这种关联,在特定节点下以分组形式显示具有相似扩展名的文件。例如,“.cpp”文件与“源文件”筛选器关联。 15 | 16 | vector.cpp 17 | 这是主应用程序源文件。 18 | 19 | ///////////////////////////////////////////////////////////////////////////// 20 | 其他标准文件: 21 | 22 | StdAfx.h, StdAfx.cpp 23 | 这些文件用于生成名为 vector.pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。 24 | 25 | ///////////////////////////////////////////////////////////////////////////// 26 | 其他注释: 27 | 28 | 应用程序向导使用“TODO:”注释来指示应添加或自定义的源代码部分。 29 | 30 | ///////////////////////////////////////////////////////////////////////////// 31 | -------------------------------------------------------------------------------- /8-6 Vector/vector/stdafx.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-6 Vector/vector/stdafx.cpp -------------------------------------------------------------------------------- /8-6 Vector/vector/stdafx.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-6 Vector/vector/stdafx.h -------------------------------------------------------------------------------- /8-6 Vector/vector/targetver.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-6 Vector/vector/targetver.h -------------------------------------------------------------------------------- /8-6 Vector/vector/test.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "vector.h" 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int capacity; 9 | cout << "Input the capacity of vector:"; 10 | cin >> capacity; 11 | 12 | cout << "--------Test for Vector---------" << endl; 13 | Vector x1; 14 | Vector x2(capacity); 15 | Vector x3 = x2; 16 | int element; 17 | int val; 18 | cout << "Size of x1 = " << x1.size() << endl; 19 | cout << "Size of x2 = " << x2.size() << endl; 20 | cout << "Size of x3 = " << x3.size() << endl; 21 | cout << "x2 is empty? " << x2.empty() << endl; 22 | cout << "Push an element into x2:"; 23 | cin >> element; 24 | x2.push_back(element); 25 | cout << "x2 is empty? " << x2.empty() << endl; 26 | cout << "Size of x2 = " << x2.size() << endl; 27 | 28 | cout << "x2[0] = " << x2[0] << endl; 29 | try { 30 | cout << "x2.at(0) = " << x2.at(0); 31 | } 32 | catch (std::out_of_range & exc) 33 | { 34 | std::cerr << exc.what(); 35 | } 36 | cout << endl; 37 | 38 | cout << "x2[1] = " << x2[1] << endl; 39 | try { 40 | cout << "x2.at(1) = " << x2.at(1); 41 | } 42 | catch (std::out_of_range & exc) 43 | { 44 | std::cerr << exc.what(); 45 | } 46 | cout << endl; 47 | 48 | x2.clear(); 49 | cout << "Clear x2!" << endl; 50 | cout << "x2 is empty? " << x2.empty() << endl; 51 | 52 | cout << "--------Test for Vector---------" << endl; 53 | Vector y1; 54 | Vector y2(capacity); 55 | Vector y3 = y2; 56 | char element2; 57 | char val2; 58 | cout << "Size of y1 = " << y1.size() << endl; 59 | cout << "Size of y2 = " << y2.size() << endl; 60 | cout << "Size of y3 = " << y3.size() << endl; 61 | cout << "y2 is empty? " << y2.empty() << endl; 62 | cout << "Push an element into y2:"; 63 | cin >> element2; 64 | y2.push_back(element2); 65 | cout << "y2 is empty? " << y2.empty() << endl; 66 | cout << "Size of y2 = " << y2.size() << endl; 67 | 68 | cout << "y2[0] = " << y2[0] << endl; 69 | try { 70 | cout << "y2.at(0) = " << y2.at(0); 71 | } 72 | catch (std::out_of_range & exc) 73 | { 74 | std::cerr << exc.what(); 75 | } 76 | cout << endl; 77 | 78 | cout << "y2[1] = " << y2[1] << endl; 79 | try { 80 | cout << "y2.at(1) = " << y2.at(1); 81 | } 82 | catch (std::out_of_range & exc) 83 | { 84 | std::cerr << exc.what(); 85 | } 86 | cout << endl; 87 | 88 | y2.clear(); 89 | cout << "Clear y2!" << endl; 90 | cout << "y2 is empty? " << y2.empty() << endl; 91 | 92 | system("pause"); 93 | return 0; 94 | } -------------------------------------------------------------------------------- /8-6 Vector/vector/vector.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "vector.h" 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /8-6 Vector/vector/vector.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-6 Vector/vector/vector.exe -------------------------------------------------------------------------------- /8-6 Vector/vector/vector.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-6 Vector/vector/vector.h -------------------------------------------------------------------------------- /8-6 Vector/vector/vector.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {9F5C8299-831D-41C6-998E-1BC463C23E62} 23 | Win32Proj 24 | vector 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Use 87 | Level3 88 | Disabled 89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 90 | true 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | Use 100 | Level3 101 | Disabled 102 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 103 | true 104 | 105 | 106 | Console 107 | true 108 | 109 | 110 | 111 | 112 | Level3 113 | Use 114 | MaxSpeed 115 | true 116 | true 117 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 118 | true 119 | 120 | 121 | Console 122 | true 123 | true 124 | true 125 | 126 | 127 | 128 | 129 | Level3 130 | Use 131 | MaxSpeed 132 | true 133 | true 134 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 135 | true 136 | 137 | 138 | Console 139 | true 140 | true 141 | true 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | Create 155 | Create 156 | Create 157 | Create 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /8-6 Vector/vector/vector.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 头文件 23 | 24 | 25 | 头文件 26 | 27 | 28 | 头文件 29 | 30 | 31 | 32 | 33 | 源文件 34 | 35 | 36 | 源文件 37 | 38 | 39 | 源文件 40 | 41 | 42 | -------------------------------------------------------------------------------- /8-7 STL allocator + memory pool (group work)/Figure1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-7 STL allocator + memory pool (group work)/Figure1.png -------------------------------------------------------------------------------- /8-7 STL allocator + memory pool (group work)/MyAllocator/code/TestMyAllocator.cpp: -------------------------------------------------------------------------------- 1 | // testallocator.cpp : 定义控制台应用程序的入口点。 2 | // 3 | 4 | #include "myallocator.h" 5 | #include 6 | #include 7 | #include 8 | 9 | #define INT 1 10 | #define FLOAT 2 11 | #define DOUBLE 3 12 | #define CLASS 4 13 | 14 | using namespace std; 15 | 16 | class vecWrapper 17 | { 18 | public: 19 | vecWrapper() { 20 | m_pVec = NULL; 21 | m_type = INT; 22 | } 23 | virtual ~vecWrapper() { 24 | } 25 | public: 26 | void setPointer(int type, void *pVec) { m_type = type; m_pVec = pVec; } 27 | virtual void visit(int index) = 0; 28 | virtual int size() = 0; 29 | virtual void resize(int size) = 0; 30 | virtual bool checkElement(int index, void *value) = 0; 31 | virtual void setElement(int idex, void *value) = 0; 32 | protected: 33 | int m_type; 34 | void *m_pVec; 35 | }; 36 | 37 | template 38 | class vecWrapperT : public vecWrapper 39 | { 40 | public: 41 | vecWrapperT(int type, std::vector > *pVec) 42 | { 43 | m_type = type; 44 | m_pVec = pVec; 45 | } 46 | virtual ~vecWrapperT() { 47 | if (m_pVec) 48 | delete ((std::vector > *)m_pVec); 49 | } 50 | public: 51 | virtual void visit(int index) 52 | { 53 | T temp = (*(std::vector > *)m_pVec)[index]; 54 | } 55 | virtual int size() 56 | { 57 | return ((std::vector > *)m_pVec)->size(); 58 | } 59 | virtual void resize(int size) 60 | { 61 | ((std::vector > *)m_pVec)->resize(size); 62 | } 63 | virtual bool checkElement(int index, void *pValue) 64 | { 65 | T temp = (*(std::vector > *)m_pVec)[index]; 66 | if (temp == (*((T *)pValue))) 67 | return true; 68 | else 69 | return false; 70 | } 71 | 72 | virtual void setElement(int index, void *value) 73 | { 74 | (*(std::vector > *)m_pVec)[index] = *((T *)value); 75 | } 76 | }; 77 | 78 | class myObject 79 | { 80 | public: 81 | myObject() : m_X(0), m_Y(0) {} 82 | myObject(int t1, int t2) :m_X(t1), m_Y(t2) {} 83 | myObject(const myObject &rhs) { m_X = rhs.m_X; m_Y = rhs.m_Y; } 84 | ~myObject() { /*std::cout << "my object destructor called" << std::endl;*/ } 85 | bool operator == (const myObject &rhs) 86 | { 87 | if ((rhs.m_X == m_X) && (rhs.m_Y == m_Y)) 88 | return true; 89 | else 90 | return false; 91 | } 92 | protected: 93 | int m_X; 94 | int m_Y; 95 | }; 96 | 97 | #define TESTSIZE 10000 98 | #define PICKSIZE 1000 99 | 100 | int main() 101 | { 102 | vecWrapper **testVec; 103 | testVec = new vecWrapper*[TESTSIZE]; 104 | clock_t start, stop;//the start and end of the runtime 105 | double runtime; 106 | 107 | int tIndex, tSize; 108 | std::cout << ">>>Test MyAllocator" << std::endl; 109 | //test allocator 110 | start = clock(); 111 | for (int i = 0; i < TESTSIZE - 4; i++) 112 | { 113 | tSize = (int)((float)rand() / (float)RAND_MAX * 10000); 114 | vecWrapperT *pNewVec = new vecWrapperT(INT, new std::vector>(tSize)); 115 | testVec[i] = (vecWrapper *)pNewVec; 116 | } 117 | 118 | for (int i = 0; i < 4; i++) 119 | { 120 | tSize = (int)((float)rand() / (float)RAND_MAX * 10000); 121 | vecWrapperT *pNewVec = new vecWrapperT(CLASS, new std::vector>(tSize)); 122 | testVec[TESTSIZE - 4 + i] = (vecWrapper *)pNewVec; 123 | } 124 | stop = clock(); 125 | runtime = (double)(stop - start) / CLOCKS_PER_SEC; 126 | std::cout << "The runtime of " << TESTSIZE << " allocate operations is " << runtime << " s" << endl; 127 | 128 | //test resize 129 | start = clock(); 130 | for (int i = 0; i < PICKSIZE; i++) 131 | { 132 | tIndex = (int)((float)rand() / (float)RAND_MAX * (float)TESTSIZE); 133 | tSize = (int)((float)rand() / (float)RAND_MAX * (float)TESTSIZE); 134 | testVec[tIndex]->resize(tSize); 135 | } 136 | stop = clock(); 137 | runtime = (double)(stop - start) / CLOCKS_PER_SEC; 138 | std::cout << "The runtime of " << PICKSIZE << " resize operations is " << runtime << " s" << endl; 139 | 140 | //test assignment 141 | start = clock(); 142 | tIndex = (int)((float)rand() / (float)RAND_MAX * (TESTSIZE - 4 - 1)); 143 | int tIntValue = 10; 144 | testVec[tIndex]->setElement(testVec[tIndex]->size() / 2, &tIntValue); 145 | if (!testVec[tIndex]->checkElement(testVec[tIndex]->size() / 2, &tIntValue)) 146 | std::cout << "incorrect assignment in vector %d\n" << tIndex << std::endl; 147 | 148 | tIndex = TESTSIZE - 4 + 3; 149 | myObject tObj(11, 15); 150 | testVec[tIndex]->setElement(testVec[tIndex]->size() / 2, &tObj); 151 | if (!testVec[tIndex]->checkElement(testVec[tIndex]->size() / 2, &tObj)) 152 | std::cout << "incorrect assignment in vector %d\n" << tIndex << std::endl; 153 | 154 | myObject tObj1(13, 20); 155 | testVec[tIndex]->setElement(testVec[tIndex]->size() / 2, &tObj1); 156 | if (!testVec[tIndex]->checkElement(testVec[tIndex]->size() / 2, &tObj1)) 157 | std::cout << "incorrect assignment in vector " << tIndex << " for object (13,20)" << std::endl; 158 | 159 | stop = clock(); 160 | runtime = (double)(stop - start) / CLOCKS_PER_SEC; 161 | std::cout << "The runtime of assignment operations is " << runtime << " s" << endl; 162 | 163 | std::cout << "new and other operation OK!" << endl; 164 | 165 | for (int i = 0; i < TESTSIZE; i++) 166 | delete testVec[i]; 167 | std::cout << "circle delete OK!" << endl; 168 | delete[]testVec; 169 | 170 | std::cout <<"delete OK!"<< endl; 171 | system("pause"); 172 | return 0; 173 | 174 | } 175 | 176 | -------------------------------------------------------------------------------- /8-7 STL allocator + memory pool (group work)/MyAllocator/code/TestWithFile.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-7 STL allocator + memory pool (group work)/MyAllocator/code/TestWithFile.cpp -------------------------------------------------------------------------------- /8-7 STL allocator + memory pool (group work)/MyAllocator/code/generate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int TESTSIZE, PICKSIZE; 10 | int tIndex, tSize; 11 | int type; 12 | string stype; 13 | string filename; 14 | cout << "Input # for quit." << endl; 15 | while(1) 16 | { 17 | cout << "Input the filename: "; 18 | cin >> filename; 19 | if(!strcmp(filename.c_str(), "#")) break; 20 | cout << "Input the number of vectors to be created: "; 21 | cin >> TESTSIZE; 22 | cout << "Input the number of vectors to be resized: "; 23 | cin >> PICKSIZE; 24 | 25 | ofstream fout(filename.c_str(), ios::out); 26 | if (!fout)//test if the file is opened successfully 27 | { 28 | cout << "Error opening file." << endl; 29 | return 0; 30 | } 31 | fout << TESTSIZE << endl; 32 | for (int i = 0; i < TESTSIZE; i++) 33 | { 34 | tSize = (int)((float)rand() / (float)RAND_MAX * 10000); 35 | type = (int)((float)rand() / (float)RAND_MAX * 4) + 1; 36 | switch (type) 37 | { 38 | case 1: stype = "int"; break; 39 | case 2: stype = "float"; break; 40 | case 3: stype = "double"; break; 41 | case 4: stype = "class"; break; 42 | default: break; 43 | } 44 | fout << tSize << " " << stype << endl; 45 | } 46 | for (int i = 0; i < PICKSIZE; i++) 47 | { 48 | tIndex = (int)((float)rand() / (float)RAND_MAX * 10000); 49 | tSize = (int)((float)rand() / (float)RAND_MAX * 10000); 50 | if(i == PICKSIZE - 1) fout << tIndex << " " << tSize; 51 | else fout << tIndex << " " << tSize << endl; 52 | } 53 | fout.close(); 54 | cout << "Finish successfully!" << endl; 55 | } 56 | system("pause"); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /8-7 STL allocator + memory pool (group work)/MyAllocator/code/myallocator.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-7 STL allocator + memory pool (group work)/MyAllocator/code/myallocator.h -------------------------------------------------------------------------------- /8-7 STL allocator + memory pool (group work)/MyAllocator/executable file/TestMyAllocator/MyAllocator.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-7 STL allocator + memory pool (group work)/MyAllocator/executable file/TestMyAllocator/MyAllocator.exe -------------------------------------------------------------------------------- /8-7 STL allocator + memory pool (group work)/MyAllocator/executable file/TestWithFile/MyAllocator.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-7 STL allocator + memory pool (group work)/MyAllocator/executable file/TestWithFile/MyAllocator.exe -------------------------------------------------------------------------------- /8-7 STL allocator + memory pool (group work)/MyAllocator/executable file/generate.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-7 STL allocator + memory pool (group work)/MyAllocator/executable file/generate.exe -------------------------------------------------------------------------------- /8-7 STL allocator + memory pool (group work)/README.md: -------------------------------------------------------------------------------- 1 | # 8-7 STL allocator + memory pool (60 分) 2 | 3 | **STL Allocator Interface** 4 | 5 | An allocator is used by standard library containers as a template parameter : 6 | 7 | ```c++ 8 | template < class T, class Alloc = allocator > class vector; 9 | template < class T, class Alloc = allocator > class list; 10 | ``` 11 | 12 | What does an `allocator` class have? Typically, it possesses: 13 | 14 | ```c++ 15 | typedef void _Not_user_specialized; 16 | typedef _Ty value_type; 17 | typedef value_type *pointer; 18 | typedef const value_type *const_pointer; 19 | typedef value_type& reference; 20 | typedef const value_type& const_reference; 21 | typedef size_t size_type; 22 | typedef ptrdiff_t difference_type; 23 | typedef true_type propagate_on_container_move_assignment; 24 | typedef true_type is_always_equal; 25 | 26 | pointer address(reference _Val) const _NOEXCEPT 27 | const_pointer address(const_reference _Val) const _NOEXCEPT 28 | void deallocate(pointer _Ptr, size_type _Count) 29 | _DECLSPEC_ALLOCATOR pointer allocate(size_type _Count) 30 | template void destroy(_Uty *_Ptr) 31 | template 32 | void construct(_Objty *_Ptr, _Types&&... _Args) 33 | ``` 34 | 35 | The above interface is just shown for illustration, please refer to [std::allocator](https://en.cppreference.com/w/cpp/memory/allocator) for the latest specification. 36 | 37 | **Memory Pool** 38 | 39 | STL provides you a default [std::allocator](https://en.cppreference.com/w/cpp/memory/allocator), but you can implement your own to replace it. For example, you can design a memory pool to speed up the dynamic allocation of a large number of small blocks (e.g., 8 bytes, 16 bytes, ...), and to reduce memory fragmentation. 40 | 41 | ![img](Figure1.png) 42 | 43 | Figure 1: Mem pool using block based allocation strategy. 44 | 45 | **Requirements** 46 | 47 | - Two people as a group to finish this project (don't forget to write down the group member names, anyone of the two can submit the final package on PTA). 48 | - Implement your own memory allocator for STL vector. 49 | - The allocator should optimize the memory allocation speed using memory pool. 50 | - The allocator should support arbitrary memory size allocation request. 51 | 52 | **How to Test Your Allocator** 53 | 54 | Basically, you should: 55 | 56 | 1. Create more than ten thousand vectors with different number of elements. 57 | 2. Pick up 1000 random vectors and resize the vectors with random sizes. 58 | 3. Release all the vectors. 59 | 60 | Feel free to extend the following code skeleton for your own tests: 61 | 62 | ```c++ 63 | #include 64 | #include 65 | #include 66 | 67 | // include header of your allocator here 68 | template 69 | using MyAllocator = std::allocator; // replace the std::allocator with your allocator 70 | using Point2D = std::pair; 71 | 72 | const int TestSize = 10000; 73 | const int PickSize = 1000; 74 | 75 | int main() 76 | { 77 | std::random_device rd; 78 | std::mt19937 gen(rd()); 79 | std::uniform_int_distribution<> dis(1, TestSize); 80 | 81 | // vector creation 82 | using IntVec = std::vector>; 83 | std::vector> vecints(TestSize); 84 | for (int i = 0; i < TestSize; i++) 85 | vecints[i].resize(dis(gen)); 86 | 87 | using PointVec = std::vector>; 88 | std::vector> vecpts(TestSize); 89 | for (int i = 0; i < TestSize; i++) 90 | vecpts[i].resize(dis(gen)); 91 | 92 | // vector resize 93 | for (int i = 0; i < PickSize; i++) 94 | { 95 | int idx = dis(gen) - 1; 96 | int size = dis(gen); 97 | vecints[idx].resize(size); 98 | vecpts[idx].resize(size); 99 | } 100 | 101 | // vector element assignment 102 | { 103 | int val = 10; 104 | int idx1 = dis(gen) - 1; 105 | int idx2 = vecints[idx1].size() / 2; 106 | vecints[idx1][idx2] = val; 107 | if (vecints[idx1][idx2] == val) 108 | std::cout << "correct assignment in vecints: " << idx1 << std::endl; 109 | else 110 | std::cout << "incorrect assignment in vecints: " << idx1 << std::endl; 111 | } 112 | { 113 | Point2D val(11, 15); 114 | int idx1 = dis(gen) - 1; 115 | int idx2 = vecpts[idx1].size() / 2; 116 | vecpts[idx1][idx2] = val; 117 | if (vecpts[idx1][idx2] == val) 118 | std::cout << "correct assignment in vecpts: " << idx1 << std::endl; 119 | else 120 | std::cout << "incorrect assignment in vecpts: " << idx1 << std::endl; 121 | } 122 | 123 | return 0; 124 | } 125 | ``` 126 | 127 | **Evaluation Standard** 128 | 129 | 1. c++ code quality (clean, compact and reasonable) 130 | 2. comments quality 131 | 3. correctness and running performance of the allocator 132 | 133 | **Files to Submit** 134 | 135 | Please prepare a .zip package including the following items: 136 | 137 | 1. the source code (including the testing code) 138 | 2. makefile (for Mac or Linux users) or .exes (for Windows users, with necessary .dlls if you use MinGW) or CMakeLists.txt -------------------------------------------------------------------------------- /8-7 STL allocator + memory pool (group work)/STL Allocator + Mem pool + 大作业要求.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyjjj/OOP/e4e8121a078a7732da235e9a1efb970e54f6b7bf/8-7 STL allocator + memory pool (group work)/STL Allocator + Mem pool + 大作业要求.pptx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OOP 2 | 2019-2020 ZJU OOP Projects 3 | --------------------------------------------------------------------------------