跳到正文

科技·工程

一个可以让你不用一个个freopen测试大样例的东西

TangLingxin约 5 分钟
原文

这是 2026年8月28日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年8月23日
收录
2026年8月23日
更新
2026年11月17日
篇幅
约 5 分钟
获赞
1

版本

  1. v3当前
  2. v2
  3. v1
1.0(仅比对,只有AC和UNAC)
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int AC=0;
	int ALL=4;//测试点数量 
	string name="game";//文件名称 
	for(int i=1;i<=ALL;i++)
	{
		string cmd1="std.exe<"+name+to_string(i)+".in>"+name+to_string(i)+".out";
		string cmd2="fc "+name+to_string(i)+".out "+name+to_string(i)+".ans";
		system(cmd1.c_str());
		if(system(cmd2.c_str())==0)
		{
			AC++;
		}	
	}
	cout<<"\n\n\n你获得了"<<(int)(AC*1.0/ALL*100.0)<<"分"; 
}
1.1(增加了RE与退出码)
C++
#include <bits/stdc++.h>
using namespace std;
// 根据退出码返回中文解释
string getExitCodeExplanation(int code)
{
	// 常见 Windows 异常代码(十进制)
	switch(code)
	{
		case 0xC0000005: // 3221225477
		{
			return "访问冲突(内存非法访问)";
		}
		case 0xC0000094: // 3221225620
		{
			return "整数除以零";
		}
		case 0xC00000FD: // 3221225725
		{
			return "栈溢出";
		}
		case 0xC000001D: // 3221225501
		{
			return "非法指令";
		}
		case 0xC0000008: // 3221225480
		{
			return "无效的句柄";
		}
		case 0xC0000135: // 3221225781
		{
			return "DLL 加载失败";
		}
		case 0xC000013A: // 3221225786
		{
			return "应用程序终止(Ctrl+C)";
		}
		case 0x00000001:
		{
			return "程序主动退出(错误码 1)";
		}
		case 0x00000000:
		{
			return "正常退出(不应出现)";
		}
		default:
		{
			// 其他非零值:如果小于 0x80000000 可能是普通退出码,否则为未知异常
			if(code<0)
			{
				return "未知异常(错误码 "+to_string(code)+")";
			}
			else
			{
				return "非零退出码("+to_string(code)+")";
			}
		}
	}
}
int main()
{
	int AC=0;
	int ALL=4;				 // 测试点数量
	string name="tree";		// 文件名称
	vector<string> statusList;   // 存储每个测试点的状态字符串
	for(int i=1;i<=ALL;i++)
	{
		string cmd1="std.exe<"+name+to_string(i)+".in>"+name+to_string(i)+".out";
		string cmd2="fc "+name+to_string(i)+".out "+name+to_string(i)+".ans";
		// 执行 std.exe,并捕获返回值
		unsigned int ret1=system(cmd1.c_str());
		string status;
		if(ret1!=0)
		{
			// 程序异常退出,输出十进制退出码及中文解释
			string explanation=getExitCodeExplanation(ret1);
			status="RE,退出码 "+to_string(ret1)+"("+explanation+")";
		}
		else
		{
			// 正常退出,比较输出
			if(system(cmd2.c_str())==0)
			{
				status="AC";
				AC++;
			}
			else
			{
				status="WA";
			}
		}
		statusList.push_back(status);
	}
	system("cls");
	// 全部测试完成后,统一输出每个测试点的信息
	for(int i=0;i<ALL;i++)
	{
		cout<<"测试点"<<(i+1)<<":"<<statusList[i]<<endl;
	}
	cout<<"\n\n\n你获得了 "<<(int)(AC*100.0/ALL)<<" 分"<<endl;
	return 0;
}

使用须知:需要确保大样例文件名都是形如名称+编号+.in/.out的形式,与CSP-J/S格式相同。std.cpp为你的程序代码,一定要编译。