天天看點

C++版題目随機測試資料編寫器

介紹

有許多OJ平台及各大比賽的出題人需要出很多的測試資料,但卻自己編不出來?

下面這些建議你看看

(雖然網上有很多其他的bat版随機測試資料編寫器,但是我還是喜歡用我這種C++版的)

主程式

.in

檔案生成(

data_make_in.cpp

#include<math.h>
#include<string>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<time.h>
#define ll long long
using namespace std;
int R(int x){return (rand()*rand())%x+1;}  //随機數的生成
string s="name";  //在此處改為題目名(僅支援英文)
void s70(){  //對于100%的資料進行以下輸出
	FILE* lin=fopen(s.c_str(),"w");
	
    
	fclose(lin);
	return ;
}
void s30(){  //對于70%的資料進行以下輸出
	FILE* lin=fopen(s.c_str(),"w");
	
    
	fclose(lin);
	return ;
}
void s00(){  //對于30%的資料進行以下輸出
	FILE* lin=fopen(s.c_str(),"w");
	
    
	fclose(lin);
	return ;
}
int main(){
	int tt;
	int typ=tt=20;  //在此處修改測試資料組數,預設為20
	int len=s.size();
	s+=char(tt/100+'0');
	s+=char((tt/10)%10+'0');
	s+=char(tt%10+'0');
	s+=".in";
	srand((unsigned)time(NULL));
	while(typ--){
		if(typ>=tt*70/100)  //表示對于100%的資料進行以下生成
			s70();
		else if(typ>=tt*30/100)  //表示對于70%的資料進行以下生成
			s30();
		else  //表示對于30%的資料進行以下生成
			s00();
        //如需增加其他節點可按照這些樣例來加入
		s[len]='0';
		s[len+1]='0'+typ/10;
		s[len+2]='0'+typ%10;
	}
	return 0;
}
           

在上方代碼的标記位置修改測試資料組數以及題目名(僅支援英文)。

在上方代碼留白的地方進行生成操作。注意 :輸出需用

fprintf(lin,項目1,項目2)

,詳情可自行搜尋

.out

檔案生成(

data_make_out.cpp

#include<cstdlib>
#include<iostream>
#include<string>
#include<cstring>
#include<ctime>
#include<cstdio>
#include<windows.h>
using namespace std;
int main(){
	string name,in=".in",out=".out",exe=".exe",exefile=".exe",infile=".in",outfile=".out";
	int n;
	printf("請輸入題目名:");
	cin>>name;
	printf("請輸入資料in個數:");
	scanf("%d",&n);
	exefile.insert(0,name);
	infile.insert(0,name);
	outfile.insert(0,name);
	string cmd2="del ",cmd4="del ";
	for(int i=n;i>=1;i--){
		string cmd1="copy ",cmd3="copy ";
		cmd1+=name;
		cmd1+=char(i/100+'0');
		cmd1+=char((i/10)%10+'0');
		cmd1+=char(i%10+'0');
		cmd1+=in;
		cmd1+=" ";
		cmd1+=infile;
		system(cmd1.c_str());
		system(exefile.c_str());
		cmd3+=outfile;
		cmd3+=" ";
		cmd3+=name;
		cmd3+=char(i/100+'0');
		cmd3+=char((i/10)%10+'0');
		cmd3+=char(i%10+'0');
		cmd3+=out;
		system(cmd3.c_str());
		printf("NO.%d\n",i); 
	}
	cmd2+=infile;
	cmd4+=outfile;
	system(cmd2.c_str());
	system(cmd4.c_str());
	return 0;
}
           

上面的代碼你不需要進行任何的改動,直接複制然後編譯運作即可,照着運作要求進行就行

注意 :所有前面生成的所有

.in

檔案、正解的

.exe

檔案和上面這個

.cpp

編譯出來的

.exe

檔案都要放在同一目錄下!!

注意 :正解需要用加上

freopen

,輸入檔案名為

題目名+.in

,輸出檔案名為

題目名+.out

,更多有關内容可自行搜尋

結果

結尾

c++

繼續閱讀