中国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-09-15 12:30
47,812 topics / 349,917 posts / today 0 new / 48,268 members
DOS批处理 & 脚本技术(批处理室) » [Challenge 6]: Calculation of hard disk capacity. Difficulty [☆☆☆]
Printable Version  2,166 / 14
Floor1 flyinspace Posted 2007-05-08 23:04
银牌会员 Posts 517 Credits 1,206
Hehe, this issue of the challenge series didn't appear on Monday on time, please forgive me (because of being busy with work and learning to debug equipment with a few expert-level people, I didn't have time).

Okay, enough chit-chat, let's get to the main topic now (calculation of hard disk capacity).

Now there is a hard disk, but we don't know how many partitions it has, the capacity of each partition, and the remaining space.
Suppose the space of this hard disk is: 300G, according to the division standard of the hard disk manufacturer, it is:
300*1000*1000*1000 in capacity.
The hard disk is divided into 4 partitions, among which
Drive letter Total capacity Remaining capacity
-------------------------------------------------------------------------------
C: 20*1024*1024*1024 1000*1024*1024
D: 40*1024*1024*1024 30*1024*1024*1024
E: 80*1024*1024*1024 60*1024*1024*1024
The remaining is drive F: We already know that 20*1024*1024*1024 of capacity has been used for drive F:.
-------------------------------------------------------------------------------
So now the question is: What is the actual capacity of drive F:?

Requirement: Save the above file as: Drivers.txt

Read this hard disk capacity file from the file and perform the calculation.

For the calculation of capacity, the rule of rounding is adopted. The basic unit is M. For the remaining capacity less than 1G, calculate the actual size (accurate to M). For the unit over 1G, it is G, but it is required to be accurate to three decimal places.

Program usage requirements: CMD (third-party tools can be used, can be combined with vbs, or can be completed alone with vbs)

For those who use third-party tools to complete the goal (points: 2 points)
For those who use CMD completely to complete (points: 5 points, full score)

Difficulty: Friends who extract the correct capacity: 1 point
Friends who use the division operation correctly: 1 point
Friends who use the accurate unit well: 1 point
Friends who get the correct capacity of drive F: 2 points.
Drivers.txt
-------------------------------------------------------------------------------
C: 20*1024*1024*1024 1000*1024*1024
D: 40*1024*1024*1024 30*1024*1024*1024
E: 80*1024*1024*1024 60*1024*1024*1024
F: ????? (???-20*1024*1024*1024)
-------------------------------------------------------------------------------
set "Volume_C=" ; accurate to bytes
set "Volume_D="
set "Volume_E="
set "Volume_F="
set "Real_Volume_C=" accurate to M or G
Dividend: dividend
Divisor: divisor
Quotient: quotient

Okay, let's start doing the problem now.

Requirement to output

C: %Volume_C%(%Real_Volume_C% ) Here M or G can only exist one.
d: %Volume_D%(%Real_Volume_D% )
e: %Volume_E%(%Real_Volume_E% )
f: %Volume_F%(%Real_Volume_F% )

Output:
For example: c: 1048576(1000M)
d: 32212254720(30G)
Use M as the unit for capacity less than 1G
Use G as the unit for capacity greater than 1G

[ Last edited by flyinspace on 2007-5-9 at 01:23 PM ]
Floor2 dikex Posted 2007-05-09 00:13
高级用户 Posts 366 Credits 788
To obtain the space of all partitions in the system (in bytes), you can use the following code. As for precisely calculating extremely large numbers, it's too cumbersome, and it's left to the next person -_-


