快速生成简单的代数表达式 [英] Generating a simple algebraic expression in swift

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

问题描述

我正在寻找一个函数,该函数返回可以在一个脑袋中执行的x数学方程式的求解(显然,这有点主观,但我不确定该如何用其他方式表达出来).

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

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

Example problem: (x - 15)/10 = 6

注意:方程式中只有1 x

Note: Only 1 x in the equation

我要使用+,-,*,/,sqrt操作(仅适用于X-> sqrt(x))

I want to use the operations +, -, *, /, sqrt (Only applied to X -> sqrt(x))

我知道let mathExpression = NSExpression(format: question)将字符串转换成数学方程式,但是求解x时我不确定该怎么做.

I know that let mathExpression = NSExpression(format: question) converts strings into math equations but when solving for x I'm not sure how to go about doing this.

我之前曾问过迅速生成随机可行的数学问题用于解决x问题,但我不确定如何将答案转换为解决x

I previously asked Generating random doable math problems swift for non solving for x problems but I'm not sure how to convert that answer into solving for x

目标是生成一个方程式,并让用户求解该变量.

Goal is to generate an equation and have the user solve for the variable.

推荐答案

由于所需的只是一个表示方程式和x值的字符串,因此您无需进行任何求解.只需从x开始并对其进行变换,直到有了一个好的方程式为止.这是一个示例:(将其复制并粘贴到Playground中进行试用)

Since all you want is a string representing an equation and a value for x, you don't need to do any solving. Just start with x and transform it until you have a nice equation. Here's a sample: (copy and paste it into a Playground to try it out)

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()函数将必须单独实现.为了使除法有用,您必须添加一个系统来查找因子(因为您可能希望结果为整数).根据所需的输出种类,还有很多工作要做,但这应该可以帮助您入门.

There are some things missing. The sqrt() function will have to be implemented separately. And for division to be useful, you'll have to add in a system to find factors (since you presumably want the results to be integers). Depending on what sort of output you want, there's a lot more work to do, but this should get you started.

以下是示例输出:

(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

这篇关于快速生成简单的代数表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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