Advertisement
PaymentOption

PaymentCalc

Apr 8th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. local firstInput = 0
  2. local secondInput = 0
  3. local operationInput = ""
  4. local screenWidth, screenHeight = term.getSize()
  5. local username = ""
  6. local selection = 1
  7. local answer = 0
  8.  
  9. if os.getComputerLabel() == nil then username = "New User"
  10. else username = os.getComputerLabel() end
  11.  
  12. local function cPrint(height, string)
  13.     xPos = screenWidth/2 - string.len(string)/2
  14.     term.setCursorPos(xPos, height)
  15.     term.write(string)
  16. end
  17.  
  18. local function rPrint(height, string)
  19.     xPos = screenWidht - string.len(string)
  20.     term.setCursorPos(xPos, height)
  21.     term.write(string)
  22. end
  23.  
  24. function getInput()
  25.     cPrint(6, "First number: ")
  26.     firstInput = read()
  27.    
  28.     cPrint(7, "Operation: ")
  29.     operationInput = read()
  30.    
  31.     cPrint(8, "Second number: ")
  32.     secondInput = read()
  33. end
  34.  
  35. function printWelcome()
  36.     cPrint("Welcome ".. username)
  37.     sleep(2)
  38.     term.clear()
  39.     term.setCursorPos(1,1)
  40. end
  41.  
  42. function printMenu()
  43.     if selection == 1 then cPrint(6, "[ Calculate ]")
  44.     else                   cPrint(6, "  Calculate  ") end
  45.    
  46.     if selection == 2 then cPrint(7, "[ Exit ]")
  47.     else                   cPrint(7, "  Exit  ") end
  48. end
  49.  
  50. function determineOperation(operationInput, answer)
  51.     local operations = {'+', '-', '*', '/', '%'}
  52.     local operation = 1
  53.    
  54.     operationInput = tostring(operationInput)
  55.    
  56.     if operationInput == operations[1] then operation = 1
  57.     elseif operationInput == operations[2] then operation = 2
  58.     elseif operationInput == operations[3] then operation = 3
  59.     elseif operationInput == operations[4] then operation = 4
  60.     elseif operationInput == operations[5] then operation = 5
  61.     else operation = 0
  62.     end
  63.    
  64.     if operation == 0 then answer = nil
  65.     elseif operation == 1 then answer = firstInput+secondInput
  66.     elseif operation == 2 then answer = firstInput-secondInput
  67.     elseif operation == 3 then answer = firstInput*secondInput
  68.     elseif operation == 4 then answer = firstInput/secondInput
  69.     elseif operation == 5 then answer = firstInput%secondInput
  70.     end
  71. end
  72.  
  73. printWelcome()
  74. while true do
  75.     answer = 0
  76.     term.clear()
  77.     term.setCursorPos(1,1)
  78.    
  79.     printMenu()
  80.         event, key = os.pullEvent("key")
  81.        
  82.         if key == 200 and selection > 1 then selection = selection - 1
  83.         elseif key == 208 and selection < 2 then selection = selection+1
  84.         end
  85.        
  86.         if selection == 1 and key == 28 then
  87.             term.clear()
  88.             term.setCursorPos(1,1)
  89.             getInput()
  90.             if answer ==  nil then
  91.                 cPrint(10, "Not a valid operation.")
  92.                 sleep(2)
  93.             else
  94.                 cPrint(10, "The answer is " ..answer)
  95.                 sleep(2)
  96.             end
  97.         elseif selection == 2 and key == 28 then term.clear(); term.setCursorPos(1,1); break
  98.         end
  99.     -- End menu operations
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement