Guest User

Untitled

a guest
Oct 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.22 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController {
  4.  
  5. @IBOutlet var buttonResponseLabel: UILabel!
  6. var userIsCurrentlyTyping: Bool = false
  7. var storedValue: Int = 0;
  8. var currentOperation = 0
  9. var currentNumberInLabel = ""
  10.  
  11. @IBAction func buttonAppend(_ sender: UIButton)
  12. {
  13. let buttonTitle = sender.currentTitle!
  14.  
  15.  
  16. switch buttonTitle
  17. {
  18. case "+":
  19. currentOperation = 1
  20. storedValue = Int(currentNumberInLabel)!
  21. buttonResponseLabel.text = ""
  22. case "-":
  23. currentOperation = 2
  24. storedValue = Int(currentNumberInLabel)!
  25. buttonResponseLabel.text = ""
  26. case "*":
  27. currentOperation = 3
  28. storedValue = Int(currentNumberInLabel)!
  29. buttonResponseLabel.text = ""
  30. case "/":
  31. currentOperation = 4
  32. storedValue = Int(currentNumberInLabel)!
  33. buttonResponseLabel.text = ""
  34. default:
  35.  
  36.  
  37. if buttonTitle == "="
  38. {
  39. var finalValue = 0
  40. print(currentNumberInLabel)
  41. let secondStoredValue = Int(currentNumberInLabel)!
  42. if currentOperation == 1
  43. {
  44. finalValue = storedValue + secondStoredValue
  45. buttonResponseLabel.text = ""
  46. buttonResponseLabel.text = String(finalValue)
  47. }
  48. else if currentOperation == 2
  49. {
  50. finalValue = storedValue - secondStoredValue
  51. buttonResponseLabel.text = ""
  52. buttonResponseLabel.text = String(finalValue)
  53. }
  54. else if currentOperation == 3
  55. {
  56. finalValue = storedValue * secondStoredValue
  57. buttonResponseLabel.text = ""
  58. buttonResponseLabel.text = String(finalValue)
  59. }
  60. else if currentOperation == 4
  61. {
  62. finalValue = storedValue / secondStoredValue
  63. buttonResponseLabel.text = ""
  64. buttonResponseLabel.text = String(finalValue)
  65. }
  66. }
  67. else
  68. {
  69. if userIsCurrentlyTyping
  70. {
  71. currentNumberInLabel = buttonResponseLabel.text! + buttonTitle
  72. buttonResponseLabel.text = currentNumberInLabel
  73. }
  74. else
  75. {
  76. userIsCurrentlyTyping = true
  77. buttonResponseLabel.text = ""
  78. buttonResponseLabel.text = buttonTitle
  79. }
  80.  
  81. }
  82.  
  83. print("(currentOperation) (buttonTitle)")
  84. }
  85.  
  86. }
  87.  
  88. }
  89.  
  90. import UIKit
  91.  
  92. class ViewController: UIViewController {
  93.  
  94. struct Operator {
  95. var stringRepresentation: String
  96. var `operator`: (Int, Int) -> Int //quotes required as operator is a keyword
  97. }
  98.  
  99. var operators = ["+":Operator(stringRepresentation:"+", operator:+),
  100. "-":Operator(stringRepresentation:"-", operator:-),
  101. "*":Operator(stringRepresentation:"*", operator:*),
  102. "/":Operator(stringRepresentation:"/", operator:/)]
  103. @IBOutlet var buttonResponseLabel: UILabel!
  104. var userIsCurrentlyTyping: Bool = false
  105. var storedValue: Int = 0;
  106. var currentOperation: Operator?
  107. var currentNumberInLabel = ""
  108. var buttonTitle = ""
  109.  
  110. @IBAction func buttonAppend(_ sender: UIButton) {
  111. buttonTitle = sender.currentTitle!
  112.  
  113. if let chosenOperator = operators[buttonTitle] {
  114. currentOperation = chosenOperator
  115. storedValue = Int(currentNumberInLabel)!
  116. buttonResponseLabel.text = ""
  117. } else if buttonTitle == "=" {
  118. showResult()
  119. } else if userIsCurrentlyTyping {
  120. currentNumberInLabel = buttonResponseLabel.text! + buttonTitle
  121. buttonResponseLabel.text = currentNumberInLabel
  122. } else {
  123. userIsCurrentlyTyping = true
  124. buttonResponseLabel.text = buttonTitle
  125. }
  126.  
  127. print("(currentOperation) (buttonTitle)")
  128. }
  129.  
  130. func showResult() {
  131. print(currentNumberInLabel)
  132. guard let currentOperator = currentOperation else {
  133. print("This should never happen. Fail silently")
  134. return
  135. }
  136. operate(withOperator: currentOperator.operator)
  137. }
  138.  
  139. func operate(withOperator op: (Int, Int) -> Int){
  140. let secondStoredValue = Int(currentNumberInLabel)!
  141. let finalValue = op(storedValue, secondStoredValue)
  142. buttonResponseLabel.text = String(finalValue)
  143. }
  144. }
  145.  
  146. enum CalculatorOperation {
  147.  
  148. case add
  149. case subtract
  150. case multiply
  151. case divide
  152.  
  153. init?(from buttonTitle: String) {
  154. switch buttonTitle {
  155. case "+": self = .add
  156. case "-": self = .subtract
  157. case "*": self = .multiply
  158. case "/": self = .divide
  159. default: return nil
  160. }
  161. }
  162.  
  163. func apply(to left: Int, and right: Int) -> Int {
  164.  
  165. switch self {
  166. case .add:
  167. return left + right
  168.  
  169. case .subtract:
  170. return left - right
  171.  
  172. case .multiply:
  173. return left * right
  174.  
  175. case .divide:
  176. return left / right
  177. }
  178. }
  179. }
  180.  
  181. class ViewController: UIViewController {
  182.  
  183. @IBOutlet var inputLabel: UILabel!
  184.  
  185. var firstNumber: Int = 0
  186. var currentOperation: CalculatorOperation? = nil
  187.  
  188. // connect 1,2,3,4,5,6,7,8,9,0 buttons to this action
  189. @IBAction func updateNumber(_ numberButton: UIButton) {
  190.  
  191. // update the input label with whatever number has been pressed
  192. let numberTitle = numberButton.currentTitle!
  193. inputLabel.text = inputLabel.text! + numberTitle
  194. }
  195.  
  196. // connect '+', '-', '*', '/' buttons to this action
  197. @IBAction func updateOperation(_ operationButton: UIButton) {
  198.  
  199. // update the operation based on the key pressed
  200. let operationTitle = operationButton.currentTitle!
  201. currentOperation = CalculatorOperation(from: operationTitle)
  202.  
  203. // save the number shown in the input as the first number
  204. let currentinput = inputLabel.text!
  205. firstNumber = Int(currentinput)!
  206.  
  207. // clear out the input label so it's ready for new number to be entered
  208. inputLabel.text = ""
  209. }
  210.  
  211. // connect '=' button to this action
  212. @IBAction func performCalculation(_ sender: UIButton) {
  213.  
  214. // get the second number from whatever is currently in the input label
  215. let currentinput = inputLabel.text!
  216. let secondNumber = Int(currentinput)!
  217.  
  218. let finalValue = currentOperation!.apply(to: firstNumber, and: secondNumber)
  219.  
  220. // update input label with the calculated value
  221. inputLabel.text = String(finalValue)
  222. }
  223. }
  224.  
  225. enter code here import UIKit
  226.  
  227. override func viewDidLoad() {
  228. super.viewDidLoad()
  229.  
  230. }
  231.  
  232. override func didReceiveMemoryWarning() {
  233. super.didReceiveMemoryWarning()
  234.  
  235. }
  236.  
  237. @IBOutlet weak var current: UITextField!
  238.  
  239. @IBOutlet weak var precentExamWorth: UITextField!
  240. @IBOutlet weak var desired: UITextField!
  241.  
  242. {
  243. var gradeNeeded = 0
  244. var precentExamWorth = 0.15
  245. var desired = 0
  246. var current = 0
  247.  
  248.  
  249.  
  250.  
  251. var x = 0 - (0.85 * 0) / 0.15
Add Comment
Please, Sign In to add comment