網站首頁 編程語言 正文
本文實例為大家分享了Swift實現簡單計算器的具體代碼,供大家參考,具體內容如下
使用Storyboard
快速而又方便的進行控件的布局,功能操作簡單的進行一些運算;
代碼實現
// // ?ViewController.swift // ?Swift_Calculator // // ?Created by 周文春 on 16/3/2. // ?Copyright ? 2016年 周文春. All rights reserved. // import UIKit class ViewController: UIViewController { ? ? @IBOutlet weak var lableResult: UILabel! ? ? //第一操作 ? ? var firstOperand: Double = 0.0 ? ? //第二操作 ? ? var secondOperand: Double = 0.0 ? ? //標記是否輸入了小數點 ? ? var decimalPointFlag: Bool = false ? ? //是否輸入第二操作數 ? ? var isSecond: Bool = false ? ? //操作符 ? ? var operatorFlag: String = "" ? ? override func viewDidLoad() { ? ? ? ? super.viewDidLoad() ? ? ? ? // Do any additional setup after loading the view, typically from a nib. ? ? } ? ? override func didReceiveMemoryWarning() { ? ? ? ? super.didReceiveMemoryWarning() ? ? ? ? // Dispose of any resources that can be recreated. ? ? } ? ? @IBAction func buttonTap(sender: UIButton) { ? ? ? ? //lableResult 中默認是0,如果開始輸入數字,則先清除0 ? ? ? ? if lableResult.text == "0" || (isSecond && secondOperand == 0.0) { ? ? ? ? ? ? lableResult.text = "" ? ? ? ? } ? ? ? ? //將用戶錄入的數添加到lableResult中 ? ? ? ? lableResult.text = lableResult.text! + sender.titleLabel!.text! ? ? ? ? if isSecond { // ? ? ? ? ? ?secondOperand = (lableResult.text! as NSString).doubleValue ? ? ? ? ? ? secondOperand = NSString(string: lableResult.text!).doubleValue ? ? ? ? }else { ? ? ? ? ? ? //將lableResult中的字符串轉化為雙精度數 // ? ? ? ? ? ?firstOperand = (lableResult.text! as NSString).doubleValue ? ? ? ? ? ? firstOperand = NSString(string: lableResult.text!).doubleValue ? ? ? ? } ? ? ? ? print(firstOperand) ? ? } ? ? @IBAction func decimalPointTap() { ? ? ? ? if !decimalPointFlag { ? ? ? ? ? ? lableResult.text = lableResult.text! + "." ? ? ? ? ? ? if isSecond { ? ? ? ? ? ? ? ? secondOperand = (lableResult.text! as NSString).doubleValue ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? firstOperand = (lableResult.text! as NSString).doubleValue ? ? ? ? ? ? } ? ? ? ? ? ? decimalPointFlag = !decimalPointFlag ? ? ? ? } ? ? } ? ? @IBAction func operatorTap(sender: UIButton) { ? ? ? ? if firstOperand != 0 { ? ? ? ? ? ? isSecond = true ? ? ? ? ? ? decimalPointFlag = false ? ? ? ? ? ? switch sender.titleLabel!.text! { ? ? ? ? ? ? ? ? case "+": ? ? ? ? ? ? ? ? ? ? ? operatorFlag = "+" ? ? ? ? ? ? ? ? case "-": ? ? ? ? ? ? ? ? ? ? ? operatorFlag = "-" ? ? ? ? ? ? ? ? case "*": ? ? ? ? ? ? ? ? ? ? ? operatorFlag = "*" ? ? ? ? ? ? ? ? case "÷": ? ? ? ? ? ? ? ? ? ? ? operatorFlag = "/" ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? operatorFlag = " " ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? @IBAction func resultTap(sender: UIButton) { ? ? ? ? //確保第二操作數有值 ? ? ? ? if isSecond { ? ? ? ? ? ? //除數不能為0 ? ? ? ? ? ? if operatorFlag == "/" && secondOperand == 0 { ? ? ? ? ? ? ? ? print("Error: 除數不能為0") ? ? ? ? ? ? ? ? return ? ? ? ? ? ? } ? ? ? ? ? ? var result: Double = 0.0 ? ? ? ? ? ? switch operatorFlag { ? ? ? ? ? ? ? ? case "+": ? ? ? ? ? ? ? ? result = firstOperand + secondOperand ? ? ? ? ? ? ? ? case "-": ? ? ? ? ? ? ? ? result = firstOperand - secondOperand ? ? ? ? ? ? ? ? case ?"*": ? ? ? ? ? ? ? ? result = firstOperand * secondOperand ? ? ? ? ? ? ? ? case ?"/": ? ? ? ? ? ? ? ? result = firstOperand / secondOperand ? ? ? ? ? ? default : ? ? ? ? ? ? ? ? result = 0.0 ? ? ? ? ? ? } ? ? ? ? ? ? lableResult.text = result.description ? ? ? ? ? ? print("第一操作: \(firstOperand)") ? ? ? ? ? ? print("操作符: \(operatorFlag)") ? ? ? ? ? ? print("第二操作: \(secondOperand)") ? ? ? ? ? ? print("結果: \(result)") ? ? ? ? } ? ? } ? ? @IBAction func clear(sender: UIButton) { ? ? ? ? //lable對象顯示0 ? ? ? ? lableResult.text = "0" ? ? ? ? //第一操作數清零 ? ? ? ? firstOperand = 0.0 ? ? ? ? //第二操作數清零 ? ? ? ? secondOperand = 0.0 ? ? ? ? //小數點標記設置為假 ? ? ? ? decimalPointFlag = false ? ? ? ? //第二操作數標記設置為假 ? ? ? ? isSecond = false ? ? ? ? //操作清空 ? ? ? ? operatorFlag = "" ? ? } }
原文鏈接:https://blog.csdn.net/qq_29284809/article/details/50803536
相關推薦
- 2022-11-26 使用HttpClient增刪改查ASP.NET?Web?API服務_實用技巧
- 2022-06-30 基于Python使用永中文檔轉換服務的方式_python
- 2023-12-13 SpringMVC——訪問action報404錯誤詳解
- 2022-03-03 解決Warning: [antdv: LocaleProvider] `LocaleProvider
- 2022-11-03 C++11?寫一個只觸發一次槽函數的Qt?connect函數_C 語言
- 2022-07-01 詳解如何在Go語言中調用C源代碼_Golang
- 2022-06-25 JQuery選擇器用法詳解_jquery
- 2022-09-04 C語言中反斜杠的作用及說明_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支