无论除数还是被除数是零,结果都是显而易见的,因此被归结为错误输入,0/5被要求重新输入
25/5是有一点小问题,问题在于这一句
if %tool%==0 echo The result of expression "%N%/%D%" is %int_part% && goto :EOF
这里判断整除之后没有用pause暂停而直接退出了
应该把goto :EOF改成goto :end
%N%应该改为%NN%
处理参数的代码也要改变一下
修改后的代码如下
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: ::
:: nd.bat ::
:: ::
:: by Shilyx mailto oversleep@163.com (C)2006.9.4 ::
:: ::
:: 将分数转化为小数的批处理程序,支持无限小数,循环体用中括号包围 ::
:: ::
:: 用法:命令行下输入按以下格式输入 nd a b ::
:: 其中参数 a 表示分子,参数 b 表示分母 ::
:: 也可以直接不加参数运行,程序将提示输入 ::
:: ::
:: 注意:程序在脚本环境中运行,对于大分母速度较慢,一般不要超过3000 ::
:: ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal EnableDelayedExpansion
if %2.==. goto input
set /A N=%1
set /A D=%2
set /A NN=%1
set /A DD=%2
goto compute
:input
echo Input N:
set /p NN=
set /A N=%NN%
echo Input D:
set /p DD=
set /A D=%DD%
goto compute
:compute
if %N%==0 echo Wrong Intry && goto input
if %D%==0 echo Wrong Intry && goto input
set str=
for /L %%a in (1,1,%D%) do @set /A Left%%a=0
for /L %%a in (1,1,%D%) do @set /A Result%%a=0
set /A int_part="%N%/%D%"
set /A N="%N%%%%D%"
set /A tool=%N%
if %tool%==0 echo The result of expression "%NN%/%D%" is %int_part% && goto :end
set /A turn=1
:start
set /A "tool*=10"
set /A Result%turn%="%tool%/%D%"
set /A Left="%tool%%%%D%"
if %Left%==0 set /A begin=-1 && set /A end="%turn%" && goto endCompute
if %Left%==%N% set /A begin=0 && set /A end="%turn%" && goto endCompute
if not !Left%Left%!==0 set /A begin="!Left%Left%!" && set /A end="%turn%" && goto endCompute
set /A Left%Left%=%turn%
set /A tool=%Left%
set /A "turn+=1"
goto start
:endCompute
if %3.==. echo The result of expression "%N%/%D%" is
set result=#
for /L %%i in (1,1,%end%) do @set result=!result!!Result%%i!
set result=%result:~1%
if %begin%==-1 echo %int_part%.%result% && goto end
if %begin%==0 echo %int_part%. && goto end
echo %int_part%.!result:~0,%begin%!
:end
if %2.==. echo. && echo Press any key to quit application && pause>nul
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: begin的返回值有一定的意义: ::
:: -1:表示已经成功整除,是可以直接输出 ::
:: 0:表示新的余数是一,直接回到了开始 ::
:: 其他:表示是最一般的数,循环体前有脖子 ::
:: int_part表示了结果的整数部分 ::
:: tool是一个迭代变量,正是它在一直工作 ::
:: turn是索引值,表示数组当前位置 ::
:: 程序中使用了两个数组,起到统计作用 ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
运行结果
Last edited by my3439955 on 2007-4-23 at 02:33 AM ]
Whether the divisor or the dividend is zero, the result is obvious, so it is classified as incorrect input, and 0/5 is required to re-enter.
There is a small problem with 25/5, and the problem is in this line:
if %tool%==0 echo The result of expression "%N%/%D%" is %int_part% && goto :EOF
Here, after judging the exact division, there is no pause with pause and it exits directly.
It should change goto :EOF to goto :end
%N% should be changed to %NN%
The code for processing parameters also needs to be changed.
The modified code is as follows
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: ::
:: nd.bat ::
:: ::
:: by Shilyx mailto oversleep@163.com (C)2006.9.4 ::
:: ::
:: Batch program to convert fractions to decimals, supports infinite decimals, and the loop body is enclosed in square brackets ::
:: ::
:: Usage: Enter in the command line in the following format nd a b ::
:: where parameter a represents the numerator and parameter b represents the denominator ::
:: You can also run directly without parameters, and the program will prompt for input ::
:: ::
:: Note: The program runs in the script environment. It is slow for large denominators. Generally, do not exceed 3000 ::
:: ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal EnableDelayedExpansion
if %2.==. goto input
set /A N=%1
set /A D=%2
set /A NN=%1
set /A DD=%2
goto compute
:input
echo Input N:
set /p NN=
set /A N=%NN%
echo Input D:
set /p DD=
set /A D=%DD%
goto compute
:compute
if %N%==0 echo Wrong Intry && goto input
if %D%==0 echo Wrong Intry && goto input
set str=
for /L %%a in (1,1,%D%) do @set /A Left%%a=0
for /L %%a in (1,1,%D%) do @set /A Result%%a=0
set /A int_part="%N%/%D%"
set /A N="%N%%%%D%"
set /A tool=%N%
if %tool%==0 echo The result of expression "%NN%/%D%" is %int_part% && goto :end
set /A turn=1
:start
set /A "tool*=10"
set /A Result%turn%="%tool%/%D%"
set /A Left="%tool%%%%D%"
if %Left%==0 set /A begin=-1 && set /A end="%turn%" && goto endCompute
if %Left%==%N% set /A begin=0 && set /A end="%turn%" && goto endCompute
if not !Left%Left%!==0 set /A begin="!Left%Left%!" && set /A end="%turn%" && goto endCompute
set /A Left%Left%=%turn%
set /A tool=%Left%
set /A "turn+=1"
goto start
:endCompute
if %3.==. echo The result of expression "%N%/%D%" is
set result=#
for /L %%i in (1,1,%end%) do @set result=!result!!Result%%i!
set result=%result:~1%
if %begin%==-1 echo %int_part%.%result% && goto end
if %begin%==0 echo %int_part%. && goto end
echo %int_part%.!result:~0,%begin%!
:end
if %2.==. echo. && echo Press any key to quit application && pause>nul
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: The return value of begin has certain meanings: ::
:: -1: Indicates that the division has been successfully completed and can be output directly ::
:: 0: Indicates that the new remainder is one and returns to the start directly ::
:: Others: Indicates that it is the most general number, and there is a neck before the loop body ::
:: int_part represents the integer part of the result ::
:: tool is an iterative variable, and it is working all the time ::
:: turn is the index value, indicating the current position of the array ::
:: Two arrays are used in the program, playing a statistical role ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Running result
Last edited by my3439955 on 2007-4-23 at 02:33 AM ]