码迷,mamicode.com
首页 > Windows程序 > 详细

Windows系统对拍程序

时间:2019-02-07 20:44:24      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:windows系统   har   info   pac   system   win   clu   nes   note   

Windows系统对拍程序,其中包含c++11用法,请使用c++11编译。

此对拍程序自动使用g++对源代码进行编译。如果出现找不到g++错误,请将g++所在目录添加至系统的环境变量列表中。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <ctime>
 5 
 6 // 设置区
 7 namespace Settings {
 8     const int MAX_LEN = 20; // 当发现错误时,显示错误行数据的最大长度
 9     const int CASE_CNT = 100; // 测试次数
10     const int BUFFER_SIZE = 1 << 10; // 每行的缓冲区大小
11     const char *const testFile = "1.cpp"; // 需要测试的源代码文件名(相对路径)
12     const char *const stdFile = "2.cpp"; // 标准答案的源代码文件名(相对路径)
13     const char *const dataFile = "3.cpp"; // 随机生成数据的源代码文件名(相对路径)
14 }
15 
16 using namespace Settings;
17 using namespace std;
18 char cmd[BUFFER_SIZE], info[BUFFER_SIZE];
19 char buf1[BUFFER_SIZE], buf2[BUFFER_SIZE];
20 
21 template <typename ...T>
22 int run(const char *str, T ...args) {
23     sprintf(cmd, str, args...);
24     return system(cmd);
25 }
26 
27 bool fileCompare(const char *stdOutput, const char *userOutput) {
28     FILE *fp_std = fopen(stdOutput, "r"), *fp_user = fopen(userOutput, "r");
29     if (fp_std == nullptr || fp_user == nullptr) {
30         perror(nullptr);
31         return false;
32     }
33     for (int i = 1; !feof(fp_std) && !feof(fp_user); ++i) {
34         char *p1 = fgets(buf1, BUFFER_SIZE, fp_std);
35         char *p2 = fgets(buf2, BUFFER_SIZE, fp_user);
36         if (p1 == nullptr && p2 == nullptr) {
37             return true;
38         } else if (!p1 || !p2 || strcmp(p1, p2)) {
39             sprintf(info,
40                     "Difference found in line %d:\n  std: %.*s user: %.*s",
41                     i, MAX_LEN, p1 ? p1 : "(EOF Detected)\n",
42                     MAX_LEN, p2 ? p2 : "(EOF Detected)\n");
43             break;
44         }
45     }
46     return false;
47 }
48 
49 int main() {
50     printf("Compiling... ");
51     run("g++ %s -o test.exe", testFile);
52     run("g++ %s -o std.exe", stdFile);
53     run("g++ %s -o data.exe", dataFile);
54     printf("Finished.\n");
55     clock_t st, ed;
56     for (int i = 1; i <= CASE_CNT; ++i) {
57         printf("Case %03d: ", i);
58         run("data.exe >data.txt");
59         run("std.exe <data.txt >std.txt");
60         st = clock();
61         run("test.exe <data.txt >test.txt");
62         ed = clock();
63         if (!fileCompare("std.txt", "test.txt")) {
64             printf("Wrong Answer.\n------------------------------\n");
65             puts(info);
66             printf("------------------------------\nNote: At most %d characters will be shown for both lines.\n", MAX_LEN);
67             printf("Press anykey to exit.\n");
68             getchar();
69             break;
70         } else {
71             printf("Accepted. Time: %dms.\n", int(ed - st));
72         }
73     }
74     return 0;
75 }

 

Windows系统对拍程序

标签:windows系统   har   info   pac   system   win   clu   nes   note   

原文地址:https://www.cnblogs.com/sandychn/p/10355239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!