在swift中生成一个简单的代数表达式

时间:2017-04-09 20:17:43

标签: swift math

我正在寻找创建一个函数,该函数返回x数学方程的求解,可以在一个头部中执行(显然这有点主观,但我不确定如何用它来表达它)。< / p>

示例问题:(x - 15)/ 10 = 6

注意:等式中只有1 x

我想使用操作+, - ,*,/,sqrt(仅适用于X - &gt; sqrt(x))

我知道([\d^5]{1}) 会将字符串转换为数学方程式,但在求解x时,我不确定如何进行此操作。

我之前曾要求enter image description here解决x问题,但我不确定如何将该答案转换为解决x

编辑:目标是生成一个等式并让用户求解变量。

2 个答案:

答案 0 :(得分:1)

因为你想要的只是一个表示方程式和x值的字符串,所以你不需要做任何解决。从x开始并转换它直到你有一个很好的方程式。这是一个示例:(将其复制并粘贴到Playground中进行试用)

import UIKit

enum Operation: String {
    case addition = "+"
    case subtraction = "-"
    case multiplication = "*"
    case division = "/"


    static func all() -> [Operation] {
        return [.addition, .subtraction, .multiplication, .division]
    }

    static func random() -> Operation {
        let all = Operation.all()
        let selection = Int(arc4random_uniform(UInt32(all.count)))
        return all[selection]
    }

}


func addNewTerm(formula: String, result: Int) -> (formula: String, result: Int) {
    // choose a random number and operation
    let operation = Operation.random()
    let number = chooseRandomNumberFor(operation: operation, on: result)
    // apply to the left side
    let newFormula = applyTermTo(formula: formula, number: number, operation: operation)
    // apply to the right side
    let newResult = applyTermTo(result: result, number: number, operation: operation)
    return (newFormula, newResult)
}

func applyTermTo(formula: String, number:Int, operation:Operation) -> String {
    return "\(formula) \(operation.rawValue) \(number)"
}

func applyTermTo(result: Int, number:Int, operation:Operation) -> Int {
    switch(operation) {
    case .addition: return result + number
    case .subtraction: return result - number
    case .multiplication: return result * number
    case .division: return result / number
    }
}

func chooseRandomNumberFor(operation: Operation, on number: Int) -> Int {
    switch(operation) {
    case .addition, .subtraction, .multiplication:
        return Int(arc4random_uniform(10) + 1)
    case .division:
        // add code here to find integer factors
        return 1
    }
}


func generateFormula(_ numTerms:Int = 1) -> (String, Int) {
    let x = Int(arc4random_uniform(10))
    var leftSide = "x"
    var result = x

    for i in 1...numTerms {
        (leftSide, result) = addNewTerm(formula: leftSide, result: result)
        if i < numTerms {
            leftSide = "(" + leftSide + ")"
        }
    }

    let formula = "\(leftSide) = \(result)"

    return (formula, x)
}

func printFormula(_ numTerms:Int = 1) {
    let (formula, x) = generateFormula(numTerms)
    print(formula, "                      x = ", x)
}


for i in 1...30 {
    printFormula(Int(arc4random_uniform(3)) + 1)
}

有些东西不见了。 sqrt()函数必须单独实现。为了使除法有用,你必须在系统中添加以找到因子(因为你可能希望结果是整数)。根据您想要的输出类型,还有很多工作要做,但这应该可以让您开始。

以下是示例输出:

(x + 10) - 5 = 11                       x =  6
((x + 6) + 6) - 1 = 20                       x =  9
x - 2 = 5                       x =  7
((x + 3) * 5) - 6 = 39                       x =  6
(x / 1) + 6 = 11                       x =  5
(x * 6) * 3 = 54                       x =  3
x * 9 = 54                       x =  6
((x / 1) - 6) + 8 = 11                       x =  9

答案 1 :(得分:0)

好的,让我们假设您说“注意:方程式中只有1 x ”,您想要的是y = 0 = β1*x + β0形式的线性方程式,其中β0 β1分别是斜率和截距系数。

任何线性方程的逆(或解)由x = -β0/β1给出。所以你真正需要做的是生成随机整数β0β1来创建你的等式。但是因为它应该在某个人的头脑中“可以解决”,所以你可能希望β0能够被β1整除,而且β1β0/β1要小于或12等于β1 ≤ 12,因为这是众所周知的乘法表的上限。在这种情况下,只需生成一个随机整数β0β1等于n次整数0 ≤ n ≤ 122/3

如果你想允许像β0那样的简单分数解,只需将分母和分子分别加到β112,注意防止分子或分母得到太大(y也是一个很好的限制)。

由于您可能希望y非零,只需在-1212之间生成第三个随机整数y = β1*x + β0 + y,并将输出等式更改为{{ 1}}。

由于您提到只能在x变量上发生,因此很容易添加;解决方案(0 = β1*sqrt(x) + β0)只是x = (β0/β1)**2

这是一些非常简单(并且非常有问题)的代码,用于生成随机整数以帮助您入门:

import func Glibc.srand
import func Glibc.rand
import func Glibc.time

srand(UInt32(time(nil)))
print(rand() % 12)

本网站上有很多关于better ways to generate random integers的答案。