最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

powershell批處理io校驗(yàn)功能

 更新時(shí)間:2025年05月12日 09:37:28   作者:.格子衫.  
這篇文章主要介紹了powershell批處理io校驗(yàn)功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

powershell批處理——io校驗(yàn)

在刷題時(shí),時(shí)?;叵?,OJ平臺(tái)是如何校驗(yàn)競(jìng)賽隊(duì)員提交的代碼的,OJ平臺(tái)并不看代碼,而是使用“黑盒測(cè)試”,用測(cè)試數(shù)據(jù)來驗(yàn)證。對(duì)于每題,都事先設(shè)定了很多組輸入數(shù)據(jù)(datain)和輸出數(shù)據(jù)(dataout),OJ主要是看輸入datain,看產(chǎn)生的輸出是否與dataout一致。

如果需要一次性知道自己的所有測(cè)試輸入數(shù)據(jù)是否能得到正確輸出,一個(gè)一個(gè)測(cè)試就太慢了,批處理操作就能很好解決這個(gè)問題。剛好在學(xué)習(xí)powershell,本篇文章主要介紹一下這個(gè)ioCode項(xiàng)目。

項(xiàng)目目錄結(jié)構(gòu)

  • input目錄:存放輸入數(shù)據(jù)文件,以.in后綴結(jié)尾
  • output目錄:存放輸出數(shù)據(jù)文件,以.out后綴結(jié)尾,每個(gè)輸出的文件名與對(duì)應(yīng)的輸入的文件名相同
  • start.ps1:編寫的腳本文件

start.ps1源代碼:

function ioCheck{
    param(
        $content, #程序輸出內(nèi)容
        $answer #輸出的答案內(nèi)容
    )
    $cnt = $answer.length
    if($content.length -ne $cnt){
        return -1  #行數(shù)不一致返回-1
    }
    for($i=0;$i -lt $cnt;$i++){
        if($answer[$i] -ne $content[$i]){
            return $i+1  #內(nèi)容不一致,返回第一個(gè)不同的行數(shù)
        } 
    }
    return 0  #相同返回0
}
function ioCode{
    param(
		[string]$command,
        [string]$inPath = './input',
        [string]$outPath = './output'
    )
    $allStartTime = Get-Date
    $acList = New-Object System.Collections.ArrayList  #創(chuàng)建正確輸出的文件名集合
    $errList = New-Object System.Collections.ArrayList  #創(chuàng)建錯(cuò)誤輸出的文件名集合
    $inFiles = Get-ChildItem -Path $inPath -Filter *.in  #獲取輸入文件集
    $len = $inFiles.length
    $cnt = 0 
    foreach($file in $inFiles){
        try {
            $startTime = Get-Date
            $fullOut = (cmd.exe /c "$command < $($file.fullName)") -split "\r\n|\r|\n"  #獲取程序?qū)嶋H輸出內(nèi)容
            $endTime = Get-Date
            $duration = $endTime - $startTime  #獲取程序執(zhí)行時(shí)間
            $answerPath = Join-Path -Path $outPath -ChildPath ($file.BaseName + '.out')
            $answer = Get-Content -Path $answerPath -ReadCount 0  #獲取該輸入文件的對(duì)應(yīng)輸出文件的內(nèi)容,-ReadCount 0指定返回一個(gè)字符串?dāng)?shù)組
            $res = ioCheck $fullOut $answer
            if($res -eq 0){
                $item = @{
                    fileName = $file.Name
                    exeTime = $duration
                }
                $acList.add($item) | Out-Null  #Out-Null表示丟棄輸出
                $cnt++
            }else{
                $item = @{
                    fileName = $file.Name
                    errLine = $res
                }
                $errList.add($item) | Out-Null 
            }
        }
        catch {
            Write-Host "處理文件 $($file.Name) 時(shí)出現(xiàn)錯(cuò)誤: $_"
        }
    }
    $allEndTime = Get-Date
    $allExeTime = $allEndTime-$allStartTime
    Write-Host "正確輸出 $cnt/$len:,總耗時(shí):$($allExeTime.Milliseconds)ms"
    foreach($item in $acList){
        Write-Host "Y: $($item.fileName) 耗時(shí):$($item.exeTime.Milliseconds)ms"
    }
    foreach($item in $errList){
        Write-Host "N: $($item.fileName) errCode:$($item.errLine)"
    }
    return @{
        CorrectCount = $cnt
        TotalCount = $len
        acList = $acList
        errList = $errList
    }
}

