感谢楼上分享, 我用ai改powershell, 几十轮都不行? 哎, ai还是不行啊- function Convert-RMB {
- param (
- [string]$input
- )
-
- # 检查输入是否为数字
- if (-not ($input -match '^[0-9]*$')) {
- Write-Host "非法输入!"
- return
- }
-
- $units = @('圆', '拾', '佰', '仟', '萬', '拾萬', '佰萬', '仟萬', '亿', '拾亿', '佰亿', '仟亿', '兆', '拾兆', '佰兆', '仟兆', '京', '拾京', '佰京', '仟京', '垓', '拾垓', '佰垓', '仟垓', '秭', '拾秭', '佰秭', '仟秭', '穰', '拾穰', '佰穰', '仟穰', '沟', '拾沟', '佰沟', '仟沟', '涧', '拾涧', '佰涧', '仟涧', '正', '拾正', '佰正', '仟正', '载', '拾载', '佰载', '仟载', '极')
- $digits = @('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖')
-
- # 计算输入的长度
- $length = $input.Length
- $result = ""
-
- for ($i = 0; $i -lt $length; $i++) {
- $digit = [int]$input[$i]
- $position = $length - $i - 1
- $unit = if ($position -lt $units.Length) { $units[$position] } else { "" }
-
- if ($digit -ne 0) {
- $result += $digits[$digit] + $unit
- } elseif ($unit -match '圆|萬|亿|兆|京|垓|秭|穰|沟|涧|正|载|极') {
- $result += $unit
- } elseif ($result -ne "" -and $result[-1] -ne '零') {
- $result += '零'
- }
- }
-
- $result = $result -replace '零+', '零'
- $result = $result -replace '零$', ''
- $result = $result -replace '零圆$', '圆'
-
- return $result
- }
-
- # 测试
- $input = Read-Host "输入一个数字"
- $result = Convert-RMB -input $input
- Write-Host "输入:" $input
- Write-Host "输出:" $result
复制代码
|