最近刚学Swift,所以就做了个计算器

1.我在做这个计算器的时候用了好多方法。我就把我觉得最的最好的给大家分享一下:

ViewController.swift

//
//  ViewController.swift
//      
//
//  Created by 张凯超 on 15/11/4.
//  Copyright © 2015年 张凯超. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    var logic = CalcuLogic()
    @IBOutlet weak var mainLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func operandPressed(sender: AnyObject) {
        mainLabel.text = logic.calculateByTag(sender.tag,withMainLabelString:mainLabel.text!)
        
        
        
    }
    @IBAction func numberButtonPressed(sender: AnyObject) {
        
        mainLabel.text = self.logic.updateMainLabelStringByNumberTag(sender.tag, withMainLabelString: mainLabel.text!)

      //chenh  print(sender.tag!)
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func decimalPressed(sender: AnyObject) {
        
        if logic.doesStringContainDecimal(mainLabel.text!) == false{
            let string = mainLabel.text! + "."
            mainLabel.text = string
            
        }
    }
      @IBAction func clearPressed(sender: AnyObject) {
        
        mainLabel.text = "0"
        logic.clean()
    }

    @IBAction func equalsPressed(sender: AnyObject) {
     
        mainLabel.text = logic.calculateByTag(sender.tag,withMainLabelString:mainLabel.text!)
        
    }
}

 

CalcuLogic.swift

//
//  CalcuLogic.swift
//  
//
//  Created by 张凯超 on 15/11/4.
//  Copyright © 2015年 张凯超. All rights reserved.
//

import Foundation


enum Operator:Int{
    
    case Plus = 200
    case Minus = 201
    case Multiply = 202
    case Devide = 203
    case Default = 0
}
class CalcuLogic {
    var lastRetainValue:Double
    var operatorV:Operator
    var isMainLabelTextTemporary:Bool
    
    init(){
        self.lastRetainValue = 0.0
        self.operatorV = Operator.Default
        isMainLabelTextTemporary = false
        
    }
    
    func doesStringContainDecimal(string:String) ->Bool{
        
        for ch in string.unicodeScalars{
            if ch == "."{
                return true
        }
         
        }
        return false
    }
    
    func clean(){
        
        lastRetainValue = 0.0
        isMainLabelTextTemporary = false
        operatorV = Operator.Default
        
    }


    
        func updateMainLabelStringByNumberTag(tag:Int,withMainLabelString mainLabelString:String ) ->String {
            var string = mainLabelString
            if isMainLabelTextTemporary {
                string = "0"
                isMainLabelTextTemporary = false
            }
            
//            let optNumber = tag - 100;
            let optNumber = tag ;
            
            let mainLabelDouble = (string as NSString).doubleValue
            if (mainLabelDouble == 0 && doesStringContainDecimal(string) == false) {
                return String(optNumber)
            }
            let resultString = string + String(optNumber)
            return resultString
        
    }
    
        func calculateByTag (tag:Int ,withMainLabelString mainLabelString:String) -> String{
        
        let currentValue = (mainLabelString as NSString).doubleValue
        switch(operatorV){
        case .Plus:
            lastRetainValue += currentValue
        case .Minus:
            lastRetainValue -= currentValue
        case .Multiply:
            lastRetainValue *= currentValue
        case .Devide:
            if currentValue != 0{
                lastRetainValue /= currentValue
                
                
            }else
            {
                operatorV = Operator.Default
                isMainLabelTextTemporary = true
                return "错误"
                
            }
            
            // 等于号  
        default:
            lastRetainValue = currentValue
            }
            operatorV = Operator(rawValue: tag)!
            let resultString = NSString(format: "%g", lastRetainValue)
            isMainLabelTextTemporary = true
            return resultString as String
}
}

 

计算器源码下载

posted @ 2015-11-10 14:12  琊戏诸侯  阅读(1205)  评论(0编辑  收藏  举报