Advertisement
Guest User

izmenjen kod

a guest
Apr 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // Calculator
  4. //
  5. // Created by Milos on 4/3/18.
  6. // Copyright © 2018 Milos. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. enum Operation:String {
  12. case ADDITION = "+"
  13. case SUBSTRACTION = "-"
  14. case DIVISION = "/"
  15. case MULTIPLICATION = "*"
  16. case NULL = "Null"
  17. }
  18.  
  19. class ViewController: UIViewController {
  20.  
  21. @IBOutlet weak var outputLbl: UILabel!
  22.  
  23. var val1 = ""
  24. var val2 = ""
  25. var runningNumber = ""
  26. var runningOpNum = ""
  27. var res = ""
  28. var currentOperation:Operation = .NULL
  29.  
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32. outputLbl.text = "0"
  33. }
  34.  
  35. override func didReceiveMemoryWarning() {
  36. super.didReceiveMemoryWarning()
  37. }
  38.  
  39. @IBAction func numberPressed(_ sender: RoundButton) {
  40. if runningNumber.count <= 8 {
  41. runningNumber += "\(sender.tag)"
  42. outputLbl.text = runningNumber
  43. }
  44. }
  45.  
  46. @IBAction func allClearPressed(_ sender: RoundButton) {
  47. val1 = ""
  48. val2 = ""
  49. runningNumber = ""
  50. res = ""
  51. currentOperation = .NULL
  52. outputLbl.text = "0"
  53. }
  54.  
  55. @IBAction func equalPressed(_ sender: RoundButton) {
  56. operation(operation: currentOperation)
  57. val1 = ""
  58. val2 = ""
  59. runningNumber = ""
  60. res = ""
  61. currentOperation = .NULL
  62. }
  63.  
  64. @IBAction func addPressed(_ sender: RoundButton) {
  65. operation(operation: .ADDITION)
  66. if outputLbl.text != "0"{
  67. outputLbl.text = "+"
  68. }
  69. }
  70.  
  71. @IBAction func substractPressed(_ sender: RoundButton) {
  72. operation(operation: .SUBSTRACTION)
  73. if outputLbl.text != "0"{
  74. outputLbl.text = "-"
  75. }
  76. }
  77.  
  78. @IBAction func multiplyPressed(_ sender: RoundButton) {
  79. operation(operation: .MULTIPLICATION)
  80. if outputLbl.text != "0"{
  81. outputLbl.text = "*"
  82. }
  83. }
  84.  
  85. @IBAction func dividePressed(_ sender: RoundButton) {
  86. operation(operation: .DIVISION)
  87. if outputLbl.text != "0"{
  88. outputLbl.text = "/"
  89. }
  90. }
  91.  
  92. func operation(operation: Operation) {
  93. if currentOperation != .NULL {
  94. if runningNumber != "" {
  95. val2 = runningNumber
  96. runningNumber = ""
  97.  
  98. switch currentOperation {
  99. case .ADDITION:
  100. res = "\(Double(val1)! + Double(val2)!)"
  101. case .SUBSTRACTION:
  102. res = "\(Double(val1)! - Double(val2)!)"
  103. case .MULTIPLICATION:
  104. res = "\(Double(val1)! * Double(val2)!)"
  105. case .DIVISION:
  106. res = "\(Double(val1)! / Double(val2)!)"
  107. case .NULL: break
  108. }
  109. if (Double(res)!.truncatingRemainder(dividingBy: 1) == 0) {
  110. res = "\(Int(Double(res)!))"
  111. }
  112. outputLbl.text = val1 + currentOperation.rawValue + val2 + "=" + res
  113. }
  114. currentOperation = operation
  115.  
  116. }else {
  117. val1 = runningNumber
  118. runningNumber = ""
  119. currentOperation = operation
  120. }
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement