中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-10 15:59
47,811 topics / 349,897 posts / today 0 new / 48,255 members
DOS批处理 & 脚本技术(批处理室) » Fraction to Decimal in Numerical Calculation (Batch Processing)
Printable Version  1,479 / 5
Floor1 my3439955 Posted 2007-04-23 00:23
中级用户 Posts 99 Credits 272
```code
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: ::
:: nd.bat ::
:: ::
:: by Shilyx mailto oversleep@163.com (C)2006.9.4 ::
:: ::
:: Batch program to convert a fraction to a decimal, supports infinite decimals, and the repeating part is enclosed in brackets ::
:: ::
:: Usage: Enter at 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 it 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
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 Input && goto input
if %D%==0 echo Wrong Input && 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%!"=="" 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 it can be output directly ::
:: 0: Indicates that the new remainder is one, returning directly to the start ::
:: Other: Indicates that it is the most common number, with a neck before the repeating part ::
:: int_part represents the integer part of the result ::
:: tool is an iterative variable, and it is the one working all the time ::
:: turn is the index value, indicating the current position of the array ::
:: Two arrays are used in the program for statistical purposes ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Floor2 my3439955 Posted 2007-04-23 00:26
中级用户 Posts 99 Credits 272
The part enclosed in square brackets is the loop body.


[ Last edited by my3439955 on 2007-4-23 at 02:19 AM ]
Floor3 bjsh Posted 2007-04-23 01:27
银牌会员 Posts 621 Credits 2,000
First, give some encouragement;

This is a bug; try using 25/5;
The result of the expression "0/5" is 5;

The dividend cannot be greater than 10^9

The following link is one I wrote before:
The dividend and the result can theoretically be of unlimited digits;

You can decide the number of digits you want to be precise to; the last digit is rounded;

However, the problem of the divisor being greater than 10^9 is not solved; the LZ can try to break through this.

http://www.cn-dos.net/forum/viewthread.php?tid=29097&fpage=1&highlight=BJSH&page=2
This is on the 26th floor
Floor4 my3439955 Posted 2007-04-23 02:07
中级用户 Posts 99 Credits 272
Your code is very good. Adding a judgment loop seems to make it unnecessary to care about the number of digits, because it will definitely cycle after a certain number of digits. When the divisor is N, this number of digits is at most N - 1
Floor5 my3439955 Posted 2007-04-23 02:17
中级用户 Posts 99 Credits 272
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



Running result


[ Last edited by my3439955 on 2007-4-23 at 02:33 AM ]
Floor6 vkill Posted 2007-04-24 00:14
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
I want to use bitwise operations can
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023