程序调试

对拍

$ 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.b
    diff 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 的内容不同,返回值为 1
        printf("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"
    endif
    exec "!gdb %<"
endfunc
posted @ 2018-10-20 08:49  昤昽  阅读(199)  评论(0编辑  收藏  举报