upgrade to python3;add some validator examples

This commit is contained in:
2025-10-11 12:33:24 +08:00
parent 65632f0e60
commit 487c041148
25 changed files with 1492 additions and 317 deletions

View File

@@ -0,0 +1,183 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
BitOJ 自定义验证程序集成示例
演示如何在实际的BitOJ系统中使用自定义验证程序
"""
import sys
sys.path.append('../python')
try:
from judgescript import InternalJudge
except ImportError:
# 如果无法导入,创建一个模拟的类
class InternalJudge:
def __init__(self, allowpe=False):
self.allowpe = allowpe
def compare_string(self, output, result):
if output == result:
return 'AC'
if self.allowpe:
# 简化的PE检查
if ''.join(output.split()) == ''.join(result.split()):
return 'PE'
return 'WA'
def demo_internal_judge():
"""演示内置判题器的使用"""
print("=== 内置判题器演示 ===")
# 标准文本比较
ij_strict = InternalJudge(allowpe=False)
print("严格文本比较:")
result = ij_strict.compare_string("hello world\n", "hello world\n")
print(f" 'hello world\\n' vs 'hello world\\n' => {result}")
result = ij_strict.compare_string("hello world", "hello world\n")
print(f" 'hello world' vs 'hello world\\n' => {result}")
# 允许格式错误的比较
ij_pe = InternalJudge(allowpe=True)
print("\n允许格式错误的比较:")
result = ij_pe.compare_string("1 2 3 4 5\n", "1\n2\n3\n4\n5\n")
print(f" '1 2 3 4 5\\n' vs '1\\n2\\n3\\n4\\n5\\n' => {result}")
result = ij_pe.compare_string("12345\n", "1 2 3 4 5\n")
print(f" '12345\\n' vs '1 2 3 4 5\\n' => {result}")
def demo_custom_judge_config():
"""演示如何配置自定义验证程序"""
print("\n=== 自定义验证程序配置示例 ===")
# 浮点数比较验证程序代码
float_validator_code = """
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
const double EPS = 1e-9;
int main(int argc, char* argv[]) {
if (argc != 4) {
cout << "WA" << endl;
return 0;
}
ifstream expected(argv[2]);
ifstream result(argv[3]);
double exp_val, res_val;
if (!(expected >> exp_val) || !(result >> res_val)) {
cout << "WA" << endl;
return 0;
}
if (fabs(exp_val - res_val) <= EPS) {
cout << "AC" << endl;
} else {
cout << "WA" << endl;
}
return 0;
}
"""
print("浮点数比较验证程序配置:")
print(" 语言: gcc-3.3")
print(" 代码长度:", len(float_validator_code), "字符")
print(" 功能: 允许浮点数误差比较")
# 多解验证程序代码
multi_solution_code = """
import sys
def main():
try:
with open(sys.argv[2], 'r') as f:
expected = f.read().strip()
with open(sys.argv[3], 'r') as f:
result = f.read().strip()
exp_nums = sorted(map(int, expected.split()))
res_nums = sorted(map(int, result.split()))
if exp_nums == res_nums:
print("AC")
else:
print("WA")
except:
print("WA")
if __name__ == "__main__":
main()
"""
print("\n多解验证程序配置:")
print(" 语言: python-2.5")
print(" 代码长度:", len(multi_solution_code), "字符")
print(" 功能: 允许多种正确答案")
def demo_judge_usage_scenarios():
"""演示不同判题方式的使用场景"""
print("\n=== 判题方式使用场景 ===")
scenarios = [
{
"名称": "字符串精确比较",
"适用": "输出结果固定的算法题",
"示例": "计算1+1的结果",
"验证方式": "InternalJudge(allowpe=False)"
},
{
"名称": "字符串容错比较",
"适用": "允许格式差异的题目",
"示例": "输出多个数字,允许不同的分隔符",
"验证方式": "InternalJudge(allowpe=True)"
},
{
"名称": "文件大小+MD5校验",
"适用": "二进制文件输出",
"示例": "图像处理、音频处理等",
"验证方式": "文件级别的校验"
},
{
"名称": "自定义验证程序",
"适用": "复杂的判题逻辑",
"示例": "浮点数比较、多解题目、交互题等",
"验证方式": "ExternalJudge"
}
]
for i, scenario in enumerate(scenarios, 1):
print(f"{i}. {scenario['名称']}")
print(f" 适用场景: {scenario['适用']}")
print(f" 典型示例: {scenario['示例']}")
print(f" 实现方式: {scenario['验证方式']}")
print()
def main():
"""主演示函数"""
print("BitOJ 判题系统验证程序演示")
print("=" * 50)
demo_internal_judge()
demo_custom_judge_config()
demo_judge_usage_scenarios()
print("演示完成!")
print("\n更多示例文件:")
print(" - float_validator.cpp: 浮点数比较验证程序")
print(" - multi_solution_validator.py: 多解验证程序")
print(" - StringFormatValidator.java: 格式验证程序")
print(" - range_validator.cpp: 数值范围验证程序")
if __name__ == "__main__":
main()