Logo Wy Online Judge

WyOJ

Paste!

#include <cassert>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

bool builtin_checker(const std::string& file1, const std::string& file2) {
	using namespace std;
    ifstream f1(file1);
    ifstream f2(file2);

    if (!f1.is_open()) {
        cout << "无法打开文件: " << file1 << endl;
        return true;
    }
    if (!f2.is_open()) {
        cout << "无法打开文件: " << file2 << endl;
        return true;
    }

    string line1, line2;
    int lineNum = 1;
    vector<int> diffLines;

    while (getline(f1, line1) && getline(f2, line2)) {
        // 去除行尾空格和换行符
        while (!line1.empty() && isspace(line1.back())) 
            line1.pop_back();
        while (!line2.empty() && isspace(line2.back())) 
            line2.pop_back();

        if (line1 != line2) {
            diffLines.push_back(lineNum);
            cout << "第 " << lineNum << " 行不同:" << endl;
            cout << "文件1: " << line1 << endl;
            cout << "文件2: " << line2 << endl;
            cout << "---" << endl;
        }
        lineNum++;
    }

    if (diffLines.empty()) {
        cout << "文件内容完全一致!" << endl;
        return false;
    }
    cout << "共发现 " << diffLines.size() << " 处不同" << endl;
    return true;
}

#define STDIO
// 答案后缀 
#define ANS_SUF "ans"

// argv[1] 是可执行文件名,大样例前缀是"",argv[2]是大样例数目 
// argv[1] 是可执行文件名,大样例前缀是argv[3],argv[2]是大样例数目 

// 用法两种
// 1. 本程序名 题目名称 要测试的大样例数量
// 2. 本程序名 题目名称 要测试的大样例数量 大样例前缀 

int main(int argc, char** argv) {
    using namespace std;
    string title = argv[1], pre = "";
    int cnt = stoi(argv[2]);
    if(argc == 4)
        pre = argv[3];
    for(int i = 1; i <= cnt; i++)
    {
        system(("copy " + pre + to_string(i) + ".in " + title + ".in").c_str());
        auto start = clock();
#ifdef STDIO
		system((title + " < " + title + ".in >" + title + ".out").c_str());
#else
        system(title.c_str());
#endif
        double time = double(clock() - start) / CLOCKS_PER_SEC;
        printf("%f s ", time);
        if(builtin_checker(title + ".out", pre + to_string(i) + "." ANS_SUF))
            return 16;
        system(("del " + title + ".in").c_str()); 
    }
    return 0;
}