對拍
$ Windows $ 下的對拍程序
- 借助 \(Windows\) 腳本
@echo off
:loop
r.exe > input.in
coronas.exe <input.in > output.a
std.exe <input.in > output.b
fc output.a output.b
if not errorlevel 1 goto loop
一直沒有找到怎樣能控制對拍次數,今天終于醒悟,可以用 \(C++\) 寫對拍 ,不僅能控制對拍次數,可移植性還更強,修改更方便
#include <cstdlib>
#include <cstdio>
#define input "input.in"
#define output1 "output.a"
#define output2 "output.b"
int main() {const int t=100;for(int i=1; i<=t; i++) {system("r.exe" ">" input);system("Coronas.exe" "<" input ">" output1);system("std.exe" "<" input ">" output2);int a=system("fc " output1 " " output2 ">null");printf("test Case %d : ",i);if(a) {printf("Error.\n");return 1;} else {printf("Right.\n");}}system("del " input);system("del " output1);system("del " output2);system("del " "null");return 0;
}
\(Linux\) 下的對拍程序
腳本:
#!bin/bash
while true;do./r>input./a<input>output.a./b<input>output.bdiff output.a output.b #文件比較if [ $? -n 0 ] ; then break; fi # 判斷返回值
done
\(Linux\) 下的對拍有很多問題,比如要開權限(上面的文件要用 source
命令運行),比如命令不回顯,所以根本不知道是在比較還是程序死循環了,這時候借助 \(C++\) 就很有意義了
#include <cstdlib>
#include <cstdio>
#define input "input"
#define output1 "output.a"
#define output2 "output.b"
int main() {const int t=100;for(int i=1; i<=t; i++) {system("./r" ">" input);system("./Coronas" "<" input ">" output1);system("./std" "<" input ">" output2);int a=system("diff " output1 " " output2); //diff a b 為比較文件 a ,b 的內容,注意如果a,b的內容相同,則返回 0//如果a,b 的內容不同,返回值為 1printf("test Case %d : ",i);if(a) {printf("Error.\n");return 1;} else {printf("Right.\n");}}system("rm " input);system("rm " output1);system("rm " output2);return 0;
}
gdb
使用 \(gdb\) 調試程序前一定要記得在編譯選項中加上 -g
。
比如 \(C\)語言程序,編譯命令應為 gcc example.c -g
若為 C++,編譯命令應為 g++ example.cpp -o example -Wall -g
然后用 gdb example.exe
(Windows) 或 gdb example
(Linux) 來加載程序就可以了。
可以在 vimrc
中添加Debug()
函數:
func Debug()exec "w"if &filetype=='cpp'exec "!g++ % -std=c++11 -Wall %< -o -g"elseif &filetype=='c'exec "!gcc %< -g"endifexec "!gdb %<"
endfunc