diff --git "a/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c10-1.cpp" "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c10-1.cpp" new file mode 100644 index 0000000..4765785 --- /dev/null +++ "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c10-1.cpp" @@ -0,0 +1,47 @@ +#include +#include + +class Bank +{ +public: + Bank(std::string name, std::string id) + { + _name = name; + _id = id; + } + Bank(std::string name, std::string id, long long count) + { + _name = name; + _id = id; + _count = count; + } + void show() + { + std::cout << _name << std::endl << _id + << std::endl << _count << std::endl; + } + void add(long long val) + { + _count += val; + } + void del(long long val) + { + _count -= val; + } + +private: + std::string _name; + std::string _id; + long long _count = 0; +}; + +int main() +{ + Bank bank("test", "000001"); + bank.show(); + bank.add(10000); + bank.show(); + bank.del(1000); + bank.show(); + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c10-2.cpp" "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c10-2.cpp" new file mode 100644 index 0000000..e08e49a --- /dev/null +++ "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c10-2.cpp" @@ -0,0 +1,37 @@ +#include + +class plorg +{ +public: + plorg(){} + plorg(char *name_in, int CI_in) + { + int a = 0; + while(name_in[a] != '\0') + { + name[a] = name_in[a]; + ++a; + } + name[a] = name_in[a]; + CI = CI_in; + } + ~plorg(){} + + void setCI(int CI_in){CI=CI_in;} + void show() + { + std::cout << name << " " << CI << std::endl; + } +private: + char name[20] = "Plorga"; + int CI = 0; +}; + +int main() +{ + plorg plorg1("TEST", 1), plorg2; + plorg1.show(); + plorg2.setCI(2); + plorg2.show(); + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c7-1.cpp" "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c7-1.cpp" new file mode 100644 index 0000000..70e838a --- /dev/null +++ "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c7-1.cpp" @@ -0,0 +1,30 @@ +#include + +using std::cin; +using std::cout; +using std::endl; + +/** + * @brief 返回调和平均数 + * + * @param x 输入1 + * @param y 输入2 + * @return double + */ +double func(double x, double y) +{ + return 2.0 * x * y / (x + y); +} + +int main() +{ + double x, y; + while(1) + { + cin >> x >> y; + if(x == 0.0 || y == 0.0) + break; + cout << func(x, y) << endl; + } + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c7-2.cpp" "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c7-2.cpp" new file mode 100644 index 0000000..88a0668 --- /dev/null +++ "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c7-2.cpp" @@ -0,0 +1,54 @@ +#include + +using std::cin; +using std::cout; +using std::endl; + +/** + * @brief 返回两数之和 + * + * @param x 被加数 + * @param y 加数 + * @return double + */ +double add(double x, double y) +{ + return x + y; +} + +/** + * @brief 返回两数之差 + * + * @param x 被减数 + * @param y 减数 + * @return double + */ +double del(double x, double y) +{ + return x - y; +} + +/** + * @brief 调用add和del函数 + * + * @param x 输入1 + * @param y 输入2 + * @return double + */ +double (*calculate[2])(double x, double y) = {add, del}; + +int main() +{ + double x, y; + while(1) + { + cin >> x >> y; + if(x == 0.0 || y == 0.0) + break; + cout << x << " add " << y << " = " + << (*calculate[0])(x, y) << endl; + cout << x << " del " << y << " = " + << (*calculate[1])(x, y) << endl; + } + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c8-1.cpp" "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c8-1.cpp" new file mode 100644 index 0000000..b583016 --- /dev/null +++ "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c8-1.cpp" @@ -0,0 +1,35 @@ +#include + +using std::cin; +using std::cout; +using std::endl; + +int cnt = 0; + +/** + * @brief 输出输入字符串 + * + * @param str 输入的字符串 + * @param n 参数 + */ +void func(char *str, int n = 0) +{ + ++cnt; + cout << str << endl; + if(n != 0) + for(int a = 1; a < cnt; a++) + cout << str << endl; + return; +} + +int main() +{ + int n; + char str[255]; + while(1) + { + cin >> str >> n; + func(str, n); + } + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c8-2.cpp" "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c8-2.cpp" new file mode 100644 index 0000000..b016e2e --- /dev/null +++ "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c8-2.cpp" @@ -0,0 +1,54 @@ +#include + +using std::cin; +using std::cout; +using std::endl; + +template +T maxn(T *num, int n = 0) +{ + if(n != 0) + { + T pmax = *num; + num++; + for(int a = 0; a < n-1; ++a, num++) + pmax = pmax > *num ? pmax : *num; + return pmax; + } + return *num; +} +template <> char * maxn (char **num, int n) +{ + if(n != 0) + { + auto getLen = [](char *str){ + int a = 0; + while(str[a] != '\0') + a++; + return a+1; + }; + int tempLen1 = getLen(num[0]), tempLen2 = 0; + char *pstr = num[0]; + for(int a = 1; a < n; a++) + { + tempLen2 = getLen(num[a]); + if(tempLen1 < tempLen2) + { + tempLen1 = tempLen2; + pstr = num[a]; + } + } + return pstr; + } + return *num; +} +int main() +{ + int num1[] = {1, 2, 3, 4, 5, 6}; + double num2[] = {1.1, 2.2, 3.3, 4.4}; + char *num3[]={"a", "ab", "abc", "abcr", "abce"}; + cout << maxn(num1, 6) << endl; + cout << maxn(num2, 4) << endl; + cout << maxn(num3, 5) << endl; + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c9-1-golf.cpp" "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c9-1-golf.cpp" new file mode 100644 index 0000000..3f6b9f1 --- /dev/null +++ "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c9-1-golf.cpp" @@ -0,0 +1,40 @@ +#include "c9-1-golf.h" + +void setgolf(golf &g, const char *name, int hc) +{ + int a = 0; + while(name[a] != '\0') + { + g.fullname[a] = name[a]; + ++a; + if(a == Len) + { + --a; + break; + } + } + g.fullname[a] = name[a]; + g.handicap = hc; + return; +} +int setgolf(golf &g) +{ + int temp = 1; + cout << "Please input name character < 40 or n to skip" << endl; + cin >> g.fullname; + if(g.fullname[0] == 'n' && g.fullname[1] == '\0') + temp = 0; + cout << "Please input hc" << endl; + cin >> g.handicap; + return temp; +} +void handicap(golf &g, int hc) +{ + g.handicap = hc; + return; +} +void showgolf(const golf &g) +{ + cout << g.fullname << endl << g.handicap << endl; + return; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c9-1-golf.h" "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c9-1-golf.h" new file mode 100644 index 0000000..880ea98 --- /dev/null +++ "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c9-1-golf.h" @@ -0,0 +1,46 @@ +#pragma once +#include + +using std::cin; +using std::cout; +using std::endl; + +const int Len = 40; + +struct golf +{ + char fullname[Len]; + int handicap; +}; + +/** + * @brief 设置golf + * + * @param g 设置对象 + * @param name 修改后的name + * @param hc 修改后的hc + */ +void setgolf(golf &g, const char *name, int hc); + +/** + * @brief 交互设置golf + * + * @param g 设置对象 + * @return int 若人名为空返回0,否则返回1 + */ +int setgolf(golf &g); + +/** + * @brief 设置handicap + * + * @param g 设置对象 + * @param hc 修改后的handicap值 + */ +void handicap(golf &g, int hc); + +/** + * @brief 展示golf数据 + * + * @param g 展示对象 + */ +void showgolf(const golf &g); \ No newline at end of file diff --git "a/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c9-1.cpp" "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c9-1.cpp" new file mode 100644 index 0000000..e69aa3d --- /dev/null +++ "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c9-1.cpp" @@ -0,0 +1,12 @@ +#include "c9-1-golf.h" + + +int main() +{ + golf tempGolf; + setgolf(tempGolf, "Amy", 1); + setgolf(tempGolf); + handicap(tempGolf, 2); + showgolf(tempGolf); + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c9-2.cpp" "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c9-2.cpp" new file mode 100644 index 0000000..452e0f6 --- /dev/null +++ "b/C++/codes/2024-07/\346\235\250\346\231\223\345\256\207/c9-2.cpp" @@ -0,0 +1,23 @@ +#include +#include + +struct chaff +{ + char dross[20]; + int slag; +}; + +int main() +{ + char buffer[1024]; + chaff *num = new (buffer) chaff[2]; + std::strcpy(num[0].dross, "1111111111"); + num[0].slag = 1; + std::strcpy(num[1].dross, "2222222222"); + num[1].slag = 2; + + for(int a = 0; a < 2; a++) + std::cout << num[a].dross << " " + << num[a].slag << std::endl; + return 0; +} \ No newline at end of file