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-07-14 23:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Get the absolute value through four arithmetic operations in the command line View 4,239 Replies 15
Original Poster Posted 2007-01-17 12:58 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Probably as follows

set /p a=input
set /a b=algorithm
%b% is the absolute value of the input number

Without using character replacement or IF judgment
Floor 2 Posted 2007-01-17 13:27 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The simplest way is to use a string replacement statement. The demonstration code is as follows:

@echo off
set input=
set /p input=Please enter a real number:
echo %input:-=%
pause

——————————————————
It turns out that character replacement is not wanted, and the use of if statements is not allowed. It's really too BT. I don't know what special needs you have for such a requirement. Here's another one:

@echo off
set input=
set /p input=Please enter a real number:
for /f "tokens=* delims=-" %%i in ("%input%") do echo %%i
pause


[ Last edited by namejm on 2007-1-17 at 12:33 AM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2007-01-17 13:42 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Hehe, the moderator you cheated, I want to get the absolute value through calculation, I've thought about it all afternoon
Floor 4 Posted 2007-01-17 22:23 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Method 1: Principle of Two Negatives Make a Positive


@echo off

set /p var=Please enter a negative number:
set /a var=-%var%

echo The absolute value is: %var%


There is an error when calculating numbers with decimals ~ : )
There is an error when calculating positive numbers : )



Method 2: (Negative number) - (Negative number) * 2 = (Absolute value)


@echo on

set /p var=Please enter a negative number:
set /a var=%var%-%var%*2

echo The absolute value is: %var%


Does not support floating-point operations.
Only supports finding the absolute value of a negative number (errors occur when entering a positive number)
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 5 Posted 2007-01-17 23:30 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
I got one:

@echo off
set /p n="Input any negative number: "
set /a m=~n+1
echo The absolute of %n% is: %m%
pause >nul

It's too easy by using it.

No decimal fraction, but zero is permitted.

[ Last edited by scriptor on 2007-1-17 at 11:16 AM ]
Floor 6 Posted 2007-01-17 23:37 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
A crude example

Each of the upper and lower limits of the value of a is reduced by half

set /a b=(a*2+1)%%2*a
Recent Ratings for This Post ( 5 in total) Click for details
RaterScoreTime
everest79 +4 2007-01-18 01:12
redtek +9 2007-01-18 02:39
namejm +4 2007-01-18 10:23
lxmxn +4 2007-01-18 11:10
minmin888 +1 2007-05-04 10:58
Floor 7 Posted 2007-01-17 23:47 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Please delete it. It was written wrong.

[ Last edited by scriptor on 2007-1-17 at 11:14 AM ]
Floor 8 Posted 2007-01-17 23:51 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Please let the moderator delete it.

[ Last edited by scriptor on 2007-1-17 at 11:12 AM ]
Floor 9 Posted 2007-01-17 23:59 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Brother scriptor

Here are two suggestions for you

1. Use more Chinese
2. Do more testing
Floor 10 Posted 2007-01-18 00:09 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Originally posted by qzwqzw at 2007-1-17 10:59:
Brother Scriptor

Here are two suggestions for you

1. Use more Chinese
2. Do more testing


You mean the code on floor 8?
Sorry, I made a mistake, that one can only calculate odd numbers

Thank you
I was in a hurry and sent it over
Floor 11 Posted 2007-01-18 00:15 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Have you tested the ~n+1 code in #4 with positive integers?

In addition, keeping the layout concise is our common obligation
Floor 12 Posted 2007-01-18 00:19 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
I have stated there:
@echo off
set /p n="Input any negative number: "
is to input a negative number.

Thanks. Remembered
I found that I made many mistakes these days.
Really embarrassed to post again
Floor 13 Posted 2007-01-18 01:13 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Originally posted by qzwqzw at 2007-1-17 10:37 AM:
A crude example

Shrink the upper and lower limits of a by half each

set /a b=(a*2+1)%%2*a


Thank you qzwqzw, it seems I overcomplicated the problem
Floor 14 Posted 2007-01-18 10:26 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
qzwqzw's idea is good, PF.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 15 Posted 2007-01-18 11:38 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
Gently ask:

Teacher qzwqzw

Can we use the binary assignment symbol of SET for conversion??

[ Last edited by tghksj on 2007-1-17 at 10:50 PM ]
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Forum Jump: