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 


how to make calculator with string like "213 + 42 - 124

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Nighthawyun
Cheater
Reputation: 0

Joined: 21 Jan 2019
Posts: 27

PostPosted: Sat May 25, 2019 9:08 pm    Post subject: how to make calculator with string like "213 + 42 - 124 Reply with quote

how?

ex)
string "899 + 6 - 33" to result "872"
and , too
Back to top
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 25

Joined: 24 Feb 2017
Posts: 733
Location: 'Merica

PostPosted: Sat May 25, 2019 9:46 pm    Post subject: Reply with quote

Code:
print(loadstring("return " .. "899 + 6 - 33")())

_________________
Back to top
View user's profile Send private message
DaSpamer
Grandmaster Cheater Supreme
Reputation: 30

Joined: 13 Sep 2011
Posts: 1511

PostPosted: Sat May 25, 2019 10:28 pm    Post subject: Reply with quote

A bit safer way to accomplish this and more dynamic.
Code:
local action = {['+']= function(a,b)return a + b;end;
            ['-']= function(a,b)return a - b;end;
            ['*']= function(a,b)return a * b;end;
            ['/']= function(a,b)return a / b;end;
            ['//']=function(a,b)return a// b;end; --> math.floor equivalent
            ['%']= function(a,b)return a % b;end;
            ['^']= function(a,b)return a ^ b;end;
      };
debug.setmetatable(0, {
   __call = function(a, op)
      if(action[op]) then
         return function(b)
            return action[op](a,b)
         end
      else
         error("Wrong syntax", 2)
      end
   end
})
function calculate(Param1,Op,Param2,...)
   if (tonumber(Param1) and action[Op] and tonumber(Param2)) then
      return calculate(((Param1) (Op) (Param2)),...);
   end
   return Param1;
end
local a = calculate(1,'+',2) --> 3
local b = calculate(3,'+',1,'*',2,'%',3) --> ((3+1) * 2 ) % 3 --> 2 Operations priority do not apply here!

_________________
HEY Hitler
Do you get lazy when making trainers?
Well no more!
My CETrainer will generate it for you in seconds, so you won't get lazy! Very Happy

http://forum.cheatengine.org/viewtopic.php?t=564919
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 33

Joined: 10 Apr 2015
Posts: 1284

PostPosted: Sun May 26, 2019 12:10 am    Post subject: Reply with quote

Here is my Calculator made using CE Lua script.
It's 95% finish. Some parts did not finish yet:

1. Radix for hex, oct, bin calculation environment
2. Date calculation / manipulating under 'Date/Time Unit Calculation Menu'
3. Offset and AOB manipulation if possible.

Plan to publish it if the project is done. Razz

EDIT:

Next, explanation for your question:

Code:
x = "899 + 6 - 33"
func = assert(load("return " .. x))
y = func()
print(y)  --> 872

x = "100 + 6 / 2"
func = assert(load("return " .. x))
y = func()
print(y)  --> 103  because 6/2 doing first then + 100

x = "(100 + 6) / 2"
func = assert(load("return " .. x))
y = func()
print(y)  --> 53  because (100+6) / 2

x = "100 + 6 * 2 - 2 / 2"  --- potensial confusion
func = assert(load("return " .. x))
y = func()
print(y)  --> 111

note = [[
Remember math logic rules for math operator:
highest is PARENTHESES / BRACKET
then is EXPONENT / SQUARE ROOT
next is MULTIPLICATION / DIVISION
lowest is ADDITION / SUBTRACTION

So, the correct answer for "100 + 6 * 2 - 2 / 2" is
level 1 = 6 x 2 = 12
level 2 = 2 / 2 = 1
level 3 = 100
result = 100 + 12 - 1 = 111
]]



CRDRLuaCalc.JPG
 Description:
Corroder CE Lua Calculator
 Filesize:  42.87 KB
 Viewed:  1035 Time(s)

CRDRLuaCalc.JPG



_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting 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 can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites