From 002bec597f12f66a72412d90404437ff3e6abe79 Mon Sep 17 00:00:00 2001 From: sparklesea Date: Mon, 15 Jul 2024 04:59:00 +0000 Subject: [PATCH 1/6] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=AC=AC=E4=B8=83?= =?UTF-8?q?=E7=AB=A0=E4=B9=A0=E9=A2=987.10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\351\273\204\345\261\261/.gitignore" | 2 ++ .../chapter7/CMakeLists.txt" | 5 +++ .../chapter7/src/7-10.cpp" | 33 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/.gitignore" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/chapter7/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/chapter7/src/7-10.cpp" diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/.gitignore" "b/C++/codes/2024-07/\351\273\204\345\261\261/.gitignore" new file mode 100644 index 0000000..1cb8379 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/.gitignore" @@ -0,0 +1,2 @@ +bin/ +build/ \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/CMakeLists.txt" new file mode 100644 index 0000000..ebf35c2 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/CMakeLists.txt" @@ -0,0 +1,5 @@ +PROJECT(exercises_7_10) +SET(SRC_LIST src/7-10.cpp) +ADD_EXECUTABLE(exercise_7_10 ${SRC_LIST}) + +set_target_properties(exercise_7_10 PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin") diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/src/7-10.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/src/7-10.cpp" new file mode 100644 index 0000000..1278b22 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/src/7-10.cpp" @@ -0,0 +1,33 @@ +#include + +using namespace std; + +double add(double x, double y){ + return x + y; +} + +double multiply(double x, double y){ + return x * y; +} + +double calculate(double x, double y, double (*pf)(double, double)){ + return pf(x, y); +} + +int main(int argc, char **argv){ + double x, y; + while (true) + { + std::cout << "请输入两个数字(用空格分隔): "; + if(!(cin>>x>>y)){ + cout << "输入格式错误,退出" << endl; + break; + } + + double sum = calculate(x, y, add); + double product = calculate(x, y, multiply); + cout << x << "+" << y << "=" << sum << endl; + cout << x << "*" << y << "=" << product << endl; + } + return 0; +} \ No newline at end of file -- Gitee From abff818be97ae53812125da52d47128789774850 Mon Sep 17 00:00:00 2001 From: sparklesea Date: Mon, 15 Jul 2024 14:06:12 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=AC=AC=E5=85=AB?= =?UTF-8?q?=E7=AB=A0=E4=B9=A0=E9=A2=988-4,8-6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chapter8/CMakeLists.txt" | 19 ++++++++ .../chapter8/src/8-4.cpp" | 48 +++++++++++++++++++ .../chapter8/src/8-6.cpp" | 40 ++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/chapter8/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-4.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-6.cpp" diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/CMakeLists.txt" new file mode 100644 index 0000000..53ac84f --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/CMakeLists.txt" @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.10) +project(chapter8) + +set(SOURCE_DIR ${CMAKE_SOURCE_DIR}/src) +set(BINARY_DIR ${CMAKE_SOURCE_DIR}/bin) + +file(MAKE_DIRECTORY ${BINARY_DIR}) + +file(GLOB SOURCES "${SOURCE_DIR}/*.cpp") + +foreach(SOURCE_FILE ${SOURCES}) + get_filename_component(FILENAME ${SOURCE_FILE} NAME_WE) + + add_executable("exercise_${FILENAME}" ${SOURCE_FILE}) + + set_target_properties("exercise_${FILENAME}" PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${BINARY_DIR} + ) +endforeach() diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-4.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-4.cpp" new file mode 100644 index 0000000..b34f61d --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-4.cpp" @@ -0,0 +1,48 @@ +#include +#include +using namespace std; + +struct stringy +{ + char *str; //points to a string + int ct; //lenght of string(not counting '\0') +}; + +void set(stringy& to_string, char* from_string){ + int length_str = 0; + while (from_string[length_str] != '\0'){ + length_str++; + } + char *copy_string = new char[length_str + 1]; + for (int i = 0; i < length_str+1;++i){ + copy_string[i] = from_string[i]; + } + to_string.str = copy_string; + to_string.ct = length_str; +} + +void show(stringy &string, int count=1){ + for (int i = 0; i < count;++i){ + cout << string.str << endl; + } +} + +void show(char *string, int count=1){ + for (int i = 0; i < count;++i){ + cout << string << endl; + } +} + +int main(){ + stringy beany; + char testing[] = "Reality isn't what it used to be."; + set(beany, testing); + show(beany); + show(beany, 2); + testing[0] = 'D'; + testing[1] = 'u'; + show(testing); + show(testing, 3); + show("Done!"); + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-6.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-6.cpp" new file mode 100644 index 0000000..ebb76ec --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-6.cpp" @@ -0,0 +1,40 @@ +#include +#include +using namespace std; + +template +T maxn(T *nums, int counts){ + int max_i = 0; + for (int i = 1; i < counts;++i){ + if(nums[i] > nums[max_i]){ + max_i = i; + } + } + return nums[max_i]; +} + +template<> +char* maxn(char** strings, int counts){ + int max_i = 0; + for (int i = 1; i < counts;++i){ + if(strlen(strings[i]) > strlen(strings[i])){ + max_i = i; + } + } + return strings[max_i]; +} + +int main(int argc, char** argv){ + int int_arrays[6] = {0, 2, 3, 1, 5, 4}; + double double_arrays[4] = {0.5, -1.4, 4.5, 3.9}; + char *string_arrays[5] = {"hello", "world", "str", "bool", "int"}; + + int max_int = maxn(int_arrays, 6); + double max_double = maxn(double_arrays, 4); + char *max_string = maxn(string_arrays, 5); + + cout << "max int: " << max_int << endl; + cout << "max_double: " << max_double << endl; + cout << "max_string: " << max_string << endl; + return 0; +} \ No newline at end of file -- Gitee From 9cbc001cacdd97ee45117240cc4df35812e6408a Mon Sep 17 00:00:00 2001 From: sparklesea Date: Wed, 17 Jul 2024 06:09:59 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=E7=AC=AC9-10=E7=AB=A0=E4=B9=A0=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../C++_exercise/chapter10/CMakeLists.txt" | 10 +++ .../chapter10/codes/10-6/CMakeLists.txt" | 10 +++ .../chapter10/codes/10-6/include/move.h" | 26 +++++++ .../chapter10/codes/10-6/src/main.cpp" | 15 ++++ .../chapter10/codes/10-8/CMakeLists.txt" | 11 +++ .../chapter10/codes/10-8/include/list.h" | 69 +++++++++++++++++++ .../chapter10/codes/10-8/src/list.cpp" | 47 +++++++++++++ .../chapter10/codes/10-8/src/main.cpp" | 43 ++++++++++++ .../C++_exercise/chapter7/CMakeLists.txt" | 1 + .../C++_exercise/chapter7/src/7-10.cpp" | 0 .../C++_exercise/chapter8/CMakeLists.txt" | 0 .../C++_exercise/chapter8/src/8-4.cpp" | 0 .../C++_exercise/chapter8/src/8-6.cpp" | 0 .../C++_exercise/chapter9/CMakeLists.txt" | 10 +++ .../chapter9/codes/9-1/CMakeLists.txt" | 10 +++ .../chapter9/codes/9-1/include/golf.h" | 15 ++++ .../chapter9/codes/9-1/src/golf.cpp" | 31 +++++++++ .../chapter9/codes/9-1/src/main.cpp" | 11 +++ .../chapter9/codes/9-4/CMakeLists.txt" | 10 +++ .../chapter9/codes/9-4/include/sales.h" | 16 +++++ .../chapter9/codes/9-4/src/main.cpp" | 10 +++ .../chapter9/codes/9-4/src/sales.cpp" | 50 ++++++++++++++ 22 files changed, 395 insertions(+) create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/include/move.h" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/src/main.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/include/list.h" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/list.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/main.cpp" rename "C++/codes/2024-07/\351\273\204\345\261\261/chapter7/CMakeLists.txt" => "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter7/CMakeLists.txt" (83%) rename "C++/codes/2024-07/\351\273\204\345\261\261/chapter7/src/7-10.cpp" => "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter7/src/7-10.cpp" (100%) rename "C++/codes/2024-07/\351\273\204\345\261\261/chapter8/CMakeLists.txt" => "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/CMakeLists.txt" (100%) rename "C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-4.cpp" => "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-4.cpp" (100%) rename "C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-6.cpp" => "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-6.cpp" (100%) create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/include/golf.h" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/src/golf.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/src/main.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/include/sales.h" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/src/main.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/src/sales.cpp" diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/CMakeLists.txt" new file mode 100644 index 0000000..4743253 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/CMakeLists.txt" @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.10) +project(chapter9) + +set(BINARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) + +file(GLOB children ${PROJECT_SOURCE_DIR}/codes/*) + +foreach(subdirs ${children}) + add_subdirectory(${subdirs}) +endforeach() diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/CMakeLists.txt" new file mode 100644 index 0000000..991cf17 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/CMakeLists.txt" @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.10) +project(10-6) + +include_directories(include) + +file(GLOB SRC_FILES src/*.cpp) + +add_executable(exercise_10_6 ${SRC_FILES}) + +set_target_properties(exercise_10_6 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BINARY_OUTPUT_PATH}) \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/include/move.h" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/include/move.h" new file mode 100644 index 0000000..a86badb --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/include/move.h" @@ -0,0 +1,26 @@ +#pragma once + +class Move{ +private: + double x; + double y; +public: + Move(double a=0, double b=0){ + this->x = a; + this->y = b; + } + + void showmove() const{ + std::cout << "x: " << this->x << " y: " << this->y << std::endl; + } + + Move add(const Move& m) const{ + Move tempmove(this->x+m.x, this->y+m.y); + return tempmove; + } + + void reset(double a=0, double b =0){ + this->x = a; + this->y = b; + } +}; \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/src/main.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/src/main.cpp" new file mode 100644 index 0000000..12ca629 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/src/main.cpp" @@ -0,0 +1,15 @@ +#include + +#include"move.h" + +int main(int argc, char** argv){ + Move m1(3, 4); + Move m2(5, 6); + m1.showmove(); + m2.showmove(); + Move m3 = m1.add(m2); + m3.showmove(); + m3.reset(); + m3.showmove(); + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/CMakeLists.txt" new file mode 100644 index 0000000..85dcc3d --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/CMakeLists.txt" @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.10) +project(10-8) + +include_directories(include) + +# 顺序不对会导致依赖问题 +# file(GLOB SRC_FILES src/*.cpp) + +add_executable(exercise_10_8 src/list.cpp src/main.cpp) + +set_target_properties(exercise_10_8 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BINARY_OUTPUT_PATH}) \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/include/list.h" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/include/list.h" new file mode 100644 index 0000000..4b7740a --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/include/list.h" @@ -0,0 +1,69 @@ +#pragma once + +#include + +template +class List{ +private: + struct Item + { + T value; + Item* next; + }; + + int size; + int capacity; + Item* HEAD; //指向第一个数据 + Item* TAIL; //指向最后一个数据 + +public: + // List(int n=0); + + // bool isEmpty(); + + // bool isFull(); + + // void append(T val); + + // void visit(void(*pf)(T&)); + + List(int n){ + HEAD = new Item; + TAIL = HEAD; + size = 0; + capacity = n; + std::cout<<"list init completion"<value = val; + temp->next = nullptr; + TAIL->next = temp; + TAIL = temp; + size++; + std::cout<<"append completion"<next != nullptr){ + pf(temp->next->value); + temp = temp->next; + } + } +}; \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/list.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/list.cpp" new file mode 100644 index 0000000..a0a06f3 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/list.cpp" @@ -0,0 +1,47 @@ +// #include"list.h" + +// template +// List::List(int n){ +// this->HEAD = new List::list; +// this->TAIL = this->HEAD; +// this->size = 0; +// this->capacity = n; +// // for(int i=0;iTAIL->next = temp; +// // this->TAIL = temp; +// // } +// } + +// template +// bool List::isEmpty(){ +// if(this->size == 0) return true; +// else return false; +// } + +// template +// bool List::isFull(){ +// if(this->size == this->capacity) return true; +// else return false; +// } + +// template +// void List::append(T val){ +// if(this->isFull()){ +// std::cout << "Can't append, this list is full." << std::endl; +// return; +// } +// list* temp = new list(val, nullptr); +// this->TAIL->next = temp; +// this->TAIL = temp; +// this->size++; +// } + +// template +// void List::visit(void(*pf)(T&)){ +// list* temp = this->HEAD; +// while(temp->next != nullptr){ +// pf(*(temp->next)); +// temp = temp->next; +// } +// } \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/main.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/main.cpp" new file mode 100644 index 0000000..936a802 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/main.cpp" @@ -0,0 +1,43 @@ +#include"list.h" + +struct move{ + double x, y; +}; + +void showint(int& num){ + std::cout<<"num: "< int_list(3); + if(int_list.isEmpty()){ + std::cout<<"the list is empty"< move_list(4); + move_list.append(m1); + move_list.append(m2); + move_list.visit(showmove); + + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter7/CMakeLists.txt" similarity index 83% rename from "C++/codes/2024-07/\351\273\204\345\261\261/chapter7/CMakeLists.txt" rename to "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter7/CMakeLists.txt" index ebf35c2..f679417 100644 --- "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/CMakeLists.txt" +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter7/CMakeLists.txt" @@ -1,3 +1,4 @@ +cmake_minimum_required(VERSION 3.10) PROJECT(exercises_7_10) SET(SRC_LIST src/7-10.cpp) ADD_EXECUTABLE(exercise_7_10 ${SRC_LIST}) diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/src/7-10.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter7/src/7-10.cpp" similarity index 100% rename from "C++/codes/2024-07/\351\273\204\345\261\261/chapter7/src/7-10.cpp" rename to "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter7/src/7-10.cpp" diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/CMakeLists.txt" similarity index 100% rename from "C++/codes/2024-07/\351\273\204\345\261\261/chapter8/CMakeLists.txt" rename to "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/CMakeLists.txt" diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-4.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-4.cpp" similarity index 100% rename from "C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-4.cpp" rename to "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-4.cpp" diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-6.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-6.cpp" similarity index 100% rename from "C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-6.cpp" rename to "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-6.cpp" diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/CMakeLists.txt" new file mode 100644 index 0000000..4743253 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/CMakeLists.txt" @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.10) +project(chapter9) + +set(BINARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) + +file(GLOB children ${PROJECT_SOURCE_DIR}/codes/*) + +foreach(subdirs ${children}) + add_subdirectory(${subdirs}) +endforeach() diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/CMakeLists.txt" new file mode 100644 index 0000000..edefed4 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/CMakeLists.txt" @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.10) +project(9-1) + +include_directories(include) + +file(GLOB SRC_FILES src/*.cpp) + +add_executable(exercise_9_1 ${SRC_FILES}) + +set_target_properties(exercise_9_1 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BINARY_OUTPUT_PATH}) \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/include/golf.h" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/include/golf.h" new file mode 100644 index 0000000..0c4ac26 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/include/golf.h" @@ -0,0 +1,15 @@ +#pragma once + +const int Len=40; +struct golf{ + char fullname[Len]; + int handicap; +}; + +void setgolf(golf& g, const char* name,int hc); + +int setgolf(golf& g); + +void handicap(golf& g, int hc); + +void showgolf(const golf& g); diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/src/golf.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/src/golf.cpp" new file mode 100644 index 0000000..fad0080 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/src/golf.cpp" @@ -0,0 +1,31 @@ +#include +#include + +#include"golf.h" + +void setgolf(golf& g, const char* name,int hc){ + strcpy(g.fullname, name); + // g.fullname = name; + g.handicap = hc; +} + +int setgolf(golf& golf_player){ + char fullname[Len]; + int handicap; + std::cout<<"请输入高尔夫选手的姓名: "; + std::cin.getline(fullname, Len); + if(fullname[0] =='\0') return 0; + std::cout<<"请输入高尔夫选手的等级:"; + std::cin >> handicap; + std::cin.get(); + setgolf(golf_player, fullname, handicap); + return 1; +} + +void handicap(golf& g, int hc){ + g.handicap = hc; +} + +void showgolf(const golf& g){ + std::cout << "姓名:" << g.fullname << " 等级:" << g.handicap << std::endl; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/src/main.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/src/main.cpp" new file mode 100644 index 0000000..0e5fd23 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-1/src/main.cpp" @@ -0,0 +1,11 @@ +#include "golf.h" + +int main(int argc, char** argv){ + while(true){ + golf g; + if(setgolf(g)==0) break; + showgolf(g); + // handicap(g, 23); + // showgolf(g); + } +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/CMakeLists.txt" new file mode 100644 index 0000000..d2f77c4 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/CMakeLists.txt" @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.10) +project(9-4) + +include_directories(include) + +file(GLOB SRC_FILES src/*.cpp) + +add_executable(exercise_9_4 ${SRC_FILES}) + +set_target_properties(exercise_9_4 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BINARY_OUTPUT_PATH}) \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/include/sales.h" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/include/sales.h" new file mode 100644 index 0000000..b572179 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/include/sales.h" @@ -0,0 +1,16 @@ +#pragma once + +namespace SALES +{ + const int QUATERS = 4; + struct Sales{ + double sales[QUATERS]; + double average; + double max; + double min; + }; + + void setSales(Sales& s, const double ar[], int n); + void setSales(Sales& s); + void showSales(const Sales& s); +} // namespace SALES \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/src/main.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/src/main.cpp" new file mode 100644 index 0000000..27fea30 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/src/main.cpp" @@ -0,0 +1,10 @@ +#include"sales.h" + +int main(int argc, char** argv){ + SALES::Sales s1, s2; + double example_sales[5] = {3.4, 6.7, -0.3, -5.4, 2.5}; + SALES::setSales(s1, example_sales, 5); + SALES::setSales(s2); + SALES::showSales(s1); + SALES::showSales(s2); +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/src/sales.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/src/sales.cpp" new file mode 100644 index 0000000..aebb8fa --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter9/codes/9-4/src/sales.cpp" @@ -0,0 +1,50 @@ +#include +#include +#include "sales.h" + +namespace SALES +{ + void setSales(Sales& s, const double ar[], int n){ + int counts = (n>4) ? 4 : n; + double sum = 0; + s.max=DBL_MIN, s.min=DBL_MAX; + + int i=0; + while(i s.max) s.max=ar[i]; + if(ar[i] < s.min) s.min=ar[i]; + i++; + } + s.average = sum / counts; + + while(i<4){ + s.sales[i] = 0; + i++; + } + } + + void setSales(Sales& s){ + std::cout<<"请输入4个数字: "; + std::cin >> s.sales[0] >> s.sales[1] >> s.sales[2] >> s.sales[3]; + std::cin.get(); + + double sum = 0; + s.max=DBL_MIN, s.min=DBL_MAX; + + for(int i=0;i<4;++i){ + sum += s.sales[i]; + if(s.sales[i] > s.max) s.max=s.sales[i]; + if(s.sales[i] < s.min) s.min=s.sales[i]; + } + s.average = sum/4; + } + + void showSales(const Sales& s){ + std::cout<<"Sales: "< Date: Sun, 21 Jul 2024 02:57:26 +0000 Subject: [PATCH 4/6] =?UTF-8?q?=E7=AC=AC11-13=E7=AB=A0=E4=B9=A0=E9=A2=98?= =?UTF-8?q?=EF=BC=8C8-10=E7=AB=A0=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../C++_exercise/chapter10/CMakeLists.txt" | 2 +- .../C++_exercise/chapter11/CMakeLists.txt" | 10 +++ .../chapter11/codes/11-7/CMakeLists.txt" | 11 +++ .../chapter11/codes/11-7/include/complex0.h" | 21 +++++ .../chapter11/codes/11-7/src/complex0.cpp" | 57 +++++++++++++ .../chapter11/codes/11-7/src/main.cpp" | 21 +++++ .../C++_exercise/chapter12/CMakeLists.txt" | 10 +++ .../chapter12/codes/12-4/CMakeLists.txt" | 11 +++ .../chapter12/codes/12-4/include/stack.h" | 22 ++++++ .../chapter12/codes/12-4/src/main.cpp" | 23 ++++++ .../chapter12/codes/12-4/src/stack.cpp" | 79 +++++++++++++++++++ .../C++_exercise/chapter13/CMakeLists.txt" | 10 +++ .../chapter13/codes/13-1/CMakeLists.txt" | 11 +++ .../chapter13/codes/13-1/include/cd.h" | 18 +++++ .../chapter13/codes/13-1/include/classic.h" | 14 ++++ .../chapter13/codes/13-1/src/cd.cpp" | 30 +++++++ .../chapter13/codes/13-1/src/classic.cpp" | 35 ++++++++ .../chapter13/codes/13-1/src/main.cpp" | 32 ++++++++ .../chapter13/codes/13-4/CMakeLists.txt" | 11 +++ .../chapter13/codes/13-4/include/port.h" | 21 +++++ .../codes/13-4/include/vintageport.h" | 17 ++++ .../chapter13/codes/13-4/src/main.cpp" | 18 +++++ .../chapter13/codes/13-4/src/port.cpp" | 41 ++++++++++ .../chapter13/codes/13-4/src/vintageport.cpp" | 34 ++++++++ 24 files changed, 558 insertions(+), 1 deletion(-) create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/include/complex0.h" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/src/complex0.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/src/main.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter12/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter12/codes/12-4/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter12/codes/12-4/include/stack.h" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter12/codes/12-4/src/main.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter12/codes/12-4/src/stack.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/include/cd.h" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/include/classic.h" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/src/cd.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/src/classic.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/src/main.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/include/port.h" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/include/vintageport.h" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/src/main.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/src/port.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/src/vintageport.cpp" diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/CMakeLists.txt" index 4743253..499fdbe 100644 --- "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/CMakeLists.txt" +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/CMakeLists.txt" @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(chapter9) +project(chapter10) set(BINARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/CMakeLists.txt" new file mode 100644 index 0000000..2f13cd9 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/CMakeLists.txt" @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.10) +project(chapter11) + +set(BINARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) + +file(GLOB children ${PROJECT_SOURCE_DIR}/codes/*) + +foreach(subdirs ${children}) + add_subdirectory(${subdirs}) +endforeach() diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/CMakeLists.txt" new file mode 100644 index 0000000..f996c1d --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/CMakeLists.txt" @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.10) +project(11-7) + +include_directories(include) + +# 顺序不对会导致依赖问题 +# file(GLOB SRC_FILES src/*.cpp) + +add_executable(exercise_11_7 src/complex0.cpp src/main.cpp) + +set_target_properties(exercise_11_7 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BINARY_OUTPUT_PATH}) \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/include/complex0.h" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/include/complex0.h" new file mode 100644 index 0000000..87fb9df --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/include/complex0.h" @@ -0,0 +1,21 @@ +#pragma once + +#include +// #include + +class complex{ +private: + double m_real; + double m_imag; +public: + complex(); + complex(double real, double imag); + complex operator+(const complex& other); + complex operator-(const complex& other); + complex operator*(const complex& other); + friend complex operator*(double a, complex& other); + complex operator~(); + + friend std::istream& operator>>(std::istream& is, complex& comp); + friend std::ostream& operator<<(std::ostream& os, const complex& comp); +}; \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/src/complex0.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/src/complex0.cpp" new file mode 100644 index 0000000..87e313a --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter11/codes/11-7/src/complex0.cpp" @@ -0,0 +1,57 @@ +#include"complex0.h" + +complex::complex(){ + this->m_real = 0; + this->m_imag = 0; +} +complex::complex(double real, double imag){ + this->m_real = real; + this->m_imag = imag; +} +complex complex::operator+(const complex& other){ + complex comp; + comp.m_real = this->m_real + other.m_real; + comp.m_imag = this->m_imag + other.m_imag; + return comp; +} +complex complex::operator-(const complex& other){ + complex comp; + comp.m_real = this->m_real - other.m_real; + comp.m_imag = this->m_imag - other.m_imag; + return comp; +} +complex complex::operator*(const complex& other){ + complex comp; + comp.m_real = this->m_real * other.m_real - this->m_imag * other.m_imag; + comp.m_imag = this->m_real * other.m_imag + this->m_imag * other.m_real; + return comp; +} +complex operator*(double a, complex& other){ + complex comp; + comp.m_real = a*other.m_real; + comp.m_imag = a*other.m_imag; + return comp; +} +complex complex::operator~(){ + complex comp; + comp.m_real = this->m_real; + comp.m_imag = -this->m_imag; + return comp; +} + +std::istream& operator>>(std::istream& is, complex& comp){ + // if(is.peek() == 'q') return false; + // else{ + // is >> comp.m_real >> comp.m_imag; + // } + // return true; + std::cout << "real: "; + is >> comp.m_real; + std::cout << "imaginary: "; + is >> comp.m_imag; + return is; +} +std::ostream& operator<<(std::ostream& os, const complex& comp){ + os << "(" << comp.m_real << ","< +using namespace std; +#include"complex0.h" + +int main(int argc, char** argv){ + complex a(3.0, 4.0); + complex c; + cout<<"Enter a complex number (q to quit):\n"; + while(cin>>c){ + cout<<"c is "< + +typedef unsigned long Item; + +class Stack{ +private: + enum {MAX = 10}; + Item* pitems; + int size; + int top; +public: + Stack(int n = MAX); + Stack(const Stack& st); + ~Stack(); + bool isempty() const; + bool isfull() const; + bool push(const Item& item); + bool pop(Item& item); + Stack& operator=(const Stack& st); +}; \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter12/codes/12-4/src/main.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter12/codes/12-4/src/main.cpp" new file mode 100644 index 0000000..4ae7c82 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter12/codes/12-4/src/main.cpp" @@ -0,0 +1,23 @@ +#include +using namespace std; +#include"stack.h" + +int main(int argc, char** argv){ + Stack s1(3); + if(s1.isempty()){ + cout << "stack is empty" << endl; + } + + int start = 3; + while(s1.push(start)){ + start++; + } + Stack s2; + s2 = s1; //调用重载的= + unsigned long temp; + s2.pop(temp); + cout << temp << endl; + + Stack s3(s2); //调用复制构造函数 + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter12/codes/12-4/src/stack.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter12/codes/12-4/src/stack.cpp" new file mode 100644 index 0000000..06f8714 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter12/codes/12-4/src/stack.cpp" @@ -0,0 +1,79 @@ +#include"stack.h" + +Stack::Stack(int n){ + this->size = n; + this->pitems = new Item[this->size]; + this->top = -1; + std::cout << "stack created" << std::endl; +} + +Stack::Stack(const Stack& st){ + this->size = st.size; + this->pitems = new Item[this->size]; + this->top = st.top; + for(int i=0;i<=this->top;++i){ + this->pitems[i] = st.pitems[i]; + } + std::cout << "stack copyed" << std::endl; +} + +Stack::~Stack(){ + delete[] (this->pitems); + std::cout << "stack destroyed" << std::endl; +} + +bool Stack::isempty() const{ + if(this->top == -1) + return true; + else + return false; +} + +bool Stack::isfull() const{ + if(this->top == this->size - 1) + return true; + else + return false; +} +bool Stack::push(const Item& item){ + if(this->isfull()){ + std::cout << "can't push, the stack is full." << std::endl; + return false; + } + this->top += 1; + this->pitems[this->top] = item; + std::cout << "push " << item << std::endl; + return true; +} +bool Stack::pop(Item& item){ + if(this->isempty()){ + std::cout << "can't pop, the stack is empty." << std::endl; + return false; + } + item = this->pitems[this->top]; + this->top -= 1; + std::cout << "pop " << item << std::endl; + return true; +} + +Stack& Stack::operator=(const Stack& st){ + this->size = st.size; + this->pitems = new Item[this->size]; + this->top = st.top; + for(int i=0;i<=this->top;++i){ + this->pitems[i] = st.pitems[i]; + } + std::cout << "stack assigned" << std::endl; + return *this; +} + +// Stack& Stack::operator=(const Stack& st){ +// Stack new_stack(st.size); +// new_stack.pitems = new Item[new_stack.size]; +// new_stack.top = st.top; +// for(int i=0;i<=new_stack.top;++i){ +// new_stack.pitems[i] = st.pitems[i]; +// } +// std::cout << "stack assigned" << std::endl; +// return new_stack; +// } \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/CMakeLists.txt" new file mode 100644 index 0000000..2c660b1 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/CMakeLists.txt" @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.10) +project(chapter13) + +set(BINARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) + +file(GLOB children ${PROJECT_SOURCE_DIR}/codes/*) + +foreach(subdirs ${children}) + add_subdirectory(${subdirs}) +endforeach() diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/CMakeLists.txt" new file mode 100644 index 0000000..889b5c5 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/CMakeLists.txt" @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.10) +project(13-1) + +include_directories(include) + +# 顺序不对会导致依赖问题 +# file(GLOB SRC_FILES src/*.cpp) + +add_executable(exercise_13_1 src/cd.cpp src/classic.cpp src/main.cpp) + +set_target_properties(exercise_13_1 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BINARY_OUTPUT_PATH}) \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/include/cd.h" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/include/cd.h" new file mode 100644 index 0000000..8bbb79b --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/include/cd.h" @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +class Cd{ +public: + Cd(char* s1, char* s2, int n, double x); + Cd(const Cd& d); + Cd(); + virtual void Report() const; + virtual Cd& operator=(const Cd& d); +protected: + char performers[50]; + char label[20]; + int selections; + double playtime; +}; \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/include/classic.h" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/include/classic.h" new file mode 100644 index 0000000..13fe147 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/include/classic.h" @@ -0,0 +1,14 @@ +#pragma once + +#include"cd.h" + +class Classic : public Cd{ +public: + Classic(char* s1, char* s2, char* s3, int n, double x); + Classic(const Classic& d); + Classic(); + virtual void Report() const; + virtual Classic& operator=(const Classic& d); +private: + char major_works[50]; +}; \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/src/cd.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/src/cd.cpp" new file mode 100644 index 0000000..af40f24 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/src/cd.cpp" @@ -0,0 +1,30 @@ +#include"cd.h" + +Cd::Cd(char* s1, char* s2, int n, double x):selections(n),playtime(x){ + strcpy(this->performers, s1); + strcpy(this->label, s2); +} +Cd::Cd(const Cd& d):selections(d.selections),playtime(d.playtime){ + strcpy(this->performers, d.performers); + strcpy(this->label, d.label); +} +Cd::Cd(){ + this->performers[0] = '\0'; + this->label[0] = '\0'; + this->selections = 0; + this->playtime = 0; +} + +void Cd::Report() const{ + std::cout<<"performers: "<performers<label<selections<playtime<performers, d.performers); + strcpy(this->label, d.label); + this->selections = d.selections; + this->playtime = d.playtime; + return *this; +} diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/src/classic.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/src/classic.cpp" new file mode 100644 index 0000000..903519a --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/src/classic.cpp" @@ -0,0 +1,35 @@ +#include"classic.h" + +Classic::Classic(char* s1, char* s2, char* s3, int n, double x):Cd::Cd(s2, s3, n, x){ + strcpy(this->major_works, s1); +} +// Classic::Classic(const Classic& d):selections(d.selections),playtime(d.playtime){ +// strcpy(this->major_works, d.major_works); +// strcpy(this->performers, d.performers); +// strcpy(this->label, d.label); +// } +Classic::Classic(const Classic& d):Cd::Cd(d){ + strcpy(this->major_works, d.major_works); +} +Classic::Classic(){ + this->major_works[0] = '\0'; + this->performers[0] = '\0'; + this->label[0] = '\0'; + this->selections = 0; + this->playtime = 0; +} +void Classic::Report() const{ + std::cout<<"major works: "<major_works<performers<label<selections<playtime<major_works, d.major_works); + strcpy(this->performers, d.performers); + strcpy(this->label, d.label); + this->selections = d.selections; + this->playtime = d.playtime; + return *this; +} diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/src/main.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/src/main.cpp" new file mode 100644 index 0000000..b533ae5 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-1/src/main.cpp" @@ -0,0 +1,32 @@ +#include +#include"classic.h" +void Bravo(const Cd& disk); + +int main(int argc, char** argv){ + Cd c1("Beatles", "Capitol", 14, 35.5); + Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17); + Cd *pcd = &c1; + std::cout << "Using object directly:\n"; + c1.Report(); + c2.Report(); + + std::cout<<"Using type cd * pointer to objects:\n"; + pcd->Report(); + pcd=&c2; + pcd->Report(); + + std::cout<<"Calling a function with a Cd reference argument:\n"; + Bravo(c1); + Bravo(c2); + + std::cout<<"Testing assignment: "; + Classic copy; + copy = c2; + copy.Report(); + + return 0; +} + +void Bravo(const Cd& disk){ + disk.Report(); +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/CMakeLists.txt" new file mode 100644 index 0000000..52ffb28 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/CMakeLists.txt" @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.10) +project(13-4) + +include_directories(include) + +# 顺序不对会导致依赖问题 +# file(GLOB SRC_FILES src/*.cpp) + +add_executable(exercise_13_4 src/port.cpp src/vintageport.cpp src/main.cpp) + +set_target_properties(exercise_13_4 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BINARY_OUTPUT_PATH}) \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/include/port.h" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/include/port.h" new file mode 100644 index 0000000..4683f73 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/include/port.h" @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +class Port{ +public: + Port(const char*br="none", const char* st="none", int b=0); + Port(const Port& p); + virtual ~Port(){delete[] brand;} + Port& operator=(const Port& p); + Port& operator+(int b); + Port& operator-(int b); + int BottleCount() const { return bottles; } + virtual void show() const; + friend std::ostream& operator<<(std::ostream& os, const Port& p); +protected: + char* brand; + char style[20]; + int bottles; +}; \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/include/vintageport.h" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/include/vintageport.h" new file mode 100644 index 0000000..bc00f7f --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/include/vintageport.h" @@ -0,0 +1,17 @@ +#pragma once + +#include"port.h" + +class VintagePort : public Port{ +public: + VintagePort(); + VintagePort(const char* br, int b, const char * nn, int y); + VintagePort(const VintagePort& vp); + ~VintagePort(){delete[] nickname;} + VintagePort& operator=(const VintagePort& vp); + void show() const; + friend std::ostream& operator<<(std::ostream& os, const VintagePort& vp); +private: + char* nickname; + int year; +}; \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/src/main.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/src/main.cpp" new file mode 100644 index 0000000..154fc9c --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/src/main.cpp" @@ -0,0 +1,18 @@ +#include +#include"vintageport.h" + +void show(const Port& pt){ + pt.show(); +} + +int main(int argc, char** argv){ + Port p1("Gallo", "tawny", 20); + VintagePort p2("Gallo", 21, "Old Velvet", 1986); + show(p1); + show(p2); + std::cout << p1 << std::endl; + std::cout << p2 << std::endl; + + return 0; +} + diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/src/port.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/src/port.cpp" new file mode 100644 index 0000000..923563f --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter13/codes/13-4/src/port.cpp" @@ -0,0 +1,41 @@ +#include"port.h" + +Port::Port(const char* br, const char* st, int b):bottles(b){ + this->brand = new char(strlen(br) + 1); + strcpy(this->brand, br); + strcpy(this->style, st); +} + +Port::Port(const Port& p){ + strcpy(this->brand, p.brand); + strcpy(this->style, p.style); + this->bottles = p.bottles; +} +// virtual ~Port(){delete[] rand;} +Port& Port::operator=(const Port& p){ + strcpy(this->brand, p.brand); + strcpy(this->style, p.style); + this->bottles = p.bottles; + return *this; +} + +Port& Port::operator+(int b){ + this->bottles += b; + return *this; +} + +Port& Port::operator-(int b){ + this->bottles -= b; + return *this; +} +// int BottleCount() const{return bottles}; +void Port::show() const{ + std::cout << "Brand: "<brand <style<bottles<year = 0; +} + +VintagePort::VintagePort(const char* br, int b, const char * nn, int y):Port::Port(br, "vintage", b){ + this->nickname = new char(strlen(nn) + 1); + strcpy(this->nickname, nn); + this->year = y; +} + +VintagePort::VintagePort(const VintagePort& vp):Port::Port(vp){ + strcpy(this->nickname, vp.nickname); + this->year = vp.year; +} + +VintagePort& VintagePort::operator=(const VintagePort& vp){ + Port::operator=(vp); + strcpy(this->nickname, vp.nickname); + this->year = vp.year; + return *this; +} +void VintagePort::show() const{ + Port::show(); + std::cout << "Nickname: " << this->nickname << std::endl; + std::cout<<"Year: "<year< Date: Mon, 22 Jul 2024 10:21:35 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BF=AE=E6=94=B98-10=E7=AB=A0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BB=A5=E7=AC=A6=E5=90=88=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chapter10/codes/10-6/include/move.h" | 25 ++--- .../chapter10/codes/10-8/include/list.h" | 101 +++++++++--------- .../chapter10/codes/10-8/src/list.cpp" | 79 +++++++------- .../chapter10/codes/10-8/src/main.cpp" | 8 +- .../C++_exercise/chapter7/src/7-10.cpp" | 10 +- .../C++_exercise/chapter8/src/8-4.cpp" | 5 +- .../C++_exercise/chapter8/src/8-6.cpp" | 7 +- 7 files changed, 113 insertions(+), 122 deletions(-) diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/include/move.h" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/include/move.h" index a86badb..b08b979 100644 --- "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/include/move.h" +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-6/include/move.h" @@ -1,26 +1,27 @@ #pragma once class Move{ -private: - double x; - double y; public: - Move(double a=0, double b=0){ - this->x = a; - this->y = b; + Move(double x=0, double y=0){ + m_x = x; + m_y = y; } void showmove() const{ - std::cout << "x: " << this->x << " y: " << this->y << std::endl; + std::cout << "x: " << m_x << " y: " << m_y << std::endl; } - Move add(const Move& m) const{ - Move tempmove(this->x+m.x, this->y+m.y); + Move add(const Move& other) const{ + Move tempmove(m_x+other.m_x, m_y+other.m_y); return tempmove; } - void reset(double a=0, double b =0){ - this->x = a; - this->y = b; + void reset(double x=0, double y=0){ + m_x = x; + m_y = y; } + +private: + double m_x; + double m_y; }; \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/include/list.h" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/include/list.h" index 4b7740a..ba3f808 100644 --- "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/include/list.h" +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/include/list.h" @@ -4,6 +4,56 @@ template class List{ +public: + List(int n=0); + + bool isEmpty(); + + bool isFull(); + + void append(T val); + + void visit(void(*pf)(T&)); + + // List(int n){ + // HEAD = new Item; + // TAIL = HEAD; + // size = 0; + // capacity = n; + // std::cout<<"list init completion"<value = val; + // temp->next = nullptr; + // TAIL->next = temp; + // TAIL = temp; + // size++; + // std::cout<<"append completion"<next != nullptr){ + // pf(temp->next->value); + // temp = temp->next; + // } + // } private: struct Item { @@ -15,55 +65,4 @@ private: int capacity; Item* HEAD; //指向第一个数据 Item* TAIL; //指向最后一个数据 - -public: - // List(int n=0); - - // bool isEmpty(); - - // bool isFull(); - - // void append(T val); - - // void visit(void(*pf)(T&)); - - List(int n){ - HEAD = new Item; - TAIL = HEAD; - size = 0; - capacity = n; - std::cout<<"list init completion"<value = val; - temp->next = nullptr; - TAIL->next = temp; - TAIL = temp; - size++; - std::cout<<"append completion"<next != nullptr){ - pf(temp->next->value); - temp = temp->next; - } - } }; \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/list.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/list.cpp" index a0a06f3..529590b 100644 --- "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/list.cpp" +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/list.cpp" @@ -1,47 +1,42 @@ -// #include"list.h" +#include"list.h" -// template -// List::List(int n){ -// this->HEAD = new List::list; -// this->TAIL = this->HEAD; -// this->size = 0; -// this->capacity = n; -// // for(int i=0;iTAIL->next = temp; -// // this->TAIL = temp; -// // } -// } +template +List::List(int n){ + this->HEAD = new List::Item; + this->TAIL = this->HEAD; + this->size = 0; + this->capacity = n; +} -// template -// bool List::isEmpty(){ -// if(this->size == 0) return true; -// else return false; -// } +template +bool List::isEmpty(){ + if(this->size == 0) return true; + else return false; +} -// template -// bool List::isFull(){ -// if(this->size == this->capacity) return true; -// else return false; -// } +template +bool List::isFull(){ + if(this->size == this->capacity) return true; + else return false; +} -// template -// void List::append(T val){ -// if(this->isFull()){ -// std::cout << "Can't append, this list is full." << std::endl; -// return; -// } -// list* temp = new list(val, nullptr); -// this->TAIL->next = temp; -// this->TAIL = temp; -// this->size++; -// } +template +void List::append(T val){ + if(this->isFull()){ + std::cout << "Can't append, this list is full." << std::endl; + return; + } + Item* temp = new Item(val, nullptr); + this->TAIL->next = temp; + this->TAIL = temp; + this->size++; +} -// template -// void List::visit(void(*pf)(T&)){ -// list* temp = this->HEAD; -// while(temp->next != nullptr){ -// pf(*(temp->next)); -// temp = temp->next; -// } -// } \ No newline at end of file +template +void List::visit(void(*pf)(T&)){ + Item* temp = this->HEAD; + while(temp->next != nullptr){ + pf(*(temp->next)); + temp = temp->next; + } +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/main.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/main.cpp" index 936a802..4f434ae 100644 --- "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/main.cpp" +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/main.cpp" @@ -4,11 +4,11 @@ struct move{ double x, y; }; -void showint(int& num){ +void showInt(int& num){ std::cout<<"num: "< move_list(4); move_list.append(m1); move_list.append(m2); - move_list.visit(showmove); + move_list.visit(showMove); return 0; } \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter7/src/7-10.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter7/src/7-10.cpp" index 1278b22..e0e2358 100644 --- "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter7/src/7-10.cpp" +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter7/src/7-10.cpp" @@ -1,7 +1,5 @@ #include -using namespace std; - double add(double x, double y){ return x + y; } @@ -18,16 +16,16 @@ int main(int argc, char **argv){ double x, y; while (true) { - std::cout << "请输入两个数字(用空格分隔): "; + std::cout << "Please enter two numbers (separated by Spaces): "; if(!(cin>>x>>y)){ - cout << "输入格式错误,退出" << endl; + std::cout << "The input format is incorrect. Exit." << std::endl; break; } double sum = calculate(x, y, add); double product = calculate(x, y, multiply); - cout << x << "+" << y << "=" << sum << endl; - cout << x << "*" << y << "=" << product << endl; + std::cout << x << "+" << y << "=" << sum << std::endl; + std::cout << x << "*" << y << "=" << product << std::endl; } return 0; } \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-4.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-4.cpp" index b34f61d..8dc2f1e 100644 --- "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-4.cpp" +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-4.cpp" @@ -1,6 +1,5 @@ #include #include -using namespace std; struct stringy { @@ -23,13 +22,13 @@ void set(stringy& to_string, char* from_string){ void show(stringy &string, int count=1){ for (int i = 0; i < count;++i){ - cout << string.str << endl; + std::cout << string.str << std::endl; } } void show(char *string, int count=1){ for (int i = 0; i < count;++i){ - cout << string << endl; + std::cout << string << std::endl; } } diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-6.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-6.cpp" index ebb76ec..0d6fbaf 100644 --- "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-6.cpp" +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-6.cpp" @@ -1,6 +1,5 @@ #include #include -using namespace std; template T maxn(T *nums, int counts){ @@ -33,8 +32,8 @@ int main(int argc, char** argv){ double max_double = maxn(double_arrays, 4); char *max_string = maxn(string_arrays, 5); - cout << "max int: " << max_int << endl; - cout << "max_double: " << max_double << endl; - cout << "max_string: " << max_string << endl; + std::cout << "max int: " << max_int << std::endl; + std::cout << "max_double: " << max_double << std::endl; + std::cout << "max_string: " << max_string << std::endl; return 0; } \ No newline at end of file -- Gitee From 7109ab1666d66f0177d3689167bb50faa7ec46e3 Mon Sep 17 00:00:00 2001 From: sparklesea Date: Tue, 23 Jul 2024 12:23:59 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8A=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=9A=84=E4=B8=80=E4=BA=9Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../C++_exercise/.gitignore" | 4 + .../C++_exercise/CMakeLists.txt" | 8 + .../chapter10/codes/10-8/include/list.h" | 80 ++++---- .../chapter10/codes/10-8/src/list.cpp" | 74 +++---- .../C++_exercise/chapter16/CMakeLists.txt" | 10 + .../chapter16/codes/16-10/CMakeLists.txt" | 11 ++ .../chapter16/codes/16-10/src/main.cpp" | 181 ++++++++++++++++++ .../chapter16/codes/16-9/CMakeLists.txt" | 11 ++ .../chapter16/codes/16-9/src/main.cpp" | 44 +++++ .../C++_exercise/chapter7/src/7-10.cpp" | 2 +- 10 files changed, 347 insertions(+), 78 deletions(-) create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/.gitignore" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-10/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-10/src/main.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-9/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-9/src/main.cpp" diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/.gitignore" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/.gitignore" new file mode 100644 index 0000000..d100a82 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/.gitignore" @@ -0,0 +1,4 @@ +bin/ +build/ +.vscode/ +boost/ \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/CMakeLists.txt" new file mode 100644 index 0000000..58966d2 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/CMakeLists.txt" @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.5.0) +project(cpp_exercise VERSION 0.1.0 LANGUAGES C CXX) + +file(GLOB chapters ${PROJECT_SOURCE_DIR}/chapter*) + +foreach(subdirs ${chapters}) + add_subdirectory(${subdirs}) +endforeach() diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/include/list.h" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/include/list.h" index ba3f808..715e88f 100644 --- "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/include/list.h" +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/include/list.h" @@ -5,55 +5,55 @@ template class List{ public: - List(int n=0); + // List(int n=0); - bool isEmpty(); + // bool isEmpty(); - bool isFull(); + // bool isFull(); - void append(T val); + // void append(T val); - void visit(void(*pf)(T&)); + // void visit(void(*pf)(T&)); - // List(int n){ - // HEAD = new Item; - // TAIL = HEAD; - // size = 0; - // capacity = n; - // std::cout<<"list init completion"<value = val; - // temp->next = nullptr; - // TAIL->next = temp; - // TAIL = temp; - // size++; - // std::cout<<"append completion"<value = val; + temp->next = nullptr; + TAIL->next = temp; + TAIL = temp; + size++; + std::cout<<"append completion"<next != nullptr){ - // pf(temp->next->value); - // temp = temp->next; - // } - // } + void visit(void(*pf)(T& )){ + Item* temp = HEAD; + while(temp->next != nullptr){ + pf(temp->next->value); + temp = temp->next; + } + } private: struct Item { diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/list.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/list.cpp" index 529590b..2b923ee 100644 --- "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/list.cpp" +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/codes/10-8/src/list.cpp" @@ -1,42 +1,42 @@ -#include"list.h" +// #include"list.h" -template -List::List(int n){ - this->HEAD = new List::Item; - this->TAIL = this->HEAD; - this->size = 0; - this->capacity = n; -} +// template +// List::List(int n){ +// this->HEAD = new List::Item; +// this->TAIL = this->HEAD; +// this->size = 0; +// this->capacity = n; +// } -template -bool List::isEmpty(){ - if(this->size == 0) return true; - else return false; -} +// template +// bool List::isEmpty(){ +// if(this->size == 0) return true; +// else return false; +// } -template -bool List::isFull(){ - if(this->size == this->capacity) return true; - else return false; -} +// template +// bool List::isFull(){ +// if(this->size == this->capacity) return true; +// else return false; +// } -template -void List::append(T val){ - if(this->isFull()){ - std::cout << "Can't append, this list is full." << std::endl; - return; - } - Item* temp = new Item(val, nullptr); - this->TAIL->next = temp; - this->TAIL = temp; - this->size++; -} +// template +// void List::append(T val){ +// if(this->isFull()){ +// std::cout << "Can't append, this list is full." << std::endl; +// return; +// } +// Item* temp = new Item(val, nullptr); +// this->TAIL->next = temp; +// this->TAIL = temp; +// this->size++; +// } -template -void List::visit(void(*pf)(T&)){ - Item* temp = this->HEAD; - while(temp->next != nullptr){ - pf(*(temp->next)); - temp = temp->next; - } -} \ No newline at end of file +// template +// void List::visit(void(*pf)(T&)){ +// Item* temp = this->HEAD; +// while(temp->next != nullptr){ +// pf(*(temp->next)); +// temp = temp->next; +// } +// } \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/CMakeLists.txt" new file mode 100644 index 0000000..6bccff8 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/CMakeLists.txt" @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.10) +project(chapter16) + +set(BINARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) + +file(GLOB children ${PROJECT_SOURCE_DIR}/codes/*) + +foreach(subdirs ${children}) + add_subdirectory(${subdirs}) +endforeach() diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-10/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-10/CMakeLists.txt" new file mode 100644 index 0000000..14e53dd --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-10/CMakeLists.txt" @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.10) +project(16-10) + +include_directories(include) + +# 顺序不对会导致依赖问题 +# file(GLOB SRC_FILES src/*.cpp) + +add_executable(exercise_16_10 src/main.cpp) + +set_target_properties(exercise_16_10 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BINARY_OUTPUT_PATH}) \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-10/src/main.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-10/src/main.cpp" new file mode 100644 index 0000000..c24079b --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-10/src/main.cpp" @@ -0,0 +1,181 @@ +#include +#include +#include +#include +#include +#include + +struct Review +{ + std::string title; + int rating; + double price; + + Review(std::string s, int r, double p):title(s),rating(r),price(p){ + + } +}; + +bool operator<(const std::shared_ptr r1, const std::shared_ptr r2); +bool ratingWorseThan(const std::shared_ptr r1, const std::shared_ptr r2); +bool ratingBetterThan(const std::shared_ptr r1, const std::shared_ptr r2); +bool priceWorseThan(const std::shared_ptr r1, const std::shared_ptr r2); +bool priceBetterThan(const std::shared_ptr r1, const std::shared_ptr r2); +bool FillReview(std::shared_ptr& rr); +void ShowReview(const std::shared_ptr rr); + +int main(int argc, char** argv){ + + std::vector> books; + std::shared_ptr temp; + while(FillReview(temp)){ + books.push_back(temp); + } + if(books.size() > 0){ + std::cout << "Enter a number to select books displayed(0:quit, 1:raw, 2:AlphaBetical, 3:rating ascend, 4:rating descend, 5:price ascend, 6:price descend): "; + enum {QUIT, RAW, AB, RA, RD, PA, PD}; + int display; + while(std::cin >> display){ + if(display == 0){ + break; + } + switch (display) + { + case RAW: + std::cout << "Thank you. You entered the following " << books.size() << " books:\n" << "Rating\tPrice\tBook\n"; + std::for_each(books.begin(), books.end(), ShowReview); + break; + case AB: + std::sort(books.begin(), books.end()); + std::cout << "Sorted by title:\nRating\tPrice\tBook\n"; + std::for_each(books.begin(), books.end(), ShowReview); + break; + case RA: + std::sort(books.begin(), books.end(), ratingWorseThan); + std::cout << "Sorted by rating in ascending order:\nRating\tPrice\tBook\n"; + std::for_each(books.begin(), books.end(), ShowReview); + break; + case RD: + std::sort(books.begin(), books.end(), ratingBetterThan); + std::cout << "Sorted by rating in descending order:\nRating\tPrice\tBook\n"; + std::for_each(books.begin(), books.end(), ShowReview); + break; + case PA: + std::sort(books.begin(), books.end(), priceWorseThan); + std::cout << "Sorted by rating in ascending order:\nRating\tPrice\tBook\n"; + std::for_each(books.begin(), books.end(), ShowReview); + break; + case PD: + std::sort(books.begin(), books.end(), priceBetterThan); + std::cout << "Sorted by rating in descending order:\nRating\tPrice\tBook\n"; + std::for_each(books.begin(), books.end(), ShowReview); + break; + default: + break; + } + std::cout << "Enter a number to select books displayed(0:quit, 1:raw, 2:AlphaBetical, 3:rating ascend, 4:rating descend, 5:price ascend, 6:price descend): "; + } + } + else{ + std::cout << "No entries. "; + } + std::cout << "Bye.\n"; + + return 0; +} + +// bool operator<(const Review& r1, const Review& r2){ +// if(r1.title < r2.title){ +// return true; +// } +// else if (r1.title == r2.title && r1.rating < r2.rating){ +// return true; +// } +// else{ +// return false; +// } +// } + +bool operator<(const std::shared_ptr r1, const std::shared_ptr r2){ + if(r1->title < r2->title){ + return true; + } + else if (r1->title == r2->title){ + if(r1->rating == r2->rating && r1->price > r2->price){ + return true; + } + else if(r1->rating < r2->rating){ + return true; + } + else{ + return false; + } + } + else{ + return false; + } +} + +bool ratingWorseThan(const std::shared_ptr r1, const std::shared_ptr r2){ + if (r1->rating < r2->rating){ + return true; + } + else{ + return false; + } +} +bool ratingBetterThan(const std::shared_ptr r1, const std::shared_ptr r2){ + if (r1->rating > r2->rating){ + return true; + } + else{ + return false; + } +} +bool priceWorseThan(const std::shared_ptr r1, const std::shared_ptr r2){ + if (r1->price < r2->price){ + return true; + } + else{ + return false; + } +} +bool priceBetterThan(const std::shared_ptr r1, const std::shared_ptr r2){ + if (r1->price > r2->price){ + return true; + } + else{ + return false; + } +} + +bool FillReview(std::shared_ptr& rr){ + std::cout << "Enter book title(quit to quit): "; + std::string title; + int rating; + double price; + + std::getline(std::cin, title); + if(title == "quit"){ + return false; + } + std::cout << "Enter book rating: "; + std::cin >> rating; + std::cout << "Enter book price: "; + std::cin >> price; + if(!std::cin){ + return false; + } + while(std::cin.get() != '\n'){ + continue; + } + + rr = std::make_shared(title, rating, price); + + return true; +} + +void ShowReview(const std::shared_ptr rr){ + std::cout << rr->rating << "\t" << rr->price << "\t" << rr->title << std::endl; +} + diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-9/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-9/CMakeLists.txt" new file mode 100644 index 0000000..125d988 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-9/CMakeLists.txt" @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.10) +project(16-9) + +include_directories(include) + +# 顺序不对会导致依赖问题 +# file(GLOB SRC_FILES src/*.cpp) + +add_executable(exercise_16_9 src/main.cpp) + +set_target_properties(exercise_16_9 PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BINARY_OUTPUT_PATH}) \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-9/src/main.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-9/src/main.cpp" new file mode 100644 index 0000000..bb48a0d --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter16/codes/16-9/src/main.cpp" @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include + +int main(int argc, char** argv){ + std::srand(static_cast(std::time(0))); + std::vector vi0, vi; + std::list li; + clock_t start, end; + for(int length=100000;length<=10000000;length*=10){ + std::cout << "length: " << length << std::endl; + vi0.resize(length); //预先分配内存 + for(int i=0;i>x>>y)){ + if(!(std::cin>>x>>y)){ std::cout << "The input format is incorrect. Exit." << std::endl; break; } -- Gitee