code.club

 找回密碼
 立即註冊
搜索
查看: 9229|回復: 2

swift 寫的功能簡單的計算機

[複製鏈接]
發表於 2016-7-3 01:27:46 | 顯示全部樓層 |閱讀模式
本帖最後由 swift 於 2016-7-5 00:06 編輯

功能簡單並沒有先乘除後加減的功能。
需要先配置好介面的元件,如按鈕等。並且由於採 Double 型,所以可能相除時會得到很長的小數點數字(最多會出現小數點之後15位數)。
會對除數為0發出錯誤警告,但不會直接當掉,你可以再改其他數字繼續進行計算。


回復

使用道具 舉報

 樓主| 發表於 2016-7-5 00:01:10 | 顯示全部樓層


新增了按鍵時的音效,而且數字鍵,加減除等鍵的音效同。出現錯誤時也有錯誤的音效。但錄影時音效無法錄下來。
回復 支持 反對

使用道具 舉報

 樓主| 發表於 2016-11-19 22:21:11 | 顯示全部樓層
本帖最後由 swift 於 2016-11-19 22:28 編輯
  1. //

  2. //  ViewController.swift

  3. //  sun ying-chi//

  4. //  Created by yesonline on 2016/7/1.

  5. //  Copyright © 2016年 yesonline. All rights reserved.

  6. //



  7. import UIKit

  8. import AVFoundation



  9. class ViewController: UIViewController {
  10.    
  11.     var clickSound:AVAudioPlayer!
  12.    
  13.     var errorSound:AVAudioPlayer!
  14.    
  15.     var okSound:AVAudioPlayer!
  16.    
  17.     var tempStr:String = ""
  18.    
  19.     var sum:Double = 0
  20.    
  21.     var total:Double = 0
  22.    
  23.     var currentMethod:Int = 0
  24.    
  25.     var errorMsg:Bool = false
  26.    
  27.     var newInput:Bool = false
  28.    
  29.     var isOK:Bool = false
  30.    
  31.     @IBOutlet var warningMsg: UILabel!
  32.    
  33.     var canChange:Bool = true
  34.    
  35.     var startOver:Bool = true
  36.    
  37.     var isSignChanged:Bool = false
  38.    
  39.    
  40.    
  41.     @IBOutlet weak var isDot: UIButton!
  42.    
  43.     @IBOutlet weak var result: UILabel!
  44.    
  45.     @IBOutlet weak var nowAction: UILabel!
  46.    
  47.    
  48.    
  49.     @IBOutlet weak var theAnswerIs: UILabel!
  50.    
  51.    
  52.    
  53.     func isInt(_ n:Double) -> Bool {
  54.         
  55.         if n == Double(Int(n)) {
  56.             
  57.             return true
  58.             
  59.         } else {
  60.             
  61.             return false
  62.             
  63.         }
  64.         
  65.     }
  66.     /*
  67.     func specialNum(){
  68.         if result.text!.hasSuffix(".0") {
  69.             let range = result.text!.endIndex.advancedBy(-2)..<result.text!.endIndex
  70.             result.text!.removeRange(range)
  71.         }
  72.     }
  73.   */
  74. func specialNum(){}
  75.    
  76.    
  77.     @IBAction func delNum(_ sender: UIButton) {
  78.         
  79.         clickSound.play()
  80.         
  81.         tempStr = result.text!
  82.         
  83.         if tempStr.characters.count > 1
  84.             
  85.         {
  86.             
  87.             tempStr.remove(at: tempStr.index(before: tempStr.endIndex))
  88.             
  89.             result.text = tempStr
  90.             
  91.         } else {
  92.             
  93.             tempStr = "0"
  94.             
  95.             result.text = tempStr
  96.             
  97.         }
  98.         
  99.         if result.text == "-" {
  100.             
  101.             result.text = "0"
  102.             
  103.         }
  104.         
  105.         
  106.         
  107.     }
  108.    
  109.    
  110.    
  111.     @IBAction func signChange(_ sender: UIButton) {
  112.         
  113.         clickSound.play()
  114.         
  115.         if isSignChanged == false {
  116.             
  117.             isSignChanged = true
  118.             
  119.             nowAction.text = "( 將輸入負數- )"
  120.             
  121.         } else {
  122.             
  123.             isSignChanged = false
  124.             
  125.             nowAction.text = "( 將輸入正數+ )"
  126.             
  127.         }
  128.         
  129.     }
  130.    
  131.    
  132.     @IBAction func numClick(_ sender: UIButton) {
  133.         
  134.         
  135.         
  136.         clickSound.play()
  137.         
  138.         
  139.         
  140.         if  startOver {
  141.             
  142.             result.text = ""
  143.             
  144.             startOver = false
  145.             
  146.         }
  147.         
  148.         
  149.         
  150.         if canChange == false {
  151.             
  152.             canChange = true
  153.             
  154.         }
  155.         
  156.         
  157.         
  158.         theAnswerIs.text = ""
  159.         
  160.         warningMsg.text = ""
  161.         
  162.         if isOK {
  163.             
  164.             result.text = ""
  165.             
  166.             isOK = false
  167.             
  168.         }
  169.         
  170.         if sender.currentTitle == "." {
  171.             
  172.             isDot.isEnabled = false
  173.             
  174.         }
  175.         
  176.         
  177.         
  178.         if newInput {
  179.             
  180.             result.text = sender.currentTitle!
  181.             
  182.             
  183.             
  184.             newInput = false
  185.             
  186.         }
  187.             
  188.             
  189.             
  190.         else {
  191.             
  192.             
  193.             
  194.             result.text = result.text! + sender.currentTitle!
  195.             
  196.         }
  197.         
  198.         
  199.         
  200.         if isSignChanged {
  201.             
  202.             result.text = "-" + result.text!
  203.             
  204.             
  205.             
  206.             isSignChanged = false
  207.             
  208.         }
  209.         
  210.     }
  211.    
  212.    
  213.     @IBAction func addClick(_ sender: UIButton) {
  214.         
  215.         
  216.         
  217.         nowAction.text = "( 加上 + )"
  218.         
  219.         clickSound.play()
  220.         
  221.         if canChange {
  222.             
  223.             
  224.             isDot.isEnabled = true
  225.             
  226.             sum = Double(result.text!)!
  227.             
  228.             if currentMethod == 0 {
  229.                
  230.                 total = sum
  231.                
  232.             }
  233.             
  234.             if currentMethod == 1 {
  235.                
  236.                 total += sum
  237.                
  238.                
  239.             }
  240.             
  241.             if currentMethod == 2 {
  242.                
  243.                 total -= sum
  244.                
  245.                
  246.             }
  247.             
  248.             if currentMethod == 3 {
  249.                
  250.                 total *= sum
  251.                
  252.                
  253.                
  254.             }
  255.             
  256.             if currentMethod == 4 && sum == 0 {
  257.                
  258.                
  259.                
  260.                 errorMsg = true
  261.                
  262.             } else if currentMethod == 4 && sum != 0  {
  263.                
  264.                 total /= sum
  265.                
  266.                
  267.                
  268.             }
  269.             
  270.             if errorMsg {
  271.                
  272.                 errorSound.play()
  273.                
  274.                 theAnswerIs.text = "除數不得為0"
  275.                
  276.                 warningMsg.text = "除數不得為0"
  277.                
  278.                 errorMsg = false
  279.                
  280.             } else {
  281.                
  282.                 result.text = ""
  283.                
  284.             }
  285.             
  286.             currentMethod = 1
  287.             
  288.             
  289.             
  290.             if isInt(total) {
  291.                
  292.                 result.text = String(Int(total))
  293.                
  294.             } else {
  295.                
  296.                 result.text = String(total)
  297.                
  298.             }
  299.             
  300.             specialNum()
  301.             
  302.             newInput = true
  303.             
  304.             sum = 0
  305.             
  306.         }
  307.         
  308.         
  309.         
  310.         canChange = false
  311.         
  312.         currentMethod = 1
  313.         
  314.         
  315.         
  316.     }
  317.    
  318.    
  319.    
  320.     @IBAction func minusClick(_ sender: UIButton) {
  321.         
  322.         nowAction.text = "( 減去 - )"
  323.         
  324.         clickSound.play()
  325.         
  326.         if canChange {
  327.             
  328.             isDot.isEnabled = true
  329.             
  330.             sum = Double(result.text!)!
  331.    
  332.             
  333.             if currentMethod == 0 {
  334.                
  335.                 total = sum
  336.                
  337.             }
  338.             
  339.             if currentMethod == 1 {
  340.                
  341.                 total += sum
  342.            
  343.                
  344.             }
  345.             
  346.             if currentMethod == 2 {
  347.                
  348.                 total -= sum
  349.               
  350.             }
  351.             
  352.             if currentMethod == 3 {
  353.                
  354.                 total *= sum
  355.       
  356.                
  357.             }
  358.             
  359.             if currentMethod == 4 && sum == 0 {
  360.                
  361.                
  362.                
  363.                 errorMsg = true
  364.                
  365.             } else if currentMethod == 4 && sum != 0  {
  366.                
  367.                 total /= sum
  368.                
  369.                
  370.                
  371.             }
  372.             
  373.             if errorMsg {
  374.                
  375.                 errorSound.play()
  376.                
  377.                 theAnswerIs.text = "除數不得為0"
  378.                
  379.                 warningMsg.text = "除數不得為0"
  380.                
  381.                 errorMsg = false
  382.                
  383.             } else {
  384.                
  385.                 result.text = ""
  386.                
  387.             }
  388.             
  389.             specialNum()
  390.             
  391.             currentMethod = 2
  392.             
  393.             if isInt(total) {
  394.                
  395.                 result.text = String(Int(total))
  396.                
  397.             } else {
  398.                
  399.                 result.text = String(total)
  400.                
  401.             }
  402.             specialNum()
  403.             newInput = true
  404.             
  405.             sum = 0
  406.             
  407.         }
  408.         
  409.         canChange = false
  410.         
  411.         currentMethod = 2
  412.         
  413.     }
  414.    
  415.    
  416.    
  417.    
  418.    
  419.     @IBAction func timesClick(_ sender: UIButton) {
  420.         
  421.         
  422.         
  423.         nowAction.text = "( 乘以 x )"
  424.         
  425.         clickSound.play()
  426.         
  427.         if canChange{
  428.             
  429.             
  430.             
  431.             isDot.isEnabled = true
  432.             
  433.             
  434.             
  435.             sum = Double(result.text!)!
  436.             
  437.             
  438.             
  439.             if currentMethod == 0 {
  440.                
  441.                 total = sum
  442.                
  443.                
  444.                
  445.                
  446.                
  447.             }
  448.             
  449.             if currentMethod == 1 {
  450.                
  451.                 total += sum
  452.                
  453.                
  454.                
  455.             }
  456.             
  457.             if currentMethod == 2 {
  458.                
  459.                 total -= sum
  460.                
  461.                
  462.                
  463.             }
  464.             
  465.             if currentMethod == 3 {
  466.                
  467.                 total *= sum
  468.                
  469.                
  470.                
  471.             }
  472.             
  473.             if currentMethod == 4 && sum == 0 {
  474.                
  475.                
  476.                
  477.                 errorMsg = true
  478.                
  479.             } else if currentMethod == 4 && sum != 0  {
  480.                
  481.                 total /= sum
  482.                
  483.                
  484.                
  485.             }
  486.             
  487.             if errorMsg {
  488.                
  489.                 errorSound.play()
  490.                
  491.                 theAnswerIs.text = "除數不得為0"
  492.                
  493.                 warningMsg.text = "除數不得為0"
  494.                
  495.                 errorMsg = false
  496.                
  497.             } else {
  498.                
  499.                 result.text = ""
  500.                
  501.             }
  502.             
  503.             specialNum()
  504.             
  505.             
  506.             currentMethod = 3
  507.             
  508.             if isInt(total) {
  509.                
  510.                 result.text = String(Int(total))
  511.                
  512.             } else {
  513.                
  514.                 result.text = String(total)
  515.                
  516.             }
  517.             
  518.             specialNum()
  519.             
  520.             newInput = true
  521.             
  522.             sum = 0
  523.             
  524.             
  525.             
  526.         }
  527.         
  528.         canChange = false
  529.         
  530.         currentMethod = 3
  531.         
  532.     }
  533.    
  534.    
  535.     @IBAction func divideClick(_ sender: UIButton) {
  536.         
  537.         
  538.         
  539.         nowAction.text = "( 除以 ÷ )"
  540.         
  541.         clickSound.play()
  542.         
  543.         if canChange{
  544.             
  545.             
  546.             
  547.             isDot.isEnabled = true
  548.             
  549.             
  550.             
  551.             sum = Double(result.text!)!
  552.             
  553.             
  554.             
  555.             if currentMethod == 0 {
  556.                
  557.                 total = sum
  558.                
  559.                
  560.                
  561.                
  562.                
  563.             }
  564.             
  565.             if currentMethod == 1 {
  566.                
  567.                 total += sum
  568.                
  569.                
  570.                
  571.             }
  572.             
  573.             if currentMethod == 2 {
  574.                
  575.                 total -= sum
  576.                
  577.                
  578.                
  579.             }
  580.             
  581.             if currentMethod == 3 {
  582.                
  583.                 total *= sum
  584.                
  585.                
  586.                
  587.             }
  588.             
  589.             if currentMethod == 4 && sum == 0 {
  590.                
  591.                
  592.                
  593.                 errorMsg = true
  594.                
  595.             } else if currentMethod == 4 && sum != 0  {
  596.                
  597.                 total /= sum
  598.                
  599.                
  600.                
  601.             }
  602.             
  603.             if errorMsg {
  604.                
  605.                 errorSound.play()
  606.                
  607.                 theAnswerIs.text = "除數不得為0"
  608.                
  609.                 warningMsg.text = "除數不得為0"
  610.                
  611.                 errorMsg = false
  612.                
  613.             } else {
  614.                
  615.                 result.text = ""
  616.                
  617.             }
  618.             
  619.             specialNum()
  620.             
  621.             
  622.             currentMethod = 4
  623.             
  624.             if isInt(total) {
  625.                
  626.                 result.text = String(Int(total))
  627.                
  628.             } else {
  629.                
  630.                 result.text = String(total)
  631.                
  632.             }
  633.             
  634.             specialNum()
  635.             newInput = true
  636.             
  637.             sum = 0
  638.             
  639.             
  640.             
  641.             
  642.             
  643.         }
  644.         
  645.         canChange = false
  646.         
  647.         currentMethod = 4
  648.         
  649.         
  650.         
  651.     }
  652.    
  653.    
  654.    
  655.    
  656.     @IBAction func clearClick(_ sender: UIButton) {
  657.         
  658.         nowAction.text = "( 清空 clear )"
  659.         
  660.         clickSound.play()
  661.         
  662.         isDot.isEnabled = true
  663.         
  664.         
  665.         
  666.         theAnswerIs.text = ""
  667.         
  668.         warningMsg.text = ""
  669.         
  670.         currentMethod = 0
  671.         
  672.         total = 0
  673.         
  674.         sum = 0
  675.         
  676.         result.text = "0"
  677.         
  678.         errorMsg = false
  679.         
  680.         canChange = true
  681.         
  682.         startOver = true
  683.         
  684.         
  685.         
  686.     }
  687.    
  688.    
  689.    
  690.     @IBAction func okClick(_ sender: UIButton) {
  691.         
  692.         nowAction.text = "( 等於 = )"
  693.         
  694.         
  695.         
  696.         isDot.isEnabled = true
  697.         
  698.         
  699.         
  700.         if currentMethod == 0 && result.text == ""
  701.             
  702.         {
  703.             
  704.             sum = 0
  705.             
  706.         }
  707.             
  708.         else {
  709.             
  710.             
  711.             
  712.             sum = Double(result.text!)!
  713.             
  714.             
  715.             
  716.             if currentMethod == 0 {
  717.                
  718.                 total = sum
  719.                
  720.                 if isInt(total) {
  721.                     
  722.                     result.text = String(Int(total))
  723.                     
  724.                 } else {
  725.                     
  726.                     result.text = String(total)
  727.                     
  728.                 }
  729.                
  730.                 specialNum()
  731.                 theAnswerIs.text = "計算的結果是:"
  732.                
  733.                 warningMsg.text = ""
  734.                
  735.                 okSound.play()
  736.                
  737.                
  738.                
  739.             }
  740.             
  741.             if currentMethod == 1 {
  742.                
  743.                 total += sum
  744.                
  745.                 if isInt(total) {
  746.                     
  747.                     result.text = String(Int(total))
  748.                     
  749.                 } else {
  750.                     
  751.                     result.text = String(total)
  752.                     
  753.                 }
  754.                
  755.                 specialNum()
  756.                
  757.                
  758.                 theAnswerIs.text = "計算的結果是:"
  759.                
  760.                 warningMsg.text = ""
  761.                
  762.                 okSound.play()
  763.                
  764.                
  765.                
  766.             }
  767.             
  768.             if currentMethod == 2 {
  769.                
  770.                 total -= sum
  771.                
  772.                 if isInt(total) {
  773.                     
  774.                     result.text = String(Int(total))
  775.                     
  776.                 } else {
  777.                     
  778.                     result.text = String(total)
  779.                     
  780.                 }
  781.                
  782.                 specialNum()
  783.                
  784.                
  785.                 theAnswerIs.text = "計算的結果是:"
  786.                
  787.                 warningMsg.text = ""
  788.                
  789.                 okSound.play()
  790.                
  791.                
  792.                
  793.             }
  794.             
  795.             if currentMethod == 3 {
  796.                
  797.                 total *= sum
  798.                
  799.                 if isInt(total) {
  800.                     
  801.                     result.text = String(Int(total))
  802.                     
  803.                 } else {
  804.                     
  805.                     result.text = String(total)
  806.                     
  807.                 }
  808.                
  809.                 specialNum()
  810.                
  811.                
  812.                 theAnswerIs.text = "計算的結果是:"
  813.                
  814.                 warningMsg.text = ""
  815.                
  816.                 okSound.play()
  817.                
  818.                
  819.                
  820.             }
  821.             
  822.             if currentMethod == 4 && sum == 0 {
  823.                
  824.                 theAnswerIs.text = "除數不得為0"
  825.                
  826.                 warningMsg.text = "除數不得為0"
  827.                
  828.                 errorSound.play()
  829.                
  830.             } else if currentMethod == 4 && sum != 0  {
  831.                
  832.                 total /= sum
  833.                
  834.                 if isInt(total) {
  835.                     
  836.                     result.text = String(Int(total))
  837.                     
  838.                 } else {
  839.                     
  840.                     result.text = String(total)
  841.                     
  842.                 }
  843.                
  844.                 specialNum()
  845.                
  846.                
  847.                 theAnswerIs.text = "計算的結果是:"
  848.                
  849.                 warningMsg.text = ""
  850.                
  851.                 okSound.play()
  852.                
  853.             }
  854.             
  855.         }
  856.         
  857.         
  858.         
  859.         if isInt(total) {
  860.             
  861.             result.text = String(Int(total))
  862.             
  863.         } else {
  864.             
  865.             result.text = String(total)
  866.             
  867.         }
  868.         
  869.         specialNum()
  870.         
  871.         
  872.         
  873.         
  874.         currentMethod = 0
  875.         
  876.         total = 0
  877.         
  878.         sum = 0
  879.         
  880.         newInput = false
  881.         
  882.         isOK = true
  883.         
  884.         canChange = true
  885.         
  886.         
  887.         
  888.     }
  889.    
  890.    
  891.    
  892.    
  893.    
  894.     override func viewDidLoad() {
  895.         
  896.         super.viewDidLoad()
  897.         
  898.         nowAction.text = "( 請輸入 )"
  899.         
  900.         do {
  901.             
  902.             let clickWav = Bundle.main.path(forResource: "press3", ofType:"wav")
  903.             
  904.             clickSound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: clickWav!))
  905.             
  906.             let errorWav = Bundle.main.path(forResource: "error1", ofType:"wav")
  907.             
  908.             errorSound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: errorWav!))
  909.             
  910.             let okWav = Bundle.main.path(forResource: "ok1", ofType:"wav")
  911.             
  912.             okSound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: okWav!))
  913.             
  914.             
  915.             
  916.         } catch {
  917.             
  918.             //print("error")
  919.             
  920.         }
  921.         
  922.         
  923.         
  924.     }
  925.    
  926.    
  927.    
  928.     override func didReceiveMemoryWarning() {
  929.         
  930.         super.didReceiveMemoryWarning()
  931.         
  932.         // Dispose of any resources that can be recreated.
  933.         
  934.     }
  935.    
  936. }
複製代碼
回復 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-3-29 13:23 , Processed in 0.111823 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表