Math Expression Class to generate math problems

This commit is contained in:
Andrew
2021-01-20 11:24:53 -05:00
parent d5cfddb3fb
commit e9d0500132
3 changed files with 91 additions and 0 deletions
+4
View File
@@ -22,6 +22,7 @@
7C5DAE9C25AF812200E44C52 /* clock.png in Resources */ = {isa = PBXBuildFile; fileRef = 7C5DAE9B25AF812200E44C52 /* clock.png */; };
7C83963625AF375B0027A94C /* NotificationLogic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C83963525AF375B0027A94C /* NotificationLogic.swift */; };
7C83963925AF68980027A94C /* TestingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C83963825AF68980027A94C /* TestingViewController.swift */; };
C7E638E825B88F8B00799512 /* MathExpressions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7E638E725B88F8B00799512 /* MathExpressions.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@@ -44,6 +45,7 @@
7C83962F25AF34F10027A94C /* HealthKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = HealthKit.framework; path = System/Library/Frameworks/HealthKit.framework; sourceTree = SDKROOT; };
7C83963525AF375B0027A94C /* NotificationLogic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationLogic.swift; sourceTree = "<group>"; };
7C83963825AF68980027A94C /* TestingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestingViewController.swift; sourceTree = "<group>"; };
C7E638E725B88F8B00799512 /* MathExpressions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MathExpressions.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -94,6 +96,7 @@
7C83963825AF68980027A94C /* TestingViewController.swift */,
4FD642D225B48C380069171E /* AlarmActivator.swift */,
4FD642DA25B4B7F60069171E /* Utils.swift */,
C7E638E725B88F8B00799512 /* MathExpressions.swift */,
);
path = ProjectClock;
sourceTree = "<group>";
@@ -189,6 +192,7 @@
4FD642DB25B4B7F60069171E /* Utils.swift in Sources */,
4FA419AF25AF93EC004CE0FC /* AlarmViewController.swift in Sources */,
4F509BD225AE22D100726227 /* Models.swift in Sources */,
C7E638E825B88F8B00799512 /* MathExpressions.swift in Sources */,
7C83963625AF375B0027A94C /* NotificationLogic.swift in Sources */,
4FD642E025B4D5F30069171E /* AlarmActivationViewController.swift in Sources */,
);
+86
View File
@@ -0,0 +1,86 @@
//
// MathExpressions.swift
// ProjectClock
//
// Class that will generate a simple math expression
import Foundation
enum MathElement : CustomStringConvertible {
case Integer(value: Int)
case Percentage(value: Int)
case Expression(expression: MathExpression)
var description: String {
switch self {
case .Integer(let value): return "\(value)"
case .Percentage(let percentage): return "\(percentage)%"
case .Expression(let expr): return expr.description
}
}
var nsExpressionFormatString : String {
switch self {
case .Integer(let value): return "\(value).0"
case .Percentage(let percentage): return "\(Double(percentage) / 100)"
case .Expression(let expr): return "(\(expr.description))"
}
}
}
enum MathOperator : String {
case plus = "+"
case minus = "-"
case multiply = "*"
case divide = "/"
static func random() -> MathOperator {
let allMathOperators: [MathOperator] = [.plus, .minus, .multiply, .divide]
let index = Int(arc4random_uniform(UInt32(allMathOperators.count)))
return allMathOperators[index]
}
}
class MathExpression : CustomStringConvertible {
var lhs: MathElement
var rhs: MathElement
var `operator`: MathOperator
init(lhs: MathElement, rhs: MathElement, operator: MathOperator) {
self.lhs = lhs
self.rhs = rhs
self.operator = `operator`
}
var description: String {
var leftString = ""
var rightString = ""
if case .Expression(_) = lhs {
leftString = "(\(lhs))"
} else {
leftString = lhs.description
}
if case .Expression(_) = rhs {
rightString = "(\(rhs))"
} else {
rightString = rhs.description
}
return "\(leftString) \(self.operator.rawValue) \(rightString)"
}
var result : Any? {
let format = "\(lhs.nsExpressionFormatString) \(`operator`.rawValue) \(rhs.nsExpressionFormatString)"
let expr = NSExpression(format: format)
return expr.expressionValue(with: nil, context: nil)
}
static func random() -> MathExpression {
let lhs = MathElement.Integer(value: Int(arc4random_uniform(10)))
let rhs = MathElement.Integer(value: Int(arc4random_uniform(10)))
return MathExpression(lhs: lhs, rhs: rhs, operator: .random())
}
}
+1
View File
@@ -34,3 +34,4 @@ func puzzleAction() {
func smashAction() {
}