对拍程序(易懂版)

目的:验证 a+b 程序的正确性


步骤1:

编写第一个程序1.cpp(正确性待验证)

#include <iostream>
using namespace std;

int main()
{
	int a,b;
	cin >> a >> b;
	if(a%10==5 || b%10==5)
        cout<<a+b+2;
    else
        cout<<a+b;
	return 0;
}

步骤2 :

编写第二个程序2.cpp(正确性毋庸置疑)

#include <iostream>
using namespace std;

int main()
{
	int a,b;
	cin >> a >> b;
    cout<<a+b;
	return 0;
}

步骤3:

编写第三个程序data.cpp(生成测试数据)

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	srand(time(0));
	int a = rand();
	int b = rand();
	cout<<a<<' '<<b<<endl;
	return 0;
}

步骤4:

编写第四个程序control.cpp(比对程序1、2的输出结果)

#include <iostream>
using namespace std;

int main()
{
    for(int i=0;i<200;i++)
    {
        system("data.exe >data.txt");
        system("1.exe <data.txt >1.txt");
        system("2.exe <data.txt >2.txt");
        if(system("fc 1.txt 2.txt"))
        {
            cout<<"通过的次数:"<<i<<endl;
            cout<<"输入的数据:";
            system("type data.txt");
            cout<<endl<<"WA"<<endl;
            return 0;
        }
    }
    cout<<"AC"<<endl;
	return 0;
}

(补充)

文件位置情况:

所有文件在同一目录下

在这里插入图片描述

编译和执行顺序:

1.cpp → 2.cpp → data.cpp → control.cpp
1.cpp、2.cpp、3.cpp 仅需要编译
control.cpp 编译加执行

执行效果:

在这里插入图片描述

打包后的所有文件

自取链接不需要积分/C币下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

todaycode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值