Skip to main content
Code Review

Return to Question

edited tags
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Tweeted twitter.com/#!/StackCodeReview/status/580582142222213121
Post Reopened by nhgrif, Mathieu Guindon, Phrancis, Simon Forsberg, 200_success
Added full code for Controller and Model
Source Link
import UIKit
class ViewController: UIViewController
{
 @IBOutlet weak var display: UILabel!
 var userIsInTheMiddleOfTypingANumber = false
 var brain = CalculatorBrain()
 var displayValue: Double
 {
 get
 {
 return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
 }
 
 set
 {
 display.text = "\(newValue)"
 userIsInTheMiddleOfTypingANumber = false
 }
 
 }
 
 @IBAction func appendDigit(sender: UIButton)
 {
 let digit = sender.currentTitle!
 if userIsInTheMiddleOfTypingANumber
 {
 display.text = display.text! + digit
 }
 else
 {
 display.text = digit
 userIsInTheMiddleOfTypingANumber = true
 }
 }
 
 @IBAction func enter()
 {
 userIsInTheMiddleOfTypingANumber = false
 if let result = brain.pushOperand(displayValue){ displayValue = result }
 else {displayValue = 0}
 }
 
 @IBAction func operate(sender: UIButton)
 {
 if userIsInTheMiddleOfTypingANumber{ enter() }
 if let operation = sender.currentTitle
 {
 if let result = brain.performOperation(operation) {displayValue = result}
 else {displayValue = 0}
 }
 }
 
 // Linked to the "C" button in the Storyboard
@IBAction func clear()
{
 userIsInTheMiddleOfTypingANumber = false
 brain.clear()
 displayValue = 0
.
.
. }
import Foundation
class CalculatorBrain
{
 
 private enum Op: Printable
 {
 case Operand(Double)
 case UnaryOperation(String, Double -> Double)
 case BinaryOperation(String, (Double, Double) -> Double)
 
 var description: String
 {
 get
 {
 switch self
 {
 case .Operand(let operand): return "\(operand)"
 case .UnaryOperation(let symbol, _): return symbol
 case .BinaryOperation(let symbol, _): return symbol
 }
 }
 }
 }
 
 private var opStack = [Op]()
 private var knownOps = [String: Op]()
 
 init()
 {
 knownOps×ばつ"] = Op.BinaryOperation(×ばつ", *)
 knownOps["÷"] = Op.BinaryOperation("÷") { 1ドル / 0ドル }
 knownOps["+"] = Op.BinaryOperation("+", +)
 knownOps["−"] = Op.BinaryOperation("−") { 1ドル - 0ドル }
 knownOps["√"] = Op.UnaryOperation("√", sqrt)
 }
 
 private func evaluate(ops: [Op]) -> (result: Double?, remainingOps: [Op])
 {
 if !ops.isEmpty {
 var remainingOps = ops
 let op = remainingOps.removeLast()
 switch op {
 
 case .Operand(let operand):
 return (operand, remainingOps)
 
 case .UnaryOperation(_, let operation):
 let operandEvaluation = evaluate(remainingOps)
 if let operand = operandEvaluation.result
 {
 return (operation(operand), operandEvaluation.remainingOps)
 }
 
 case .BinaryOperation(_, let operation):
 let op1Evaluation = evaluate(remainingOps)
 if let operand1 = op1Evaluation.result
 {
 let op2Evaluation = evaluate(op1Evaluation.remainingOps)
 if let operand2 = op2Evaluation.result
 {
 return (operation(operand1, operand2), op2Evaluation.remainingOps)
 }
 }
 }
 }
 return (nil, ops)
 }
 
 func evaluate() -> Double?
{
 let (result, remainder) = evaluate(opStack)
 println("\(opStack) = \(result) with \(remainder) left over")
 return result
}

func clearpushOperand(operand: Double) -> Double?
{
 opStack.append(Op.Operand(operand))
  return evaluate()
 }
 
