DOS下的加减运算

来源:互联网 发布:java 环形链表 编辑:程序博客网 时间:2024/05/16 15:50

REM 开始界面
:begin
cls
color 0a
echo.
echo ╭——————╮
echo ╭———┤ DOS 计算器 ├———╮
echo │ ╰——————╯ │
echo │ =by x2009.cn= │
echo │ │
echo │ 请选择: │
echo │ │
echo │ 1 - 普通+-×÷ │
echo │ 2 - 求N次方 │
echo │ 3 - 求阶乘 │
echo │ h - 帮助 │
echo │ q - 退出 │
echo │ │
echo ╰——————————————╯
REM 取得用户输入
echo.
set UserChoice=""
set /p UserChoice=请选择:
if /I %UserChoice%==q goto end
if /I %UserChoice%==h goto help
if %UserChoice%==1 goto normal
if %UserChoice%==2 goto fang
if %UserChoice%==3 goto jiecheng
goto error

REM 普通运算
:normal
cls
echo.
echo.
echo.
echo   ╭—————————╮
echo   │ 普通+-×÷运算 │
echo   ╰—————————╯
echo.
set /p UserInput=请输入表达式:
REM 检测表达式
echo %UserInput% | findstr "[0-9]">nul || goto error
echo %UserInput% | findstr /I "[a-z]">nul && goto error
echo %UserInput% | find ".">nul && goto error
echo %UserInput% | findstr "+ - * /">nul || goto error
echo %UserInput% | find "+">nul && goto add
echo %UserInput% | find "-">nul && goto minus
echo %UserInput% | find "*">nul && goto multiply
echo %UserInput% | find "/">nul && goto divide
REM 实际运算
:add
for /f "tokens=1,2 delims=+" %%a in ("%UserInput%") do set /a result=%%a+%%b
echo 计算结果=%result%
goto refresh
:minus
for /f "tokens=1,2 delims=-" %%a in ("%UserInput%") do set /a result=%%a-%%b
echo 计算结果=%result%
goto refresh
:multiply
for /f "tokens=1,2 delims=*" %%a in ("%UserInput%") do set /a result=%%a*%%b
echo 计算结果=%result%
goto refresh
:devide
for /f "tokens=1,2 delims=/" %%a in ("%UserInput%") do set /a result=%%a/%%b
echo 计算结果=%result%
goto refresh

REM 计算N次方
:fang
cls
echo.
echo.
echo.
echo   ╭—————————╮
echo   │ 求 N 次 方 │
echo   ╰—————————╯
echo.
set /p UserInput=请输入表达式:
REM 检测表达式
echo %UserInput% | findstr "[0-9]">nul || goto error
echo %UserInput% | findstr /I "[a-z]">nul && goto error
echo %UserInput% | find ".">nul && goto error
echo %UserInput% | find "_">nul || goto error
set num=
set n=
set /a result=1
for /f "tokens=1,2 delims=_" %%a in ("%UserInput%") do set /a num=%%a & set /a n=%%b
if %n%==0 goto showfang
for /L %%i in (1,1,%n%) do set /a result*=%num%
:showfang
echo 计算结果=%result%
goto refresh

REM 计算阶乘
:jiecheng
cls
echo.
echo.
echo.
echo   ╭—————————╮
echo   │ 求 阶 乘 │
echo   ╰—————————╯
echo.
set /p UserInput=请输入要求阶乘的整数:
REM 检测表达式
echo %UserInput% | findstr "[0-9]">nul || goto error
echo %UserInput% | findstr /I "[a-z]">nul && goto error
echo %UserInput% | find ".">nul && goto error
set /a result=1
REM 不可以直接判断UserInput是否为0
set num=%UserInput%
if %num%==0 goto showjiecheng
for /L %%i in (%num%,-1,1) do set /a result*=%%i
:showjiecheng
echo 计算结果=%result%
goto refresh

REM 错误
:error
cls
color 0c
echo.
echo.
echo.
echo ╭————————————╮
echo │ 输入错误,请参考帮助! │
echo ╰————————————╯
echo.

REM 刷新
:refresh
echo.
set UserChoice=""
set /p UserChoice=请按任意键继续,退出请按q,帮助请按h:
if /I %UserChoice%==q goto end
if /I %UserChoice%==h goto help
goto begin

REM 帮助
:help
cls
color 0a
echo.
echo ╭——————╮
echo ╭————┤ 帮 助 ├————╮
echo │ ╰——————╯ │
echo │ │
echo │ 1.普通运算请使用以下形式: │
echo │ 4+1 5-2 2*3 6/2 │
echo │ │
echo │ 2.计算N次方请使用以下形式: │
echo │ 2_5 (求2的5次方) │
echo │ │
echo │ 3.只支持整数运算 │
echo │ │
echo │ 4.输入部分特殊符号可能会 │
echo │ 导致意外退出 │
echo │ │
echo ╰————————————————╯
goto refresh

REM 退出
:end
cls
color 0a
echo.
echo.
echo.
echo ╭————————————————╮
echo │ │
echo │ 非常感谢您的使用,再见!  │
echo │ │
echo ╰————————————————╯
ping -n 2 127.1>nul
exit

0 0
原创粉丝点击