upgrade to python3;add some validator examples
This commit is contained in:
54
examples/float_validator.cpp
Normal file
54
examples/float_validator.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
// 浮点数误差比较验证程序
|
||||
// 编译: g++ -o float_validator float_validator.cpp
|
||||
// 使用: ./float_validator input.txt expected.txt result.txt
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
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]); // 用户输出
|
||||
|
||||
if (!expected.is_open() || !result.is_open()) {
|
||||
cout << "WA" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
double exp_val, res_val;
|
||||
|
||||
// 读取期望的浮点数
|
||||
if (!(expected >> exp_val)) {
|
||||
cout << "WA" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 读取用户输出的浮点数
|
||||
if (!(result >> res_val)) {
|
||||
cout << "WA" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 比较数值,允许相对误差和绝对误差
|
||||
double diff = fabs(exp_val - res_val);
|
||||
double rel_error = (fabs(exp_val) > EPS) ? diff / fabs(exp_val) : diff;
|
||||
|
||||
if (diff <= 1e-6 || rel_error <= 1e-6) { // 放宽误差范围
|
||||
cout << "AC" << endl;
|
||||
} else {
|
||||
cout << "WA" << endl;
|
||||
}
|
||||
|
||||
expected.close();
|
||||
result.close();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user