Board logo

标题: 数值计算之分数变换小数(批处理) [打印本页]

作者: my3439955     时间: 2007-4-23 00:23    标题: 数值计算之分数变换小数(批处理)


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::                                                                                         ::
::               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
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 "%N%/%D%" is %int_part% && goto :EOF
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%.[%result%] && goto end
echo %int_part%.!result:~0,%begin%![!result:~%begin%!]

:end
if %2.==. echo. && echo Press any key to quit application && pause>nul

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::            begin的返回值有一定的意义:                                                  ::
::                 -1:表示已经成功整除,是可以直接输出                                     ::
::                  0:表示新的余数是一,直接回到了开始                                     ::
::               其他:表示是最一般的数,循环体前有脖子                                     ::
::          int_part表示了结果的整数部分                                                   ::
::          tool是一个迭代变量,正是它在一直工作                                           ::
::          turn是索引值,表示数组当前位置                                                 ::
::          程序中使用了两个数组,起到统计作用                                             ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

作者: my3439955     时间: 2007-4-23 00:26
中括号包围的部分是循环体


[ Last edited by my3439955 on 2007-4-23 at 02:19 AM ]
作者: bjsh     时间: 2007-4-23 01:27
先鼓励下;

这是个bug;用25/5试下;
The result of expression "0/5" is 5;

被除数 不能大于10^9


下面这个链接是我以前写过的一个:
被除数和结果理论上可以无限位;

可以自己决定欲精确的位数;最后一位四舍五入;

不过没解决 除数大于10^9的;lz可以尝试下突破这个.

http://www.cn-dos.net/forum/view ... ght=BJSH&page=2
这个第26楼
作者: my3439955     时间: 2007-4-23 02:07
你的代码很不错
加上判断循环的话似乎就不必在乎显示位数了,因为到了一定的位数必然循环.当除数为N时,这个位数最大是N-1
作者: my3439955     时间: 2007-4-23 02:17
无论除数还是被除数是零,结果都是显而易见的,因此被归结为错误输入,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%.[%result%] && goto end
echo %int_part%.!result:~0,%begin%![!result:~%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 ]
作者: vkill     时间: 2007-4-24 00:14
我想用位运算可以