Created
April 22, 2019 06:06
-
-
Save donguri9/a598749fffefe25ef8f40ed2762494c6 to your computer and use it in GitHub Desktop.
MVCtest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct CaluculatorBrain { | |
private var accumulator:Double? | |
mutating func performOperation(_ symbol:String){ | |
switch symbol { | |
case "π": | |
accumulator = Double.pi | |
case "√": | |
if let operand = accumulator{ | |
accumulator = sqrt(operand) | |
} | |
default: | |
break | |
} | |
} | |
mutating func setOperand(_ operand:Double){ | |
accumulator = operand | |
} | |
var result:Double?{ | |
get{ | |
return accumulator | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var displayValue:Double{ | |
get { | |
return Double(display.text!)! | |
} | |
set { | |
display.text = String(newValue) | |
} | |
} | |
private var brain = CaluculatorBrain() | |
@IBAction func performOperation(_ sender: UIButton) { | |
if userIsMiddleOfTyping{ | |
brain.setOperand(displayValue) | |
userIsMiddleOfTyping = false | |
} | |
if let mathematicalSymbol = sender.currentTitle{ | |
brain.performOperation(mathematicalSymbol) | |
} | |
if let result = brain.result{ | |
displayValue = result | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment