gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:21:31 27 4
gpt4 key购买 nike

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

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

注意:方程中只有1个x

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

我知道 let mathExpression = NSExpression(format: question) 将字符串转换为数学方程式,但是在求解 x 时我不确定如何去做。

我之前问过Generating random doable math problems swift对于无法解决 x 问题,但我不确定如何将该答案转换为解决 x 问题

编辑:目标是生成方程式并让用户求解变量。

最佳答案

由于您只需要一个表示方程式的字符串和 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

关于swift - 在swift中生成一个简单的代数表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43311474/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com