Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 生成随机可行数学问题_Swift_Math - Fatal编程技术网

Swift 生成随机可行数学问题

Swift 生成随机可行数学问题,swift,math,Swift,Math,我想创建一个函数,返回一个数学方程,可以在一个人的头脑中实现(显然这有点主观,但我不知道如何表达它) 我想使用操作+、-、*、/、%和() 目前我有这个功能: func createMathString() -> String{ let firstNum = Int(arc4random_uniform(300)) let secNum = Int(arc4random_uniform(300)) let exp = Int(arc4random_uniform(4)) print("\

我想创建一个函数,返回一个数学方程,可以在一个人的头脑中实现(显然这有点主观,但我不知道如何表达它)

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

目前我有这个功能:

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)"

}
let question = createMathString()
                let mathExpression = NSExpression(format: question)
                let mathValue = mathExpression.expressionValue(with: nil, context: nil) as? Int
其中self.expression是包含+,-,*,/

此函数返回一个字符串,然后使用此函数解释该字符串:

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)"

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

1) 数字越高,除法和乘法就越困难

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

3) 我希望这些问题足够简单,可以在某人的头脑中完成,但不容易,因为我正在将随机数减少到0-50


我寻找了一个可能的API,但没有找到一个适合我的需要

我开始用Turbo Pascal编程,正如Niklaus Wirth所说:。您需要定义适合您的程序的数据结构

首先,一些基本的数据结构。(Swift enum比其他语言的功能强大得多)

现在主要的课程是:

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

我很荣幸知道如何改变它来解决x问题?什么x问题?你的意思是乘法和除法越难,值越大?不,我只是想知道是否有一种方法可以生成代数方程,例如(x-15)/10=6->用户需要输入75。你可以扩展
MathElement
以包含变量的枚举大小写。我现在不在电脑附近。无论如何,发布一个新问题