diff --git a/CMakeLists.txt b/CMakeLists.txt index e357262..4475835 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(MSTL) set(CMAKE_CXX_STANDARD 14) -add_executable(MSTL main.cpp h/String.h h/LinkList.h) +add_executable(MSTL main.cpp h/String.h h/List.h h/Singleton.h h/Faction.h) diff --git a/cmake-build-debug/.cmake/api/v1/reply/codemodel-v2-05830bec4f0034487f3b.json b/cmake-build-debug/.cmake/api/v1/reply/codemodel-v2-05830bec4f0034487f3b.json deleted file mode 100644 index 6287e03..0000000 --- a/cmake-build-debug/.cmake/api/v1/reply/codemodel-v2-05830bec4f0034487f3b.json +++ /dev/null @@ -1,60 +0,0 @@ -{ - "configurations" : - [ - { - "directories" : - [ - { - "build" : ".", - "jsonFile" : "directory-.-Debug-f5ebdc15457944623624.json", - "minimumCMakeVersion" : - { - "string" : "3.23" - }, - "projectIndex" : 0, - "source" : ".", - "targetIndexes" : - [ - 0 - ] - } - ], - "name" : "Debug", - "projects" : - [ - { - "directoryIndexes" : - [ - 0 - ], - "name" : "MSTL", - "targetIndexes" : - [ - 0 - ] - } - ], - "targets" : - [ - { - "directoryIndex" : 0, - "id" : "MSTL::@6890427a1f51a3e7e1df", - "jsonFile" : "target-MSTL-Debug-7d2d0f05e169997e8a2f.json", - "name" : "MSTL", - "projectIndex" : 0 - } - ] - } - ], - "kind" : "codemodel", - "paths" : - { - "build" : "/home/dongl/code/MSTL/cmake-build-debug", - "source" : "/home/dongl/code/MSTL" - }, - "version" : - { - "major" : 2, - "minor" : 4 - } -} diff --git a/cmake-build-debug/.cmake/api/v1/reply/index-2022-09-25T14-07-17-0281.json b/cmake-build-debug/.cmake/api/v1/reply/index-2022-09-25T14-07-17-0281.json deleted file mode 100644 index 9d1947d..0000000 --- a/cmake-build-debug/.cmake/api/v1/reply/index-2022-09-25T14-07-17-0281.json +++ /dev/null @@ -1,108 +0,0 @@ -{ - "cmake" : - { - "generator" : - { - "multiConfig" : false, - "name" : "Ninja" - }, - "paths" : - { - "cmake" : "/home/dongl/app/ide/clion/clion-2022.2.1/bin/cmake/linux/bin/cmake", - "cpack" : "/home/dongl/app/ide/clion/clion-2022.2.1/bin/cmake/linux/bin/cpack", - "ctest" : "/home/dongl/app/ide/clion/clion-2022.2.1/bin/cmake/linux/bin/ctest", - "root" : "/home/dongl/app/ide/clion/clion-2022.2.1/bin/cmake/linux/share/cmake-3.23" - }, - "version" : - { - "isDirty" : false, - "major" : 3, - "minor" : 23, - "patch" : 2, - "string" : "3.23.2", - "suffix" : "" - } - }, - "objects" : - [ - { - "jsonFile" : "codemodel-v2-05830bec4f0034487f3b.json", - "kind" : "codemodel", - "version" : - { - "major" : 2, - "minor" : 4 - } - }, - { - "jsonFile" : "cache-v2-471ef0fba02a766483a6.json", - "kind" : "cache", - "version" : - { - "major" : 2, - "minor" : 0 - } - }, - { - "jsonFile" : "cmakeFiles-v1-7c70437388afc6a20846.json", - "kind" : "cmakeFiles", - "version" : - { - "major" : 1, - "minor" : 0 - } - }, - { - "jsonFile" : "toolchains-v1-d06480eadcc461d85546.json", - "kind" : "toolchains", - "version" : - { - "major" : 1, - "minor" : 0 - } - } - ], - "reply" : - { - "cache-v2" : - { - "jsonFile" : "cache-v2-471ef0fba02a766483a6.json", - "kind" : "cache", - "version" : - { - "major" : 2, - "minor" : 0 - } - }, - "cmakeFiles-v1" : - { - "jsonFile" : "cmakeFiles-v1-7c70437388afc6a20846.json", - "kind" : "cmakeFiles", - "version" : - { - "major" : 1, - "minor" : 0 - } - }, - "codemodel-v2" : - { - "jsonFile" : "codemodel-v2-05830bec4f0034487f3b.json", - "kind" : "codemodel", - "version" : - { - "major" : 2, - "minor" : 4 - } - }, - "toolchains-v1" : - { - "jsonFile" : "toolchains-v1-d06480eadcc461d85546.json", - "kind" : "toolchains", - "version" : - { - "major" : 1, - "minor" : 0 - } - } - } -} diff --git a/cmake-build-debug/.cmake/api/v1/reply/target-MSTL-Debug-7d2d0f05e169997e8a2f.json b/cmake-build-debug/.cmake/api/v1/reply/target-MSTL-Debug-7d2d0f05e169997e8a2f.json deleted file mode 100644 index 89daca4..0000000 --- a/cmake-build-debug/.cmake/api/v1/reply/target-MSTL-Debug-7d2d0f05e169997e8a2f.json +++ /dev/null @@ -1,120 +0,0 @@ -{ - "artifacts" : - [ - { - "path" : "MSTL" - } - ], - "backtrace" : 1, - "backtraceGraph" : - { - "commands" : - [ - "add_executable" - ], - "files" : - [ - "CMakeLists.txt" - ], - "nodes" : - [ - { - "file" : 0 - }, - { - "command" : 0, - "file" : 0, - "line" : 6, - "parent" : 0 - } - ] - }, - "compileGroups" : - [ - { - "compileCommandFragments" : - [ - { - "fragment" : "-g" - }, - { - "fragment" : "-std=gnu++14" - } - ], - "language" : "CXX", - "languageStandard" : - { - "backtraces" : - [ - 1 - ], - "standard" : "14" - }, - "sourceIndexes" : - [ - 0 - ] - } - ], - "id" : "MSTL::@6890427a1f51a3e7e1df", - "link" : - { - "commandFragments" : - [ - { - "fragment" : "-g", - "role" : "flags" - }, - { - "fragment" : "", - "role" : "flags" - } - ], - "language" : "CXX" - }, - "name" : "MSTL", - "nameOnDisk" : "MSTL", - "paths" : - { - "build" : ".", - "source" : "." - }, - "sourceGroups" : - [ - { - "name" : "Source Files", - "sourceIndexes" : - [ - 0 - ] - }, - { - "name" : "Header Files", - "sourceIndexes" : - [ - 1, - 2 - ] - } - ], - "sources" : - [ - { - "backtrace" : 1, - "compileGroupIndex" : 0, - "path" : "main.cpp", - "sourceGroupIndex" : 0 - }, - { - "backtrace" : 1, - "path" : "h/String.h", - "sourceGroupIndex" : 1 - }, - { - "backtrace" : 1, - "path" : "h/LinkList.h", - "sourceGroupIndex" : 1 - } - ], - "type" : "EXECUTABLE" -} diff --git a/cmake-build-debug/.ninja_deps b/cmake-build-debug/.ninja_deps index 98cc5ff..bcc653f 100644 Binary files a/cmake-build-debug/.ninja_deps and b/cmake-build-debug/.ninja_deps differ diff --git a/cmake-build-debug/.ninja_log b/cmake-build-debug/.ninja_log index acbd072..d5e96a5 100644 --- a/cmake-build-debug/.ninja_log +++ b/cmake-build-debug/.ninja_log @@ -1,23 +1,3 @@ # ninja log v5 -0 176 1664114584453149321 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52 -176 227 1664114584501149051 MSTL 5fce564c316cd3aa -0 196 1664115258161179799 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52 -196 250 1664115258213179483 MSTL 5fce564c316cd3aa -1 206 1664115415616221673 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52 -206 253 1664115415664221380 MSTL 5fce564c316cd3aa -0 195 1664115434932103707 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52 -195 247 1664115434984103389 MSTL 5fce564c316cd3aa -0 186 1664115539947460993 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52 -186 238 1664115539995460699 MSTL 5fce564c316cd3aa -0 185 1664115559843338991 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52 -185 234 1664115559891338697 MSTL 5fce564c316cd3aa -0 187 1664115711810405059 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52 -187 236 1664115711858404763 MSTL 5fce564c316cd3aa -0 183 1664115718762362256 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52 -183 233 1664115718810361961 MSTL 5fce564c316cd3aa -0 180 1664115743494209937 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52 -180 229 1664115743542209642 MSTL 5fce564c316cd3aa -0 181 1664115749406173515 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52 -181 230 1664115749454173219 MSTL 5fce564c316cd3aa -1 192 1664116226535223733 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52 -192 243 1664116226587223410 MSTL 5fce564c316cd3aa +0 188 1664284334515694444 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52 +188 243 1664284334567693897 MSTL 5fce564c316cd3aa diff --git a/cmake-build-debug/CMakeFiles/MSTL.dir/main.cpp.o b/cmake-build-debug/CMakeFiles/MSTL.dir/main.cpp.o index fec3019..9b5a22c 100644 Binary files a/cmake-build-debug/CMakeFiles/MSTL.dir/main.cpp.o and b/cmake-build-debug/CMakeFiles/MSTL.dir/main.cpp.o differ diff --git a/cmake-build-debug/MSTL b/cmake-build-debug/MSTL index a130e4a..5757a65 100755 Binary files a/cmake-build-debug/MSTL and b/cmake-build-debug/MSTL differ diff --git a/cmake-build-debug/Testing/Temporary/LastTest.log b/cmake-build-debug/Testing/Temporary/LastTest.log index 9364158..7978312 100644 --- a/cmake-build-debug/Testing/Temporary/LastTest.log +++ b/cmake-build-debug/Testing/Temporary/LastTest.log @@ -1,3 +1,3 @@ -Start testing: Sep 25 22:30 CST +Start testing: Sep 27 21:14 CST ---------------------------------------------------------- -End testing: Sep 25 22:30 CST +End testing: Sep 27 21:14 CST diff --git a/h/Faction.h b/h/Faction.h new file mode 100644 index 0000000..b820d9f --- /dev/null +++ b/h/Faction.h @@ -0,0 +1,21 @@ +// +// Created by dongl on 22-9-27. +// + +#ifndef MSTL_FACTION_H +#define MSTL_FACTION_H + +#endif //MSTL_FACTION_H + +// 转换函数 重载 +class Faction{ +public: + Faction(int num, int den = 1):m_numerator(num),m_denominator(den){} + + operator double() const{ + return (double) m_numerator / m_denominator; + } +private: + int m_numerator; // 分子 + int m_denominator; // 分母 +}; \ No newline at end of file diff --git a/h/LinkList.h b/h/List.h similarity index 57% rename from h/LinkList.h rename to h/List.h index 0b5b36c..3f5d023 100644 --- a/h/LinkList.h +++ b/h/List.h @@ -8,38 +8,40 @@ #include #endif //CPP_LINKLIST_H - +template class LNode{ - - friend class Link; - public: - int data = 0x0; -private: - LNode *next = nullptr; - void update(int el){ + U data; + + LNode *next = nullptr; + + LNode(): next(nullptr){ + + } + void update(U el){ data = el; } }; -class Link{ - LNode *lNode = (LNode*) new LNode; +template +class List{ + LNode *lNode = new LNode; unsigned int _length = 0; - LNode* _begin = lNode; + LNode* _begin = lNode; public: - Link(){ + List(){ } - void add(int el){ - lNode = lNode->next = new LNode; + void add(T el){ + lNode = lNode->next = new LNode; lNode->data = el; _length++; - lNode->next = new LNode; + lNode->next = new LNode; }; - int get(int index){ + int get(T index){ int el; - LNode* node = _begin; + LNode* node = _begin; for (int i = 0; i < index; ++i) { node = node->next; el = node->data; @@ -49,21 +51,22 @@ public: void remove(unsigned int index){ if(index > _length+1) - std::cout << "超越范围之外" << std::endl; + std::cerr << "超越范围之外:" << __FILE__ << + ":" <<__LINE__ <* begin = _begin; for (int i = 1; i < index; ++i) { begin = _begin->next; } - LNode* node = begin->next; + LNode* node = begin->next; begin->next = begin->next->next; delete node; _length--; } - void update(int index, int el){ - LNode* node; + void update(int index, T el){ + LNode* node; for (int i = 0; i < index; ++i) { node = _begin->next; } @@ -74,7 +77,7 @@ public: return _length; } - LNode*& begin(){ + LNode*& begin(){ return _begin->next; } }; diff --git a/h/Singleton.h b/h/Singleton.h new file mode 100644 index 0000000..3f4bad9 --- /dev/null +++ b/h/Singleton.h @@ -0,0 +1,26 @@ +// +// Created by dongl on 22-9-26. +// + +#ifndef MSTL_SINGLETON_H +#define MSTL_SINGLETON_H + +#endif //MSTL_SINGLETON_H + + +/// 设计模式 单链 单体 单键 +class A{ +public: + static A& getInstance(); + void setup(){ + + } +private: + A(); + A(const A& rhs); +}; + +A& A::getInstance() { + static A a; + return a; +} diff --git a/h/String.h b/h/String.h index 247b3fc..45b2038 100644 --- a/h/String.h +++ b/h/String.h @@ -61,7 +61,13 @@ String::String(const String& str) { } inline -String &String::operator=(const String &str) { +String &String::operator=(const String& str) { // String& 引用 + // 拷贝赋值 是否是自我赋值 + // 来源端与目的端是否相同 指针对比地址 &str取地址 + if(this == &str){ + return *this; + } + // 目的端 本来存在 delete[] m_data; // 在重新申请一块空间 堆 diff --git a/main.cpp b/main.cpp index 73f83ab..5b3f538 100644 --- a/main.cpp +++ b/main.cpp @@ -1,13 +1,11 @@ #include -#include "h/LinkList.h" +#include "h/List.h" #include "h/String.h" +#include "h/Faction.h" int main() { - String string("123456"); - // 赋值 重载函数 返回了 *this - // 这使得以 目的值具有 对象的this - // 更改的值 不在函数内 而是申请的堆中 - String str = string; - std::cout << str.get_c_str() << std::endl; + double f = *new Faction(7,5); + + std::cout << f << std::endl; return 0; }