Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Collection of Math Functions(For My Homework)


  • Please log in to reply
7 replies to this topic
System Monitor
  • Members
  • 508 posts
  • Last active: Mar 26 2012 05:13 AM
  • Joined: 09 Mar 2007
I don't really like to do the repetitive math homework so I created a bunch of different ways to solve different problems. In the future I plan to add many many more, although right now I have all I need. If you have any requested functions for me to add, I will attempt to learn how it works and add it to the list. I will also be updating this as much as possible. Hope these are of good use! :p

Here is the code
#NoEnv
#SingleInstance, Force
SetBatchlines -1
minaddhowmuch := 1

SetWorkingdir, %A_ScriptDir%\

Gui, 3:Font, s11 normal
Gui, 3:Add, ListView,x5 y5 w340 h294 Grid Sort Count Report grunfunction, Function|Detail
Gui, 3:Add, Button,x233 y300 w110 h22 gExit , Exit
;Gui, 3:Add, Button, x285 y300 w50 h22 grunfunction, Run
Gui, 3:Add, Groupbox,x4 y295 w115 h27,


;;;
Gui, 3:Add, Edit, x130 y300 w100 h22 vMyEdit gSearchForMovie,
;;;
Gui, 3:Add, Text,x7 y304 , Search
Gui, 3:Show,w350 h325,Functions
;;;
load:
Gui, 3:Default
LV_Delete()
x := 1
loop
{
Filereadline,function,functions.txt,%x%
Filereadline,description,functions.txt,% x+1
If errorlevel = 1
  break
LV_Add(%A_Index%,function, description)
x := x+2
}
Lv_ModifyCol()
return

SearchForMovie:
Gui, 3: Submit, NoHide
Gui, 3: Default
LV_Delete()
if Myedit =
{
  Gosub,load
  return
}
x := 1
Loop
{
    Filereadline,function,functions.txt,%x%
    If errorlevel
      break
    Filereadline,description,functions.txt,% x+1
    if InStr(function, MyEdit)
    {
      ;Msgbox %function% = %Myedit%
      LV_Add("",function,description)  ; Select each row whose first field contains the filter-text
    }
    x := x+2
}
Lv_ModifyCol()
Return

Quadf:
Inputbox,a,Quadratic,Enter a
Inputbox,b,Quadratic,Enter b
Inputbox,c,Quadratic,Enter c
dis := Eval(b**2-4*a*c)()
dis := Sqrt(dis)
andwer1 := (-b+dis)/(2*a)
andwer2 := (-b-dis)/(2*a)
Msgbox, %answer1% and %answer2%
return

FacAdd:
Inputbox,num,Adding Factorial, Give a number
answer := 0
Loop,%num%
{
answer := answer+A_Index
}
Msgbox,%num%! = %answer%
return

SolveReg:
Inputbox,tosolve,Solve,Enter an Expression
answer := Eval(tosolve)
Msgbox, %tosolve% = %answer%
return

LCD: ;Least common denominator
InputBox,lcd1,LCD,Enter First Number
Inputbox,lcd2,LCD,Enter Second Number`nFirst number was %lcd1%
answer := LCD(lcd1,lcd2)
Msgbox, The least common denominator of %lcd1% and %lcd2% is %answer%
return

Exp:
Inputbox,nume,Exponent,Give a number
Inputbox,numeu,Exponent,Give a number to the power of %nume%
answer := nume**numeu
Msgbox, %nume%**%numeu% = %answer%
return

Factorial:
Inputbox,num,Factorial, Give a number
answer := 1
Loop,%num%
{
answer := answer*A_Index
}
Msgbox,%num%! = %answer%
return

Distance:
;Formula Sqrt(|x-y|+|xs1-ys1)
Inputbox, xy,X and Y,Give x and y
Inputbox,xsys,Xsub1 and Ysub1, Give xsub1 and ysub1`nYou previously entered (%xy%)
;x is xy1 and y is xy2
Stringsplit, xy,xy,`,,`,
;x is xys1 y is xys2
Stringsplit,xys,xsys,`,,`,
;Retruns absolute value of x-xs1
xmy := Abs(xys1 - xy1)
xsmys := Abs(xys2 - xy2)
tosqrt := xmy**2+xsmys**2
answer := Sqrt(xmy**2+xsmys**2)
Msgbox, Distance is %answer% = Sqrt of %tosqrt%
return


