w22116972
11/14/2014 - 9:55 AM

calculator1

//
//  ViewController.swift
//  hw2-swift
//
//  Created by Edward on 11/2/14.
//  Copyright (c) 2014 Edward. All rights reserved.
//

import UIKit
//import Foundation

class ViewController: UIViewController {
    
    //var num:Int = 0
    
    var isNum :Bool = false
    var num1 :Int!
    var num2 :Int!
    var op :String = ""
    
    @IBAction func equal(sender: AnyObject) {
        isNum = false
        var result = 0
        num2 = ans.text?.toInt()!
        if op == "+"{
            result = num1 + num2
        }
        else if op == "-"{
            result = num1 - num2
        }
        else if op == "*"{
            result = num1 * num2
        }
        else if op == "/"{
            result = num1 / num2
        }
        ans.text = "\(result)"
        
    }
    @IBOutlet weak var ans: UITextField!
    
    @IBAction func digit(sender: AnyObject) {
        var num = sender.currentTitle
        
        if isNum {
            ans.text = ans.text! + num!!
        }
        else{
            ans.text = num
            isNum = true
        }
    }
    
    @IBAction func ac(sender: AnyObject) {
        ans.text = "0"
        num1 = 0
        num2 = 0
        op = ""
    }
    
    @IBAction func c(sender: AnyObject) {
        let len = countElements(ans.text)
        let index = len - 1
        ans.text = ans.text.substringToIndex(advance(ans.text.startIndex,index))
    }
    
    @IBAction func operation(sender: AnyObject) {
        isNum = false
        num1 = ans.text?.toInt()!
        op = sender.currentTitle!!
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        num1 = 0
        num2 = 0
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}