 func performOperation(symbol: String) -> Double?
 {
 if let operation = [Op]knownOps[symbol] { opStack.append(operation) }
 return evaluate()
}
.
. func clear()
. {
 opStack = [Op]()
 evaluate()
 }
}
class ViewController: UIViewController
.
.
.
// Linked to the "C" button in the Storyboard
@IBAction func clear()
{
 userIsInTheMiddleOfTypingANumber = false
 brain.clear()
 displayValue = 0
.
.
.
class CalculatorBrain
.
.
.
func evaluate() -> Double?
{
 let (result, remainder) = evaluate(opStack)
 println("\(opStack) = \(result) with \(remainder) left over")
 return result
}
func clear()
{
 opStack = [Op]()
 evaluate()
}
.
.
.
import UIKit
class ViewController: UIViewController
{
 @IBOutlet weak var display: UILabel!
 var userIsInTheMiddleOfTypingANumber = false
 var brain = CalculatorBrain()
 var displayValue: Double
 {
 get
 {
 return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
 }
 
 set
 {
 display.text = "\(newValue)"
 userIsInTheMiddleOfTypingANumber = false
 }
 
 }
 
 @IBAction func appendDigit(sender: UIButton)
 {
 let digit = sender.currentTitle!
 if userIsInTheMiddleOfTypingANumber
 {
 display.text = display.text! + digit
 }
 else
 {
 display.text = digit
 userIsInTheMiddleOfTypingANumber = true
 }
 }
 
 @IBAction func enter()
 {
 userIsInTheMiddleOfTypingANumber = false
 if let result = brain.pushOperand(displayValue){ displayValue = result }
 else {displayValue = 0}
 }
 
 @IBAction func operate(sender: UIButton)
 {
 if userIsInTheMiddleOfTypingANumber{ enter() }
 if let operation = sender.currentTitle
 {
 if let result = brain.performOperation(operation) {displayValue = result}
 else {displayValue = 0}
 }
 }
 
 // Linked to the "C" button in the Storyboard
@IBAction func clear()
{
 userIsInTheMiddleOfTypingANumber = false
 brain.clear()
 displayValue = 0
 }
import Foundation
class CalculatorBrain
{
 
 private enum Op: Printable
 {
 case Operand(Double)
 case UnaryOperation(String, Double -> Double)
 case BinaryOperation(String, (Double, Double) -> Double)
 
 var description: String
 {
 get
 {
 switch self
 {
 case .Operand(let operand): return "\(operand)"
 case .UnaryOperation(let symbol, _): return symbol
 case .BinaryOperation(let symbol, _): return symbol
 }
 }
 }
 }
 
 private var opStack = [Op]()
 private var knownOps = [String: Op]()
 
 init()
 {
 knownOps×ばつ"] = Op.BinaryOperation(×ばつ", *)
 knownOps["÷"] = Op.BinaryOperation("÷") { 1ドル / 0ドル }
 knownOps["+"] = Op.BinaryOperation("+", +)
 knownOps["−"] = Op.BinaryOperation("−") { 1ドル - 0ドル }
 knownOps["√"] = Op.UnaryOperation("√", sqrt)
 }
 
 private func evaluate(ops: [Op]) -> (result: Double?, remainingOps: [Op])
 {
 if !ops.isEmpty {
 var remainingOps = ops
 let op = remainingOps.removeLast()
 switch op {
 
 case .Operand(let operand):
 return (operand, remainingOps)
 
 case .UnaryOperation(_, let operation):
 let operandEvaluation = evaluate(remainingOps)
 if let operand = operandEvaluation.result
 {
 return (operation(operand), operandEvaluation.remainingOps)
 }
 
 case .BinaryOperation(_, let operation):
 let op1Evaluation = evaluate(remainingOps)
 if let operand1 = op1Evaluation.result
 {
 let op2Evaluation = evaluate(op1Evaluation.remainingOps)
 if let operand2 = op2Evaluation.result
 {
 return (operation(operand1, operand2), op2Evaluation.remainingOps)
 }
 }
 }
 }
 return (nil, ops)
 }
 
 func evaluate() -> Double?
{
 let (result, remainder) = evaluate(opStack)
 println("\(opStack) = \(result) with \(remainder) left over")
 return result
}

func pushOperand(operand: Double) -> Double?
{
 opStack.append(Op.Operand(operand))
  return evaluate()
 }
 
 func performOperation(symbol: String) -> Double?
 {
 if let operation = knownOps[symbol] { opStack.append(operation) }
 return evaluate()
}

 func clear()
 {
 opStack = [Op]()
 evaluate()
 }
}
Post Closed as "Not suitable for this site" by nhgrif, 200_success
deleted 277 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

clear Clear function for Stanford iOS 8 Calculator application

I originally posted this question on stackexchange.com and someone told me I should be posting this kind of question here. I did not know this portion of stackexchange existed, so I apologize in advanced if there is any kind of cross posting issues.

Original Question:

I am following the Stanford iOS 8 lectures and I believe I have successfully added the "Clear" functionality to the application, but I don't know if I am doing it properly. My code is as follows:

And my log output is:

[8.0] = Optional(8.0) with [] left over
[8.0, 9.0] = Optional(9.0) with [8.0] left over
[8.0, 9.0, ×ばつ] = Optional(72.0) with [] left over
[8.0, 9.0, ×ばつ, 2.0] = Optional(2.0) with [8.0, 9.0, ×ばつ] left over
[8.0, 9.0, ×ばつ, 2.0, −] = Optional(70.0) with [] left over
[8.0, 9.0, ×ばつ, 2.0, −, 5.0] = Optional(5.0) with [8.0, 9.0, ×ばつ, 2.0, −] left over
[8.0, 9.0, ×ばつ, 2.0, −, 5.0, ÷] = Optional(14.0) with [] left over
[] = nil with [] left over
[8.0] = Optional(8.0) with [] left over
[8.0, 9.0] = Optional(9.0) with [8.0] left over
[8.0, 9.0, ×ばつ] = Optional(72.0) with [] left over
[8.0, 9.0, ×ばつ, 2.0] = Optional(2.0) with [8.0, 9.0, ×ばつ] left over
[8.0, 9.0, ×ばつ, 2.0, −] = Optional(70.0) with [] left over
[8.0, 9.0, ×ばつ, 2.0, −, 5.0] = Optional(5.0) with [8.0, 9.0, ×ばつ, 2.0, −] left over
[8.0, 9.0, ×ばつ, 2.0, −, 5.0, ÷] = Optional(14.0) with [] left over
[] = nil with [] left over

I am pretty sure the last line in the log output is correct. Basically I was just hoping someone with more iOS/MVC experience could tell me whether or not I implemented the "Clear"clear functionality correctly. If I didn't, how might I gotgo about fixing my code?

Thank you in advanced

clear function for Stanford iOS 8 Calculator application

I originally posted this question on stackexchange.com and someone told me I should be posting this kind of question here. I did not know this portion of stackexchange existed, so I apologize in advanced if there is any kind of cross posting issues.

Original Question:

I am following the Stanford iOS 8 lectures and I believe I have successfully added the "Clear" functionality to the application, but I don't know if I am doing it properly. My code is as follows:

And my log output is

[8.0] = Optional(8.0) with [] left over
[8.0, 9.0] = Optional(9.0) with [8.0] left over
[8.0, 9.0, ×ばつ] = Optional(72.0) with [] left over
[8.0, 9.0, ×ばつ, 2.0] = Optional(2.0) with [8.0, 9.0, ×ばつ] left over
[8.0, 9.0, ×ばつ, 2.0, −] = Optional(70.0) with [] left over
[8.0, 9.0, ×ばつ, 2.0, −, 5.0] = Optional(5.0) with [8.0, 9.0, ×ばつ, 2.0, −] left over
[8.0, 9.0, ×ばつ, 2.0, −, 5.0, ÷] = Optional(14.0) with [] left over
[] = nil with [] left over

I am pretty sure the last line in the log output is correct. Basically I was just hoping someone with more iOS/MVC experience could tell me whether or not I implemented the "Clear" functionality correctly. If I didn't, how might I got about fixing my code?

Thank you in advanced

Clear function for Stanford iOS 8 Calculator application

I am following the Stanford iOS 8 lectures and I believe I have successfully added the "Clear" functionality to the application, but I don't know if I am doing it properly.

And my log output is:

[8.0] = Optional(8.0) with [] left over
[8.0, 9.0] = Optional(9.0) with [8.0] left over
[8.0, 9.0, ×ばつ] = Optional(72.0) with [] left over
[8.0, 9.0, ×ばつ, 2.0] = Optional(2.0) with [8.0, 9.0, ×ばつ] left over
[8.0, 9.0, ×ばつ, 2.0, −] = Optional(70.0) with [] left over
[8.0, 9.0, ×ばつ, 2.0, −, 5.0] = Optional(5.0) with [8.0, 9.0, ×ばつ, 2.0, −] left over
[8.0, 9.0, ×ばつ, 2.0, −, 5.0, ÷] = Optional(14.0) with [] left over
[] = nil with [] left over

I am pretty sure the last line in the log output is correct. Basically I was just hoping someone with more iOS/MVC experience could tell me whether or not I implemented the clear functionality correctly. If I didn't, how might I go about fixing my code?

Source Link
Loading
default

AltStyle によって変換されたページ (->オリジナル) /