纯代码版
没有多少注释,请谅解
//第一次修改:"+/-"按键在某种情况下无效BUG
//
// ViewController.swift
// uiTest
//
// Created by Input on 16/7/20.
// Copyright © 2016年 Input. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
let outputLabel = UILabel()
let dicIntStr = [ 11: "AC", 12: "DEL", 13: "%", 14: "/",
21: "7", 22: "8", 23: "9", 24: "*",
31: "4", 32: "5", 33: "6", 34: "-",
41: "1", 42: "2", 43: "3", 44: "+",
51: "0", 52: ".", 53: "+/-", 54: "=" ]
var strOne = "0", strTwo = "", ch = "=";
//接受运算符事件函数
func testOperation (operation str : String) -> Void {
var sum :Double = 0.0
if strTwo.isEmpty {
strTwo = strOne;
}
else if ch != "=" && !strOne.isEmpty {
switch ch {
case "+":
sum = (Double)(strTwo)! + (Double)(strOne)!
case "-":
sum = (Double)(strTwo)! - (Double)(strOne)!
case "*":
sum = (Double)(strTwo)! * (Double)(strOne)!
case "/":
sum = (Double)(strTwo)! / (Double)(strOne)!
default:
break
}
strTwo = (String)(sum)
}
if strTwo == "0.0" { strTwo = "0" }
ch = str; outputLabel.text = strTwo; strOne = "0";
}
//数字接受事件函数
func inputNumber (numberCharcter number : String ) -> Void {
if ch == "=" {
strTwo = ""
}
if !strOne.isEmpty{
if strOne[strOne.characters.startIndex] == "0" && !strOne.containsString(".") {
strOne = ""
}
}
strOne += number; outputLabel.text = strOne;
}
func idDButton() {
if ch == "=" {
strTwo = ""
}
let count = strOne.containsString(".")
if !count {
if strOne.isEmpty{
strOne += "0."
}
else{
strOne += "."
}
}
outputLabel.text = strOne
}
func id0Button() {
if ch == "=" {
strTwo = ""
}
if !strOne.isEmpty {
if strOne[strOne.characters.startIndex] == "0" {
if strOne.containsString(".") {
strOne += "0"
}
}
else {
strOne += "0"
}
}
else {
strOne += "0"
}
outputLabel.text = strOne;
}
//清除缓存,重新开始计算
func idACButton() {
strOne = "0"; strTwo = ""; ch = "="
outputLabel.text = strOne;
}
//取反
func idXFButton() {
if !strTwo.isEmpty && ch == "="{
if strTwo != "0" {
if strTwo[strTwo.characters.startIndex] == "-"{
strTwo.removeAtIndex(strTwo.characters.startIndex)
}
else {
strTwo.insert("-", atIndex: strTwo.characters.startIndex)
}
outputLabel.text = strTwo
}
}
else{
if !strOne.isEmpty && (strOne != "0" && strOne != "0."){
if strOne[strOne.characters.startIndex] == "-"{
strOne.removeAtIndex(strOne.characters.startIndex)
}
else {
strOne.insert("-", atIndex: strOne.characters.startIndex)
}
outputLabel.text = strOne
}
}
}
//取百分数
func idQYButton() {
//调用计算函数:
if strTwo.isEmpty{
strTwo = strOne
}
else{
testOperation(operation: "=")
}
ch = "/"; strOne = "100"
testOperation(operation: "=")
}
//Del事件
func idDelButton(){
if !strOne.isEmpty && strOne != "0" {
strOne = strOne.substringToIndex(strOne.endIndex.predecessor())
if strOne.isEmpty { strOne = "0" }
}
if strOne == "-" {strOne = "0"}
outputLabel.text = strOne
}
func funtest(tag :UIButton) {
let T = (Int)(tag.tag)
switch T {
case 21, 22, 23, 31, 32, 33, 41, 42, 43:
inputNumber(numberCharcter: "\((4 - T/10 )*3 + T%10)")
case 14, 24, 34, 44, 54:
testOperation(operation: dicIntStr[T]!)
case 51:
id0Button()
case 52:
idDButton()
case 11:
idACButton()
case 13:
idQYButton()
case 53:
idXFButton()
case 12:
idDelButton()
default:
break
}
}
override func viewDidLoad() {
super.viewDidLoad(); view.backgroundColor = UIColor.init(red: 0.8, green: 0.7, blue: 0.6, alpha: 1)
let wiDth = UIScreen.mainScreen().bounds.width,heiGht = UIScreen.mainScreen().bounds.height
let butWidth = (wiDth - 9)/4, butHeight = 0.65*heiGht/5 - 3
let tile = UILabel()
tile.text = "简单计算器"; tile.frame = CGRectMake(0, 20, wiDth, 0.07*heiGht)
tile.textAlignment = .Center;tile.backgroundColor = UIColor.init(red: 0.9, green: 0.5, blue: 0.3, alpha: 1)
self.view.addSubview(tile)
outputLabel.font = UIFont.systemFontOfSize(72)
outputLabel.textAlignment = .Right;
outputLabel.backgroundColor = UIColor.greenColor()
outputLabel.adjustsFontSizeToFitWidth = true;
outputLabel.text = "0"
outputLabel.frame = CGRectMake(0, 0.1*heiGht, wiDth, 0.25*heiGht)
self.view.addSubview(outputLabel)
for i in 1 ..< 6 {
for j in 1 ..< 5 {
let button = UIButton()
button.frame = CGRectMake((CGFloat)(j - 1)*(butWidth + 3), 0.35*heiGht + ((CGFloat)(i) - 1)*(butHeight + 3) + 3, butWidth, butHeight)
button.tag = i*10 + j; button.setTitle(dicIntStr[button.tag], forState: .Normal)
button.setTitleColor(UIColor.blackColor(), forState: .Highlighted)
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: #selector(funtest), forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}
}
}
}