首先測(cè)試輸入兩個(gè)數(shù),看能否成功輸出兩數(shù)之和

在input目錄中創(chuàng)建三個(gè)文件分別為001.in,002.in,003.in

內(nèi)容分別為

3 4

35749 47985

544 6755

對(duì)應(yīng)的在output目錄創(chuàng)建三個(gè)同名的.out文件,分別為001.out,002.out,003.out

內(nèi)容就是對(duì)應(yīng).in文件中兩個(gè)輸入數(shù)據(jù)的相加的結(jié)果。

使用

編寫好測(cè)試用例,就是可以測(cè)試我們寫好的代碼了,這里不僅可以測(cè)試C/C++生成的.exe可執(zhí)行文件,還可以測(cè)試java生成的.class可執(zhí)行字節(jié)碼文件(前提是電腦具備java環(huán)境)。

測(cè)試.exe文件

在當(dāng)前目錄下準(zhǔn)備一個(gè)test.cpp文件,代碼如下:

#include<iostream>
using namespace std;
int main()
{
    int a, b;
    cin >> a >> b;
    cout<< 7;
    return 0;
}

將其編譯為可執(zhí)行文件test.exe

g++ -o test.exe test.cpp

在當(dāng)前目錄下打開powershell控制臺(tái)

通過點(diǎn)加載start.ps1文件

. './start.ps1'

輸入命令ioCode {command} {inPath} {outPath},command為可執(zhí)行命令,inPath為輸入文件目錄(默認(rèn)為當(dāng)前目錄的input目錄),outPath為輸出答案文件目錄(默認(rèn)為當(dāng)前目錄的output目錄)

ioCode test.exe

示例:

控制臺(tái)輸出顯示我們的三個(gè)測(cè)試用例都通過了。

測(cè)試可執(zhí)行.class文件

在當(dāng)前目錄下準(zhǔn)備一個(gè)Main.java文件,代碼如下:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        System.out.println(a+b);
    }
}

將其編譯為可執(zhí)行文件Main.class

javac Main.java

在當(dāng)前目錄下打開powershell控制臺(tái)

通過點(diǎn)加載start.ps1文件

. './start.ps1'

輸入命令ioCode {command} {inPath} {outPath},command為可執(zhí)行命令,inPath為輸入文件目錄(默認(rèn)為當(dāng)前目錄的input目錄),outPath為輸出答案文件目錄(默認(rèn)為當(dāng)前目錄的output目錄)

ioCode 'java Main'  #注意java Main是一個(gè)完整的命令,中間帶有空格,需要用引號(hào)引起來

示例:

測(cè)試可執(zhí)行.py文件

在當(dāng)前目錄下準(zhǔn)備一個(gè)Main.java文件,代碼如下:

a,b=map(int,input().split())
print(a+b)

在當(dāng)前目錄下打開powershell控制臺(tái)

通過點(diǎn)加載start.ps1文件

. './start.ps1'

輸入命令ioCode {command} {inPath} {outPath},command為可執(zhí)行命令,inPath為輸入文件目錄(默認(rèn)為當(dāng)前目錄的input目錄),outPath為輸出答案文件目錄(默認(rèn)為當(dāng)前目錄的output目錄)

ioCode 'python test.py'  #注意python test.py是一個(gè)完整的命令,中間帶有空格,需要用引號(hào)引起來

示例:

以上就是這個(gè)小項(xiàng)目的內(nèi)容,通過ioCode可以批處理測(cè)試多個(gè)輸入輸出用例,并能清晰的看到哪些輸入文件正確輸出方便DeBug。

到此這篇關(guān)于powershell批處理——io校驗(yàn)的文章就介紹到這了,更多相關(guān)powershell批處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論

竹山县| 金乡县| 光山县| 额尔古纳市| 汤原县| 兴业县| 红原县| 翁源县| 祁东县| 花垣县| 乌拉特中旗| 峡江县| 淮滨县| 左云县| 镇赉县| 尼木县| 石渠县| 德昌县| 衡东县| 武陟县| 宜丰县| 博湖县| 邵武市| 雅江县| 五原县| 南京市| 奉贤区| 宜兰市| 永康市| 雷山县| 旺苍县| 偃师市| 离岛区| 辽阳市| 腾冲县| 昭觉县| 西昌市| 宁阳县| 新民市| 利川市| 新郑市|