The last few things tweaked in the calculator are mostly related to the logic of the calculator. Â It is very, very streamlined compared to where I started. Â It was a classic case of I spent about 10 hours to REDUCE the number of lines of code. Â This is where the state diagram was worth its weight in gold.
Other than that, there was some minor tweaks to show only 2 decimal points. Â There’s still some bugs, but it functions well overall. Â The main known bug is if you press operations consecutively, it will perform the operation on the displayed number. Â For example, if there’s a 2 on the screen (from a previous calculation), and you press +, it will do 2+2 and display 4. Â If you press + again, it will do 4+4 and display 8.
Here’s my final presentation for the coursework I did for the past few months.
Final Presentation Pecha Kucha (20 slides, 20 seconds per slide)
Here is the final project files:
Swift Calculator Final (zip)
And here’s the code for the calculator for viewcontroller.swift (in the zipped project, but included for readability)
// // ViewController.swift // Calculator // // Created by Jin S. An on 10/1/14. // Copyright (c) 2014 jinsungpsu.com. All rights reserved. // import UIKit import Foundation class ViewController: UIViewController { @IBOutlet var calcDisplay1: UITextField! @IBOutlet var calcDisplayOp: UITextField! var clearDisplay:Bool = false var firstOp:Bool = true var num1:Float = 0.1 var num2:Float = 0.0 var numCalc:Float = 0.0 var prevOp:String = "" @IBOutlet var btn01: UIButton! @IBOutlet var btn02: UIButton! @IBOutlet var btn03: UIButton! @IBOutlet var btn04: UIButton! @IBOutlet var btn05: UIButton! @IBOutlet var btn06: UIButton! @IBOutlet var btn07: UIButton! @IBOutlet var btn08: UIButton! @IBOutlet var btn09: UIButton! @IBOutlet var btnDecimal: UIButton! @IBOutlet var btn00: UIButton! @IBOutlet var btnEqual: UIButton! @IBOutlet var btnClear: UIButton! @IBOutlet var btnPlus: UIButton! @IBOutlet var btnMinus: UIButton! @IBOutlet var btnMultiply: UIButton! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. btn01.showsTouchWhenHighlighted = true; btn02.showsTouchWhenHighlighted = true; btn03.showsTouchWhenHighlighted = true; btn04.showsTouchWhenHighlighted = true; btn05.showsTouchWhenHighlighted = true; btn06.showsTouchWhenHighlighted = true; btn07.showsTouchWhenHighlighted = true; btn08.showsTouchWhenHighlighted = true; btn09.showsTouchWhenHighlighted = true; btn00.showsTouchWhenHighlighted = true; btnDecimal.showsTouchWhenHighlighted = true; btnEqual.showsTouchWhenHighlighted = true; btnClear.showsTouchWhenHighlighted = true; btnPlus.showsTouchWhenHighlighted = true; btnMinus.showsTouchWhenHighlighted = true; btnMultiply.showsTouchWhenHighlighted = true; self.view.backgroundColor = UIColor.lightGrayColor() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func digitPressed(buttonPressed: String) { if (calcDisplayOp.text != "") { firstOp = false } if (calcDisplayOp.text == "=") { firstOp = true clearDisplay = true } if (clearDisplay == true){ calcDisplay1.text = "" clearDisplay = false } calcDisplay1.text = (calcDisplay1.text as NSString)+buttonPressed } func calculateValue(op: String, n1: Float, n2: Float) -> Float{ var calculatedValue:Float = 0.0 switch (op) { case "+": calculatedValue = n1 + n2 case "-": calculatedValue = n1 - n2 case "x": calculatedValue = n1 * n2 default: calculatedValue = -9999.9999 } return calculatedValue } func operatorPressed(buttonPressed: String) { calcDisplayOp.text = buttonPressed if (prevOp == "="){ firstOp = true } switch(buttonPressed) { case "+", "-", "x", "=": if (firstOp == true) { // this is the first operation... don't present a calculated value until 2nd input from user num1 = (calcDisplay1.text as NSString).floatValue } else { // and operator was pressed and there is already a value present in num1 num2 = (calcDisplay1.text as NSString).floatValue numCalc = calculateValue(prevOp, n1: num1, n2: num2) calcDisplay1.text = NSString(format: "%.2f", numCalc) num1=numCalc } default: calcDisplay1.text="?" } prevOp = buttonPressed clearDisplay = true } @IBAction func calcEqual(sender: AnyObject) { operatorPressed("=") } @IBAction func calc01(sender: AnyObject) { digitPressed("1") } @IBAction func calc02(sender: AnyObject) { digitPressed("2") } @IBAction func calc03(sender: AnyObject) { digitPressed("3") } @IBAction func calc04(sender: AnyObject) { digitPressed("4") } @IBAction func calc05(sender: AnyObject) { digitPressed("5") } @IBAction func calc06(sender: AnyObject) { digitPressed("6") } @IBAction func calc07(sender: AnyObject) { digitPressed("7") } @IBAction func calc08(sender: AnyObject) { digitPressed("8") } @IBAction func calc09(sender: AnyObject) { digitPressed("9") } @IBAction func calcDecimal(sender: AnyObject) { digitPressed(".") } @IBAction func calc00(sender: AnyObject) { digitPressed("0") } @IBAction func calcPlus(sender: AnyObject) { operatorPressed("+") } @IBAction func calcMinus(sender: AnyObject) { operatorPressed("-") } @IBAction func calcMultiply(sender: AnyObject) { operatorPressed("x") } @IBAction func calcClear(sender: AnyObject) { calcDisplayOp.text = "" calcDisplay1.text = "" calcDisplayOp.text = "" num1 = 1 num2 = 0 numCalc = 0 firstOp = true prevOp = "" clearDisplay = false } }