UPDATE: I have fixed all quadratics, I have added support for negatives and decimals, as well as finishing all forms listed with a 'Y' under 'Solved'. This is version 11, not released to deviantart yet, but version 09 is available for download. Updates will come rather quite frequently (but not on weekends most likely). Here is the code:
Code: Select all
--Solveable# Solved/Negatives/Decimals Type
--0 Y/Y/Y Expression
--1 Y/Y/Y x^2+x+1=0 or x^2+x=1
--2 Y/Y/Y x+1=0 or x=0
--3 Y/Y/Y 1+x=0
--4 N/N/N x^2+1=0
--5 N/N/N x^2=0
function Initialize()
Equation = SKIN:GetVariable('Equation')
Solveable = 2
Coefficients = {}
Numbers = {}
Check = {}
Length = {}
k=1
i=1
j=0
m=1
n=0
o=0
p=0
XNumbers = {}
Xlocations = {}
Alocations = {}
Elocations = {}
for i=1,#Equation do
Coefficients[i] = '0'
end
for i=1,#Equation do
Coefficients[i] = string.sub(Equation, i, i)
end
for i=1,#Equation do
Numbers[i] = '0'
end
for i=1,#Equation do
Numbers[i] = string.sub(Equation, i, i)
end
for i=1,#Equation do
Xlocations[i] = '0'
end
for i=1,#Equation do
Xlocations[i] = string.sub(Equation, i, i)
end
for i=1,#Equation do
Alocations[i] = '0'
end
for i=1,#Equation do
Elocations[i] = '0'
end
for i=1,#Equation do
Elocations[i] = string.sub(Equation, i, i)
end
i = 0
repeat
repeat
i = i+1
until Coefficients[i] == 'x' or Coefficients[i] == nil
if Coefficients[i] == 'x' then
j = j+1
end
if Coefficients[i] ~= nil then
Xlocations[j] = i
end
i = i+1
until Coefficients[i] == nil
i = 0
repeat
repeat
i = i+1
until Coefficients[i] == '+' or Coefficients[i] == '-' or Coefficients[i] == nil
if Coefficients[i] == '+' or Coefficients[i] == '-' then
o = o+1
end
if Coefficients[i] ~= nil then
Alocations[o] = i
end
i = i+1
until Coefficients[i] == nil
i=0
repeat
repeat
i = i+1
until Coefficients[i] == '=' or Coefficients[i] == nil
if Coefficients[i] == '=' then
p = p+1
end
if Coefficients[i] ~= nil then
Elocations[p] = i
end
i = i+1
until Coefficients[i] == nil
i=0
repeat
n=0
i = i+1
if j == 0 and p == 0 and Solveable ~= -2 then
Solveable = 0
else
Solveable = -2
end
until Coefficients[i] == nil
if Coefficients[1] == '-' then
if j == 2 and (o == 2 or o == 3 or o == 4) and p == 1 and Coefficients[Xlocations[1]+1] == '^' and Coefficients[Xlocations[1]+2] == '2' and Alocations[2] < Elocations[1] and Xlocations[1] < Elocations[1] then
Solveable = 1
end
else
if j == 2 and (o == 1 or o == 2 or o == 3) and p ==1 and Coefficients[Xlocations[1]+1] == '^' and Coefficients[Xlocations[1]+2] == '2' and Alocations[1] < Elocations[1] and Xlocations[1] < Elocations[1] then
Solveable = 1
end
end
if Alocations[1] ~= '0' then
if Coefficients[1] ~= '-' and Coefficients[2] ~= 'x' then
if Alocations[2] ~= '0' then
if j == 1 and (o == 1 or o == 2) and p == 1 and Alocations[1] > Xlocations[1] and Alocations[1] < Elocations[1] and Xlocations [1] < Elocations[1] then
Solveable = 2
end
else
if j == 1 and (o == 1 or o == 2) and p == 1 and Alocations[1] < Elocations[1] and Xlocations[1] < Elocations[1] then
Solveable = 2
end
end
else
if Alocations[2] ~= '0' then
if j == 1 and (o == 1 or o == 2) and p == 1 and Alocations[2] > Xlocations[1] and Alocations[2] < Elocations[1] and Xlocations [1] < Elocations[1] then
Solveable = 2
end
else
if j == 1 and (o == 1 or o == 2) and p == 1 and Xlocations[1] < Elocations[1] then
Solveable = 2
end
end
end
else
if j == 1 and (o == 0 or o == 1) and p == 1 and Xlocations[1] < Elocations[1] then
Solveable = 2
end
end
if Alocations[1] ~= '0' then
if j == 1 and (o == 1 or o == 2) and p == 1 and Alocations[1] < Xlocations[1] and Alocations[1] < Elocations[1] and Xlocations[1] < Elocations[1] then
Solveable = 3
end
end
if Equation == '0' then
SKIN:Bang('!SetVariable', 'Res1', 'Enter an equation above.')
SKIN:Bang('!SetVariable', 'Res2', '')
else
SKIN:Bang('!SetVariable', 'Res1', 'I don\'t understand. Send a')
SKIN:Bang('!SetVariable', 'Res2', 'request to the developer.')
end
if Solveable == 0 then
if Equation == '0' then
SKIN:Bang('!SetVariable', 'Res1', 'Enter an equation above.')
SKIN:Bang('!SetVariable', 'Res2', '')
else
SKIN:Bang('!SetVariable', 'Res1', Equation)
SKIN:Bang('!SetVariable', 'Res2', 'Reset before next input.')
end
else
if Solveable == 1 then
i=1
if Coefficients[1] == 'x' then
Numbers[1] = '1'
else
repeat
Check[i] = Coefficients[i]
Numbers[1] = Numbers[1]..Check[i]
i = i+1
until i == Xlocations[1]
Numbers[1] = string.sub(Numbers[1], 2, i-k+1)
end
Length[1] = i-k
if Coefficients[1] == 'x' then
Length[1] = '1'
end
i = i+1
if Coefficients[i] == '^' then
i = i+1
if Coefficients[i] == '2' then
i = i+1
if Coefficients[i] == '+' then
i = i+1
end
end
end
k = i
if Coefficients[k] == 'x' then
Numbers[2] = '1'
else
repeat
Check[i] = Coefficients[i]
Numbers[2] = Numbers[2]..Check[i]
i = i+1
until i == Xlocations[2]
Numbers[2] = string.sub(Numbers[2], 2, i-k+1)
end
Length[2] = i-k
if Coefficients[k] == 'x' then
Length[2] = '1'
end
i = i+1
if Coefficients[i] == '+' then
i = i+1
end
k = i
if Coefficients[i] == '=' then
Numbers[3] = '0'
else
repeat
Check[i] = Coefficients[i]
Numbers[3] = Numbers[3]..Check[i]
i = i+1
until i == Elocations[1]
Numbers[3] = string.sub(Numbers[3], 2, i-k+1)
end
Length[3] = i-k
if Coefficients[k] == 'x' then
Length[3] = '1'
end
i = i+1
repeat
Check[i] = Coefficients[i]
Numbers[4] = Numbers[4]..Check[i]
i = i+1
until Coefficients[i] == nil
Numbers[4] = string.sub(Numbers[4], 2, i-k)
Length[4] = i-k
if Coefficients[k] == 'x' then
Length[4] = '1'
end
a, b, c = tonumber(Numbers[1]), tonumber(Numbers[2]), (tonumber(Numbers[3]) - tonumber(Numbers[4]))
n = 0
i = 0
if Delta(a, b, c) > 0 then
SKIN:Bang('!SetVariable', 'Res1', 'x1='..Result1(a, b, c))
SKIN:Bang('!SetVariable', 'Res2', 'x2='..Result2(a, b, c))
else
if Delta(a, b, c) == 0 then
SKIN:Bang('!SetVariable', 'Res1', 'x='..Result1(a, b, c))
SKIN:Bang('!SetVariable', 'Res2', '')
else
SKIN:Bang('!SetVariable', 'Res1', 'There are no real values of x.')
SKIN:Bang('!SetVariable', 'Res2', '')
end
end
else
if Solveable == 2 then
k=1
i=1
if Coefficients[1] == '-' and Coefficients[2] == 'x' then
Numbers[1] = '-1'
i=i+1
else
if Coefficients[1] == 'x' then
Numbers[1] = '1'
else
repeat
Check[i] = Coefficients[i]
Numbers[1] = Numbers[1]..Check[i]
i = i+1
until i == Xlocations[1]
Numbers[1] = string.sub(Numbers[1], 2, i-k+1)
end
end
Length[1] = i-k
if Coefficients[1] == 'x' then
Length[1] = '1'
end
i = i+1
if Coefficients[i] == '+' then
i = i+1
end
k = i
if Coefficients[k] == '=' then
Numbers[2] = '0'
else
repeat
Check[i] = Coefficients[i]
Numbers[2] = Numbers[2]..Check[i]
i = i+1
until i == Elocations[1]
Numbers[2] = string.sub(Numbers[2], 2, i-k+1)
end
Length[2] = i-k
if Coefficients[k] == '=' then
Length[2] = '0'
end
i = i+1
k = i
repeat
Check[i] = Coefficients[i]
Numbers[3] = Numbers[3]..Check[i]
i = i+1
until Coefficients[i] == nil
Numbers[3] = string.sub(Numbers[3], 2, i-k+1)
Length[3] = i-k
Numbers[3] = Numbers[3] - Numbers[2]
Numbers[3] = Numbers[3] / Numbers[1]
SKIN:Bang('!SetVariable', 'Res1', 'x='..Numbers[3])
SKIN:Bang('!SetVariable', 'Res2', '')
else
if Solveable == 3 then
k=1
i=1
if Coefficients[1] == '-' then
if Coefficients[2] == 'x' and Coefficients[3] == '=' then
Numbers[1] = '-1'
i = i+1
Length[1] = i-k
if Coefficients[1] == 'x' then
Length[1] = '1'
end
i = i+1
if Coefficients[i] == '+' then
i = i+1
end
k = i
if Coefficients[k] == '=' then
Numbers[2] = '0'
else
repeat
Check[i] = Coefficients[i]
Numbers[2] = Numbers[2]..Check[i]
i = i+1
until i == Elocations[1]
Numbers[2] = string.sub(Numbers[2], 2, i-k+1)
end
Length[2] = i-k
if Coefficients[k] == '=' then
Length[2] = '0'
end
i = i+1
k = i
repeat
Check[i] = Coefficients[i]
Numbers[3] = Numbers[3]..Check[i]
i = i+1
until Coefficients[i] == nil
Numbers[3] = string.sub(Numbers[3], 2, i-k+1)
Length[3] = i-k
Numbers[3] = Numbers[3] - Numbers[2]
Numbers[3] = Numbers[3] / Numbers[1]
SKIN:Bang('!SetVariable', 'Res1', 'x='..Numbers[3])
SKIN:Bang('!SetVariable', 'Res2', '')
else
repeat
Check[i] = Coefficients[i]
Numbers[1] = Numbers[1]..Check[i]
i = i+1
until i == Xlocations[1]
Numbers[1] = string.sub(Numbers[1], 2, i-k+1)
Length[1] = i-k
if i ~= Elocations[1] then
i = i+1
end
if Coefficients[i] == '+' then
i = i+1
end
k = i
if Coefficients[k] == '=' then
Numbers[2] = '0'
else
repeat
Check[i] = Coefficients[i]
Numbers[2] = Numbers[2]..Check[i]
i = i+1
until i == Xlocations[1]
Numbers[2] = string.sub(Numbers[2], 2, i-k+1)
end
Length[2] = i-k
if Coefficients[k] == 'x' then
Length[2] = '1'
end
i = i+1
if Coefficients[i] == '=' then
i = i+1
end
k = i
repeat
Check[i] = Coefficients[i]
Numbers[3] = Numbers[3]..Check[i]
i = i+1
until Coefficients[i] == nil
Numbers[3] = string.sub(Numbers[3], 2, i-k+1)
Length[3] = i-k
Numbers[3] = Numbers[3] - Numbers[2]
Numbers[3] = Numbers[3] / Numbers[1]
SKIN:Bang('!SetVariable', 'Res1', 'x='..Numbers[3])
SKIN:Bang('!SetVariable', 'Res2', '')
end
else
if Alocations[1] ~= '1' then
repeat
Check[i] = Coefficients[i]
Numbers[1] = Numbers[1]..Check[i]
i = i+1
until i == Alocations[1]
else
repeat
Check[i] = Coefficients[i]
Numbers[1] = Numbers[1]..Check[i]
i = i+1
until i == Xlocations[1]
end
Numbers[1] = string.sub(Numbers[1], 2, i-k+1)
Length[1] = i-k
i = i+1
if Coefficients[i] == '+' then
i = i+1
end
k = i
if Coefficients[k] == 'x' then
Numbers[2] = '1'
else
if Coefficients[k] == '=' then
Numbers[2] = '0'
else
repeat
Check[i] = Coefficients[i]
Numbers[2] = Numbers[2]..Check[i]
i = i+1
until i == Xlocations[1]
Numbers[2] = string.sub(Numbers[2], 2, i-k+1)
end
end
Length[2] = i-k
if Coefficients[k] == 'x' then
Length[2] = '1'
end
i = i+1
if Coefficients[i] == '=' then
i = i+1
end
k = i
repeat
Check[i] = Coefficients[i]
Numbers[3] = Numbers[3]..Check[i]
i = i+1
until Coefficients[i] == nil
Numbers[3] = string.sub(Numbers[3], 2, i-k+1)
Length[3] = i-k
Numbers[3] = Numbers[3] - Numbers[1]
Numbers[3] = Numbers[3] / Numbers[2]
SKIN:Bang('!SetVariable', 'Res1', 'x='..Numbers[3])
SKIN:Bang('!SetVariable', 'Res2', '')
end
else
end
end
end
end
end
function Delta(a, b, c)
return (b*b - 4*a*c)
end
function Result1(a, b, c)
if (((-b)-math.sqrt(Delta(a, b, c)))/(2*a)) == 0 then
return 0
else
return (((-b)-math.sqrt(Delta(a, b, c)))/(2*a))
end
end
function Result2(a, b, c)
if (((-b)+math.sqrt(Delta(a, b, c)))/(2*a)) == 0 then
return 0
else
return (((-b)+math.sqrt(Delta(a, b, c)))/(2*a))
end
end
function Update()
end
Code: Select all
[Rainmeter]
DynamicWindowSize=1
IfConditionMode=1
Update=100
BackgroundMode=2
SolidColor=80,80,80,220
;Hidden=1
;Group=Left
[Variables]
Equation=0
FontColorQuestion=255,255,255,255
FontColorAnswer=211,255,207,255
FieldColor=100,255,100,255
Res1=-1
Res2=-1
[MeasureCalc]
DynamicVariables=1
Measure=calc
Formula=#Equation#
[MeasureCalc2]
DynamicVariables=1
Measure=calc
Formula=1
IfCondition=(#Res1#=#Equation#)
IfTrueAction=[!SetVariable Res1 [MeasureCalc]]
[MeasureInput]
DynamicVariables=1
Measure=Plugin
Plugin=InputText.dll
X=5
Y=5
H=16
W=210
FontColor=#FontColorQuestion#
SolidColor=#FieldColor#
FontFace=Seqoe UI
StringStyle=Bold
FontSize=10
FocusDismiss=1
DefaultValue=""
UpdateDivider=86400
Command1=[!SetVariable Equation "$UserInput$"][!CommandMeasure "MeasureLuaScript" "Initialize()"]
[MeasureLuaScript]
DynamicVariables=1
Measure=Script
ScriptFile=Algebra.lua
[MeterBack]
DynamicVariables=1
Meter=Image
SolidColor=0,0,0,255
W=220
H=69
[MeterInput]
DynamicVariables=1
Meter=String
X=5
Y=5
H=19
W=210
FontFace=Seqoe UI
FontSize=11
FontColor=#FontColorQuestion#
SolidColor=#FieldColor#
StringStyle=Bold
AntiAlias=1
DynamicVariables=1
Text=#Equation#
ToolTipType=1
ToolTipTitle="Algebra Solver"
ToolTipText="Click and Enter Formula#CRLF#Right Click to Reset"
ClipString=1
LeftMouseUpAction=!RainmeterPluginBang "MeasureInput ExecuteBatch 1"
RightMouseUpAction=!RainmeterRefresh
[MeterAnswer]
DynamicVariables=1
Meter=String
X=5
Y=1R
H=19
W=210
FontFace=Seqoe UI
FontSize=11
FontColor=#FontColorAnswer#
SolidColor=#FieldColor#
StringStyle=Bold
AntiAlias=1
DynamicVariables=1
Text=#Res1#
ToolTipType=1
ToolTipTitle="Algebra Solver"
ToolTipText="Use '**' for '^' when evaluating#CRLF#an expression, for an equation#CRLF#with 'x' use '^'."
[MeterAnswer2]
DynamicVariables=1
Meter=String
X=5
Y=1R
H=19
W=210
FontFace=Seqoe UI
FontSize=11
FontColor=#FontColorAnswer#
SolidColor=#FieldColor#
StringStyle=Bold
AntiAlias=1
DynamicVariables=1
Text=#Res2#
ToolTipType=1
ToolTipTitle="Algebra Solver"
ToolTipText="Use '**' for '^' when evaluating#CRLF#an expression, for an equation#CRLF#with 'x' use '^'.
If you would report any bugs, as well as request euqations/forms, it would be much appreciated!