A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Roman Numeral in Powershell page! Here, you'll find the source code for this program as well as a description of how the program works.
$Romans = @{
M = 1000
D = 500
C = 100
L = 50
X = 10
V = 5
I = 1
}
function Show-Usage() {
Write-Host 'Usage: please provide a string of roman numerals'
Exit 1
}
function Show-Error() {
Write-Host 'Error: invalid string of roman numerals'
Exit 1
}
function RomanNumeral([string]$Str) {
$total = 0
$prevValue = 0
foreach ($ch in $Str.ToCharArray()) {
$value = $Romans[[string]$ch]
if (-not $value) {
$total = -1
break
}
$total += $value
if ($prevValue -gt 0 -and $value -gt $prevValue) {
$total -= 2 * $prevValue
$prevValue = 0
} else {
$prevValue = $value
}
}
$total
}
if ($args.Length -lt 1) {
Show-Usage
}
$result = RomanNumeral $args[0]
if ($result -lt 0) {
Show-Error
}
Write-Output $result
Roman Numeral in Powershell was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.