Przesyłam Ci funkcję do tworzenia pliku z liczbami naturalnymi.
function New-RandomNumericFile([uint64]$maxNumer,[uint64]$amount, [string]$fileName)
{
[uint64]$counter=0
$stream = [System.IO.StreamWriter]$fileName
if($stream)
{
$random = New-Object 'System.Random'
while($counter -le $amount)
{
$counter++
$randomNumber = $random.Next($maxNumer)
$stream.WriteLine($randomNumber)
}
$stream.Close()
}
return $counter
}
Przydatna będzie funkcja sprawdzająca czy liczby w pliku są posortowane:
function IsSorted-NumericFile
{
[CmdletBinding()]
param([string]$fileName)
$stream = [System.IO.StreamReader]$fileName
[uint64]$counter=0
[int]$lastNumber=-1
[bool]$isSorted=$true
while(-not($stream.EndOfStream))
{
$line = $stream.ReadLine()
$counter++
$number = [Convert]::ToInt32( $line)
Write-verbose "$counter line has: $number "
if($isSorted)
{
if($lastNumber -gt $number)
{
$isSorted =$false
break
}
}
$lastNumber = $number
}
$stream.Close()
return $isSorted
}
A jak już masz posortowane pliki to można je porównać:
function CompareLine-NumericFile
{
[CmdletBinding()]
param([string]$fileLeft, [string]$fileRight='' )
[uint64]$counter=0
$streamLeft = [System.IO.StreamReader]$fileLeft
$streamRight = [System.IO.StreamReader]$fileRight
$aresame = $true
if($streamLeft -AND $streamRight)
{
while(-not($streamLeft.EndOfStream))
{
if($streamRight.EndOfStream)
{
Write-Verbose "EOF has been reached for file $fileRight"
$aresame =$false
}
$lineLeft = $streamLeft.ReadLine()
$lineRight = $streamRight.ReadLine()
if([string]::IsNullOrEmpty($lineLeft) -and [string]::IsNullOrEmpty($lineRight))
{
continue
}
if($lineLeft -ne $lineRight)
{
$aresame = $false
break
}
$counter++
}
if($streamLeft.EndOfStream -and -not($streamRight.EndOfStream))
{
Write-Verbose "EOF has been reached for file $fileLeft"
$aresame =$false
}
$streamLeft.Close()
$streamRight.Close()
}
else
{
Write-Verbose "One of file is empty"
$aresame = $false;
}
if($aresame)
{
Write-Verbose "Checked $counter lines in 2 files"
}
else
{
Write-Verbose "First inequality in line $counter"
}
return $aresame
}
Wywołanie funkcji do porównywania wygląda następująco:
CompareLine-NumericFile -fileLeft "test_sorted1.txt" -fileRight "test_sorted2.txt" -Verbose
Brak komentarzy:
Prześlij komentarz