網站首頁 編程語言 正文
關于計算器的實現在做之前想了幾個方案。
首先是做一個輸入功能,再以后綴表達式來進行計算,但是這個更適用于做一個科學計算器,在平日生活中的計算器需要一些便捷的計算效果。
所以實現這個計算器使用了將數字存入Label然后轉化String為Int的方式。
算數符號可以存于一個數組內,以數組自帶的編號來表示運算符,
程序內需要儲存的數字可以簡單的兩個,但是只用兩個數字以類似 sum1 += sum2的形式會有一些小BUG出現,所以中間加了一個過渡數字。
計算器里有三個布爾類型的值來分別判斷:之前是否輸入過等于號,這個數字是否是負數,以及是否輸入過操作符號。(等于號的判斷是為了查看過度數字應該取哪個值,是否輸入過加減號可以讓等于結果之后按數字就可以進入下一個運算。)
這個計算器可以實現一些比較簡單的運算,運算符的優先級并未作出判定,但是還是很適用于生活中的運算。
這個計算機的小bug就是在每輸入兩個數字進行運算后必須按等于號,不能以a+b+c+…………的形式計算,只能用于a + b = ?
// // ?ViewController.swift // ?Calculator // // ?Created by CQUPT-ZHX on 2019/4/27. // ?Copyright ? 2019 cquptzhx. All rights reserved. // import UIKit import Darwin class ViewController: UIViewController { ? ? ? ? var Priority = ["+","-","*","÷","="] ? ? var isEq:Bool = false//判斷是否輸入等于號 ? ? var isMinus:Bool = false//判斷是否負數 ? ? var isControl:Bool = false//判斷是否輸入操作符 ? ? var input:Double = 0.0//存儲輸入數字 ? ? var lastRes:Double = 0.0//存儲過度數字 ? ? var res:Double = 0.0//存儲a答案 ? ? var fh:Int = 0//符號tag ? ? var math:Int = 0//運算符tag ? ?? ? ? @IBOutlet weak var resultsum: UILabel! ? ? override func viewDidLoad() { ? ? ? ? super.viewDidLoad() ? ? ? ? // Do any additional setup after loading the view. ? ? } ? ?? ? ? @IBAction func takesum(_ sender: UIButton) { ? ? ? ? //取數字 ? ? ? ? if isMinus {//取負數后下次顯示區清零 ? ? ? ? ? ? resultsum.text = "0" ? ? ? ? } ? ? ? ? if isControl{//輸入運算符后下次顯示區清零 ? ? ? ? ? ? resultsum.text = "0" ? ? ? ? } ? ? ? ? if(resultsum.text! != "0"){ ? ? ? ? ? ? resultsum.text! += String(sender.tag) ? ? ? ? }else{ ? ? ? ? ? ? resultsum.text! = String(sender.tag) ? ? ? ? } ? ? ? ? input = (resultsum.text! as NSString).doubleValue ? ? ? ? //獲得數字并存儲 ? ? ? ? isEq = false ? ? ? ? isMinus = false ? ? ? ? isControl = false ? ? } ? ? @IBAction func touchPoint(_ sender: UIButton) { ? ? ? ? resultsum.text! += "." ? ? ? ? //加入小數點 ? ? } ? ? @IBAction func touchMinus(_ sender: UIButton) { ? ? ? ? //負數操作 ? ? ? ? if (res == 0){ ? ? ? ? ? ? equal(sender) ? ? ? ? ? ? res = -input ? ? ? ? } else{ ? ? ? ? ? ? res = -res ? ? ? ? } ? ? ? ? resultsum.text = String(res) ? ? ? ? isMinus = true ? ? } ? ? @IBAction func equal(_ sender: UIButton) { ? ? ? ? //等號運算 ? ? ? ? switch(fh) { ? ? ? ? ? ? case 1: ? ? ? ? ? ? ? ? res = lastRes + input ? ? ? ? ? ? case 2: ? ? ? ? ? ? ? ? res = lastRes - input ? ? ? ? ? ? case 3: ? ? ? ? ? ? ? ? res = lastRes * input ? ? ? ? ? ? case 4: ? ? ? ? ? ? ? ? res = lastRes / input ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? break ? ? ? ? } ? ? ? ? resultsum.text! = "\(res)" ? ? ? ? lastRes = res ? ? ? ? isEq = true ? ? ? ? isControl = true ? ? } ? ? @IBAction func backC(_ sender: UIButton) { ? ? ? ? //刪除上一個字符 ? ? ? ? if resultsum.text?.count == 1 { ? ? ? ? ? ? resultsum.text = "0" ? ? ? ? } ? ? ? ? else if (resultsum.text! as NSString).doubleValue != 0 { ? ? ? ? ? ? resultsum.text?.removeLast() ? ? ? ? } ? ? ? ? input = (resultsum.text! as NSString).doubleValue ? ? } ? ?? ? ? @IBAction func getsign(_ sender: UIButton) { ? ? ? ? //輸入運算符 ? ? ? ? if sender.tag < 5 { ? ? ? ? ? ? resultsum.text! = Priority[sender.tag - 1] ? ? ? ? ? ? if isEq { ? ? ? ? ? ? ? ? lastRes = res ? ? ? ? ? ? } ? ? ? ? ? ? else { ? ? ? ? ? ? ? ? lastRes = input ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? fh = sender.tag ? ? ? ? isControl = true ? ? } ? ?? ? ?? ? ? @IBAction func touchClean(_ sender: UIButton) { ? ? ? ? //清空所有儲存 ? ? ? ? res = 0 ? ? ? ? lastRes = 0 ? ? ? ? input = 0 ? ? ? ? resultsum.text = "0" ? ? ? ? isControl = false ? ? } ? ?? ? ?? ? ? @IBAction func touchMath(_ sender: UIButton) { ? ? ? ? //便捷運算 ? ? ? ? math = sender.tag ? ? ? ? if(res == 0){ ? ? ? ? ? ? res = input ? ? ? ? } ? ? ? ? switch(math){ ? ? ? ? case 7: ? ? ? ? ? ? res = res * 3.14 ? ? ? ? case 8: ? ? ? ? ? ? res = res * res ? ? ? ? case 9: ? ? ? ? ? ? res = sin(res) ? ? ? ? case 10: ? ? ? ? ? ? res = cos(res) ? ? ? ? default: ? ? ? ? ? ? break ? ? ? ? } ? ? ? ? resultsum.text! = "\(res)" ? ? ? ? ?lastRes = res ? ? ? ? isEq = true ? ? ? ?? ? ? } ? ?? }
原文鏈接:https://blog.csdn.net/weixin_44729676/article/details/89791736
相關推薦
- 2022-03-26 .Net?Core微服務rpc框架GRPC通信基礎_基礎應用
- 2022-12-24 python項目運行導致內存越來越大的原因詳析_python
- 2022-06-21 C#實現Array,List,Dictionary相互轉換_C#教程
- 2022-07-02 C#并行編程之數據并行Tasks.Parallel類_C#教程
- 2022-07-14 React?Native采用Hermes熱更新打包方案詳解_React
- 2022-03-19 C#?壓榨cpu的辦法(推薦)_C#教程
- 2022-06-29 python人工智能tensorflow函數tf.get_variable使用方法_python
- 2022-04-17 小程序 底部懸浮的輸入框聚焦的時候 鍵盤緊貼輸入框
- 最近更新
-
- 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同步修改后的遠程分支