AutoHotkey Community

It is currently May 27th, 2012, 3:16 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 43 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: March 21st, 2010, 1:42 pm 
Offline

Joined: March 20th, 2010, 9:49 am
Posts: 224
DISCONTINUED


Last edited by XYZ on April 29th, 2010, 8:43 am, edited 16 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 22nd, 2010, 7:05 pm 
Offline

Joined: February 10th, 2007, 5:18 am
Posts: 92
The thing about polynomials is that the highest power variable is telling you how many results exist, in both the real and imaginary number lines.

For quadratic equations, there are ALWAYS 2 answers (unless the 2 answers are identical). You are only giving real answers and failing to give imaginary solutions when and if they exist.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 27th, 2010, 3:04 pm 
Offline

Joined: March 20th, 2010, 9:49 am
Posts: 224
in v1.5
its a global solver


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2010, 9:15 am 
Offline

Joined: February 10th, 2007, 5:18 am
Posts: 92
I updated this a little bit to include the complex results. Note that in the complex plane, the coordinate are a combination of real and imaginary numbers.

I made the complex answers have integers rounded to 2 decimal places so the whole result can be seen.

Code:
;Quadratic Equation Solver v1.0
;By Muhammad Umar

;########## Add GUI Controls #############################
Gui, Add, Picture, x6 y0 w417 h102 , %A_ScriptDir%\button.gif
Gui, Add, GroupBox, x6 y110 w175 h190 , 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, gEquation va1 x66 y160 w80 h20 , +1
Gui, Add, Edit, gEquation vb1 x66 y190 w80 h20 , +5
Gui, Add, Edit, gEquation vc1 x66 y220 w80 h20 , +6
Gui, Add, Button, gSolve x16 y250 w150 h40, Solve!
Gui, Add, Button, gAbout x385 y115 w45 h185, About
Gui, Add, GroupBox, x6 y305 w423 h50 , Equation
Gui, Add, Text,x16 y325, Original Equation:
Gui, Add, Edit, vEq ReadOnly r1 x120 y320 w150 h40 ,1x^2+5x+6=0
Gui, Add, GroupBox, x190 y110 w190 h190 , Answer
Gui, Font, S12 CDefault
Gui, Add, Text, r1 x216 y150 w70 h40 , x=
Gui, Add, Edit, vAn1 r1 x245 y150 w100 h40 , ?
Gui, Add, Text, r1 x216 y190 w70 h40 , or
Gui, Add, Text, r1 x216 y230 w70 h40 , x=
Gui, Add, Edit, vAn2 r1 x245 y230 w100 h40 , ?

;########## Show GUI #####################################
Gui, Show, center h360 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 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)
}

GuiControl,, An1, %ans1%
GuiControl,, An2, %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%x^2+%b1%x+%c1%=0
StringReplace, result, result, `+`+, `+, A
StringReplace, result, result, `+`-, `-, A
GuiControl,, Eq, %result%


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2010, 1:11 pm 
Offline

Joined: March 20th, 2010, 9:49 am
Posts: 224
thx
it needed improvement
will upload v1.7 with your changes later


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 29th, 2010, 12:10 am 
Offline

Joined: February 10th, 2007, 5:18 am
Posts: 92
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 29th, 2010, 6:54 am 
Offline

Joined: March 20th, 2010, 9:49 am
Posts: 224
excellent work, maximo
i was also thinking of auto solving
actually i am working on another unlaunched project and paying little attention to this
but thx so much for your additions

i will have to make a large update to it now


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 29th, 2010, 11:42 am 
Offline

Joined: March 20th, 2010, 9:49 am
Posts: 224
i edited the post and updated it to v1.8

from version 2 i hope to renew the GUI :idea:

and use 2 decimal placed versions like 1.81 :lol:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2010, 7:03 am 
Offline

Joined: March 20th, 2010, 9:49 am
Posts: 224
Code:
ans2 := (ansp1 = 0 ? "" : ansp1) "-" ansp2 "i"


i dont understand this

is it the ternary operator??


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2010, 5:28 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
UMAR wrote:
is it the ternary operator??

Why didn't you search?
1) Manual
2) [VxE]'s guide

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2010, 10:31 pm 
Offline

Joined: February 10th, 2007, 5:18 am
Posts: 92
Yes it is,

var := (condition ? "true" : "false")

If the condition is true, then var would be "true" otherwise it would be "false"


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2010, 9:43 am 
Offline

Joined: March 20th, 2010, 9:49 am
Posts: 224
this project is a hit!
550+ views already!

its good in hw help :P :P


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 1st, 2010, 3:53 am 
Offline

Joined: March 20th, 2010, 9:49 am
Posts: 224
can i add this addition to restrict input to numbers only?

Code:
gui,submit,nohide
loop,parse,edit
{
 if a_loopfield not in 1,2,3,4,5,6,7,8,9,0,+,-
 stringreplace,edit,edit,%a_loopfield%,,all
}
guicontrol,,edit,%edit%
send,{end}
return


this is supposed to be in the gSubroutine for all the updown edits namely a1,b1,c1


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 1st, 2010, 8:22 am 
Offline

Joined: February 10th, 2007, 5:18 am
Posts: 92
I would add this to the top of the Equation subroutine right after the gui, submit, no hide

Also, do not change '%var%' to 'val' in the initiation of the second loop. This ensures that the replace function doesn't change the value the loop is parsing midway through the actual parse. I'm not sure how AutoHotKey handles this, but this could usually create problems or inaccurate results.

Code:
vars := "a1,b1,c1"
loop, parse, vars, `,
{
   var := a_loopfield
   val := %var%
   
   first := SubStr(val, 1, 1)
   if first not in +,-
      first := ""
   
   loop,parse,%var%
   {
      if a_loopfield not in 1,2,3,4,5,6,7,8,9,0
      {
         stringreplace,val,val,%a_loopfield%,,all
      }
   }

   guicontrol,,%var%,% first val
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 1st, 2010, 6:28 pm 
Offline

Joined: March 20th, 2010, 9:49 am
Posts: 224
the script i wrote is without reference to correct vars in the real script

of course when i add it i will change and test it


Last edited by XYZ on April 2nd, 2010, 2:43 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 43 posts ]  Go to page 1, 2, 3  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Stigg, tidbit and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group