[F] Fix MathExpressions.swift indentation and add comments
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
//CREDITS: user:2538939 on Stack Exchange
|
// CREDITS: user:2538939 on Stack Exchange
|
||||||
enum MathElement : CustomStringConvertible {
|
enum MathElement : CustomStringConvertible {
|
||||||
case Integer(value: Int)
|
case Integer(value: Int)
|
||||||
case Percentage(value: Int)
|
case Percentage(value: Int)
|
||||||
@@ -46,16 +46,16 @@ enum MathOperator : String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//CREDITS: user:2538939 on Stack Exchange
|
// CREDITS: user:2538939 on Stack Exchange
|
||||||
class MathExpression : CustomStringConvertible {
|
class MathExpression : CustomStringConvertible {
|
||||||
var lhs: MathElement
|
var lhs: MathElement
|
||||||
var rhs: MathElement
|
var rhs: MathElement
|
||||||
var `operator`: MathOperator
|
var op: MathOperator
|
||||||
|
|
||||||
init(lhs: MathElement, rhs: MathElement, operator: MathOperator) {
|
init(lhs: MathElement, rhs: MathElement, op: MathOperator) {
|
||||||
self.lhs = lhs
|
self.lhs = lhs
|
||||||
self.rhs = rhs
|
self.rhs = rhs
|
||||||
self.operator = `operator`
|
self.op = op
|
||||||
}
|
}
|
||||||
|
|
||||||
var description: String {
|
var description: String {
|
||||||
@@ -73,11 +73,11 @@ class MathExpression : CustomStringConvertible {
|
|||||||
rightString = rhs.description
|
rightString = rhs.description
|
||||||
}
|
}
|
||||||
|
|
||||||
return "\(leftString) \(self.operator.rawValue) \(rightString)"
|
return "\(leftString) \(self.op.rawValue) \(rightString)"
|
||||||
}
|
}
|
||||||
|
|
||||||
var result : Int? {
|
var result : Int? {
|
||||||
let format = "\(lhs.nsExpressionFormatString) \(`operator`.rawValue) \(rhs.nsExpressionFormatString)"
|
let format = "\(lhs.nsExpressionFormatString) \(op.rawValue) \(rhs.nsExpressionFormatString)"
|
||||||
let expr = NSExpression(format: format)
|
let expr = NSExpression(format: format)
|
||||||
return expr.expressionValue(with: nil, context: nil) as? Int
|
return expr.expressionValue(with: nil, context: nil) as? Int
|
||||||
}
|
}
|
||||||
@@ -86,49 +86,55 @@ class MathExpression : CustomStringConvertible {
|
|||||||
let lhs = MathElement.Integer(value: Int(arc4random_uniform(10)))
|
let lhs = MathElement.Integer(value: Int(arc4random_uniform(10)))
|
||||||
let rhs = MathElement.Integer(value: Int(arc4random_uniform(10)))
|
let rhs = MathElement.Integer(value: Int(arc4random_uniform(10)))
|
||||||
|
|
||||||
return MathExpression(lhs: lhs, rhs: rhs, operator: .random())
|
return MathExpression(lhs: lhs, rhs: rhs, op: .random())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//WARNING: This code is ugly, and probably out of place...but it works!
|
/**
|
||||||
// Simple Problem - 2 expressions
|
Generate simple problem - 2 expressions
|
||||||
class AlgProb2 : MathExpression{
|
*/
|
||||||
let a = MathExpression.random()
|
class AlgProb2 : MathExpression {
|
||||||
let b = MathExpression.random()
|
let a = MathExpression.random()
|
||||||
|
let b = MathExpression.random()
|
||||||
|
|
||||||
func getProblem() -> String {
|
func getProblem() -> String {
|
||||||
let problem = ("\(a) + \(b)")
|
return "\(a) + \(b)"
|
||||||
return problem
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAnswer() -> String {
|
func getAnswer() -> String {
|
||||||
let answer = "\(a.result! + b.result!)"
|
return "\(a.result! + b.result!)"
|
||||||
return answer
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Simple Problem - 3 expressions
|
|
||||||
class AlgProb3 : MathExpression{
|
/**
|
||||||
let a = MathExpression.random()
|
Generate simple problem - 3 expressions
|
||||||
let b = MathExpression.random()
|
*/
|
||||||
let c = MathExpression.random()
|
class AlgProb3 : MathExpression {
|
||||||
|
let a = MathExpression.random()
|
||||||
|
let b = MathExpression.random()
|
||||||
|
let c = MathExpression.random()
|
||||||
|
|
||||||
func getProblem() -> String {
|
func getProblem() -> String {
|
||||||
let problem = ("\(a) + \(b) + \(c)")
|
return "\(a) + \(b) + \(c)"
|
||||||
return problem
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAnswer() -> String {
|
func getAnswer() -> String {
|
||||||
let answer = "\(a.result! + b.result! + c.result!)"
|
return "\(a.result! + b.result! + c.result!)"
|
||||||
return answer
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class QuadraticProb{
|
/**
|
||||||
//generates the roots
|
Generate quadratic factorization problem
|
||||||
|
*/
|
||||||
|
class QuadraticProb {
|
||||||
|
// Generates the roots
|
||||||
let root1 = Int.random(in: 1...10)
|
let root1 = Int.random(in: 1...10)
|
||||||
let root2 = Int.random(in: 1...10)
|
let root2 = Int.random(in: 1...10)
|
||||||
|
|
||||||
|
/**
|
||||||
func getProblem() -> String{
|
Generate problem description
|
||||||
|
*/
|
||||||
|
func getProblem() -> String {
|
||||||
//a is 1
|
//a is 1
|
||||||
let b = root1 + root2 //bx
|
let b = root1 + root2 //bx
|
||||||
let c = root1 * root2 //x
|
let c = root1 * root2 //x
|
||||||
@@ -136,15 +142,15 @@ class QuadraticProb{
|
|||||||
return "x^2 + \(b) + \(c)"
|
return "x^2 + \(b) + \(c)"
|
||||||
}
|
}
|
||||||
|
|
||||||
//finds the roots of the quadratic **NOTE**: the return type is [Int], not a String
|
/**
|
||||||
func getAnswer() -> [Int]{
|
Finds the roots of the quadratic **NOTE**: the return type is [Int], not a String
|
||||||
let roots = [root1, root2]
|
*/
|
||||||
return roots
|
func getAnswer() -> [Int] {
|
||||||
|
return [root1, root2]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RPS {
|
class RPS {
|
||||||
|
|
||||||
//@IBOutlet weak var resultsLabel: UILabel!
|
//@IBOutlet weak var resultsLabel: UILabel!
|
||||||
|
|
||||||
enum Choice: String {
|
enum Choice: String {
|
||||||
@@ -157,7 +163,8 @@ class RPS {
|
|||||||
return choices[Int.random(in: 0...2)]
|
return choices[Int.random(in: 0...2)]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
|
/*
|
||||||
@IBAction func rock(_ sender: UIButton) {
|
@IBAction func rock(_ sender: UIButton) {
|
||||||
let computerChoice = Choice.randomComputerChoice()
|
let computerChoice = Choice.randomComputerChoice()
|
||||||
resultsLabel.text = playRPS(you: .rock, computer: computerChoice)
|
resultsLabel.text = playRPS(you: .rock, computer: computerChoice)
|
||||||
@@ -172,7 +179,7 @@ class RPS {
|
|||||||
let computerChoice = Choice.randomComputerChoice()
|
let computerChoice = Choice.randomComputerChoice()
|
||||||
resultsLabel.text = playRPS(you: .scissors, computer: computerChoice)
|
resultsLabel.text = playRPS(you: .scissors, computer: computerChoice)
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func playRPS(you: Choice, computer: Choice) -> String {
|
func playRPS(you: Choice, computer: Choice) -> String {
|
||||||
if you == computer { return "It's a tie... GO AGAIN!" }
|
if you == computer { return "It's a tie... GO AGAIN!" }
|
||||||
|
|||||||
Reference in New Issue
Block a user