Skip to content

Commit

Permalink
update readme, add questionD and it's solution.
Browse files Browse the repository at this point in the history
Signed-off-by: Certseeds <[email protected]>
  • Loading branch information
Certseeds committed Jul 21, 2020
1 parent 8a88b75 commit a3ae8d4
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 9 deletions.
5 changes: 3 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -15,6 +15,7 @@
*.LICENSE text
*.template text diff=text
*.in text eol=lf
*.out text eol=lf

# scripts
*.sh text eol=lf
Expand Down Expand Up @@ -114,7 +115,7 @@

# Executables
*.exe binary
*.out binary
#*.out binary
*.app binary

# Visual Studio
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-->

Expand Down Expand Up @@ -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: 介绍一些神秘操作.比如手动开优化等.
Expand Down
134 changes: 134 additions & 0 deletions source/lab_00/lab_00_D.cpp
Original file line number Diff line number Diff line change
@@ -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 <algorithm>
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::string;
using std::tuple;
using std::vector;
const std::string end = "\n";

const vector<std::pair<int32_t, string>> spis{
{2, "+---+"},
{1, "/ /|"},
{0, "+---+ |"},
{0, "| | +"},
{0, "| |/"},
{0, "+---+"}
};

using input_data = vector<vector<int32_t>>;
using result_data = vector<vector<char>>;

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<int32_t>(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<int>(spis.size()); i++) {
for (int j = 0; j < static_cast<int>(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<vector<char>> out(302, vector<char>(301, '.'));
int m = static_cast<int>(hi.size()) - 1;
int n = static_cast<int>(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;
}
}
2 changes: 1 addition & 1 deletion test/lab_00/lab_00_A_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions test/lab_00/lab_00_C_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]") {
Expand Down Expand Up @@ -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]") {
Expand All @@ -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]") {
Expand Down
4 changes: 4 additions & 0 deletions test/lab_00/lab_00_D_data/01.data.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3 4
2 2 1 2
2 2 1 1
3 2 1 2
13 changes: 13 additions & 0 deletions test/lab_00/lab_00_D_data/01.data.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
......+---+---+...+---+
..+---+ / /|../ /|
./ /|-+---+ |.+---+ |
+---+ |/ /| +-| | +
| | +---+ |/+---+ |/|
| |/ /| +/ /|-+ |
+---+---+ |/+---+ |/| +
| | | +-| | + |/.
| | |/ | |/| +..
+---+---+---+---+ |/...
| | | | | +....
| | | | |/.....
+---+---+---+---+......
13 changes: 13 additions & 0 deletions test/lab_00/lab_00_D_data/01.test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
......+---+---+...+---+
..+---+ / /|../ /|
./ /|-+---+ |.+---+ |
+---+ |/ /| +-| | +
| | +---+ |/+---+ |/|
| |/ /| +/ /|-+ |
+---+---+ |/+---+ |/| +
| | | +-| | + |/.
| | |/ | |/| +..
+---+---+---+---+ |/...
| | | | | +....
| | | | |/.....
+---+---+---+---+......
78 changes: 78 additions & 0 deletions test/lab_00/lab_00_D_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <tuple>
#include <iostream>
#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

0 comments on commit a3ae8d4

Please sign in to comment.