GraphingCalculator: Assignment 3, Stanford University, CS193P, Winter 2015

The Graphing Calculator project is a continuation of the previous 2 assignments from the Stanford University CS193P course Developing iOS 8 Apps with Swift, available for free on iTunes. This is the third and final assignment in the Calculator series.

Full source code available here at Github repository

For the other 2 projects, please see:
CalculatorBrain: Assignment 2, Stanford University Winter 2015 (iOS)
Calculator: Assignment 1, Stanford University Winter 2015 (iOS)

Graphing-Calculator_cs193p_Stanford-University

This project provides graphing features in addition to the regular calculator functions. Graphs can be plotted by entering expressions where M is the independent variable. Please see examples below.

To plot M, enter:

M, Graph

To plot sin(M), enter:

M, Sin, Graph

To plot M x cos(M), enter:

M, M, cos, x, Graph

Some interesting notes

  • The CalculatorBrain is reused to calculate the values of M for graphing
  • Plot values are set in the GraphingViewController as the data source of GraphingView
  • To fix the sluggish performance when zoomed out and panned, only the number of plots required for the screen width are calculated
  • Graph scale and origin is stored in NSUserDefaults
  • Upon device rotation, the graph is centered and adjusted slightly with the width and height change ratio

Video demo

Some Interesting Swift Tidbits

Some interesting Swift tidbits with practical code examples. Implications: use them in your projects for more readable and maintainable code.

Declaring multiple values in a single line
Multiple variables or constants can be declared in a single line using commas to separate them:

var red = 141.0, green = 185.0, blue = 230.0, alpha = 1.0

 
They can also be declared in a single line by adding the type annotation to the final variable:

var red, green, blue, alpha: Double

 
Large number readability
For large numbers, we can use underscore for better readability:

let mapRadius = 100_000

 
Using typealias for readable code
Using typealias for more readable code. For example, var program contains AnyObject, the primary purpose of creating var program is to use it as a property list. So instead of declaring:

var program: AnyObject?

 
We can typealias PropertyList = AnyObject, and use PropertyList for the var program type.

typealias PropertyList = AnyObject
var program: PropertyList?

 
Defining optional variables with nil value
When defining an unknown optional variable it doesn’t need to be explicitly set to nil. Swift automatically sets the variable to nil. For example:

var error: String? = nil // Not needed
var error: String?       // Swift automatically sets it to nil

 
Multiple optional bindings for concise and legible code
Multiple optional bindings help avoid many layers of hard to read nested code. For example,

The pyramid of nested optional bindings (hard to read):

if let firstNumber = Int("123") {
    if let secondNumber = Int("456") {
        if firstNumber < secondNumber {
            print("\(firstNumber) < \(secondNumber)")
        }
    }
}
// prints "123 < 456"

 
Multiple optional bindings (elegant):

if let firstNumber = Int("123"), secondNumber = Int("456") where firstNumber < secondNumber {
    print("\(firstNumber) < \(secondNumber)")
}
// prints "123 < 456"

 
Nil coalescing operator
Helps provide a default value if the evaluation is nil. For example,

tipPercentageForRestaurant is nil, so tipPercentage is 0.0:

var tipPercentageForRestaurant: Double?
let tipPercentage = tipPercentageForRestaurant ?? 0
print(tipPercentage)
// prints 0.0

tipPercentageForRestaurant is 0.15, so tipPercentage is 0.15

var tipPercentageForRestaurant: Double?
tipPercentageForRestaurant = 0.15
let tipPercentage = tipPercentageForRestaurant ?? 0
print(tipPercentage)
// prints 0.15

 

CalculatorBrain: Assignment 2, Stanford University Winter 2015 (iOS)

CalculatorBrainThis project CalculatorBrain is a continuation of the previous project Calculator: Assignment 1, Stanford University Winter 2015 (iOS)

Source code available at Github

The ViewController code is now more lean and better organized since all calculation related logic has been moved to the model.

The project covers all the required tasks and most extra credit tasks.

In addition to the features listed in the previous project Calculator, this assignment provides:

  • A separate model for all calculations
  • Additional scientific functions
  • Memory functions
  • An Undo button (which previously functioned as the Backspace button)
  • Replaces the previous History function with a better implementation
  • Provides error messages

Two items were not implemented as described in the Extra Credit Hints section in the project specifications document:

  • The error messages come from the model with full error text instead of error codes for the ViewController to translate
  • Error handling is not implemented by associating any value (a function) with UnaryOperations and BinaryOperations

Video Demo: