China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-25 06:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Fraction to Decimal in Numerical Calculation (Batch Processing) View 1,428 Replies 5
Original Poster Posted 2007-04-23 00:23 ·  中国 山东 淄博 联通
中级用户
★★
Credits 272
Posts 99
Joined 2006-06-02 09:12
20-year member
UID 56414
Status Offline
```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 ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Floor 2 Posted 2007-04-23 00:26 ·  中国 山东 淄博 联通
中级用户
★★
Credits 272
Posts 99
Joined 2006-06-02 09:12
20-year member
UID 56414
Status Offline
The part enclosed in square brackets is the loop body.


[ Last edited by my3439955 on 2007-4-23 at 02:19 AM ]
Floor 3 Posted 2007-04-23 01:27 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
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
Floor 4 Posted 2007-04-23 02:07 ·  中国 山东 淄博 联通
中级用户
★★
Credits 272
Posts 99
Joined 2006-06-02 09:12
20-year member
UID 56414
Status Offline
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
Floor 5 Posted 2007-04-23 02:17 ·  中国 山东 淄博 联通
中级用户
★★
Credits 272
Posts 99
Joined 2006-06-02 09:12
20-year member
UID 56414
Status Offline
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 ]
Floor 6 Posted 2007-04-24 00:14 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
I want to use bitwise operations can
Forum Jump: