- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻求创建一个函数来返回 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/
我已经将一些变量声明为 bool 值,我希望 C++ 在我进行一些 bool 加法时知道该做什么,但它并没有按照我希望的方式发生。我如何让它工作。 #include using namespace s
我正在创建小型配置文件查看器,我想在 SQL 中模拟环。怎么运行的?对于简单:我的 table 上有 5 个用户。当我从中间获得一个到我的实验(如用户编号 2、3、4)时,下一个和上一个用户是 CUR
我计划使用低级 API 实现 FUSE 文件系统,目前正在尝试了解 fuse_entry_param结构体。 不知道是什么unsigned long fuse_entry_param::generat
从事 JS 工作,刚刚开始学习 boolean 代数。想知道是否有办法简化这个表达式: (!variableOne || !variableTwo) 我记得听说过两个“not”意味着你可以改变符号,但
我有这个 F-代数 (introduced in a previous question),我想在它上面施放一个有效的代数。通过绝望的试验,我设法组合了一个有效的 monadic catamorphi
我在 Google 上到处寻找德摩根定律的 bool 代数(不是集合论)证明,但找不到。 Stack Overflow 也缺少 DeMorgan 定律问题。 作为我的 CIS 251 类(class)
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
正在解决这个问题。 验证闰年的基本规则如下: - 大多数能被4整除的年份都是闰年。 - 异常(exception):世纪年不是闰年,除非它们可以被 400 整除。 这就是答案: if(y%4==0 &
假设算法的最坏情况运行时间可以描述为: T(n) = O(n) + O(r^2) + O(n-r) n 是输入大小,r 是根据算法创建分区的索引。 这个等式可以进一步简化吗?如果变量都是 n 那么它将
有没有办法在 JS 中使用 bool 代数? 例如,我想遍历一个包含 true 和 false 的数组,并将其简化为只有 true 或 false。 用 bool 代数来做这件事似乎是一种优雅的方式.
假设我有两个数据框 df1 df2 我可以加入 df1_keys 和 df2_keys。 我想做的事: (A-B) (A-B) U (B-A) A=df1 和 B=df2。 根据我在 document
这个问题已经有答案了: How can I remove a flag in C? (3 个回答) How to set, clear, and toggle a single bit? (27 个回
大家 我实现了自定义聚合 pig UDF。UDF 实现了 Algebraic 接口(interface),并且有 3 个类 - Initial、Intermed 和 Final 在不同阶段进行工作。它
我想制作一个 bool 向量,它是通过比较两个输入 bool 向量创建的。我可以使用 for 循环,但有更好的方法吗? 我理想的解决方案是这样的: df['A'] = [True, False, Fa
我想编写一个 pig 代码来执行分组并生成 31 个字段的总和,但在此之前我需要做一些自定义处理,为此我编写了一个 eval 函数。我想如果我可以将 GROUP 和 SUM 操作包含到 UDF 中,我
我试图通过 boolean 代数来理解使用 4 个与非门如何等同于 1 个异或门。 如果我们从维基百科看这张图片 http://en.wikipedia.org/wiki/XOR_gate#Alter
我是一名优秀的程序员,十分优秀!