nickname912

Project Math - Shell Calculator In Development

May 23rd, 2015
263
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Project Math
  2. --Variable
  3. w,h = term.getSize()
  4. running = true
  5. version = "1.0a"
  6.  
  7. --Functions:
  8. --Read/Write APIs
  9. local function comRead()
  10.     x,y = term.getCursorPos()
  11.    term.setCursorPos(1,y)
  12.    print(">")
  13.    term.setCursorPos(3,y)
  14.    command = read()
  15.    return command
  16. end
  17.  
  18. local function printCentered(str, ypos)
  19.   term.setCursorPos(w/2 - #str/2, ypos)
  20.   term.write(str)
  21. end
  22.  
  23. --String API
  24. function split(pString, pPattern)
  25.    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  26.    local fpat = "(.-)" .. pPattern
  27.    local last_end = 1
  28.    local s, e, cap = pString:find(fpat, 1)
  29.    while s do
  30.           if s ~= 1 or cap ~= "" then
  31.          table.insert(Table,cap)
  32.           end
  33.           last_end = e+1
  34.           s, e, cap = pString:find(fpat, last_end)
  35.    end
  36.    if last_end <= #pString then
  37.           cap = pString:sub(last_end)
  38.           table.insert(Table, cap)
  39.    end
  40.    return Table
  41. end
  42.  
  43. --Graphical Functions:
  44.  
  45. --Simple header
  46. function drawHeader()
  47.   printCentered("Project Math Alpha 1.0", 1)
  48.   printCentered(string.rep("-", w), 2)
  49. end
  50.  
  51. --Calculator Main Functions:
  52.  
  53. --Splits string, ex: 5+5 -> 5 , 5 , +
  54. function SplitStrings(com)
  55.     local splitPattern = nil
  56.     local mult = string.find(com,"*")
  57.     local div = string.find(com,"/")
  58.     local sub = string.find(com,"-")
  59.     local add = string.find(com,"+")
  60.    
  61.     if mult ~= nil then
  62.         splitPattern = "*"
  63.     elseif div ~= nil then
  64.         splitPattern = "/"
  65.     elseif sub ~= nil then
  66.         splitPattern = "-"
  67.     elseif add ~= nil then
  68.         splitPattern = "+"
  69.     end
  70.        
  71.     splitS = split(command,splitPattern)
  72.  
  73.     math = {
  74.         ["num1"] = splitS[1],
  75.         ["num2"] = splitS[2],
  76.         ["sign"] = splitPattern
  77.     }
  78.    
  79. end
  80.  
  81. --doing the math, ex 5+5 = 10
  82. function Calculation()
  83.     local awnser = 0
  84.    
  85.     if math["sign"] == "*" then
  86.         awnser = tonumber(math["num1"]) * tonumber(math["num2"])
  87.     elseif math["sign"] == "/" then
  88.         awnser = tonumber(math["num1"]) / tonumber(math["num2"])
  89.     elseif math["sign"] == "-" then
  90.         awnser = tonumber(math["num1"]) - tonumber(math["num2"])
  91.     elseif math["sign"] == "+" then
  92.         awnser = tonumber(math["num1"]) + tonumber(math["num2"])
  93.     end
  94.     return awnser
  95.     end
  96.  
  97. --the shell / gui
  98. function Shell()
  99.     drawHeader()
  100.     print("Input your numbers or commands")
  101.     while running == true do
  102.         command = comRead()
  103.         if command == "exit" then
  104.             return
  105.         else
  106.             SplitStrings(command)
  107.             awnser = Calculation()
  108.             print("Answer = " .. awnser)
  109.         end
  110.     end
  111. end
  112. --Runs the program
  113. term.clear()
  114. Shell()
  115.  
  116. --Exit Screen
  117. term.clear()
  118. term.setCursorPos(1,1)
  119. print("Thanks for using Project Math Alpha 1.0")
  120. print("by nickname912")
RAW Paste Data