Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Lua basic Calculator. (V2)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Tutorials -> LUA Tutorials
View previous topic :: View next topic  
Author Message
AylinCE
Grandmaster Cheater Supreme
Reputation: 34

Joined: 16 Feb 2017
Posts: 1420

PostPosted: Wed Oct 30, 2024 5:24 pm    Post subject: Lua basic Calculator. (V2) Reply with quote

It could probably be coded shorter, but even the examples didn't give the desired result.

It was coded only for basic calculation operations.

*Addition (+)
*Subtraction (-)
*Division (/)
*Multiplication (*)

Note: It was designed as a tool that does multiple operations at the same time and in the order you specify, not according to the global mathematical order of operations.

Code:
function calculate(x,op,y)
    if (op == "+") then
        return x + y
    elseif (op == "-") then
        return x - y
    elseif (op == "*") then
        return x * y
    elseif (op == "/") then
        return x / y
    else
        return 0
    end
end

function characterFilter(text)
txt2 = (text):gsub("[^0123456789+-/* ]","")
 if #txt2~=#text then
   return false
 end
end

function Calculate_res(line1)
line = (line1):gsub(" ","")
l_filter = characterFilter(line)
   if l_filter==false then
     showMessage("Use only supported characters!\n0 1 2 3 4 5 6 7 8 9 + - / *")
     return
   end
local nm, nm1, nm2, nm3 = 0,0,0,0
local nmtbl3 = {}
local optbl3 = {}
for item in line:gmatch("(%d+)") do table.insert(nmtbl3,item) end
for item1 in line:gmatch("(%D)") do table.insert(optbl3,item1) end
  for l,k in pairs(optbl3) do
    nm = tonumber(nm) + 1
    nm1=nmtbl3[nm]
    nm2=nmtbl3[nm + 1]
    print(l,k)
      if l==1 then
        nm3 = nm3 + math.abs(calculate(nm1,k,nm2))
      else
        nm3 = math.abs(calculate(nm3,k,nm2))
         a1,a2 = math.modf(nm3)
         if a2==0 then nm3 = math.floor(nm3) end
        --print(nm3)
      end
   end
   return nm3
end


use:
Code:
res1 = Calculate_res("30 / 2 * 4 - 20")
res2 = Calculate_res("410 * 8 + 6")

result res1 = 40
result res2 = 3286

--------------------------------------------
--------------------------------------- Version: 2.0
--------------------------------------- Added: Double, Float values..
Code:
function calculate(x,op,y)
    if (op == "+") then
        return x + y
    elseif (op == "-") then
        return x - y
    elseif (op == "*") then
        return x * y
    elseif (op == "/") then
        return x / y
    else
        return 0
    end
end

function characterFilter(text)
txt2 = (text):gsub("[^0123456789+-/* .]","")
 if #txt2~=#text then
   return false
 end
end

function Calculate_res(line1)
line = (line1):gsub(" ","")
l_filter = characterFilter(line)
   if l_filter==false then
     showMessage("Use only supported characters!\n0 1 2 3 4 5 6 7 8 9 + - / * .")
     return
   end
local nm, nm1, nm2, nm3 = 0,0,0,0
aa2 = ""
aa3 = 0
local nmtbl3 = {}
local optbl3 = {}
for str in line:gmatch("%d+") do
 if string.find(line,str.."%.") then aa2=str.."." aa3=1 end
 if aa3==0 then table.insert(nmtbl3, tonumber(str) + .0) end
 if aa3==1 then
  if string.find(line,"%."..str) then
   aa2=aa2..str
  table.insert(nmtbl3, tonumber(aa2))
  aa3=0
 end
 end
end
for item1 in line:gmatch("(%D)") do
  if item1~="." then
    table.insert(optbl3,item1)
  end
end
  for l,k in pairs(optbl3) do
    nm = tonumber(nm) + 1
    nm1=nmtbl3[nm]
    nm2=nmtbl3[nm + 1]
      if l==1 then
        nm3 = calculate(nm1,k,nm2) --math.abs()
      else
        nm3 = calculate(nm3,k,nm2) --math.abs()
      end
         a1,a2 = math.modf(nm3)
         if a2==0 then nm3 = math.floor(nm3) end
         --print(nm3)
   end
   return nm3
end


-- use
Code:
line = "410.34591 * 8 + 6.422"
res = Calculate_res(line)
print(res)


res:
3289.18928 -- > Tested with calculator.
---------------------------------------
---------------------------------------

If you want to use this in a form:
1, Form
1, Button
2, Edit box

Code:
UDF1.CEButton1.OnClick=function(sender)
  if UDF1.CEEdit1.Text=="" then
    print("No number found for operation!")
  else
    line = UDF1.CEEdit1.Text
    res = Calculate_res(line)
    UDF1.CEEdit2.Text = res
  end
end

UDF1.CEEdit1.OnChange=function(sender)
 res2 = characterFilter(UDF1.CEEdit1.Text)
   if res2==false then
     UDF1.CEEdit1.Text=""
     showMessage("Use only supported characters!\n0 1 2 3 4 5 6 7 8 9 + - / *")
   end
end


---------------------------------------------------------
---------------------------------------------------------
Important note:
The mathematical operation coding given above will determine and return the result in the order you specify, as warned.

This is the same as using "=" after each operation with a calculator and then moving on to the next operation (addition).

However, the original mathematical operations follow this order:
1) What is in parentheses.
2) Exponents.
3) Multiplication and Division, from left to right.
4) Addition and Subtraction, from left to right.

In this respect, the mathematical operations are available in Lua itself and the coding will be short and precise, as in the example:
Code:
local input = "544.3655 * 8 + 6.455 / 2 "

local success, result = pcall(function()
    return load("return " .. input)()
end)

if success then
    print("Result: " .. result)
else
    print("Error: " .. result)
end


As always; Until we meet on a different topic, enjoy it.

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past


Last edited by AylinCE on Fri Nov 01, 2024 10:47 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
AylinCE
Grandmaster Cheater Supreme
Reputation: 34

Joined: 16 Feb 2017
Posts: 1420

PostPosted: Fri Nov 01, 2024 4:40 pm    Post subject: Reply with quote

Version: 2.0
Added: Double, Float values..

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Tutorials -> LUA Tutorials All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites