Linux相关——手写测试程序

由于本人太弱,,,不会lemon,,,也不会在ubuntu下安装lemon,所以我选择手写测试程序emmmm

首先要写这个东西我们要先知道对拍怎么写。

 1     for(int i = 1; i <= 1000; i++)
 2     {
 3         system("./make >in.in");
 4         system("./work >out.out");
 5         system("./work1 >out1.out");
 6         if(system("diff -bB out.out out1.out"))
 7         {
 8             printf("error in %d\n", i);
 9             break;
10         }
11         else
12         {
13             printf("passed... %d\n", i);
14             system("sleep 1");
15         }
16     } 

那么我们的测试程序就是基于这个对拍程序改的。

因为system里面的都是字符串,因此我们要用到字符串,但怎么用呢?

我们可以用一些小字符串来拼凑出一个大字符串。比如这样:

1         s = "./work < " + tmp + ".in";

其中s和tmp都是string。

tmp即代表当前测试的数据组数。

因为通常我们的数据包是这样的:1.in 1.out 2.in 2.out……

因为后缀相同,所以我们只需要用tmp存下前面的数字就好了。那么我们要怎么获取呢?

 1 void get(int x)
 2 {
 3     tot = 0;
 4     while(x) m[++tot] = x % 10, x /= 10;
 5     tmp = "";
 6     for(R i = tot; i; i --)
 7     {
 8         tmp += m[i] + '0';
 9     }
10 }

因为tmp也是字符串,所以我们可以手写一个get函数,来实现把数x变为字符串后放入tmp

值得注意的是,字符串的初始化不能直接等于0,(虽然我看网上好多blog都是直接=0,但是我这么用会报错,,,我也不知到为什么);

所以我们令tmp = "";

然后再把数放进去,放进去的方式也是和前面一样的加入一个个的小字符串。

于是我们现在有了应该放在system("")里的字符串,那么我们要如何放进去呢?

 1     for(R i = 1; i <= 10; i++)
 2     {
 3         get(i);
 4         s = "./work < " + tmp + ".in";
 5         system(s.c_str());
 6         s = "diff -bB a.out " + tmp + ".out";
 7         if(system(s.c_str()))
 8         {
 9             printf("get %d points\n", (i - 1) * 10);
10             exit(0);  
11         }
12     }

我们可以直接使用.c_str()来获取当前字符串的首字符地址,然后放入原本需要字符串的地方,注意不要 " "  哦。

放上完整代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define R register int
 4 string s,tmp;
 5 int m[10], tot;
 6 
 7 void get(int x)
 8 {
 9     tot = 0;
10     while(x) m[++tot] = x % 10, x /= 10;
11     tmp = "";
12     for(R i = tot; i; i --)
13     {
14         tmp += m[i] + '0';
15     }
16 }
17 
18 int main()
19 {
20     for(R i = 1; i <= 10; i++)
21     {
22         get(i);
23         s = "./work < " + tmp + ".in";
24         system(s.c_str());
25         s = "diff -bB a.out " + tmp + ".out";
26         if(system(s.c_str()))//貌似这里不是很妥当,因为测到一组错误就退出了,其实应该可以加个变量记录正确组数,最后输出得分来着
27         {
28             printf("get %d points\n", (i - 1) * 10);
29             exit(0);  
30         }
31     }
32     printf("get 100 points!!!🤠\n");
33     return 0;
34 }

上面那个表情只是来卖萌的O__O "…

 

今天修正了一个错误……我居然还用了那么久。。。

posted @ 2018-07-20 23:42  ww3113306  阅读(576)  评论(2编辑  收藏  举报
知识共享许可协议
本作品采用知识共享署名-非商业性使用-禁止演绎 3.0 未本地化版本许可协议进行许可。