-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson_test.cpp
More file actions
330 lines (305 loc) · 9.37 KB
/
lesson_test.cpp
File metadata and controls
330 lines (305 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "lesson_test.hpp"
#include "zip/miniz.h"
#include <algorithm>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#if _WIN32
#include <windows.h>
#endif
namespace lesson_test
{
std::string read()
{
std::string str;
std::getline(std::cin, str);
while (!str.empty())
{
auto back = str.back();
if (back == '\n' || back == '\t' || back == ' ')
{
str.pop_back();
continue;
}
break;
}
return str;
}
MCQ::MCQ(std::string_view question, std::vector<std::pair<std::string_view, bool>> &&options, std::string_view hint,
std::string_view solution)
{
this->info.question = std::string{question};
std::uint32_t index{0};
for (const auto &[text, is_answer] : options)
{
this->info.options.emplace_back(std::string{text});
if (is_answer)
this->info.answer += static_cast<char>('A' + index);
++index;
}
this->info.hint = std::string{hint};
this->info.solution = std::string{solution};
}
void MCQ::test() const
{
std::cout << this->info.question << std::endl << std::endl;
std::cout << "选项:" << std::endl;
std::size_t index{0};
for (const auto context : this->info.options)
{
std::cout << static_cast<char>('A' + index) << '.' << context << std::endl;
++index;
}
std::cout << std::endl;
if (!this->info.hint.empty())
std::cout << "ℹ️ 提示:" << info.hint << std::endl << std::endl;
bool is_correct{false};
bool has_input{false};
while (true)
{
std::cout << "输入你的答案:";
auto user_input = read();
user_input.erase(std::remove_if(user_input.begin(), user_input.end(), [](auto ch) { return std::isspace(ch); }),
user_input.end());
if (user_input.length() >= 1)
{
has_input = true;
std::transform(user_input.begin(), user_input.end(), user_input.begin(),
[](auto ch) { return std::toupper(ch); });
std::sort(user_input.begin(), user_input.end());
if (user_input == this->info.answer)
{
is_correct = true;
if (this->type == test_type::practice)
std::cout << "✅ 正确" << std::endl << std::endl;
}
else if (this->type == test_type::practice)
{
std::cout << "❌ 错误" << std::endl << std::endl;
}
}
else
{
std::cout << "你没有输入任何字符" << std::endl << std::endl;
}
if (type == test_type::examination && has_input)
{
std::cout << std::endl;
score_percent = 0;
return;
}
if (is_correct)
break;
}
if (!this->info.solution.empty())
std::cout << "📝 解析:" << this->info.solution << std::endl << std::endl;
score_percent = 1;
return;
}
CRP::CRP(std::string_view source_code)
{
this->source_code = source_code;
}
void CRP::add(std::string_view input, std::string_view answer, std::string_view hint, std::string_view solution)
{
test_info info;
info.input = std::string{input};
info.answer = this->split(answer);
info.hint = std::string{hint};
info.solution = std::string{solution};
this->infos.emplace_back(std::move(info));
}
void CRP::add(std::string_view input, const std::function<void()> &answer, std::string_view hint,
std::string_view solution)
{
test_info info;
info.input = std::string{input};
std::ostringstream oss;
std::streambuf *oldBuf = std::cout.rdbuf(oss.rdbuf());
answer();
std::cout.rdbuf(oldBuf);
info.answer = this->split(oss.str());
info.hint = std::string{hint};
info.solution = std::string{solution};
this->infos.emplace_back(std::move(info));
}
std::vector<std::string> CRP::split(std::string_view output) const
{
std::istringstream iss{std::string{output}};
std::vector<std::string> realLines;
std::string line;
while (std::getline(iss, line))
{
realLines.push_back(line);
}
return realLines;
}
void CRP::test() const
{
std::cout << "源代码:" << std::endl;
std::cout << this->source_code << std::endl << std::endl;
std::uint64_t test_group_number{this->infos.size()};
std::int32_t pass_number{0};
for (auto info : this->infos)
{
std::int32_t attempt{1};
while (true)
{
if (!info.input.empty())
{
std::cout << "输入:" << std::endl;
std::cout << info.input << std::endl << std::endl;
}
if (!info.hint.empty())
std::cout << "ℹ️ 提示:" << info.hint << std::endl << std::endl;
std::cout << "程序应该输出:" << std::endl;
bool all_correct{true};
for (size_t i = 0; i < info.answer.size(); ++i)
{
std::cout << "第 " << (i + 1) << " 行: ";
auto user_ans = read();
if (user_ans == info.answer[i])
{
if (this->type == test_type::practice)
std::cout << "✅ 正确" << std::endl << std::endl;
}
else
{
if (this->type == test_type::practice)
std::cout << "❌ 错误" << std::endl << std::endl;
all_correct = false;
break;
}
}
if (type == test_type::examination)
{
std::cout << std::endl;
if (all_correct)
++pass_number;
break;
}
if (all_correct)
{
std::cout << "全部正确!共 " << info.answer.size() << " 行,尝试次数:" << attempt << std::endl
<< std::endl;
break;
}
++attempt;
}
if (!info.solution.empty())
{
if (type == test_type::practice)
std::cout << "📝 解析:" << info.solution << std::endl << std::endl;
}
}
score_percent = static_cast<double>(pass_number) / test_group_number;
return;
}
ICP::ICP(unsigned char *zip, unsigned int size, std::string_view path, std::string_view key)
{
info.data = zip;
info.size = size;
info.path = std::string{path};
info.key = std::string{path};
}
void ICP::write_zip() const
{
std::ofstream f(info.path, std::ios::binary);
f.write((const char *)info.data, info.size);
}
void ICP::unzip() const
{
mz_zip_archive zip{};
mz_zip_zero_struct(&zip);
mz_zip_reader_init_file(&zip, info.path.c_str(), 0);
int file_count = (int)mz_zip_reader_get_num_files(&zip);
for (int i = 0; i < file_count; i++)
{
mz_zip_archive_file_stat st;
mz_zip_reader_file_stat(&zip, i, &st);
std::string out_path = std::string{"./"} + st.m_filename;
if (st.m_is_directory)
{
std::filesystem::create_directories(out_path);
}
else
{
std::filesystem::create_directories(std::filesystem::path(out_path).parent_path());
mz_zip_reader_extract_to_file(&zip, i, out_path.c_str(), 0);
}
}
mz_zip_reader_end(&zip);
}
void ICP::test() const
{
while (true)
{
std::cout << "输入你获取到的key:";
auto user_input = read();
if (user_input.empty())
{
std::cout << "你没有输入任何字符" << std::endl << std::endl;
continue;
}
if (user_input == info.key)
this->score_percent = 1;
if (type == test_type::examination)
break;
if (this->score_percent == 1)
{
std::cout << "✅ 正确" << std::endl << std::endl;
break;
}
else
std::cout << "❌ 错误" << std::endl << std::endl;
}
}
void interact_tester::set_console_utf8()
{
#if _WIN32
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
#endif
}
interact_tester::interact_tester()
{
#if _WIN32
this->set_console_utf8();
#endif
}
interact_tester::interact_tester(std::string_view title)
{
#if _WIN32
this->set_console_utf8();
#endif
this->title = std::string{title};
}
void interact_tester::add(base_question *question)
{
this->question_list.emplace_back(question);
}
void interact_tester::run(test_type type)
{
if (!this->title.empty())
{
std::cout << "😀 欢迎来到 " << title << " 测试程序!" << std::endl;
}
std::cout << "本次测试共有 " << question_list.size() << " 道题目" << std::endl << std::endl;
std::uint64_t average_score{100 / question_list.size()};
std::int32_t real_score{0};
std::int32_t id{0};
for (const auto question : question_list)
{
std::cout << "第 " << ++id << " 题:" << std::endl << std::endl;
question->test();
real_score += question->score_percent * average_score;
}
if (type == test_type::practice)
std::cout << "🎉 恭喜通过所有测试! 按回车键退出程序" << std::endl;
else
std::cout << "🎉 恭喜完成考试! 得分:" << real_score << " 分 按回车键退出程序" << std::endl;
std::cin.get();
}
} // namespace lesson_test