对拍程序

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream fans("1.ans");  // 答案文件,命名为fans变量 
    ifstream fout("1.out");  // 输出文件,命名为fout变量 
    string ans_line, out_line;
    int line = 1;
    while (getline(fans, ans_line)) { //先从答案ans读取 
        if (!getline(fout, out_line)) { //如果无法从out读取,说明输出缺少行 
            cout << "1.out缺少第" << line << "行,应为: " << ans_line << endl;
            return 0;
        }
        
        if (ans_line != out_line) { //行对不上 
            cout << "" << line << "行错误" << endl;
            cout << "期望: " << ans_line << endl;
            cout << "实际: " << out_line << endl;
            return 0;
        }
        line++;
    }
    // 检查1.out是否有多余行
    if (getline(fout, out_line)) {
        cout << "1.out多出第" << line << "行: " << out_line << endl;
        return 0;
    }
    
    cout << "AC" << endl;
    return 0;
}
/*
ifstream:输入文件流,用于从文件读取数据

getline(fans, ans_line) 功能:
    1.从文件流 fans 中读取一行内容
    2.将内容存储到字符串 ans_line 中
成功读取返回 true,读到文件末尾返回 false
*/

 

posted @ 2025-10-31 21:17  CRt0729  阅读(8)  评论(0)    收藏  举报