[ Last edited by dikex on 2007-5-9 at 12:36 AM ]
Floor3 youxi01 Posted 2007-05-09 09:46
高级用户 Posts 247 Credits 846 From 湖南==》广东
It seems that the person above has misunderstood the LZ's meaning.
The question in 1F looks more like a calculation problem.
Rather than querying the hard disk.
Floor4 everest79 Posted 2007-05-09 09:50
金牌会员 Posts 1,127 Credits 2,564
You need to indicate what file system it is, as it affects the accuracy of the final available space calculation.
Floor5 zhoushijay Posted 2007-05-09 11:54
高级用户 Posts 375 Credits 845
Does this problem have anything to do with the file system? It's just pure multiplication and division of numbers. The difficulty lies in reading the text. It's relatively easier to read with P, but the value is too large to calculate. VBS can be used for calculation but has some difficulties in reading.
Floor6 flyinspace Posted 2007-05-09 12:23
银牌会员 Posts 517 Credits 1,206
To youxi01:
Mainly to query the capacity of the hard disk (apparently there are large number operations, but here, a series of methods can be used to bypass it.)
To everest79:
Brother, you're overthinking. It's just to test comprehensive ability (if you really don't take shortcuts, the content includes addition, subtraction, multiplication, division, and remainder operations.)
Then here, the issue of subroutine calls will be involved, because if a large number of four arithmetic operations are still put together, the length of the code is unimaginable : )
Those with beautiful code, flyinspace will additionally add 3 points here (the difficulty is: reasonable matching, no need to comment every sentence, but need to mark what this subroutine is for in front of the subroutine. Just wrote a small four arithmetic operation program in C++, which is just to call each subroutine and see if it is correct.)
ps: Now I'm increasingly feeling the importance of algorithms. Everyone can go to the Internet when writing this script, and it is recommended to find posts about large number operations on CSDN.
Floor7 zhoushijay Posted 2007-05-09 13:13
高级用户 Posts 375 Credits 845
I'll interject, omitting the reading of the text. Hehe
Just some things I don't quite understand the meaning of the LZ's title...


Floor8 zhoushijay Posted 2007-05-09 13:15
高级用户 Posts 375 Credits 845
Uh... The available space on drive C seems to be miswritten
Floor9 youxi01 Posted 2007-05-09 23:50
高级用户 Posts 247 Credits 846 From 湖南==》广东
Then the LZ's meaning is: First read the number of disks, then read out the capacity and other information of the previous drive letters, and keep the last one to be calculated (that is, not allowed to read). Is this the meaning?
Floor10 wld Posted 2007-05-10 16:51
初级用户 Posts 32 Credits 61
Learning
Floor11 flyinspace Posted 2007-05-10 17:19
银牌会员 Posts 517 Credits 1,206
Originally posted by youxi01 at 2007-5-9 11:50 PM:
So the meaning of the original poster is:

First read the number of disks, then read the capacity and other information of the previous drive letters, and keep the last one to be calculated (that is, not allowed to read). Is that the meaning?

Hmm, yes. ..

All items have given conditions...

It can be calculated by four arithmetic operations. ..
Floor12 hhasee Posted 2009-01-10 18:11
初级用户 Posts 88 Credits 185
Can you send one that can also be used under VISTA?
Floor13 netbenton Posted 2009-01-11 23:04
银牌会员 Posts 752 Credits 1,916 From 广西
@echo off&setlocal enabledelayedexpansion
::Temporary variables in for are _v1, _v2, _v(n)
::disk=Total hard disk capacity/1024
::_n=Number of partitions

::for1 Read correct string, if it is drive f:, remove symbols like??-() etc.
:: _d:Drive letter _zc:Total capacity string _yc:Used space string

::for2 Segment the string
:: _zt:Head part of total capacity string _zw:Tail _yt:Head part of used space string _yw:Tail

::for3 Calculate total space and free space of F:
:: Use temporary variables _v1, _v2, _v3

::for4 Process results in units of M/G
:: _zm:Total capacity _sm:Free space _ym:Used space

::for5 Process into bytes (B), megabytes M/G
:: _zb:Total bytes B _yb:Used bytes B _sb:Free bytes

::for6 Display output


echo off
set /a disk=300*1024*1024

::for1 Read correct numeric string, remove??() etc.
set _n=0
echo. Reading data&echo.************************************
for /f "tokens=1,2,3* delims= " %%i in ('findstr : drivers.txt') do (
set /a _n+=1&echo %%i %%j %%k
set _v1=%%i&set _v2=%%j&set _v3=%%k
if "!_v1!"=="F:" set _v2=!_v2:?=!&set _v3=!_v3:*-=!&set _v3=!_v3:^)=!&set _y!_n!=!_v3!
set _d!_n!=!_v1!&set _z!_n!=!_v2!&set _s!_n!=!_v3!
)
echo.************************************&echo.&echo.

