簡化程式碼
This commit is contained in:
@@ -28,7 +28,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickNumberButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="fKV-LM-t0P"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="fKV-LM-t0P"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="nAR-KS-hdA">
|
||||
@@ -39,7 +39,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickNumberButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="3tS-Vs-uwN"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="3tS-Vs-uwN"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="kDf-qC-jcV">
|
||||
@@ -50,7 +50,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickNumberButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="7dd-g7-d1V"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="7dd-g7-d1V"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="JA3-GM-FCC">
|
||||
@@ -61,7 +61,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickNumberButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="SQe-6n-LS3"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="SQe-6n-LS3"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="lNe-oW-jh1">
|
||||
@@ -72,7 +72,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickNumberButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="YHI-GM-GHt"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="YHI-GM-GHt"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="K0o-vl-kE7">
|
||||
@@ -83,7 +83,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickNumberButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="fTX-1h-ZFC"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="fTX-1h-ZFC"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="1p3-kG-6iM">
|
||||
@@ -94,7 +94,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickNumberButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="dK6-yT-RSF"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="dK6-yT-RSF"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="wGn-JZ-stq">
|
||||
@@ -105,7 +105,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickNumberButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="8jc-pf-wxa"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="8jc-pf-wxa"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="XMC-8P-7aZ">
|
||||
@@ -116,7 +116,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickNumberButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="X0L-rt-8nz"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="X0L-rt-8nz"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ePk-jK-elJ">
|
||||
@@ -127,7 +127,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickNumberButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="5kj-8D-26R"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="5kj-8D-26R"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="caQ-65-jBq">
|
||||
@@ -138,7 +138,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickDotButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="n4O-i6-tej"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="fpp-0T-t6o"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="jgo-wI-dJm">
|
||||
@@ -149,7 +149,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickEquationButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Wlj-nE-CFP"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="eyW-0b-i2h"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="I4O-WX-Rsc">
|
||||
@@ -160,7 +160,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickInversionButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="qMX-2i-KY2"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="tu2-ee-UdF"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Dlz-OI-x3i">
|
||||
@@ -171,7 +171,7 @@
|
||||
<color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="clickPercentButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="sKA-Mn-mXq"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="eB9-ym-MPP"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="icb-C3-YOh">
|
||||
@@ -180,7 +180,7 @@
|
||||
<fontDescription key="fontDescription" type="system" weight="light" pointSize="34"/>
|
||||
<state key="normal" title="+"/>
|
||||
<connections>
|
||||
<action selector="clickPlusButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Jtc-RR-BM3"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="k8G-ba-JDK"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="JAI-XN-CDt">
|
||||
@@ -189,7 +189,7 @@
|
||||
<fontDescription key="fontDescription" type="system" weight="light" pointSize="42"/>
|
||||
<state key="normal" title="-"/>
|
||||
<connections>
|
||||
<action selector="clickMinusButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="F3p-G6-2vG"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="zte-8Z-2HO"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cuz-9a-BkO">
|
||||
@@ -198,7 +198,7 @@
|
||||
<fontDescription key="fontDescription" type="system" weight="light" pointSize="34"/>
|
||||
<state key="normal" title="×"/>
|
||||
<connections>
|
||||
<action selector="clickMultiplicationButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="QmT-uo-S8J"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="wqp-oo-6PZ"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="RA4-Vt-6OG">
|
||||
@@ -207,7 +207,7 @@
|
||||
<fontDescription key="fontDescription" type="system" weight="light" pointSize="34"/>
|
||||
<state key="normal" title="÷"/>
|
||||
<connections>
|
||||
<action selector="clickDivisionButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="JbS-w3-mTU"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="JKP-ji-JNS"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="K66-JV-tKt">
|
||||
@@ -216,7 +216,7 @@
|
||||
<fontDescription key="fontDescription" type="system" pointSize="22"/>
|
||||
<state key="normal" title="AC"/>
|
||||
<connections>
|
||||
<action selector="clickAllClearButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Ovk-lI-xeN"/>
|
||||
<action selector="clickButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="gEZ-Ma-WBZ"/>
|
||||
</connections>
|
||||
</button>
|
||||
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="hLf-ut-UVB">
|
||||
|
||||
@@ -9,56 +9,14 @@
|
||||
import Foundation
|
||||
|
||||
class Calculator {
|
||||
var formula: String = ""
|
||||
var result: String = ""
|
||||
var formula = ""
|
||||
var result = ""
|
||||
|
||||
func clickNumber(_ number: Int) {
|
||||
formula += String(number)
|
||||
result += String(number)
|
||||
}
|
||||
|
||||
func clickDot() {
|
||||
formula += "."
|
||||
result += "."
|
||||
}
|
||||
|
||||
func clickEquation() {
|
||||
formula += "="
|
||||
result += "="
|
||||
}
|
||||
|
||||
func clickPlus() {
|
||||
formula += "+"
|
||||
result += "+"
|
||||
}
|
||||
|
||||
func clickMinus() {
|
||||
formula += "-"
|
||||
result += "-"
|
||||
}
|
||||
|
||||
func clickMultiplication() {
|
||||
formula += "*"
|
||||
result += "*"
|
||||
}
|
||||
|
||||
func clickDivision() {
|
||||
formula += "/"
|
||||
result += "/"
|
||||
}
|
||||
|
||||
func clickPercent() {
|
||||
formula += "%"
|
||||
result += "%"
|
||||
}
|
||||
|
||||
func clickInversion() {
|
||||
formula += "+/-"
|
||||
result += "+/-"
|
||||
}
|
||||
|
||||
func clickAllClear() {
|
||||
func click(_ text: String) {
|
||||
if text == "AC" {
|
||||
formula = ""
|
||||
result = ""
|
||||
} else {
|
||||
formula += text
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,60 +15,14 @@ class ViewController: UIViewController {
|
||||
@IBOutlet weak var resultLabel: UILabel!
|
||||
@IBOutlet var numberButtons: [UIButton]!
|
||||
|
||||
var calculator: Calculator = Calculator()
|
||||
var calculator = Calculator()
|
||||
|
||||
@IBAction func clickNumberButton(_ sender: UIButton) {
|
||||
let number = numberButtons.firstIndex(of: sender)!
|
||||
calculator.clickNumber(number)
|
||||
updateFormulaAndResult()
|
||||
@IBAction func clickButton(_ sender: UIButton) {
|
||||
calculator.click(sender.currentTitle!)
|
||||
updateView()
|
||||
}
|
||||
|
||||
@IBAction func clickDotButton(_ sender: UIButton) {
|
||||
calculator.clickDot()
|
||||
updateFormulaAndResult()
|
||||
}
|
||||
|
||||
@IBAction func clickEquationButton(_ sender: UIButton) {
|
||||
calculator.clickEquation()
|
||||
updateFormulaAndResult()
|
||||
}
|
||||
|
||||
@IBAction func clickPlusButton(_ sender: UIButton) {
|
||||
calculator.clickPlus()
|
||||
updateFormulaAndResult()
|
||||
}
|
||||
|
||||
@IBAction func clickMinusButton(_ sender: UIButton) {
|
||||
calculator.clickMinus()
|
||||
updateFormulaAndResult()
|
||||
}
|
||||
|
||||
@IBAction func clickMultiplicationButton(_ sender: UIButton) {
|
||||
calculator.clickMultiplication()
|
||||
updateFormulaAndResult()
|
||||
}
|
||||
|
||||
@IBAction func clickDivisionButton(_ sender: UIButton) {
|
||||
calculator.clickDivision()
|
||||
updateFormulaAndResult()
|
||||
}
|
||||
|
||||
@IBAction func clickPercentButton(_ sender: UIButton) {
|
||||
calculator.clickPercent()
|
||||
updateFormulaAndResult()
|
||||
}
|
||||
|
||||
@IBAction func clickInversionButton(_ sender: UIButton) {
|
||||
calculator.clickInversion()
|
||||
updateFormulaAndResult()
|
||||
}
|
||||
|
||||
@IBAction func clickAllClearButton(_ sender: UIButton) {
|
||||
calculator.clickAllClear()
|
||||
updateFormulaAndResult()
|
||||
}
|
||||
|
||||
func updateFormulaAndResult() {
|
||||
func updateView() {
|
||||
formulaLabel.text = calculator.formula
|
||||
resultLabel.text = calculator.result
|
||||
}
|
||||
@@ -78,7 +32,7 @@ class ViewController: UIViewController {
|
||||
// Do any additional setup after loading the view.
|
||||
borderView.layer.borderWidth = 1
|
||||
borderView.layer.borderColor = #colorLiteral(red: 0.8374180198, green: 0.8374378085, blue: 0.8374271393, alpha: 1)
|
||||
updateFormulaAndResult()
|
||||
updateView()
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user