Skip to content

Instantly share code, notes, and snippets.

@justinmakaila
Created June 11, 2015 13:24
Show Gist options
  • Select an option

  • Save justinmakaila/604be55b80b0587bd469 to your computer and use it in GitHub Desktop.

Select an option

Save justinmakaila/604be55b80b0587bd469 to your computer and use it in GitHub Desktop.
// Consider a generic class where `lhs` and `rhs` can be of any numeric type
struct Operation: Printable {
let lhs: Float
let rhs: Float
let `operator`: Operator
var description: String {
return "\(lhs) \(`operator`.symbol) \(rhs)"
}
}
// Create an equation
let equation = "5 + 3 * 6 - ( 5 / 3 ) + 7"
// Create a MathSolver
let solver = MathSolver()
// Pass the equation to the `solve` method.
let steps = solver.solve(equation)
/**
The solve method outputs an array of `Operation`s:
3.0 * 6.0 = 18.0
5.0 + 18.0 = 23.0
5.0 / 3.0 = 1.66667
23.0 - 1.66667 = 21.3333
21.3333 + 7.0 = 28.3333
There are slight issues with "linking" operations, that is, establishing which number came from the previous step and how to explain it to students.
We could also change the output of `solve` to output a `Solution` object, which may look like this:
struct Solution {
var operations: [Operation]
var solution: Float
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment