判别解计算批处理文件(平方根)

Discriminant Solutions Calculation Batch File (Square Root)

这里是新内容,但我会尽量简明扼要。我正在尝试创建一个程序,该程序具有用户输入的 3 个值,然后该程序继续在 ax^2+bx+c(判别式 = D)中分别用作 A、B 和 C,并为您提供 "solutions" 它有(如果 D>0 那么 2 个解,如果 D = 0 一个 "double" 解,如果 D < 0 它将显示没有真正的解)。问题是,无论我多么努力,我都无法在稍后的过程中想出一种方法来计算平方根(如果判别式为正)。我试过而不是试图找到平方根来将判别式提高到 1/2(即 D^(1/2))但没有结果,也许我做错了

:positive
:squareroot
set /p M==%B%*%B%-4*%A%*%C% 
set /a number=%M%, last=2, sqrt=number/last
:next
set /a last=(last+sqrt)/2, sqrt=number/last
if %sqrt% lss %last% goto next
set /a Q=(-%B%+%last%)/2*%A%
set /a R=(-%B%-%last%)/2*%A%
echo ----------------------------------------------------
echo The first solution is ~%Q%~ and the second is ~%R%~ !
echo ----------------------------------------------------
pause
goto start

在网络上尝试和搜索后,我发现上面的代码实际上以某种方式计算了一个数字的平方根,非常接近(即 9 的平方根 = 3 但 15 的平方根也 = 3 ).尽管如此,出于项目的目的,它符合我的需要。

程序显示 %B%*%B%-4*%A%*%C%(而不是变量,用户选择的值)而不是计算数字,然后关闭。

好吧,我来猜猜你想做什么。 (用一些变量扩展了示例,这些变量显然在您的代码中的其他地方定义,以使其工作)。

你的台词set /p M==%B%*%B%-4*%A%*%C%是胡说八道。 set /p 要求用户输入,因此您不想使用 /p。变量设置为简单的 =,而不是 ==,我认为您希望变量保存 公式 ,而不是结果。 %B%被变量B的值代替,可能不是,你想要什么。要转义百分号,将它们加倍,这样 %M%字面上 包含 %B%*%B%-4*%A%*%C%.
只设置 /a number=%M% 是行不通的,因为你想在 这里 计算变量,所以你需要另一层解析(你可以用 call).

@echo off
setlocal
:positive
:squareroot
set b=3
set a=4
set c=5

set  "M=%%B%%*%%B%%-4*%%A%%*%%C%%" 
call set /a number=%M%
set /a last=2, sqrt=number/last 

:next
set /a last=(last+sqrt)/2, sqrt=number/last
if %sqrt% lss %last% goto next
set /a Q=(-%B%+%last%)/2*%A%
set /a R=(-%B%-%last%)/2*%A%
echo ----------------------------------------------------
echo The first solution is ~%Q%~ and the second is ~%R%~ !
echo ----------------------------------------------------
pause
goto start

(注意:我没有分析其余代码,因为它似乎工作正常)