快速生成随机可行的数学问题 [英] Generating random doable math problems swift

查看:75
本文介绍了快速生成随机可行的数学问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个函数,该函数返回可以在脑海中进行的数学方程式(很明显,这有点主观,但我不知道该如何表达它).

I'm looking to create a function that returns a math equation that can be preformed in ones head (Clearly thats a bit subjective but I'm not sure how else to phrase it).

我想使用+,-,*,/,%和()操作.

I want to use the operations +,-,*,/,% and ().

当前我具有此功能:

func createMathString() -> String{

let firstNum = Int(arc4random_uniform(300))
let secNum = Int(arc4random_uniform(300))
let exp = Int(arc4random_uniform(4))
print("\(firstNum) \(self.expressions[exp]) \(secNum)")
return "\(firstNum) \(self.expressions[exp]) \(secNum)"

}

其中self.expression是一个包含+,-,*,/的数组.

where self.expression is an array that contains +,-,*,/.

此函数返回一个字符串,然后使用此函数对其进行解释:

This function returns a string which is then interpreted using this function:

let question = createMathString()
                let mathExpression = NSExpression(format: question)
                let mathValue = mathExpression.expressionValue(with: nil, context: nil) as? Int

我的问题:

1)随着数字的增加,除法和乘法变得困难

1) Division and Multiplication get difficult as numbers get higher

2)我不确定是否必须添加(). (并非每个问题都将由它们组成,具体取决于术语的数量.

2) I'm not sure had to add the ( ). (Not every problem will consist of them, depends on the number of terms.

3)我希望问题能够很容易地在某人的脑海中完成,但又不希望将随机数减少到0-50.

3) I want the questions to be easy enough to be completed in someones head but not to easy that I'm reducing the random numbers to 0-50.

我正在寻找一种可能的API,但找不到适合我需求的API.

I looked for a possible API but could not find one that suited my needs.

推荐答案

我开始使用Turbo Pascal进行编程,正如Niklaus Wirth所说:

I started programming with Turbo Pascal and as Niklaus Wirth said: Algorithms + Data Structure = Programs. You need to define data structures appropriate for your program.

首先,一些基本的数据结构. (快速枚举比其他语言功能强大得多)

First, some basic data structures. (Swift enum is much more powerful than that in other languages)

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())
    }
}

用法:

let a = MathExpression(lhs: .Integer(value: 1), rhs: .Integer(value: 2), operator: .divide)
let b = MathExpression(lhs: .Integer(value: 10), rhs: .Percentage(value: 20), operator: .minus)
let c = MathExpression.random()

let d = MathExpression(lhs: .Integer(value: 1), rhs: .Integer(value: 2), operator: .plus)
let e = MathExpression(lhs: .Integer(value: 3), rhs: .Integer(value: 4), operator: .plus)
let f = MathExpression(lhs: .Expression(expression: d), rhs: .Expression(expression: e), operator: .multiply)

let x = MathExpression.random()
let y = MathExpression.random()
let z = MathExpression(lhs: .Expression(expression: x), rhs: .Expression(expression: y), operator: .plus)


print("a: \(a) = \(a.result!)")
print("b: \(b) = \(b.result!)")
print("c: \(c) = \(c.result!)")

print("f: \(f) = \(f.result!)") // the classic (1 + 2) * (3 + 4)
print("z: \(z) = \(z.result!)") // this is completely random

这篇关于快速生成随机可行的数学问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