2

Here is the code for my UIButton

@IBAction private func performOperation(sender: UIButton) {
    if userIsInTheMiddleOfTyping {
        brain.setOperand(displayValue)
        userIsInTheMiddleOfTyping = false
    }
    if let mathematicalSymbol = sender.currentTitle {
        brain.performOperation(mathematicalSymbol)
    }
    displayValue = brain.result
}

Here is my model or viewcontroller code

private var operations: Dictionary<String,Operation> = [
    "π":    Operation.Constant(M_PI),
    "e":    Operation.Constant(M_E),
    "√":    Operation.UnaryOperation(sqrt),
    "cos":  Operation.UnaryOperation(cos),
    "✕":    Operation.BinaryOperation({ $0 * $1 }),
    "÷":    Operation.BinaryOperation({ $0 / $1 }),
    "+":    Operation.BinaryOperation({ $0 + $1 }),
    "−":    Operation.BinaryOperation({ $0 - $1 }),
    "±":    Operation.UnaryOperation({ -$0 }),
    "=":    Operation.Equals,
    ".":    Operation.Period
]

private enum Operation {
    case Constant(Double)
    case UnaryOperation((Double) -> Double)
    case BinaryOperation((Double, Double) -> Double)
    case Equals
    case Period
}

func performOperation (symbol: String) {
    if let operation = operations[symbol] {
        switch operation {
        case .Constant(let associatedConstantValue):
            accumulator = associatedConstantValue
            break
        case .UnaryOperation(let associatedFunction):
            accumulator = associatedFunction(accumulator)
            break
        case .BinaryOperation(let associatedFunction):
            executePendingBinaryOperation()
            pending = PendingBinaryOperationInfo(binaryFunction: associatedFunction, firstOperand: accumulator)
            break
        case .Equals:
            executePendingBinaryOperation()
            break
        case .Period:
            displayTextContainsPeriod()
            break
        }
    }
}

private func displayTextContainsPeriod() -> Bool
{

}

I know to check if an existing period exists I need to check if the String contains a substring "." but I am not sure how to get the display text in my func displayTextContainsPeriod

software is fun
  • 7,286
  • 18
  • 71
  • 129

3 Answers3

3

You're taking the wrong approach. The . shouldn't be an operator. Your calculator brain should not be involved. This work should be done in your ViewController, not your model. It should operate like the digits and append the . character to the display string if the display doesn't already contain a ..

You'll want to think about the case when . is entered and the user is not in the middle of typing a number. You might want to start your display with 0. instead of just . for instance.

vacawama
  • 150,663
  • 30
  • 266
  • 294
  • You posted your comment under the wrong answer. The brain shouldn't be involved in the input of the number. It is just for calculating. You don't call the brain for every digit typed. Likewise, you wouldn't call the brain when a decimal point is entered. Just append the `.` character to the display string after making sure you don't already have one. – vacawama May 26 '16 at 19:42
2

Assuming that the display text is in a UITextField and that you built it in a storyboard (if not, update the question).

You need to add an outlet to the controller

 @IBOutlet var displayTextField: UITextField!

Then, connect it to the field in your storyboard.

In your code, you can refer to self.displayTextField.text to get the current text in the UITextField.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • Hmm I thought the model's job was to determine if the text is valid or not. So if someone pressed 1.0.0.0.0.0.0.0 the model would simply ignore all "." after the 1st "." – software is fun May 26 '16 at 18:32
  • what model are you talking about? – Lou Franco May 26 '16 at 19:22
  • What would the model be in a calculator app? – software is fun May 27 '16 at 12:14
  • You have to make it yourself. Assuming you make `CalcModel` and it has a way to validate text, you still need to get the text from the textfield and pass it in. So, you still need an outlet and you then call `calcModel.hasPeriod(self.displayTextField.text)` – Lou Franco May 27 '16 at 13:08
1

Here is my function touchDigit (thanks to @vacawama),maybe a bit messy. As a beginner, hope to get some useful advice. :)

 @IBAction private func touchDigit(sender: UIButton) {
    let digit = sender.currentTitle!
    if userIsInTheMiddleOfTyping{
        let textCurrentlyInDisplay = display.text!
        display.text = (digit == "." && textCurrentlyInDisplay.rangeOfString(".") != nil) ? textCurrentlyInDisplay : textCurrentlyInDisplay + digit

    }else{
        display.text = (digit == ".") ? "0." : digit
    }
    userIsInTheMiddleOfTyping = true
}
Halo Fool
  • 11
  • 2