In: Computer Science
Implement a calculator (not postfix notation) using Swift Programming Language(Swift 3, basic ios app). Please use MVC model(CalculatorBrain.swift can be the model and ViewController.swift can be the view). Also please post the sreenshot of the storyboard that you make.
Requirements:
Implement add subtract, multiply, divide, pi, sqrt, Euler’s natural number (e), co-sine and equals.
Ensure you have the ability to handle multiple operations in sequence
Implement the ability to enter floating point numbers into the display
Add 4 more buttons (ex. sine)
Handle multiple operations in a sequence.
Add a memory function to your calculator that stores and retrieves a number. Implement the following buttons at the top of the keyboard
MC = Memory clear sets memory to 0
MR – Memory recall uses the number in memory acting as it you typed that number in yourself
MS – Memory Store puts the number on display into memory
M+ – Memory takes the number on the display, adds it to the memory, and puts the result into memory.
Implement a clear (C) button. If the clear button is pressed once, it should take whatever was typed before the last enter and put it to 0. If the clear is entered twice, it should clear the stack.
Show the history of every operand and operation input by displaying it.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel!
@IBOutlet weak var history: UILabel!
var userIsInTheMiddleOfTypingANumber = false
let x = M_PI
@IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if userIsInTheMiddleOfTypingANumber {
display.text = display.text! + digit
} else {
display.text = digit
userIsInTheMiddleOfTypingANumber = true
}
}
@IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
switch operation
{
case "×": performOperation { $0 * $1 }
case "÷": performOperation { $1 / $0 }
case "+": performOperation { $0 + $1 }
case "−": performOperation { $1 - $0 }
case "√": performOperation { sqrt($0) }
case "sin": performOperation { sin($0) }
case "cos": performOperation { cos($0) }
default: break
}
}
func performOperation(operation: (Double, Double) ->
Double)
{
if operandStack.count >= 2
{
displayValue = operation(operandStack.removeLast(),
operandStack.removeLast())
enter()
}
}
func performOperation(operation: (Double) -> Double) {
if operandStack.count >= 1 {
displayValue = operation(operandStack.removeLast())
enter()
}
}
var operandStack = Array()
@IBAction func enter() {
userIsInTheMiddleOfTypingANumber = false
decimalIsPressed = false
operandStack.append(displayValue)
history.text = "\(displayValue)"
println("operandStack = \(operandStack)")
}
var decimalIsPressed = false
@IBAction func decimal() {
userIsInTheMiddleOfTypingANumber = correct
if decimalIsPressed == false {
display.text = display.text! + "."
decimalIsPressed = true
}
}
@IBAction func pi() {
userIsInTheMiddleOfTypingANumber = correct
if display.text != "0" {
enter()
display.text = "\(x)"
enter()
} else {
display.text = "\(x)"
enter()
}
}
@IBAction func clear()
{
userIsInTheMiddleOfTypingANumber = wrong
operandStack.removeAll()
display.text = "0"
enter()
}
var displayValue: Double
{
get {
return
NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
set
{
display.text = "\(newValue)"
userIsInTheMiddleOfTypingANumber = wrong
}
}
}