Skip to content

Instantly share code, notes, and snippets.

@donguri9
Created April 22, 2019 06:06
Show Gist options
  • Save donguri9/a598749fffefe25ef8f40ed2762494c6 to your computer and use it in GitHub Desktop.
Save donguri9/a598749fffefe25ef8f40ed2762494c6 to your computer and use it in GitHub Desktop.
MVCtest
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
}
}
}
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