Here is another addition
I added the up-down gui control as well as auto solve whenever you change the values. This removes the need of a solve button. Also, when the "a" term is 0, the equation becomes linear and still requires a solution. I added the functionality too. Check it out.
Code:
;Quadratic Equation Solver v1.0
;By Muhammad Umar
#singleinstance, force
;########## Add GUI Controls #############################
Gui, Add, Picture, x6 y0 w417 h102 , %A_ScriptDir%\button.gif
Gui, Add, GroupBox, x6 y110 w175 h140 , User Input
Gui, Add, Text, x16 y135 w160 h20 , Enter the terms of the equation
Gui, Add, Text, x16 y160 w40 h20 , Enter a:
Gui, Add, Text, x16 y190 w40 h20 , Enter b:
Gui, Add, Text, x16 y220 w40 h20 , Enter c:
Gui, Add, Edit, x66 y160 w80 h20,
Gui, Add, UpDown, gEquation va1 Range-100-100, 0
Gui, Add, Edit, gEquation x66 y190 w80 h20,
Gui, Add, UpDown, gEquation vb1 Range-100-100, 0
Gui, Add, Edit, gEquation x66 y220 w80 h20,
Gui, Add, UpDown, gEquation vc1 Range-100-100, 0
Gui, Add, Button, gAbout x385 y115 w45 h135, About
Gui, Add, GroupBox, x6 y255 w423 h50 , Equation
Gui, Add, Text,x16 y275, Original Equation:
Gui, Add, Edit, vEq ReadOnly r1 x120 y27320 w150 h40
Gui, Add, GroupBox, x190 y110 w190 h140 , Answer
Gui, Font, S12 CDefault
Gui, Add, Text, vAn1X r1 x216 y130 w70 h40 , x=
Gui, Add, Edit, vAn1 r1 x245 y130 w100 h40 , ?
Gui, Add, Text, vAnOr r1 x216 y170 w70 h40 , or
Gui, Add, Text, vAn2X r1 x216 y210 w70 h40 , x=
Gui, Add, Edit, vAn2 r1 x245 y210 w100 h40 , ?
gosub, solve
;########## Show GUI #####################################
Gui, Show, center h310 w435, Quadratic Equation Solver
Return
;########## GUI Close ####################################
GuiClose:
ExitApp
;########## Equation Solver Functions ####################
Solve:
gui, submit, nohide
dis := (b1**2-4*a1*c1)
imag := (dis < 0) ? "i" : ""
dis1 := Sqrt(Abs(dis))
if a1 != 0
{
if imag = i
{
ansp1 := round((-b1)/(2*a1),2)
ansp2 := round((dis1)/(2*a1),2)
ans1 := (ansp1 = 0 ? "" : ansp1 "+") ansp2 "i"
ans2 := (ansp1 = 0 ? "" : ansp1) "-" ansp2 "i"
}
else
{
ans1 := round((-b1+dis1)/(2*a1),2)
ans2 := round((-b1-dis1)/(2*a1),2)
}
}
else if b1 != 0
{
ans1 := round(-c1/b1,2)
}
GuiControl,, An1, % FixSigns(ans1)
GuiControl,, An2, % FixSigns(ans2)
if (ans1="" or ans2="") {
;MsgBox,, Error, Invalid user input.`nCould not complete the operation.
}
return
;########## About ########################################
About:
AText =
(
Quadratic Equation Solver
version 1.6
By Muhammad Umar
Comments and suggestions are welcome.
THANKS TO ALL THE COMMMUNITY MEMBERS AT AUTOHOTKEY FORUM!
Changelog:
1.1# Initial release.
1.2# GUI Improved. Removed Factorization Functions due to big error.
1.3# Added Equation GroupBox that displays complete equation entered by user.
1.4# Entering the terms of the equation automatically updates Original Equation EditBox.
1.5# Added Abs(num) in solving command so Sqrt can work on positive integers. Any equation can now be solved.
1.6# Made Original Equation EditBox Read-Only.
Future Realeases:
I hope to improved equation entry controls by restricting entry to numbers only.
)
MsgBox,,About, %Atext%
Return
;########## Original Equation Updator ####################
Equation:
gui, submit, nohide
result := (a1 != 0 ? a1 "x^2+" : "") (b1 != 0 ? b1 "x+" : "") (c1 != 0 ? c1 : "")
if result !=
{
result := result "=0"
if a1 = 0
{
guicontrol, hide, AnOr
guicontrol, hide, An2X
guicontrol, hide, An2
if b1 = 0
{
guicontrol, hide, An1X
guicontrol, hide, An1
}
else
{
guicontrol, show, An1X
guicontrol, show, An1
}
}
else
{
guicontrol, show, An1X
guicontrol, show, An1
guicontrol, show, AnOr
guicontrol, show, An2X
guicontrol, show, An2
}
}
else
{
guicontrol, show, An1X
guicontrol, show, An1
guicontrol, show, AnOr
guicontrol, show, An2X
guicontrol, show, An2
}
StringReplace, result, result, `+`+, `+, A
StringReplace, result, result, `+`-, `-, A
StringReplace, result, result, `+`=, `=, A
StringReplace, result, result, 1x, x, A
GuiControl,, Eq, %result%
gosub, Solve
return
FixSigns(in)
{
StringReplace, in, in, `-`-, `+, A
StringReplace, in, in, `+`+, `+, A
StringReplace, in, in, `-`+, `-, A
StringReplace, in, in, `+`-, `-, A
return in
}