::for2 Segment processing of numeric string
:: _zt:Head part of total capacity string _zw:Tail _yt:Head part of used space string _yw:Tail
for /l %%a in (1,1,!_n!) do (
set _v2=!_z%%a!&set _v3=!_s%%a!&set _v4=!_y%%a!
set _zt%%a=!_v2:~0,-5!
set _zw%%a=!_v2:~-4!
set _st%%a=!_v3:~0,-5!
set _sw%%a=!_v3:~-4!
set _yt%%a=!_v4:~0,-5!
set _yw%%a=!_v4:~-4!
)


::for3 Calculate total space and free space of F:
:: Use temporary variables _v1, _v2, _v3
set _v1=
for /l %%a in (1,1,!_n!) do (
set /a _zt%%a=!_zt%%a!&set /a _st%%a=!_st%%a!&set /a _yt%%a=!_yt%%a!
if "!_d%%a!"=="F:" (set /a _zt%%a=!disk!-!_v1!&set /a _st%%a=!_zt%%a!-!_yt%%a!&set _zw%%a=!_yw%%a!) else (set /a _yt%%a=!_zt%%a!-!_st%%a!&set _yw%%a=!_zw%%a!&set /a _v1=!_v1!+!_zt%%a!)
)


::for4 Calculate results in units of M, if greater than 1000M, use G as unit
:: _zm:Total capacity _sm:Free space _ym:Used space
for /l %%a in (1,1,!_n!) do (
set /a _zm%%a=!_zt%%a!/1024&set /a _sm%%a=!_st%%a!/1024&set /a _ym%%a=!_yt%%a!/1024
if !_zm%%a! leq 1000 (set _zm%%a=^(!_zm%%a!M^)) else (set /a _zm%%a=!_zm%%a!/1024 & set _zm%%a=^(!_zm%%a!G^))
if !_sm%%a! leq 1000 (set _sm%%a=^(!_sm%%a!M^)) else (set /a _sm%%a=!_sm%%a!/1024 & set _sm%%a=^(!_sm%%a!G^))
if !_ym%%a! leq 1000 (set _ym%%a=^(!_ym%%a!M^)) else (set /a _ym%%a=!_ym%%a!/1024 & set _ym%%a=^(!_ym%%a!G^))
)


::for5 Process into byte display (B)
:: _zb:Total B _yb:Used B _sb:Free bytes
for /l %%a in (1,1,!_n!) do (
rem Multiply %1 by %2 and put it in the variable pointed to by %3
call :sub !_zt%%a! !_zw%%a! _zb%%a
call :sub !_st%%a! !_sw%%a! _sb%%a
call :sub !_yt%%a! !_yw%%a! _yb%%a
)

::for6 Display output
echo.Hard disk data:
echo._______________________________________________________________________________
for /l %%a in (1,1,!_n!) do (echo.!_d%%a! Total capacity:!_zb%%a!!_zm%%a! Free:!_sb%%a!!_sm%%a! Used:!_yb%%a!!_ym%%a!)
echo._______________________________________________________________________________
pause
goto :eof

:sub
set _v1=%1&set _v2=%2&set _v3=%3
set /a _vt=!_v1:~0,-5!*!_v2!
set /a _vw=!_v1:~-5!*!_v2!
set _vx=!_vt!00000
set /a _vy=!_vx:~-9!+!_vw!
set _vx=!_vx:~0,-9!
set _v4=!_vy:~0,-9!
if not "!_va!"=="" set /a _vx=!_vx!+!_v4!
set !_v3!=!_vx!!_vy!

goto :eof

[ Last edited by netbenton on 2009-1-11 at 23:07 ]
Floor14 knoppix7 Posted 2009-01-12 09:40
银牌会员 Posts 634 Credits 1,287 From cmd.exe
DISKPART> list disk

The definition of the external command has not been... That's it...
Floor15 netbenton Posted 2009-01-12 14:46
银牌会员 Posts 752 Credits 1,916 From 广西
Puzzled:
Maybe you still don't understand the LZ's meaning!
What external commands need to be defined?
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023