From a3ae8d484416a3b76f19e2eff5cb07da492f8bbe Mon Sep 17 00:00:00 2001 From: Certseeds <51754303+Certseeds@users.noreply.github.com> Date: Wed, 22 Jul 2020 00:04:00 +0800 Subject: [PATCH] update readme, add questionD and it's solution. Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com> --- .gitattributes | 5 +- CMakeLists.txt | 2 +- README.md | 26 ++++- source/lab_00/lab_00_D.cpp | 134 ++++++++++++++++++++++++++ test/lab_00/lab_00_A_test.cpp | 2 +- test/lab_00/lab_00_C_test.cpp | 6 +- test/lab_00/lab_00_D_data/01.data.in | 4 + test/lab_00/lab_00_D_data/01.data.out | 13 +++ test/lab_00/lab_00_D_data/01.test.out | 13 +++ test/lab_00/lab_00_D_test.cpp | 78 +++++++++++++++ 10 files changed, 274 insertions(+), 9 deletions(-) create mode 100644 source/lab_00/lab_00_D.cpp create mode 100644 test/lab_00/lab_00_D_data/01.data.in create mode 100644 test/lab_00/lab_00_D_data/01.data.out create mode 100644 test/lab_00/lab_00_D_data/01.test.out create mode 100644 test/lab_00/lab_00_D_test.cpp diff --git a/.gitattributes b/.gitattributes index 7caa83ee..a5735a2a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,7 +4,7 @@ # @Author: nanoseeds # @Date: 2020-07-15 23:48:29 # @LastEditors: nanoseeds - # @LastEditTime: 2020-07-15 23:48:29 + # @LastEditTime: 2020-07-21 21:51:12 ### * text=auto @@ -15,6 +15,7 @@ *.LICENSE text *.template text diff=text *.in text eol=lf +*.out text eol=lf # scripts *.sh text eol=lf @@ -114,7 +115,7 @@ # Executables *.exe binary -*.out binary +#*.out binary *.app binary # Visual Studio diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f5e8e96..99900b83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -O0") set(lab_number 00) -set(problem_order C) +set(problem_order D) include_directories(./includes) include_directories(./thrid_party) diff --git a/README.md b/README.md index 5b9f28e3..1be5a780 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ * @Author: nanoseeds * @Date: 2020-07-15 23:52:04 * @LastEditors: nanoseeds - * @LastEditTime: 2020-07-21 00:10:38 + * @LastEditTime: 2020-07-21 23:54:35 * @License: CC-BY-NC-SA_V4_0 or any later version --> @@ -112,7 +112,29 @@ + PS2: 文件重定向输出不说了,因为输出就两个,保证数据正确和格式正确,数据正确完全可以通过读取*.data.out之后来判断, 格式正确主要是看output函数. -4. TODO: 介绍为什么要将 `读取` `数据处理` `输出` 分开,以及这样做有什么好处  +4. 文本的输出重定向: + + 一般来说,题目的输出不会太复杂,但是反例也不是没有.:比如专门考输出的[立体图](./source/lab_00/lab_00_D.cpp) + + 这种情况下,使用c++的重定向输出就可以较为方便的对输入进行处理,同时保存输出方便调试. + ```c++ + std::streambuf *strmin_buf = cin.rdbuf(); + std::streambuf *strmout_buf = cout.rdbuf(); + std::ifstream file_in; + std::ofstream file_out; + file_in.open("./../test/lab_00/lab_00_D_data/01.data.in"); // input path + file_out.open("./../test/lab_00/lab_00_D_data/01.test.out"); // output path + cin.rdbuf(file_in.rdbuf()); + cout.rdbuf(file_out.rdbuf()); + cal(read()); + cin.rdbuf(strmin_buf); + ``` + 这样就将标准输出重定向到了01.test.out中. + + 至于比较文件之间的差异,就需要用bash脚本了. + +5. 为什么要将 `读取` `数据处理` `输出` 分开? + + 便于理清思路,读完题目之后,不管别的,先把数据读入,输出的函数写好,方便后续写作. + + 交流代码逻辑的时候不会受到无关逻辑的影响 + + 可以互相分享少量代码而不触及核心逻辑,方便协作. + + 便于使用测试. TODO: 为什么要选择C++做题. TODO: 介绍一些神秘操作.比如手动开优化等. diff --git a/source/lab_00/lab_00_D.cpp b/source/lab_00/lab_00_D.cpp new file mode 100644 index 00000000..85a1f8e3 --- /dev/null +++ b/source/lab_00/lab_00_D.cpp @@ -0,0 +1,134 @@ +/** + * @Github: https://github.com/Certseeds/CS203_DSAA_template + * @Organization: SUSTech + * @Author: nanoseds + * @Date: 2020-07-21 21:44:25 + * @LastEditors: nanoseds + * @LICENSE: MIT + */ +/* +MIT License + +CS203_DSAA_template + +Copyright (C) 2020 nanoseds + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +// https://www.luogu.com.cn/problem/P1058 +// luogu +// id: P1058 +#include +#include +#include +#include + +using std::cin; +using std::cout; +using std::string; +using std::tuple; +using std::vector; +const std::string end = "\n"; + +const vector> spis{ + {2, "+---+"}, + {1, "/ /|"}, + {0, "+---+ |"}, + {0, "| | +"}, + {0, "| |/"}, + {0, "+---+"} +}; + +using input_data = vector>; +using result_data = vector>; + +inline input_data read(); + +//result_data cal(input_data &&data); +void cal(input_data &&hi); + +void output(const result_data &map, int high, int wide); + +void print(result_data &map, int a, int b); + +#ifndef CS203_DSAA_TEST_MACRO +#define CS203_DSAA_TEST_MACRO + +int main() { + cal(read()); + // auto result_data = cal(read()); + // output(result_data); + return 0; +} + +#endif // !CS203_DSAA_TEST_MACRO + +inline input_data read() { + int32_t m{0}; + int32_t n{0}; + std::cin >> m >> n; + input_data mat(m + 1, vector(n + 1, -0x3f3f)); + for (int k = 1; k <= m; ++k) { + for (int i = 1; i <= n; ++i) { + std::cin >> mat[k][i]; + } + } + return mat; +} + +void print(result_data &map, int a, int b) { + for (int i = 0; i < static_cast(spis.size()); i++) { + for (int j = 0; j < static_cast(spis[i].second.size()); j++) { + map[a - 1 + i][b + j + spis[i].first] = spis[i].second[j]; + } + } + return; +} + +void cal(input_data &&hi) { + vector> out(302, vector(301, '.')); + int m = static_cast(hi.size()) - 1; + int n = static_cast(hi.front().size()) - 1; + int wide = 4 * n + 2 * m + 1; + int h = -0x3f3f; + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + h = std::max(h, hi[i][j] * 3 + 2 * (m - i + 1) + 1); + } + } + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + for (int k = 0; k < hi[i][j]; k++) { + int x = h - 3 * (k + 2) - 2 * (m - i) + 1; + int y = 4 * j + 2 * (m - i - 1) - 1; + print(out, x, y); + } + } + } + output(out, h, wide); +} + +void output(const result_data &map, int high, int wide) { + for (int i = 0; i < high; i++) { + for (int j = 1; j <= wide; j++) { + cout << map[i][j]; + } + cout << end; + } +} \ No newline at end of file diff --git a/test/lab_00/lab_00_A_test.cpp b/test/lab_00/lab_00_A_test.cpp index 466b7cce..9e50a3cc 100644 --- a/test/lab_00/lab_00_A_test.cpp +++ b/test/lab_00/lab_00_A_test.cpp @@ -58,7 +58,7 @@ using std::vector; TEST_CASE("test case 1", "[test 00 A]") { auto output1 = cal(std::make_tuple(0, 0)); auto output2 = cal(std::make_tuple(100000, 100000)); - CHECK(output1 == 0); + CHECK_FALSE(output1); CHECK(output2 == 200000); } diff --git a/test/lab_00/lab_00_C_test.cpp b/test/lab_00/lab_00_C_test.cpp index e90cdaf0..ac3e2029 100644 --- a/test/lab_00/lab_00_C_test.cpp +++ b/test/lab_00/lab_00_C_test.cpp @@ -57,7 +57,7 @@ TEST_CASE("test case 1", "[test 00 C]") { auto result_data = isBipartite(read()); // 重定向结束 cin.rdbuf(backup); - CHECK(result_data == 0); + CHECK_FALSE(result_data); } TEST_CASE("test case 2", "[test 00 C]") { @@ -90,7 +90,7 @@ TEST_CASE("test case 4", "[test 00 C]") { cin.rdbuf(fin.rdbuf()); auto result_data = isBipartite(read()); cin.rdbuf(backup); - CHECK(result_data == 0); + CHECK_FALSE(result_data); } TEST_CASE("test case 5", "[test 00 C]") { @@ -101,7 +101,7 @@ TEST_CASE("test case 5", "[test 00 C]") { cin.rdbuf(fin.rdbuf()); auto result_data = isBipartite(read()); cin.rdbuf(backup); - CHECK(result_data == 0); + CHECK_FALSE(result_data); } TEST_CASE("test case in loop", "[test 00 C]") { diff --git a/test/lab_00/lab_00_D_data/01.data.in b/test/lab_00/lab_00_D_data/01.data.in new file mode 100644 index 00000000..8d6bb7b6 --- /dev/null +++ b/test/lab_00/lab_00_D_data/01.data.in @@ -0,0 +1,4 @@ +3 4 +2 2 1 2 +2 2 1 1 +3 2 1 2 \ No newline at end of file diff --git a/test/lab_00/lab_00_D_data/01.data.out b/test/lab_00/lab_00_D_data/01.data.out new file mode 100644 index 00000000..a7e1e72a --- /dev/null +++ b/test/lab_00/lab_00_D_data/01.data.out @@ -0,0 +1,13 @@ +......+---+---+...+---+ +..+---+ / /|../ /| +./ /|-+---+ |.+---+ | ++---+ |/ /| +-| | + +| | +---+ |/+---+ |/| +| |/ /| +/ /|-+ | ++---+---+ |/+---+ |/| + +| | | +-| | + |/. +| | |/ | |/| +.. ++---+---+---+---+ |/... +| | | | | +.... +| | | | |/..... ++---+---+---+---+...... \ No newline at end of file diff --git a/test/lab_00/lab_00_D_data/01.test.out b/test/lab_00/lab_00_D_data/01.test.out new file mode 100644 index 00000000..43169fad --- /dev/null +++ b/test/lab_00/lab_00_D_data/01.test.out @@ -0,0 +1,13 @@ +......+---+---+...+---+ +..+---+ / /|../ /| +./ /|-+---+ |.+---+ | ++---+ |/ /| +-| | + +| | +---+ |/+---+ |/| +| |/ /| +/ /|-+ | ++---+---+ |/+---+ |/| + +| | | +-| | + |/. +| | |/ | |/| +.. ++---+---+---+---+ |/... +| | | | | +.... +| | | | |/..... ++---+---+---+---+...... diff --git a/test/lab_00/lab_00_D_test.cpp b/test/lab_00/lab_00_D_test.cpp new file mode 100644 index 00000000..be6c8d1c --- /dev/null +++ b/test/lab_00/lab_00_D_test.cpp @@ -0,0 +1,78 @@ +/** + * @Github: https://github.com/Certseeds/CS203_DSAA_template + * @Organization: SUSTech + * @Author: nanoseds + * @Date: 2020-07-21 21:44:25 + * @LastEditors: nanoseds + * @LICENSE: MIT + */ +/* +MIT License + +CS203_DSAA_template + +Copyright (C) 2020 nanoseds + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +#ifndef CS203_DSAA_TEST_MACRO +#define CS203_DSAA_TEST_MACRO + +#include +#include +#include "catch_main.hpp" +#include "lab_00_D.cpp" + +using std::tie; +using std::cin; +using std::cout; +using std::tuple; +using std::vector; + +using Catch::Matchers::Equals; +using Catch::Matchers::UnorderedEquals; +using Catch::Matchers::Contains; + +TEST_CASE("test case 1", "[test 00 D]") { + std::streambuf *backup; + std::ifstream fin; + fin.open("./../test/lab_00/lab_00_D_data/01.data.in"); + backup = cin.rdbuf(); + cin.rdbuf(fin.rdbuf()); + // 重定向开始,开始run + cal(read()); + // 重定向结束 + cin.rdbuf(backup); +} + +TEST_CASE("test case 2", "[test 00 C]") { + std::streambuf *strmin_buf = cin.rdbuf(); + std::streambuf *strmout_buf = cout.rdbuf(); + std::ifstream file_in; + std::ofstream file_out; + file_in.open("./../test/lab_00/lab_00_D_data/01.data.in"); + file_out.open("./../test/lab_00/lab_00_D_data/01.test.out"); + cin.rdbuf(file_in.rdbuf()); + cout.rdbuf(file_out.rdbuf()); + cal(read()); + cin.rdbuf(strmin_buf); + cout.rdbuf(strmout_buf); +} + +#endif //CS203_DSAA_TEST_MACRO \ No newline at end of file