您好我是初学者,我正在构建一个RPN计算器。 我的所有操作都在一个名为calcengine的独立视图控制器中完成。 我有一些AC的代码,我有两个问题:
@IBAction func AllClear(sender: UIButton) {
userHasStartedTyping = false
labelDisplay.text = "\(0)"
self.calcEngine!.operandStack.removeAll()
}
以下是viewcontroller中计算的代码:
@IBAction func operation(sender: UIButton) {
let operation = sender.currentTitle!
if userHasStartedTyping {
Enter()
}
self.displayValue = (self.calcEngine?.operate(operation))!
Enter()
}
和calcengine中的计算代码:
class CalculatorEngine: NSObject
{
var operandStack = Array<Double>() //array
func updateStackWithValue(value: Double)
{ self.operandStack.append(value) }
func operate(operation: String) ->Double
{ switch operation
{
case "×":
if operandStack.count >= 2 {
return self.operandStack.removeLast() * self.operandStack.removeLast()
}
case "÷":
if operandStack.count >= 2 {
return self.operandStack.removeFirst() / self.operandStack.removeLast()
}
case "+":
if operandStack.count >= 2 {
return self.operandStack.removeLast() + self.operandStack.removeLast()
}
case "−":
if operandStack.count >= 2 {
return self.operandStack.removeFirst() - self.operandStack.removeLast()
}
default:break
}
return 0.0
}
}
答案 0 :(得分:0)
一个典型的&#34; Clear&#34;计算器上的按钮仅清除用户输入的数字。在你的情况下,这将类似于AllClear(),但没有清空你的RPN堆栈:
@IBAction func Clear(sender: UIButton)
{
userHasStartedTyping = false
labelDisplay.text = "\(0)"
}