Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Advanced Calculator In Lua

calculator

  • Please log in to reply
1 reply to this topic

#1 Static

Static

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts
  • Programming Language:C, C++, C#, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, Visual Basic .NET, Lua, ActionScript, Haskell, VBScript
  • Learning:C, Bash, Others

Posted 04 May 2012 - 03:45 PM

Lua can be fun, actually.
I just started yesterday...
-- By Static --


function add(f, l)
print ("Your answer is " , f + l ,"\n")
end
function sub(f, l)
print ("Your answer is " , f - l , "\n")
end
function multi(f, l)
print ("Your answer is " , f * l , "\n")
end
function div(f, l)
print ("Your answer is " , f / l , "\n")
end
function sqr(f)
print ("Your answer is " , f * f , "\n")
end
function root(f)
print ("Your answer is " , math.sqrt(f) , "\n")
end
--

-- My cool introduction --
io.write ("##################################\n")
io.write ("##################################\n")
io.write (" Welcome to advanced Calculator \n")
io.write (" Developed by Static \n")
io.write ("##################################\n")
io.write ("##################################\n")
io.write ("\n")
io.write ("\nThe commands are : ")
io.write ("add, ")
io.write ("sub, ")
io.write ("multi, ")
io.write ("div, ")
io.write ("sqr, ")
io.write ("root\n")
io.write ("\n")
--
-- User input --
io.write ("\nType your first number => ")
f = io.read()
io.write("\nType your command => ")
c = io.read()
io.write ("\nType in your last number => ")
l = io.read()
-- Real stuffs [img]http://rile5.com/public/style_emoticons/default/tongue.png[/img] --
if c=='add' then
add(f, l)
elseif c=='sub' then
sub(f, l)
elseif c=='multi' then
multi(f, l)
elseif c=='div' then
div(f, l)
elseif c=='sqr' then
sqr(f)
elseif c=='root' then
root(f)
else
io.write ("\nYou have entered an invalid command!")

  • 0

#2 shill

shill

    CC Newcomer

  • Member
  • PipPip
  • 13 posts

Posted 09 May 2012 - 07:00 PM

Good work, but you've got a lot of duplicated strings in those functions. Ideally, you should return a value from the functions instead of printing within them, and have another function that prints whatever value you've calculated.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download