版本比较
Win-Cpp-Judger:一个轻量级C++代码测评工具
正文
增加 25 行删除 25 行
65 行未改动66676869707071727273747575767778798080818283848586878889909091929393949596979898991001011026 行未改动10911011111211311311411511611711711811912012012112212312412512612612712812913013113213313313413513613713713813914014114114214314314414514614714814814915015115215215315415515615715815916016116116216316416416516616716816917017117217317417417517617717817918018118218318418418518618618718818919019119119219319419563 行未改动25926026126226326326426526626716 行未改动28428528628728828828929029129256 行未改动65 行未改动// 运行程序并获取输出string runProgram(const string& inputFile) { string command = TEMP_EXE + " < " + inputFile + " > temp_output.txt"; system(command.c_str());
ifstream outputFile("temp_output.txt"); string output((istreambuf_iterator<char>(outputFile)), string output((istreambuf_iterator<char>(outputFile)), istreambuf_iterator<char>()); outputFile.close();
// 去除末尾的换行和空格 while (!output.empty() && (output.back() == '\n' || output.back() == '\r' || output.back() == ' ')) { output.pop_back(); }
return output;}
// 读取文件内容string readFile(const string& filename) { ifstream file(filename); if (!file.is_open()) { return ""; } string content((istreambuf_iterator<char>(file)), string content((istreambuf_iterator<char>(file)), istreambuf_iterator<char>()); file.close();
// 去除末尾的换行和空格 while (!content.empty() && (content.back() == '\n' || content.back() == '\r' || content.back() == ' ')) { content.pop_back(); }
return content;}
// 检查目录是否存在,不存在则创建6 行未改动int main() { int n, m; cout << "请输入需测试代码数和测试点数: "; cin >> n >> m;
// 检查并创建必要的目录 checkAndCreateDir(TESTPOINT_DIR); checkAndCreateDir(CPP_DIR);
for (int i = 1; i <= n; i++) { string cppFile = CPP_DIR + "/" + to_string(i) + ".cpp";
// 检查cpp文件是否存在 if (_access(cppFile.c_str(), 0) == -1) { cout << "代码文件 " << cppFile << " 不存在,跳过测试" << endl; continue; }
// 编译程序 cout << "正在编译 " << cppFile << "..." << endl; if (!compileProgram(cppFile)) { cout << "编译失败: " << cppFile << endl; continue; }
vector<int> passedTests; vector<int> failedTests; int score = 0;
for (int j = 1; j <= m; j++) { string inputFile = TESTPOINT_DIR + "/" + to_string(j) + ".in"; string expectedOutputFile = TESTPOINT_DIR + "/" + to_string(j) + ".out";
// 检查测试文件是否存在 if (_access(inputFile.c_str(), 0) == -1 || if (_access(inputFile.c_str(), 0) == -1 || _access(expectedOutputFile.c_str(), 0) == -1) { cout << "测试点 " << j << " 的文件不存在,跳过" << endl; continue; }
// 运行程序并获取输出 string actualOutput = runProgram(inputFile); string expectedOutput = readFile(expectedOutputFile);
// 比较输出 if (actualOutput == expectedOutput) { passedTests.push_back(j); score += 10; } else { failedTests.push_back(j); } }
// 输出结果 cout << "\n目前测试代码名: " << cppFile << endl;
cout << "通过测试点: "; if (passedTests.empty()) { cout << "无"; } else { for (int test : passedTests) { cout << test << " "; } } cout << endl;
cout << "未通过测试点: "; if (failedTests.empty()) { cout << "无"; } else { for (int test : failedTests) { cout << test << " "; } } cout << endl;
cout << "得分: " << score << "/" << m * 10 << "\n\n" << endl;
// 清理临时文件 remove(TEMP_EXE.c_str()); remove("temp_output.txt"); }
return 0;}```
63 行未改动正在编译 cpps/1.cpp...正在编译 cpps/2.cpp...
目前测试代码名: cpps/1.cpp通过测试点: 1 2 3 通过测试点: 1 2 3未通过测试点: 无得分: 30/30
16 行未改动自动检查并创建所需目录,避免因目录缺失导致的运行错误。
### 输出标准化```cppwhile (!output.empty() && while (!output.empty() && (output.back() == '\n' || output.back() == '\r' || output.back() == ' ')) { output.pop_back();}```56 行未改动