Question

Implement a scientific calculator using MVC constructs in Swift 3 Programming language(basic calculator IOS app) Requirements:...

Implement a scientific calculator using MVC constructs in Swift 3 Programming language(basic calculator IOS app)

Requirements:

It needs to be able to handle multiple operations in a sequence.

It should ONLY use the last operand if multiple operands are entered consecutively

Example: “5++-1=”is equivalent to “5-1=”

Proper error handling and self-correction.

“+-5” is equivalent to “negative 5”

“0004+32 ++ 0101 -- 04=” is equivalent to “4+32+101-4=”

Implement 3 more buttons: ¼ ½ and ¾ at the bottom of the keyboard. The fractions should concatenate the number just typed in such as 24.75. In the case where an enter is in between this would be a 24 and then a .75 separately put in the stack.

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. Put it to the left of the 0. 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.

Implement a percent (%) button.

Show the history of every operand and operation input by displaying it.  

Homework Answers

Answer #1

import UIKit
import Darwin
import Foundation

class ViewController: UIViewController
{

@IBOutlet weak var display: UILabel!
  
var Click:Bool = true

@IBAction func AddToDisplay(sender: UIButton)
{
let input = sender.currentTitle!
if(!Click)
{
display.text = display.text! + input
}
else
{
display.text = input
Click = false;
}
}
  
@IBAction func Clear()
{
display.text = "0"
Click = true
}
  
func isDigit(char:Character)->Bool
{
if char >= "0" && char <= "9"
{
return true
}
return false
}
func isDigit(char:String)->Bool
{
if char >= "0" && char <= "9"
{
return true
}
return false
}
func isOperator(char:String)->Bool
{
if char == "+" || char == "-" || char == "*" || char == "/"
{
return true
}
return false
}

func check(char:String, char2:String)->Bool
{
if char == "+" || char == "-"
{
if char2 == "+" || char2 == "-"
{
return true
}
}
else if char == "*" || char == "/"
{
if char2 == "*" || char2 == "/" || char2 == "+" || char2 == "-"
{
return true
}
}
return false
}
  
func doMath (operation: String, first:String, second:String)->Double
{
var tree = BSTree()
tree.insert(operation)
tree.insert(first)
tree.insert(second)
return tree.evaluateBSTree(tree.getRoot())
}

var negNums = Array<Boolean>()
  
func getAsStack(str:String)->Array<String>
{
negNums.removeAll(keepCapacity: true)
var numStack = Array<Double>()
var results = Array<String>()
var numDecimals = 0
var tokenInt = ""
var tokenOperand:Character = " "
var firstNum = true

var alreadyNegativeDecimal = false
for char in display.text!
{
if isDigit(char) || char == "."
{
if tokenOperand != " "
{
results.append(String(tokenOperand))
negNums.append(0)
}
tokenOperand = " "
tokenInt.append(char)
firstNum = false
}
else if char == "*" || char == "/" || char == "+" || char == "-"
{
if ((tokenOperand != " " || firstNum) && char == "-")
{
if !firstNum
{
results.append(String(tokenOperand))
negNums.append(0)
}
negNums.append(1)
tokenOperand = " "
alreadyNegativeDecimal = true
continue
}
else if(tokenInt[tokenInt.startIndex] == "." && !alreadyNegativeDecimal)
{
tokenInt = String(tokenInt)
tokenInt = "0" + tokenInt
results.append(tokenInt)
negNums.append(0)
}
else if tokenInt[tokenInt.startIndex] == "."
{
tokenInt = String(tokenInt)
tokenInt = "0" + tokenInt
results.append(tokenInt)
alreadyNegativeDecimal = false
}
else
{
results.append(String(tokenInt))
if alreadyNegativeDecimal
{

}
else
{
negNums.append(0)
}
}
tokenInt = ""
tokenOperand = char
firstNum = false
}
}
if tokenInt.utf16Count > 0
{
print("the tokenInt is")
println(tokenInt)
if(tokenInt[tokenInt.startIndex] == ".")
{
tokenInt = String(tokenInt)
tokenInt = "0" + tokenInt
results.append(tokenInt)
if(negNums.count == 0 || negNums.last == 0)
{
negNums.append(0)
}
  
}
else
{
results.append(String(tokenInt))
if (tokenOperand != " ")
{
negNums.append(0)
}
}

}
if tokenOperand != " "
{
results.append(String(tokenOperand))
}
println("RESULTS ARE")
for i in results
{
println(i)
}
println("negatives are")
for i in negNums
{
println(i)
}
return results
}
  
var negNum2 = Array<Boolean>()

func toPostFix(str:String)->Array<String>
{
negNum2.removeAll(keepCapacity: true)
var negNumStack = Array<Boolean>()
var OperatorS = Array<String>()
var postfix = Array<String>()
var numStack = Array<Double>()
var results = getAsStack(str)
let size = results.count
print("the size is ")
println(size)
for (var i = 0; i < size; i++)
{
let argument:String = results.removeAtIndex(0)
if(isDigit(argument))
{
postfix.append(argument)
negNum2.append(negNums.removeAtIndex(0))
}
else if (isOperator(argument))
{
if(OperatorS.count == 0)
{
OperatorS.append(argument)
negNumStack.append(negNums.removeAtIndex(0))
}
  
else
{
for j in OperatorS.reverse()
{
  
  
if check(j, char2: argument)
{
if OperatorS.count != 0
{
postfix.append(j)
negNum2.append(negNumStack.removeLast())
OperatorS.removeLast()
}
else
{
  
break
}
}
}
OperatorS.append(argument)
negNumStack.append(negNums.removeAtIndex(0))
}

}
}
for i in OperatorS.reverse()
{
postfix.append(i)
negNum2.append(negNumStack.removeAtIndex(countElements(negNumStack) - 1))
}
println("Neg nums are")
for i in negNum2{
println(i)
}
println("numbers are ")
for i in postfix{
println(i)
}
return postfix
}
  
func evaluate (postfix:Array<String>)
{
var negNum3 = Array<Boolean>()
var numStack = Array<String>()
var OperatorS = Array<String>()
for i in postfix{
if(isDigit(i))
{
numStack.append(i)
negNum3.append(negNum2.removeAtIndex(0))
}
else
{
let second = numStack.last
numStack.removeLast()
let first = numStack.last
numStack.removeLast()
let signsecond = negNum3.last
negNum3.removeLast()
let signfirst = negNum3.last
negNum3.removeLast()
var value:Double
switch (i)
{
case "+":

if(signsecond == 1 && signsecond == 1)
{
negNum3.append(1)
value = doMath(i, first: first!, second: second!)
}

else if (signsecond == 1 && signsecond == 0)
{
value = doMath("-", first: second!, second: first!)
  
if (value < 0)
{
negNum3.append(1)
}
else
{
negNum3.append(0)
}
}
  
else if (signsecond == 0 && signsecond == 1)
{
value = doMath(i, first: first!, second: second!)
if (value < 0)
{
negNum3.append(1)
}
else
{
negNum3.append(0)
}
}
else{
value = doMath(i, first: first!, second: second!)
negNum3.append(0)
}
break
case "-":

if(signsecond == 1 && signsecond == 1)
{
negNum3.append(1)
value = doMath(i, first: second!, second: first!)
}

else if (signsecond == 1 && signsecond == 0)
{
value = doMath("+", first: first!, second: second!)

negNum3.append(1)
}
  
else if (signsecond == 0 && signsecond == 1)
{
value = doMath("+", first: first!, second: second!)
negNum3.append(0)
}
else{
value = doMath(i, first: first!, second: second!)
negNum3.append(0)
}
break
case "*":

if(signsecond == 1 && signsecond == 1)
{
negNum3.append(0)
value = doMath(i, first: second!, second: first!)
}
  
else if (signsecond == 1 || signsecond == 1)
{
negNum3.append(1)
value = doMath(i, first: second!, second: first!)
}

else{
value = doMath(i, first: first!, second: second!)
negNum3.append(0)
}
break
case "/":

if(signsecond == 1 && signsecond == 1)
{
negNum3.append(0)
value = doMath(i, first: second!, second: first!)
}

else if (signsecond == 1 || signsecond == 1)
{
negNum3.append(1)
value = doMath(i, first: second!, second: first!)
}

else{
value = doMath(i, first: first!, second: second!)
negNum3.append(0)
}
break
default:
value = 0
break
}
let myString = NSString(format: "%.7f", value)
numStack.append(myString)
}
}
if(negNum3.first! == 1)
{
display.text! = "-"
}
else{
display.text! = ""
}
display.text! += numStack.first!
Click = true
}
  
@IBAction func Root() {
var numbers = 0
var operands = 0
var decimals = 0
Compute()
println(display.text!)
for char in display.text!{
if (char == "+" || char == "-" || char == "/" || char == "*")
{
operands++
}
else if(char == ".")
{
decimals++
}
}
if (operands > 0 || decimals > 1)
{
display.text! = "ERROR"
Click = true
return
}
display.text = "\(sqrt(NSNumberFormatter().numberFromString(display.text!)!.doubleValue))"
}
  
@IBAction func Compute()
{
evaluate(toPostFix(display.text!))
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT