使用bat将阿拉伯数字转换成罗马数字
我自己尝试过很多版本,但都不太尽如人意,所以还是使用了穷举法
代码如下:- @echo off
- REM 阿拉伯数字转换为罗马数字
- setlocal EnableDelayedExpansion
-
- REM 定义罗马数字组
- set "ones_romans[0]="
- set "ones_romans[1]=I"
- set "ones_romans[2]=II"
- set "ones_romans[3]=III"
- set "ones_romans[4]=IV"
- set "ones_romans[5]=V"
- set "ones_romans[6]=VI"
- set "ones_romans[7]=VII"
- set "ones_romans[8]=VIII"
- set "ones_romans[9]=IX"
- set "tens_romans[0]="
- set "tens_romans[1]=X"
- set "tens_romans[2]=XX"
- set "tens_romans[3]=XXX"
- set "tens_romans[4]=XL"
- set "tens_romans[5]=L"
- set "tens_romans[6]=LX"
- set "tens_romans[7]=LXX"
- set "tens_romans[8]=LXXX"
- set "tens_romans[9]=XC"
- set "hundreds_romans[0]="
- set "hundreds_romans[1]=C"
- set "hundreds_romans[2]=CC"
- set "hundreds_romans[3]=CCC"
- set "hundreds_romans[4]=CD"
- set "hundreds_romans[5]=D"
- set "hundreds_romans[6]=DC"
- set "hundreds_romans[7]=DCC"
- set "hundreds_romans[8]=DCCC"
- set "hundreds_romans[9]=CM"
- set "thousands_romans[0]="
- set "thousands_romans[1]=M"
- set "thousands_romans[2]=MM"
- set "thousands_romans[3]=MMM"
-
- :ConvertToRoman
- REM 接收输入端
- cls
- echo.
- set "number="
- set /p number=请输入一个整数:
- set "result="
-
- REM 检测是否大于3999
- set "stand=3999"
- if "%number%" equ "%stand%" goto :processing
- if "%number%" gtr "%stand%" (
- cls
- echo.
- echo 被转换数字不可大于3999
- pause >nul
- goto :ConvertToRoman
- )
- if "%number%" lss "%stand%" goto :processing
-
- :processing
- REM 千位
- set /a t=number/1000
- set /a number=number%%1000
- set "result=!result!!thousands_romans[%t%]!"
-
- REM 百位
- set /a t=number/100
- set /a number=number%%100
- set "result=!result!!hundreds_romans[%t%]!"
-
- REM 十位
- set /a t=number/10
- set /a number=number%%10
- set "result=!result!!tens_romans[%t%]!"
-
- REM 个位
- set "result=!result!!ones_romans[%number%]!"
-
- echo.
- echo 得到罗马数字为:%result%
- pause >nul
- goto :ConvertToRoman
复制代码
|