code.club
標題:
swift 寫的功能簡單的計算機
[打印本頁]
作者:
swift
時間:
2016-7-3 01:27
標題:
swift 寫的功能簡單的計算機
本帖最後由 swift 於 2016-7-5 00:06 編輯
功能簡單並沒有先乘除後加減的功能。
需要先配置好介面的元件,如按鈕等。並且由於採 Double 型,所以可能相除時會得到很長的小數點數字(最多會出現小數點之後15位數)。
會對除數為0發出錯誤警告,但不會直接當掉,你可以再改其他數字繼續進行計算。
[youtube]Z-o7JZVQ1AM[/youtube]
作者:
swift
時間:
2016-7-5 00:01
[youtube]LpRXLcgWvlI[/youtube]
新增了按鍵時的音效,而且數字鍵,加減除等鍵的音效同。出現錯誤時也有錯誤的音效。但錄影時音效無法錄下來。
作者:
swift
時間:
2016-11-19 22:21
本帖最後由 swift 於 2016-11-19 22:28 編輯
//
// ViewController.swift
// sun ying-chi//
// Created by yesonline on 2016/7/1.
// Copyright © 2016年 yesonline. All rights reserved.
//
import UIKit
import AVFoundation
class ViewController: UIViewController {
var clickSound:AVAudioPlayer!
var errorSound:AVAudioPlayer!
var okSound:AVAudioPlayer!
var tempStr:String = ""
var sum:Double = 0
var total:Double = 0
var currentMethod:Int = 0
var errorMsg:Bool = false
var newInput:Bool = false
var isOK:Bool = false
@IBOutlet var warningMsg: UILabel!
var canChange:Bool = true
var startOver:Bool = true
var isSignChanged:Bool = false
@IBOutlet weak var isDot: UIButton!
@IBOutlet weak var result: UILabel!
@IBOutlet weak var nowAction: UILabel!
@IBOutlet weak var theAnswerIs: UILabel!
func isInt(_ n:Double) -> Bool {
if n == Double(Int(n)) {
return true
} else {
return false
}
}
/*
func specialNum(){
if result.text!.hasSuffix(".0") {
let range = result.text!.endIndex.advancedBy(-2)..<result.text!.endIndex
result.text!.removeRange(range)
}
}
*/
func specialNum(){}
@IBAction func delNum(_ sender: UIButton) {
clickSound.play()
tempStr = result.text!
if tempStr.characters.count > 1
{
tempStr.remove(at: tempStr.index(before: tempStr.endIndex))
result.text = tempStr
} else {
tempStr = "0"
result.text = tempStr
}
if result.text == "-" {
result.text = "0"
}
}
@IBAction func signChange(_ sender: UIButton) {
clickSound.play()
if isSignChanged == false {
isSignChanged = true
nowAction.text = "( 將輸入負數- )"
} else {
isSignChanged = false
nowAction.text = "( 將輸入正數+ )"
}
}
@IBAction func numClick(_ sender: UIButton) {
clickSound.play()
if startOver {
result.text = ""
startOver = false
}
if canChange == false {
canChange = true
}
theAnswerIs.text = ""
warningMsg.text = ""
if isOK {
result.text = ""
isOK = false
}
if sender.currentTitle == "." {
isDot.isEnabled = false
}
if newInput {
result.text = sender.currentTitle!
newInput = false
}
else {
result.text = result.text! + sender.currentTitle!
}
if isSignChanged {
result.text = "-" + result.text!
isSignChanged = false
}
}
@IBAction func addClick(_ sender: UIButton) {
nowAction.text = "( 加上 + )"
clickSound.play()
if canChange {
isDot.isEnabled = true
sum = Double(result.text!)!
if currentMethod == 0 {
total = sum
}
if currentMethod == 1 {
total += sum
}
if currentMethod == 2 {
total -= sum
}
if currentMethod == 3 {
total *= sum
}
if currentMethod == 4 && sum == 0 {
errorMsg = true
} else if currentMethod == 4 && sum != 0 {
total /= sum
}
if errorMsg {
errorSound.play()
theAnswerIs.text = "除數不得為0"
warningMsg.text = "除數不得為0"
errorMsg = false
} else {
result.text = ""
}
currentMethod = 1
if isInt(total) {
result.text = String(Int(total))
} else {
result.text = String(total)
}
specialNum()
newInput = true
sum = 0
}
canChange = false
currentMethod = 1
}
@IBAction func minusClick(_ sender: UIButton) {
nowAction.text = "( 減去 - )"
clickSound.play()
if canChange {
isDot.isEnabled = true
sum = Double(result.text!)!
if currentMethod == 0 {
total = sum
}
if currentMethod == 1 {
total += sum
}
if currentMethod == 2 {
total -= sum
}
if currentMethod == 3 {
total *= sum
}
if currentMethod == 4 && sum == 0 {
errorMsg = true
} else if currentMethod == 4 && sum != 0 {
total /= sum
}
if errorMsg {
errorSound.play()
theAnswerIs.text = "除數不得為0"
warningMsg.text = "除數不得為0"
errorMsg = false
} else {
result.text = ""
}
specialNum()
currentMethod = 2
if isInt(total) {
result.text = String(Int(total))
} else {
result.text = String(total)
}
specialNum()
newInput = true
sum = 0
}
canChange = false
currentMethod = 2
}
@IBAction func timesClick(_ sender: UIButton) {
nowAction.text = "( 乘以 x )"
clickSound.play()
if canChange{
isDot.isEnabled = true
sum = Double(result.text!)!
if currentMethod == 0 {
total = sum
}
if currentMethod == 1 {
total += sum
}
if currentMethod == 2 {
total -= sum
}
if currentMethod == 3 {
total *= sum
}
if currentMethod == 4 && sum == 0 {
errorMsg = true
} else if currentMethod == 4 && sum != 0 {
total /= sum
}
if errorMsg {
errorSound.play()
theAnswerIs.text = "除數不得為0"
warningMsg.text = "除數不得為0"
errorMsg = false
} else {
result.text = ""
}
specialNum()
currentMethod = 3
if isInt(total) {
result.text = String(Int(total))
} else {
result.text = String(total)
}
specialNum()
newInput = true
sum = 0
}
canChange = false
currentMethod = 3
}
@IBAction func divideClick(_ sender: UIButton) {
nowAction.text = "( 除以 ÷ )"
clickSound.play()
if canChange{
isDot.isEnabled = true
sum = Double(result.text!)!
if currentMethod == 0 {
total = sum
}
if currentMethod == 1 {
total += sum
}
if currentMethod == 2 {
total -= sum
}
if currentMethod == 3 {
total *= sum
}
if currentMethod == 4 && sum == 0 {
errorMsg = true
} else if currentMethod == 4 && sum != 0 {
total /= sum
}
if errorMsg {
errorSound.play()
theAnswerIs.text = "除數不得為0"
warningMsg.text = "除數不得為0"
errorMsg = false
} else {
result.text = ""
}
specialNum()
currentMethod = 4
if isInt(total) {
result.text = String(Int(total))
} else {
result.text = String(total)
}
specialNum()
newInput = true
sum = 0
}
canChange = false
currentMethod = 4
}
@IBAction func clearClick(_ sender: UIButton) {
nowAction.text = "( 清空 clear )"
clickSound.play()
isDot.isEnabled = true
theAnswerIs.text = ""
warningMsg.text = ""
currentMethod = 0
total = 0
sum = 0
result.text = "0"
errorMsg = false
canChange = true
startOver = true
}
@IBAction func okClick(_ sender: UIButton) {
nowAction.text = "( 等於 = )"
isDot.isEnabled = true
if currentMethod == 0 && result.text == ""
{
sum = 0
}
else {
sum = Double(result.text!)!
if currentMethod == 0 {
total = sum
if isInt(total) {
result.text = String(Int(total))
} else {
result.text = String(total)
}
specialNum()
theAnswerIs.text = "計算的結果是:"
warningMsg.text = ""
okSound.play()
}
if currentMethod == 1 {
total += sum
if isInt(total) {
result.text = String(Int(total))
} else {
result.text = String(total)
}
specialNum()
theAnswerIs.text = "計算的結果是:"
warningMsg.text = ""
okSound.play()
}
if currentMethod == 2 {
total -= sum
if isInt(total) {
result.text = String(Int(total))
} else {
result.text = String(total)
}
specialNum()
theAnswerIs.text = "計算的結果是:"
warningMsg.text = ""
okSound.play()
}
if currentMethod == 3 {
total *= sum
if isInt(total) {
result.text = String(Int(total))
} else {
result.text = String(total)
}
specialNum()
theAnswerIs.text = "計算的結果是:"
warningMsg.text = ""
okSound.play()
}
if currentMethod == 4 && sum == 0 {
theAnswerIs.text = "除數不得為0"
warningMsg.text = "除數不得為0"
errorSound.play()
} else if currentMethod == 4 && sum != 0 {
total /= sum
if isInt(total) {
result.text = String(Int(total))
} else {
result.text = String(total)
}
specialNum()
theAnswerIs.text = "計算的結果是:"
warningMsg.text = ""
okSound.play()
}
}
if isInt(total) {
result.text = String(Int(total))
} else {
result.text = String(total)
}
specialNum()
currentMethod = 0
total = 0
sum = 0
newInput = false
isOK = true
canChange = true
}
override func viewDidLoad() {
super.viewDidLoad()
nowAction.text = "( 請輸入 )"
do {
let clickWav = Bundle.main.path(forResource: "press3", ofType:"wav")
clickSound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: clickWav!))
let errorWav = Bundle.main.path(forResource: "error1", ofType:"wav")
errorSound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: errorWav!))
let okWav = Bundle.main.path(forResource: "ok1", ofType:"wav")
okSound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: okWav!))
} catch {
//print("error")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
複製代碼
歡迎光臨 code.club (https://code.club/)
Powered by Discuz! X3.2