Advertisement
PaymentOption

Calculator

May 20th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.73 KB | None | 0 0
  1. -- ShadOS Calculator by PaymentOption --
  2. VERSION = "Alpha 1.0"
  3. ----------------------------------------
  4.  
  5. -- VARS --
  6. sExpression = ""
  7. nAnswer = nil
  8. screenWidth, screenHeight = term.getSize()
  9. selection = 1
  10. ----------
  11.  
  12. -- Helper Functions --
  13. function cPrint(height, string)
  14.     local xPos = screenWidth/2 - string.len(string)/2
  15.     term.setCursorPos(xPos, height); term.write(string)
  16. end
  17.  
  18. function rPrint(height, string)
  19.     local xPos = screenWidth - string.len(string)
  20.     term.setCursorPos(xPos, height); term.write(string)
  21. end
  22.  
  23. function clear() term.clear(); term.setCursorPos(1,1) end
  24.  
  25. function split(str, pat)
  26.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  27.    local fpat = "(.-)" .. pat
  28.    local last_end = 1
  29.    local s, e, cap = str:find(fpat, 1)
  30.    while s do
  31.       if s ~= 1 or cap ~= "" then
  32.      table.insert(t,cap)
  33.       end
  34.       last_end = e+1
  35.       s, e, cap = str:find(fpat, last_end)
  36.    end
  37.    if last_end <= #str then
  38.       cap = str:sub(last_end)
  39.       table.insert(t, cap)
  40.    end
  41.    return t
  42. end
  43. ----------------------
  44.  
  45. -- Application related functions --
  46. function printLogo()
  47.     term.setCursorPos(2, 2)
  48.     print("   -_-/  ,,           |\\     ,-||-,     -_-/  ")
  49.     print("  (_ /   ||      _     \\\\   ('|||  )   (_ /   ")
  50.     print(" (_ --_  ||/\\\\  < \\,  / \\\\ (( |||--)) (_ --_  ")
  51.     print("   --_ ) || ||  /-|| || || (( |||--))   --_ ) ")
  52.     print("  _/  )) || || (( || || ||  ( / |  )   _/  )) ")
  53.     print(" (_-_-   \\\\ |/  \\/\\\\  \\\\/    -____-   (_-_-   ")
  54.     print("           _/                                 ")
  55.  
  56.     print("           ,- _~.       ,,      ")
  57.     print("          (' /|     _   ||      ")
  58.     print("         ((  ||    < \\, ||  _-_ ")
  59.     print("         ((  ||    /-|| || ||   ")
  60.     print("          ( / |   (( || || ||   ")
  61.     print("           -____-  \\/\\\\ \\\\ \\\\,/ ")
  62. end
  63.  
  64. function printBorder() --Print Logo then border!
  65.     local w,h = term.getSize()
  66.     write(" ".. string.rep('*', w-2).."\n")
  67.     for i=1, h-2 do write(" "..'*'..string.rep(" ", w-4)..'*'.."\n") end
  68.     write(" ".. string.rep('*', w-2))
  69.    
  70.     cPrint(2, "ShadOS Calculator")
  71.     rPrint(17, "Version: "..VERSION.."*")
  72.    
  73.     if bGetInput == true then cPrint(5, "Format: Num1 OPERATOR Num2") end
  74. end
  75.  
  76.  function getInput()
  77.     local nFirstNum = 0;
  78.     local nSecondNum = 0;
  79.     local cOperator = '';
  80.     local tExpression = {}
  81.     local sTempExpression = "" -- This will be used to grab the expression via read()
  82.  
  83.     bGetInput = true
  84.     while true do
  85.         clear()
  86.         printBorder()
  87.         term.setCursorPos(3,17); term.write("Write 'exit' to exit")
  88.         if nAnswer ~= nil then term.setCursorPos(3, 3); term.write("ans: "..nAnswer..string.rep(" ", screenWidth-string.len("ans: "..nAnswer.."    "))) end
  89.        
  90.         term.write("*"..string.rep(" ", screenWidth-4).."*")
  91.         term.setCursorPos(5, 7); term.write("Expression: ")
  92.         sTempExpression = tostring(read())
  93.        
  94.         if sTempExpression == "exit" then bGetInput = false; break
  95.         else
  96.             tExpression = split(sTempExpression, " ")
  97.            
  98.             if tExpression[1] == "ans"then
  99.                 if nAnswer ~= nil then tExpression[1] = nAnswer
  100.                 else cPrint(8, "Error: No value held by prior answer"); sleep(1.3) end
  101.             elseif tExpression[2] == "ans" then
  102.                 if nAnswer ~= nil then tExpression[1] = nAnswer
  103.                 else cPrint(8, "Error: No value held by prior answer"); sleep(1.3) end
  104.             end
  105.                 nFirstNum = tExpression[1]
  106.                 cOperator = tExpression[2]
  107.                 nSecondNum = tExpression[3]
  108.                
  109.                 -- Long ass operator checking.... --
  110.                 if cOperator == '+' then nAnswer = nFirstNum+nSecondNum; cPrint(8, "Answer: "..nAnswer); sleep(1.3)
  111.                 elseif cOpeartor == '-' then nAnswer = nFirstNum-nSecondNum; cPrint(8, "Answer: "..nAnswer); sleep(1.3)
  112.                 elseif cOperator == '*' then nAnswer = nFirstNum*nSecondNum; cPrint(8, "Answer: "..nAnswer); sleep(1.3)
  113.                 elseif cOperator == '/' then
  114.                     if nSecondNum == 0 then cPrint(8, "Cannot divide by 0"); sleep(1.3)
  115.                     else nAnswer = nFirstNum/nSecondNum; cPrint(8, "Answer: "..nAnswer); sleep(1.3) end
  116.                 elseif cOperator == '%' then nAnswer = nFirstNum%nSecondNum; cPrint(8, "Answer: "..nAnswer); sleep(1.3)
  117.                 else cPrint(8, "Invalid expression: Operator unknown."); sleep(1.3) end
  118.         end
  119.     end
  120. end
  121.  
  122. function printMenu()
  123.     if selection == 1 then cPrint(6, "[ Custom Expression ]")
  124.     else                   cPrint(6, "  Custom Expression  ") end
  125.    
  126.     if selection == 2 then cPrint(7, "[ Exit ]")
  127.     else                   cPrint(7, "  Exit  ") end
  128. end
  129. -----------------------------------
  130.  
  131. clear()
  132. printLogo()
  133. sleep(2)
  134. while true do
  135.     clear()
  136.     printBorder()
  137.     printMenu()
  138.    
  139.     event, key = os.pullEvent("key")
  140.    
  141.     if key == 200 and selection > 1 then selection = selection-1
  142.     elseif key == 208 and selection < 2 then selection = selection+1
  143.    
  144.     elseif key == 28 and selection == 1 then getInput()
  145.     elseif key == 28 and selection == 2 then clear(); break
  146.     end
  147. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement