首先需要新建一个对拍文件夹,在该文件夹下建立源程序,需要对拍的程序,数据生成器,还有对拍程序文件。
例如我们接下来要对拍一个A+B的程序:
//这是我们的程序,命名为my.cpp
#include<iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
while (b--) a++;
cout << a;
return 0;
}
//这是源程序,命名为std.cpp
#include<iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a+b;
return 0;
}
//数据生成器,命名为data.cpp
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main() {
srand(time(0));//以当前时间点初始化随机数种子
int a = rand()%100+1, b = rand()%100+1;//1-100内的随机数
cout << a << " " << b;
return 0;
}
//对拍程序,命名为check.cpp
#include<iostream>
#include<cstdlib>
using namespace std;
int main() {
int t = 10;//对拍十组
while (t--) {
system("data.exe>data.txt");
system("my.exe<data.txt>my.txt");
system("std.exe<data.txt>std.txt");
if (system("fc std.txt my.txt")) {
cout <<"error" << endl;
break;
}
else cout << "no error" << endl;
}
return 0;
}
要对拍时需要将my和std以及data先编译,然后在编译check,最后运行check可执行程序即可。