ios swift 版计算器素材及源码

关键代码:

//
// ViewController.swift
// Calculator
//
// Created by aecc on 2017/3/12.
// Copyright © 2017年 aecc. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var resulttxt: UILabel!
var fristval:Double = 0
var sencendval:Double = 0
var fhval:Int = 0
var isfinash:Bool = false

override func viewDidLoad() {
super.viewDidLoad()

}

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

@IBAction func setnum(_ sender: UIButton) {
if(resulttxt.text! != "0" && isfinash == false){
resulttxt.text! += String(sender.tag)
}else{
resulttxt.text! = String(sender.tag)
isfinash = false
}
}

@IBAction func setfh(_ sender: UIButton) {
if(fristval == 0){
fristval = (resulttxt.text! as NSString).doubleValue
}else{
onjs(sender)
}
fhval = sender.tag
isfinash = true
}


@IBAction func onjs(_ sender: Any) {
if fristval != 0 && fhval != 0 {
sencendval = (resulttxt.text! as NSString).doubleValue
switch (fhval){
case 1:
fristval += sencendval
case 2:
fristval -= sencendval
case 3:
fristval *= sencendval
case 4:
fristval /= sencendval
default:
break
}
var result:String = String(fristval)
let len = result.characters.index(of:".")
let str = "0"+result.substring(from: len!)

if ((str as NSString).doubleValue == 0)
{
result = result.substring(to: len!)
}
resulttxt.text! = result
fhval = 0
isfinash = true
}
}

@IBAction func setxs(_ sender: Any) {
if(!resulttxt.text!.contains(".") && isfinash == false){
resulttxt.text! += "."
}
}

@IBAction func setbfb(_ sender: Any) {
if resulttxt.text! != "0" {
fristval = (resulttxt.text! as NSString).doubleValue/100
resulttxt.text! = String(fristval)
isfinash = true
}
}
@IBAction func setzf(_ sender: Any) {
if resulttxt.text!.contains("-") {
resulttxt.text! = resulttxt.text!.replacingOccurrences(of: "-", with: "")
}else{
resulttxt.text! = "-"+resulttxt.text!
}
}
@IBAction func reset(_ sender: Any) {
resulttxt.text = "0"
fristval = 0
sencendval = 0
fhval = 0
}
}



效果图:

[img]http://dl2.iteye.com/upload/attachment/0123/6760/7a473873-edfd-3b1a-bdf7-808bf6847ae8.png[/img]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值