我试图让我的计算器功能看起来更有条理。我决定创建一个完成所有功能的计算器类。目前这不起作用,我唯一的问题是每次按下一个数字时我都会创建一个类的新实例,这是因为我不知道接下来要按什么数字所以我不能在我的构造函数中定义LBLOutput直到它传给了我。这是我的两个文件
问题出在我调用cb.functions的地方。目前它们可以正常工作,但由于每次都会声明一个新类,因此一次只能在屏幕上显示一个数字。
这是制作计算器的可行方法吗?我需要从头开始吗?
//
// calculatorBrain.swift
// SalvageAttempt
//
// Created by Kevin Maldjian on 3/13/17.
// Copyright © 2017 Kevin Maldjian. All rights reserved.
//
import Foundation
import UIKit
class calculatorBrain
{
var num1 = ""
var num2 = ""
var finalString = ""
var isFirstNumber = true
var safeToAdd = true
var hasFinalString = false
var isClear = true
var hasDecimal = false
var doubleChecker = 0
var hasSecondDecimal = false
var LBLOutput = UILabel()
var sender = UIButton()
init(LBLOutput : UILabel, sender : UIButton) {
self.num1 = " "
self.num2 = " "
self.finalString = ""
self.isFirstNumber = true
self.safeToAdd = true
self.hasFinalString = false
self.isClear = true
self.hasDecimal = false
self.doubleChecker = 0
self.hasSecondDecimal = false
self.LBLOutput = LBLOutput
self.sender = sender
}
func btrNumberClick() {
if isClear {
LBLOutput.text = ""
isClear = false
}
let currentText = LBLOutput.text!
let textLabel = sender.titleLabel?.text
if let text = textLabel {
switch text {
case "+", "x", "/", "-","%":
if hasFinalString {
return
}
finalString = text
isFirstNumber = false
hasFinalString = true
safeToAdd = true
LBLOutput.text = "\(currentText) \(finalString) "
break
case "=":
if !hasFinalString
{break}
isFirstNumber = true
hasFinalString = false
isClear = true
var result = calculate()
if result.truncatingRemainder(dividingBy:1) == 0
{
result = (result)
}
LBLOutput.text = "\(result)"
num1 = "\(result)"
safeToAdd = false
hasSecondDecimal = false
hasDecimal = false
break
case ".":
if !safeToAdd{
num1 = ""
LBLOutput.text = ""
num1 = "\(num1)\(text)"
LBLOutput.text = LBLOutput.text! + num1
safeToAdd = true
} else if isFirstNumber{
if hasDecimal
{break}
LBLOutput.text = ""
num1 = "\(num1)\(text)"
LBLOutput.text = LBLOutput.text! + num1
hasDecimal = true
}else{
if hasSecondDecimal
{break}
num2 = "\(num2)\(text)"
LBLOutput.text = "\(currentText)\(text)"
hasSecondDecimal = true
}
break
default:
if !safeToAdd{
num1 = ""
LBLOutput.text = ""
num1 = "\(num1)\(text)"
LBLOutput.text = LBLOutput.text! + num1
safeToAdd = true
} else if isFirstNumber{
LBLOutput.text = ""
num1 = "\(num1)\(text)"
LBLOutput.text = LBLOutput.text! + num1
}else{ num2 = "\(num2)\(text)"
LBLOutput.text = "\(currentText)\(text)"
}
break;
}
}
}
func goNegative(){
var negativeChecker = Double(num1)!
if negativeChecker > 0
{ num1 = ("-" + num1)
LBLOutput.text = num1
}else{
negativeChecker = (-2 * negativeChecker) + negativeChecker
num1 = String(negativeChecker)
LBLOutput.text = String(negativeChecker)
}
}
func squareRoot() {
let squareRootX = sqrt(Double(LBLOutput.text!)!)
num1 = String(squareRootX)
LBLOutput.text = String(squareRootX)
}
@IBAction func clearTheLabel() {
num1 = ""
num2 = ""
LBLOutput.text = "0"
isClear = true
}
func calculate() -> Double {
let firstNumber = Double(self.num1)!
let secondNumber = Double(self.num2)!
num1 = ""
num2 = ""
self.isClear = false
switch finalString {
case "+":
return firstNumber + secondNumber
case "-":
return firstNumber - secondNumber
case "x":
return firstNumber * secondNumber
case "/":
return firstNumber / secondNumber
case "%":
return (firstNumber * 100) / secondNumber
default:
return 0
}
}
}
主要功能在这里完成
Imports <xmlns="http://g.h.i">
答案 0 :(得分:0)
将所有函数移出此类名为“calculatorBrain”的函数。只需将它们公开,或者打开一个单独的目标(如框架)。例如:
class MyVC:UIViewController {
let a:Float = 1.0
let b:Float = 1.0
let currentSum = sum(a,b)
print(currentSum) // displays 2.0
}
在另一个文件中:
public sum(var1:Float, var2:Float) {
return var1+var2
}
没有更多。再次,从类中删除所有内容,您将不需要实例。