Question

In: Computer Science

Implement a calculator (not postfix notation) using Swift Programming Language(Swift 3, basic ios app). Please use...

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.  

Solutions

Expert Solution



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
}

}

}


Related Solutions

Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack • When an operand is read, push it on statck • When an operator is read i.e +, *. /, - – Pop two from the top of the stack and apply the operator and push the result on stack if there is one value instead of two display an error message • Keep repeating until an equal sign, = is read; pop from...
I need swift programming code and instruction of app Named Shortest path. Please explain at the...
I need swift programming code and instruction of app Named Shortest path. Please explain at the end of the code how to run the app on Xcode. Thanks
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm to sort the digits: Use Counting Sort or Bucket Sort. • Assume the numbers are maximum 4-digit numbers. • If using Counting Sort, you can see that your digit range is between 0 and 9 ([0…9]). • If using Bucket Sort, you will have ten buckets labeled from 0 to 9. Please add comments and explain every step carefully.
Write in C++ programming language Given the following postfix string, from left to right, use the...
Write in C++ programming language Given the following postfix string, from left to right, use the stack push operation to put items on the stack and the pop operation whenever an operator is encountered and to get two items and apply the operator to the operands then push back the result to evaluate the expression … rewrite the entire stack as it appears before each operator is applied. 12 3  / 5  + 32 2  3  ^  2  *  /  -
hot use blutooth on your app stimulation to connect to a device. xcode swift. please inclide...
hot use blutooth on your app stimulation to connect to a device. xcode swift. please inclide a code if possible.
Week 8 Assignment 4 Submission If you are using the Blackboard Mobile Learn iOS App, please...
Week 8 Assignment 4 Submission If you are using the Blackboard Mobile Learn iOS App, please click "View in Browser”. Click the link above to submit your assignment. Students, please view the "Submit a Clickable Rubric Assignment" in the Student Center. Instructors, training on how to grade is within the Instructor Center. Assignment 4: Win the Contract Due Week 8 and worth 120 points Imagine your small business produces tiny remote control aircraft capable of long sustained flights. You are...
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
Please Use C language to Make a calculator. Make sure calculator is able to take up...
Please Use C language to Make a calculator. Make sure calculator is able to take up to 5 values. Try to make calculator using simple coding As you can. Please create a simple calculator with only +, -,* and divide. It has to be able to enter any numbers (including decimals) and be able to do the following functions: +, -, divide and multiply. Please have the answers, always rounded to two decimal figures. The calculator will also have to...
Book - Introduction to Programming Using Visual Basic 11th Edition by David I. Schneider Programming Language...
Book - Introduction to Programming Using Visual Basic 11th Edition by David I. Schneider Programming Language - Visual Studio 2017 RESTAURANT MENU Write a program to place an order from the restaurant menu in Table 4.13. Use the form in Fig. 4.70, and write the program so that each group box is invisible and becomes visible only when its corresponding check box is checked. After the button is clicked, the cost of the meal should be calculated. (NOTE: The Checked...
a summary explaining the basic understanding of the following programming concepts using a language of python:...
a summary explaining the basic understanding of the following programming concepts using a language of python: •Variables •Arithmetic and Logical operations •Sequential coding (Structured programming •Decision structure (If statements) •Repetition structure •Functions with some coding demos inside visual studio code python IDE which can be sent as screenshot. preferably a typed summary please which can be written into powerpoint pleaseeeee ???
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT