【问题标题】:Calculator in SwiftSwift 中的计算器
【发布时间】:2015-08-13 06:59:52
【问题描述】:

我对这个计算器有一些逻辑问题。当我在第一次执行后按下“=”时,它将使用相同的操作数执行两次。

例如:

"1"+"2"="3"

“3”+“2”,然后在我按下另一个操作数之前输出->“5”。

这里是源代码:

https://drive.google.com/file/d/0B1a-AefbM9rOUTdyVmdJOWhNNnc/view?usp=sharing

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var display: UILabel!
    
    var MiddleTyping = false
    
    @IBAction func appandDigit(sender: UIButton) {
        let digit = sender.currentTitle!
        if MiddleTyping{
            display.text = display.text! + digit
        }else{
            display.text = digit
            MiddleTyping = true
        }
    }
    
    
    
    var operandStack = [Double]()
    var binaryoperation = [String]();
    var firstTimeInputNumber = true
    
    var displayValue: Double{
        get{
            return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
        }
        set{
            display.text = "\(newValue)"
            MiddleTyping = false
        }
    }
    @IBAction func Operate(sender: UIButton) {
        MiddleTyping = false
        var equalOperand = ""
        var Operation = ""
        if sender.currentTitle == "="{
             equalOperand = binaryoperation.last ?? ""
        }else{
              Operation = sender.currentTitle!
        }
        if operandStack.count == 1 && binaryoperation.count != 0{
            operandStack.append(displayValue)
        }
        if operandStack.count == 2{
            if equalOperand == binaryoperation.last ?? "" {
                switch equalOperand{
                case "×":
                    displayValue = operandStack[0] * operandStack[1]
                    operandStack[0] = displayValue
                case "÷":
                    displayValue = operandStack[0] / operandStack[1]
                    operandStack[0] = displayValue
                case "+":
                    displayValue = operandStack[0] + operandStack[1]
                    operandStack[0] = displayValue
                case "−":
                    displayValue = operandStack[0] - operandStack[1]
                    operandStack[0] = displayValue
                default: break
                }
            }else{
                let Operation = binaryoperation.last ?? "";
                switch Operation {
                case "×":
                    displayValue = operandStack[0] * operandStack[1]
                    operandStack[0] = displayValue
                    operandStack.removeLast()
                case "÷":
                    displayValue = operandStack[0] / operandStack[1]
                    operandStack[0] = displayValue
                    operandStack.removeLast()
                case "+":
                    displayValue = operandStack[0] + operandStack[1]
                    operandStack[0] = displayValue
                    operandStack.removeLast()
                case "−":
                    displayValue = operandStack[0] - operandStack[1]
                    operandStack[0] = displayValue
                    operandStack.removeLast()
                default: break
                }
            }
        }
        if displayValue != 0 {
            if firstTimeInputNumber{
                operandStack.append(displayValue)
            }
            firstTimeInputNumber = false
        }
        if Operation != ""{
            binaryoperation.append(Operation)
        }
        println("push op \(operandStack)")
        println("push op \(binaryoperation)")
    }
    @IBAction func ClearButton() {
        operandStack.removeAll()
        binaryoperation.removeAll()
        display.text = "0"
        firstTimeInputNumber = true
        MiddleTyping = false
        
    }
}

【问题讨论】:

  • 你在关注 stanford 的 cs193 吗?
  • 是的,但是我想创建一个立即执行的计算器
  • 计算器上的操作按钮重做操作数的动作。
  • 问题在于您处理“middleTyping”的方式。这些讲座完全是为 RPN 而不是 PN 计算器设计的。
  • 你的操作只需要自己操作,而不是后面的所有操作数。

标签: ios swift cs193p


【解决方案1】:

我所能想象的只是条件binaryoperation.count != 0 可能是 binaryoperation.count == 1,也就是你在计算的时候不要去掉操作符

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 2016-08-13
    • 2016-02-26
    • 1970-01-01
    • 2016-07-29
    • 2015-10-04
    • 1970-01-01
    相关资源
    最近更新 更多