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 0000000000000000000000000000000000000000..1cb83798b6420225ff38b8b0a8ad98d3ef42ebfd --- /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/C++_exercise/.gitignore" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/.gitignore" new file mode 100644 index 0000000000000000000000000000000000000000..d100a82acba57393bfd35ab67c24e2a9884fa7b7 --- /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 0000000000000000000000000000000000000000..58966d20fd0f961fe90f6bd55e58404423d3f833 --- /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/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter10/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..499fdbe9b240f6a492a4f3a747dba119875fa2ac --- /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(chapter10) + +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 0000000000000000000000000000000000000000..991cf1737eb0d0857c96ab60c681119eed981a65 --- /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 0000000000000000000000000000000000000000..b08b979037df8f6bf766729e78db00caa7e7c52d --- /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,27 @@ +#pragma once + +class Move{ +public: + Move(double x=0, double y=0){ + m_x = x; + m_y = y; + } + + void showmove() const{ + std::cout << "x: " << m_x << " y: " << m_y << std::endl; + } + + Move add(const Move& other) const{ + Move tempmove(m_x+other.m_x, m_y+other.m_y); + return tempmove; + } + + 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-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 0000000000000000000000000000000000000000..12ca62947a60a2aaa931a371832696bcfcbd332e --- /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 0000000000000000000000000000000000000000..85dcc3dabdf4adcf07135188cdebcae01f0fce93 --- /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 0000000000000000000000000000000000000000..715e88f1ed32c2c049a058c050989f74f843dede --- /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,68 @@ +#pragma once + +#include + +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 + { + T value; + Item* next; + }; + + int size; + int capacity; + Item* HEAD; //指向第一个数据 + Item* TAIL; //指向最后一个数据 +}; \ 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 0000000000000000000000000000000000000000..2b923eea6ee1461a478c161f4095480e300668cb --- /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,42 @@ +// #include"list.h" + +// 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::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::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" new file mode 100644 index 0000000000000000000000000000000000000000..4f434ae03d9234aae0a40212eccbdae6a62d00b5 --- /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/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 0000000000000000000000000000000000000000..2f13cd9301be6536e40666b30943b3bfafb49a3d --- /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 0000000000000000000000000000000000000000..f996c1d4f35313aa9ed5491aff5f8e9e5276f191 --- /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 0000000000000000000000000000000000000000..87fb9dfff2460b651128ff69f9fb48ae1bf64a28 --- /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 0000000000000000000000000000000000000000..87e313acaf6b74f969315460886ca587554b8da9 --- /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 0000000000000000000000000000000000000000..4ae7c827878510813ee07ec1a727a22703166614 --- /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 0000000000000000000000000000000000000000..06f87140d50e17f4384ee3dabeab26c67e870796 --- /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 0000000000000000000000000000000000000000..2c660b1f9e168e211977b333daf3014bb36e6751 --- /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 0000000000000000000000000000000000000000..889b5c5e0749d7bc301919d277e3914cd7946f4a --- /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 0000000000000000000000000000000000000000..8bbb79b0fcff58401f6bef3d20ba92fd7e0414df --- /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 0000000000000000000000000000000000000000..13fe1475053b3ea277d61797c626e62331e2730d --- /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 0000000000000000000000000000000000000000..af40f2414c22c4834156a55c07980ecc822b6d97 --- /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 0000000000000000000000000000000000000000..903519aff22aca2bb28601e04f25e38b8d72c69e --- /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 0000000000000000000000000000000000000000..b533ae5567a3dc88b96c30e1ae60197e8acbf70e --- /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 0000000000000000000000000000000000000000..52ffb28f5a50c9f01e80441e219b25f70d4dd088 --- /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 0000000000000000000000000000000000000000..4683f733e6e00295af1904e34e3e9a3f8bc36908 --- /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 0000000000000000000000000000000000000000..bc00f7fd9448982788f989623a58c7c35da180b4 --- /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 0000000000000000000000000000000000000000..154fc9cce9121f609867eb8e896464e2efa9e8a0 --- /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 0000000000000000000000000000000000000000..923563f8ede3961ea45456c27b32f39c8bf45bbf --- /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< +#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 0000000000000000000000000000000000000000..125d9883e6108e7d3fca8ef641b3ecdd5349bb70 --- /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 0000000000000000000000000000000000000000..bb48a0dccd8954b69627acf3e9f7bdae7000fbe2 --- /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 + +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 << "Please enter two numbers (separated by Spaces): "; + if(!(std::cin>>x>>y)){ + std::cout << "The input format is incorrect. Exit." << std::endl; + break; + } + + double sum = calculate(x, y, add); + double product = calculate(x, y, multiply); + 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/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..53ac84f583b5b29e2e32644b81f5e3b379266c6d --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/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/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" new file mode 100644 index 0000000000000000000000000000000000000000..8dc2f1ec8140fa120211e18e8a39223f53efb233 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-4.cpp" @@ -0,0 +1,47 @@ +#include +#include + +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){ + std::cout << string.str << std::endl; + } +} + +void show(char *string, int count=1){ + for (int i = 0; i < count;++i){ + std::cout << string << std::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/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" new file mode 100644 index 0000000000000000000000000000000000000000..0d6fbaf6e8833d1bd3bb6a34cbeee2b298d63c0e --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/C++_exercise/chapter8/src/8-6.cpp" @@ -0,0 +1,39 @@ +#include +#include + +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); + + 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 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 0000000000000000000000000000000000000000..4743253664be4bf61d079f354338b649f651362c --- /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 0000000000000000000000000000000000000000..edefed48a58923be9aac6e1ffab541f9f65080cd --- /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 0000000000000000000000000000000000000000..0c4ac269737d1dccd535e4843b4d5907903bacc6 --- /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 0000000000000000000000000000000000000000..fad0080aee8c0f3a370774bef0295f3585209b00 --- /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 0000000000000000000000000000000000000000..0e5fd23f31b2557729a94f53f28d09228c5752e6 --- /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 0000000000000000000000000000000000000000..d2f77c44aae6d2b96f91493a9f856767e8fdcb2b --- /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 0000000000000000000000000000000000000000..b572179d66778bdf8720d7aa1fb1593e4874f2a8 --- /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 0000000000000000000000000000000000000000..27fea3037c2dd0dad05e474a5098c09fbaf03b81 --- /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 0000000000000000000000000000000000000000..aebb8fa8533fd5ff141d1e34812ded131c39d76c --- /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: "<