gpt4 book ai didi

ios - Swift 计算器运算符问题

转载 作者:行者123 更新时间:2023-11-29 01:09:54 25 4
gpt4 key购买 nike

大家好,感谢您访问我的页面。我目前正在学习为我的类(class)创建一个计算器,但注意到在我输入时出现错误,例如:

5 + 5 = 10//这是正确的

5 + 2 * 3 + 5 = 26//还没有问题

5 + 2 = 7 +(将添加最后一个带有数字的运算符。这意味着在我选择要添加的数字之前屏幕将显示 9。)

任何关于我应该如何思考以解决这个问题的建议将不胜感激。

import UIKit
import AVFoundation

class ViewController: UIViewController {

enum Operation: String {
case Divide = "/"
case Multiple = "*"
case Subtract = "-"
case Add = "+"
case Empty = "Empty"
}

@IBOutlet weak var outputLbl: UILabel!

// This is initialized once the app loads.

var btnSound: AVAudioPlayer!

var runningNumber = ""
var leftValStr = ""
var rightValStr = ""
var currentOperation: Operation = Operation.Empty
var result = ""


override func viewDidLoad() {
super.viewDidLoad()

let path = NSBundle.mainBundle().pathForResource("btn", ofType: "wav")
let soundUrl = NSURL(fileURLWithPath: path!)

do {
try btnSound = AVAudioPlayer(contentsOfURL: soundUrl)
btnSound.prepareToPlay()
} catch let err as NSError {
print(err.debugDescription)
}
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func numberPressed(btn: UIButton) {
playSound()

// Each button is assigned to a tag from 0-9.
// When button is pressed it is stored to btn.
runningNumber += "\(btn.tag)"
outputLbl.text = runningNumber
}

@IBAction func onDividePressed(sender: AnyObject) {
processOperation(Operation.Divide)
}

@IBAction func onMultiplyPressed(sender: AnyObject) {
processOperation(Operation.Multiple)
}

@IBAction func onSubtractPressed(sender: AnyObject) {
processOperation(Operation.Subtract)
}

@IBAction func onAddPressed(sender: AnyObject) {
processOperation(Operation.Add)
}

@IBAction func onEqualPressed(sender: AnyObject) {
processOperation(currentOperation)
}

@IBAction func onClearPressed(sender: AnyObject) {
clearCalcuator()
}



// op is what is storing the Operator. It could have been named anything
func processOperation(op: Operation) {
playSound()

if currentOperation != Operation.Empty {
// Run some math.

// A user selected an operator, but then selected another
// operator without first selecting a number
if runningNumber != "" {
rightValStr = runningNumber
runningNumber = ""

}

if currentOperation == Operation.Multiple {
result = "\(Double(leftValStr)! * Double(rightValStr)!)"
} else if currentOperation == Operation.Divide {
result = "\(Double(leftValStr)! / Double(rightValStr)!)"
} else if currentOperation == Operation.Subtract {
result = "\(Double(leftValStr)! - Double(rightValStr)!)"
} else if currentOperation == Operation.Add {
result = "\(Double(leftValStr)! + Double(rightValStr)!)"
}

leftValStr = result
outputLbl.text = result

currentOperation = op

} else {
// This is the first time an Operator has been pressed.

leftValStr = runningNumber
runningNumber = ""
currentOperation = op
}
}

func clearCalcuator() {
playSound()

if outputLbl.text != "0" {
runningNumber = ""
leftValStr = ""
rightValStr = ""
currentOperation = Operation.Empty

}

outputLbl.text = "0"
}


// Function was created to keep the code DRY.
// Now can use playSound() on top. Exp: Line 51
func playSound() {
if btnSound.playing {
btnSound.stop()
}

btnSound.play()
}
}

我仍然是 SWIFT 的初学者,但希望通过向更了解 SWIFT 的人学习来提高水平。如果您对我当前的代码有任何关于如何改进的建议,欢迎所有建议。再次感谢你。

最佳答案

您描述的行为是 =@IBAction 正在传递 currentOperation< 的 Operation 的结果,这意味着如果最后一个操作是 +,则意味着 = 按钮的行为就像再次点击 + .这就是为什么您会看到自己的行为。

为了解决这个问题,您需要确保当您按下 = 时,processOperation 会将 currentOperation 设置为其他内容,以便当您下一次调用 processOperation,不再执行进一步的计算。为此,您需要做的就是为 = 添加一个 caseOperation:

enum Operation: String {
case Divide = "/"
case Multiple = "*"
case Subtract = "-"
case Add = "+"
case Equal = "="
case Empty = "Empty"
}

然后将 =@IBAction 更改为仅传递此 Operation 而不是 currentOperation:

@IBAction func onEqualPressed(sender: AnyObject) {
processOperation(.Equal)
}

因此,当您调用 processOperation 时,这将执行 currentOperation,然后当它完成时,它将 currentOperation 重置为 Operation.Equal,下一次调用 processOperation 时,它不会重复任何基于 .Equal 的进一步计算。

关于ios - Swift 计算器运算符问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35880051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com