Pythag:
InputBox,a,Pythag,Enter a
InputBox,b,Pythag,Enter b`nA was %a%
answer := (a ** 2 + b ** 2) ** 0.5
Msgbox, C is %answer%
return

Algebra:
Inputbox, equation, Equation, Enter a equation`nNote:Cannot do 5x must do 5*x`n5*x*x or 5x(squared) should be use as 5(x*x)
Inputbox, min, Min, Min number in the search proccess`nYour equation was %equation%
Inputbox, max, Max, Max number in the search proccess`nYour equation was %equation%`nThe min number was %min%
howmuch := (1-min+max) + 100 ; how many times to loop
prog := 100/howmuch
Loop %howmuch%
{
  ;Progress, %prog%, %min%,, Progress
  StringReplace, eq, equation, x, (%min%), all
  StringSplit,eq,eq,=
  deq := Eval(eq1) 
  deq2 := Eval(eq2)
  if deq = %deq2% ; check if each side of the equation equals the same
    goto, answer
  if min + 1 < deq
  {
    min--
    minaddhowmuch := 0.01
  }
  min := min + minaddhowmuch
}
Progress,off
Msgbox, 4, Couldn't Find, Could not find Answer, Do you wish to try again with one more decimal place?
If Msgbox Yes
{
  minaddhowmuch := minaddhowmuch / 10
  gosub, Algebra
}
return

answer:
Progress,off
Msgbox x = %min%
return

Parabola:
Inputbox, equation, Equation, Enter your equation
Inputbox, min, Domain min, Enter the min number in the domain`nYour equation was: %equation%
Inputbox, max, Domain max, Enter the max number in the domain`nYour equation was: %equation%`nMin for domain: %min%
Gui, 2:Add, ListView, x6 y10 w380 h160 , When x is ...|%equation% is ...
Gui, 2:Default
Loop % 1-min+max {
  StringReplace num, equation, x, (%min%), all
  num := Eval(num)
  LV_Add("", min, num)
  min++
}
Gui, 2:Show,h205 w395 , Answers
Return

GuiClose:
ExitApp

Reload:
Reload
return

runfunction:
if A_GuiEvent = DoubleClick
{
  LV_GetText(RowText, A_EventInfo)
  ;The  following converts row to gosub label
  if Rowtext = Solve Regular Expressions
    label := "SolveReg"
  else if Rowtext = Algebra
    label := "Algebra"
  else if Rowtext = Distance
    label := "Distance"
  else if Rowtext = Exponents
    label := "Exp"
  else if Rowtext =  f(x)
    label := "f(x)"
  else if Rowtext = Adding Factorial?
    label := "FacAdd"
  else if Rowtext = Factorial
    label := "Factorial"
  else if Rowtext = Pythagoreon
    label := "Pythag"
  else if Rowtext = Quadratic Formula
    label := "Quadf"
  else if Rowtext = Least Common Denominator
    label := "LCD"
  Gosub, %label%
}
return

Exit:
ExitApp

Eval(x) {   ; Evaluate arithmetic expression with numbers, + - / * ( )
   Return Eval#(RegExReplace(x,"-","#")) ; # = subtraction, to distinguish from sign
}

Eval#(x) {  ; Evaluate expression with numbers, + #(subtract) / * ( ). Recurse into (..)
   Return RegExMatch(x,"(.*)\(([^\(\)]+)\)(.*)",y) ? Eval#(y1 . Eval@(y2) . y3) : Eval@(x)
}

Eval@(x) {  ; Evaluate expression with numbers, + #(subtract) / *
   RegExMatch(x,"(.*)(\+|#)(.*)",y)    ; last + or -
   IfEqual y2,+, Return Eval@(y1) + Eval@(y3)
   IfEqual y2,#, Return Eval@(y1) - Eval@(y3)

   RegExMatch(x,"(.*)(\*|/)(.*)",y)    ; last * or /
   IfEqual y2,*, Return Eval@(y1) * Eval@(y3)
   IfEqual y2,/, Return Eval@(y1) / Eval@(y3)

   Return x ? x : 0                    ; empty expression: 0, number: unchanged
}

;Least Common Denominator Created By "invalid user"
;Returns the LCD of N1 and N2 
LCD(N1,N2) 
{ 
   NL = 1,2,3,4,5,6,7,8,9 
   If N1 = %N2% 
      Return "1" 
   ;CFL is Common Factor List 
   Loop, 2 
   { 
      I := N%A_Index% 
      LInt := A_Index 
      Loop, %I% 
      { 
         R := N%LInt% / A_Index 
         StringSplit, R, R, . 
         If R2 Not Contains %NL% 
         ;Use this method so second array not needed to check how many items list 
         ;for trimming comma(s) 
         { 
            If CFL%LInt% = 
               CFL%LInt% = %R1% 
            Else 
               CFL%LInt% := R1 "," CFL%LInt% 
         }    
      } 
   } 
   Sort, CFL1, N D, 
   Sort, CFL2, N D, 
   StringSplit, CL, CFL1, `, 
   Loop, %CL0% 
   { 
      Factor := CL%A_Index% 
      If Factor = 1 
         Continue 
      If Factor In %CFL2% 
         Break 
   } 
   If Factor Not In %CFL2% 
      Return "1" 
   Return Factor 
} 

