網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Swift實現(xiàn)簡單計算器的具體代碼,供大家參考,具體內(nèi)容如下
使用Storyboard
快速而又方便的進行控件的布局,功能操作簡單的進行一些運算;
代碼實現(xiàn)
// // ?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 ? ? //標記是否輸入了小數(shù)點 ? ? var decimalPointFlag: Bool = false ? ? //是否輸入第二操作數(shù) ? ? 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,如果開始輸入數(shù)字,則先清除0 ? ? ? ? if lableResult.text == "0" || (isSecond && secondOperand == 0.0) { ? ? ? ? ? ? lableResult.text = "" ? ? ? ? } ? ? ? ? //將用戶錄入的數(shù)添加到lableResult中 ? ? ? ? lableResult.text = lableResult.text! + sender.titleLabel!.text! ? ? ? ? if isSecond { // ? ? ? ? ? ?secondOperand = (lableResult.text! as NSString).doubleValue ? ? ? ? ? ? secondOperand = NSString(string: lableResult.text!).doubleValue ? ? ? ? }else { ? ? ? ? ? ? //將lableResult中的字符串轉(zhuǎn)化為雙精度數(shù) // ? ? ? ? ? ?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) { ? ? ? ? //確保第二操作數(shù)有值 ? ? ? ? if isSecond { ? ? ? ? ? ? //除數(shù)不能為0 ? ? ? ? ? ? if operatorFlag == "/" && secondOperand == 0 { ? ? ? ? ? ? ? ? print("Error: 除數(shù)不能為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("結(jié)果: \(result)") ? ? ? ? } ? ? } ? ? @IBAction func clear(sender: UIButton) { ? ? ? ? //lable對象顯示0 ? ? ? ? lableResult.text = "0" ? ? ? ? //第一操作數(shù)清零 ? ? ? ? firstOperand = 0.0 ? ? ? ? //第二操作數(shù)清零 ? ? ? ? secondOperand = 0.0 ? ? ? ? //小數(shù)點標記設置為假 ? ? ? ? decimalPointFlag = false ? ? ? ? //第二操作數(shù)標記設置為假 ? ? ? ? isSecond = false ? ? ? ? //操作清空 ? ? ? ? operatorFlag = "" ? ? } }
原文鏈接:https://blog.csdn.net/qq_29284809/article/details/50803536
相關推薦
- 2022-07-01 docker搭建mongodb單節(jié)點副本集的實現(xiàn)_docker
- 2022-11-04 ASP.NET?MVC獲取多級類別組合下的產(chǎn)品_實用技巧
- 2022-08-16 C++超詳細梳理基礎知識_C 語言
- 2022-04-11 Nginx性能優(yōu)化之Gzip壓縮設置詳解(最大程度提高頁面打開速度)_nginx
- 2023-04-07 C語言解決字符串中插入和刪除某段字符串問題_C 語言
- 2024-03-06 SpringAOP基于注解方式實現(xiàn)和細節(jié)
- 2022-07-09 使用cgroup控制cpu、內(nèi)存、IO資源實踐
- 2022-10-24 iOS開發(fā)之Objective-c的Runtime理解指南_IOS
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支