代码库> 其他> swift 计算器
swift 计算器
关注
上传者:武诚治 分类:其他(Others)
查看次数:4264 下载次数:0
上传时间:2014-06-29 大小:
//
//  ViewController.swift
//  +-*/
//
//  Created by EnderZhou on 14-6-26.
//  Copyright (c) 2014年 zptxwd@gmail.com. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    @IBOutlet var ResultLabel : UILabel
  
    var vaule1:String = ""
    var vaule2:String = ""
    var Result:String = ""
    var Resultnum:Int = 0
    var yunsuanfu:String = ""
    var xunhuan:String = ""
                          
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    @IBAction func click(sender : UIButton) {
        let value:String = sender.currentTitle
        //没有指定value为String时switch报错
        switch value {
        case "1" , "2", "3" , "4" , "5" , "6" , "7" , "8" , "9" , "0" :
            if yunsuanfu == ""{
            vaule1 += value
                ResultLabel.text = vaule1
            }else{
            vaule2 += value
                ResultLabel.text = vaule1 + yunsuanfu + vaule2
            }
          
        case "+","-","*","/":
            if vaule1 == ""{break}//当vaule1没有值的时候按运算符无效
            if Result != "" {//当想要使用之前运算所得到的结果继续计算时
                vaule1 = Result //把之前的结果移到vaule1中
                vaule2 = "";Result = "";Resultnum = 0//清空其变量
                yunsuanfu = value
                ResultLabel.text = vaule1 + yunsuanfu
            }else{
                yunsuanfu = value
                ResultLabel.text = vaule1 + yunsuanfu
            }
          
        case "=":
            if vaule1 == "" || vaule2 == "" {
                //修正了当未输入完成时直接按等号产生的错误
                break
            }else{
                jisuan()
            }
        case ".":
             break //转化为浮点型的函数还不知道,待完善
        case "C":
            ResultLabel.text = ""
            vaule1 = ""
            vaule2 = ""
            Result = ""
            Resultnum = 0
            yunsuanfu = ""
            xunhuan = ""
        default :
            break
        }
    }

    func jisuan() {
        switch yunsuanfu{
        case "+":
            Resultnum = vaule1.toInt()! + vaule2.toInt()!
            Result = "(Resultnum)"
            ResultLabel.text = vaule1 + yunsuanfu + vaule2 + "=" + Result
        case "-":
            Resultnum = vaule1.toInt()! - vaule2.toInt()!
            Result = "(Resultnum)"
            ResultLabel.text = vaule1 + yunsuanfu + vaule2 + "=" + Result
        case "*":
            Resultnum = vaule1.toInt()! * vaule2.toInt()!
            Result = "(Resultnum)"
            ResultLabel.text = vaule1 + yunsuanfu + vaule2 + "=" + Result
        case "/":
            Resultnum = vaule1.toInt()! / vaule2.toInt()!
            Result = "(Resultnum)"
            ResultLabel.text = vaule1 + yunsuanfu + vaule2 + "=" + Result
        default:
            ResultLabel.text = "Error!出错!请按C清零"
                }
    }


}
收藏
我来说两句
发表评论
您还没有登录!请登录注册
所有评论(0
下载过该代码的还下载了
本周热门下载
综合评论
提示
sina weixin mail 回到顶部