Numlock::Sendinput,x
End::Sendinput,(
PgDn::Sendinput,)
Pgup::Sendinput,=

Here is functions.txt
It needs to be in the same folder as the script
Solve Regular Expressions
Solve Regualr Problems, such as 2+2
Algebra
Solve algebra problems, variable must be x and can only be on one side of the equation
Distance
Find the distance between two points on a cooridante plane
Exponents
Find the answer to exponents
f(x)
Give numbers for graphing f(x) equations
Adding Factorial?
Add Factorial ex. 1+2+3=6
Factorial
Factorials ex. 3!=6
Pythagoreon
Find C for A**2+B**2=C**2
Quadratic Formula
Find the horizontal intercept for graphing Quadratic Equations
Least Common Denominator
The least common Denominator of two numbers


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
You may want to check the Popup calculator, which has all this and much more with a different user interface. You can copy the functions from there, which you need. For example
LCM(a,b) {      ; Least Common Multiple

   Return a * (b // GCD(a,b))

}



GCD(a,b) {      ; Euclidean GCD: Greatest Common Divisor

   Return b=0 ? Abs(a) : GCD(b, mod(a,b))

}


System Monitor
  • Members
  • 508 posts
  • Last active: Mar 26 2012 05:13 AM
  • Joined: 09 Mar 2007
Wow.
Thx a million

interiot
  • Members
  • 74 posts
  • Last active: May 31 2014 07:15 PM
  • Joined: 06 Nov 2005
WOW. Popup calculator gets my vote for the "most heroic use of AHK for something it's ill-suited for" award. AHK is great as a low-overhead GUI automation tool, but its syntax seems to consciously avoid features that facilitate writing larger-scale programs.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
The calculator provides an interface to hundreds of math functions, graphics and vector lists. It does not require structured, or object oriented code. The only difficulty is with the dynamic expression evaluator, with all the variables being global. They could be in conflict with local variables. Otherwise, AHK is not "ill-suited" for this: easy UI, regular expressions, support for double precision, long-long integer and string types, hotkeys, etc., all what is needed.

XYZ
  • Members
  • 224 posts
  • Last active: Apr 29 2010 01:48 PM
  • Joined: 20 Mar 2010
Quadf:
Inputbox,a,Quadratic,Enter a
Inputbox,b,Quadratic,Enter b
Inputbox,c,Quadratic,Enter c
dis := Eval(b**2-4*a*c)()
dis := Sqrt(dis)
andwer1 := (-b+dis)/(2*a) u wrote andwer1
andwer2 := (-b-dis)/(2*a)
Msgbox, %answer1% and %answer2% u wrote answer1
return


just the mix up of andwer and answer

marian33
  • Members
  • 1 posts
  • Last active: Sep 02 2010 03:16 AM
  • Joined: 02 Sep 2010
Honestly speaking, math is a very difficult subject and homework for me. I have no idea that it is that simple with the help of the right tools. If I had known this before, maybe I would have passed it the first time.

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
The way maths are taught in schools is IMO flawed.
First of all, they make you study maths. This is wrong, as you can only either understand a certain math concept or not. The effort should be spent into making people actually know what they're doing instead of repeating the same thing over and over.
Second, it's all pointless if they don't show you how can it be useful, specially latter things like exponents or graphing.
In conclusion, schools must try harder :)