栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

脚本编程和对拍

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

脚本编程和对拍

常用linux和windows命令
  • 分类 Linux命令 Windows命令
  • 文件列表 ls dir
  • 改变/创建/删除目录 cd/mkdir/rmdir cd/md/rd
  • 显示文件内容 cat/more type/more
  • 比较文件内容 diff fc
  • 修改文件属性 chmod attrib
  • 复制文件 cp copy/xcopy
  • 删除文件 rm del
  • 文件改名 mv ren
  • 回显 echo echo
  • 关闭命令行 exit exit
  • 在文件中查找字符串 grep find
  • 查看/修改环境变量 set set
  • 帮助 man <命令> help <命令>
脚本 windows脚本
@echo off
:loop
	gen > t.in
	a < t.in > a.out
	b < t.in > b.out
	fc a.out b.out
if not errorlevel 1 goto loop
pause
linux脚本
#!/bin/bash
while true; do
	./gen > input   #生成随机数据
	./a < input > a.out
	./b < input > b.out
	diff output a.out b.out          #文件比较
	if [ $? -ne 0 ] ; then break; fi #判断返回值
done

保存为test.sh后,再执行chmod +x tesh.sh, 即可用./test.sh来执行它。

对拍

在平常或比赛时,常常需要验证程序的正确性或找出bug。采用对拍可以极大地提高程序的正确率。一般写一个暴力的程序(不考虑时间、只确保正确性)来和要验证的程序对比。此外还需要一个数据生成器,随机生成数据。利用脚本的批处理来对比程序不一样的结果。

  • 暴力程序
  • 待测试程序
  • 随机数据生成
  • 脚本执行
随机数

利用系统自带的随机数发生 器:在Windows下是环境变量%random%,而在bash中是$RANDOM。它们都是0~32767之间 的随机整数。

#include

using namespace std;
typedef long long LL;

#define random(a,b) ((a)+rand()%((b)-(a)+1))
const LL N = 1e16 + 10;

stringstream ss;

LL randll(){//处理成ll形随机数
	LL t = 0;
	for(int i= 0; i<4; i++){
		t *= (LL)1<<15;
		t += rand();
	}
	return t;
}

int main( int argc, int argv[] ) //在脚本中传入%random%随机数
{ 
    srand(argv[0]);
    
    printf("%lld %lldn", randll(), randll());
    return 0;
}
脚本
@echo off
:loop
	gen %random% > t.in ;生成随机输入
	a < t.in > a.out
	b < t.in > b.out
	fc a.out b.out      ;比较两个文件
if not errorlevel 1 goto loop ;相同继续循环
pause
c实现
#include 
#include 

int main() {
  // For Windows
  // 对拍时不开文件输入输出
  // 当然,这段程序也可以改写成批处理的形式
  while (true) {
    system("gen > test.txt");  // 数据生成器将生成数据写入输入文件
    system("test1.exe < test.txt > 1.txt");  // 获取程序1输出
    system("test2.exe < test.txt > 2.txt");  // 获取程序2输出
    if (system("fc 1.txt 2.txt")) {
      // 该行语句比对输入输出
      // fc返回0时表示输出一致,否则表示有不同处
      system("pause");  // 方便查看不同处
      return 0;
      // 该输入数据已经存放在test.in文件中,可以直接利用进行调试
    }
  }
}
windows下运行脚本结果 两程序结果无差异

有差异就会显示并暂停

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917287.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号