Swift中的计算器

时间:2015-08-13 06:59:52

标签: ios swift cs193p

我对这个计算器有一些逻辑问题。 但我无法找到它,请帮我一个忙!

当我推动" ="在第一次执行后

它将使用相同的操作数执行两次

例如:

" 1" +" 2" =" 3"

" 3" +然后输出 - >" 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

    }
}

1 个答案:

答案 0 :(得分:0)

所有我能想象的是sessionInfo()可能的条件 R version 3.2.1 (2015-06-18) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1 locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] ggplot2_1.0.1 gWidgetsRGtk2_0.0-83 cairoDevice_2.22 [4] gWidgets_0.0-54 RGtk2_2.20.31 loaded via a namespace (and not attached): [1] Rcpp_0.12.0 digest_0.6.8 MASS_7.3-43 grid_3.2.1 [5] plyr_1.8.3 gtable_0.1.2 magrittr_1.5 scales_0.2.5 [9] stringi_0.5-5 reshape2_1.4.1 labeling_0.3 proto_0.3-10 [13] tools_3.2.1 stringr_1.0.0 munsell_0.4.2 colorspace_1.2-6 ,也就是说,执行calculate

时不会删除运算符