我已经看过所有以前对这类问题的回复,并没有看到任何似乎合适的回答。给我错误的行是:
"÷" : .binaryOperation(/,{$0 + "÷" + $1}, {$1 == 0 ? "Division by zero" : nil}),
当我将其更改为此代码时,代码编译时没有错误:
"÷" : .binaryOperation(/,{$0 + "÷" + $1}, {_,_ nil}),
完整的表达式i:
private var operations:Dictionary<String,Operation> =
[
"rand": .nullOperation({Double(arc4random()) / Double(UInt32.max)}, "rand()"),
"π" : .constant(M_PI),
"e" : .constant(M_E),
"√" : .unaryOperation(sqrt,{"√(" + $0 + ")"},{$0 < 0 ? "Sqrt of negative number" : nil}),
"cos" :.unaryOperation(cos,{"cos(" + $0 + ")"},{ _ in nil}),
"∓" : .unaryOperation({-$0},{"-(" + $0 + ")"},{ _ in nil}),
"x²" :.unaryOperation({ pow($0, 2) }, { "(" + $0 + ")²" },{ _ in nil}),
"x³" :.unaryOperation({ pow($0, 3) }, { "(" + $0 + ")³" },{ _ in nil}),
"x⁻¹" :.unaryOperation({ 1 / $0 }, { "(" + $0 + ")⁻¹" },{ _ in nil}),
"sin" :.unaryOperation(sin, { "sin(" + $0 + ")" },{ _ in nil}),
"tan" :.unaryOperation(tan, { "tan(" + $0 + ")" },{ _ in nil}),
"sinh" :.unaryOperation(sinh, { "sinh(" + $0 + ")" },{ _ in nil}),
"cosh" :.unaryOperation(cosh, { "cosh(" + $0 + ")" },{ _ in nil}),
"tanh" :.unaryOperation(tanh, { "tanh(" + $0 + ")" },{ _ in nil}),
"ln" : .unaryOperation(log, { "ln(" + $0 + ")" },{ _ in nil}),
"log" : .unaryOperation(log10, { "log(" + $0 + ")" },{ _ in nil}),
"eˣ" :.unaryOperation(exp, { "e^(" + $0 + ")" },{ _ in nil}),
"10ˣ" :.unaryOperation({ pow(10, $0) }, { "10^(" + $0 + ")" },{ _ in nil}),
"x!" :.unaryOperation(factorial, { "(" + $0 + ")!" },{ _ in nil}),
"xʸ" :.binaryOperation(pow, { $0 + "^" + $1 },{ _,_ in nil}),
"+" : .binaryOperation(+,{$0 + "+" + $1},{ _,_ in nil}),
"−" : .binaryOperation(-,{$0 + "-" + $1},{ _,_ in nil}),
"÷" : .binaryOperation(/,{$0 + "÷" + $1}, {$1 == 0 ? "Division by zero" : nil}),
"×" : .binaryOperation(*,{$0 + "*" + $1},{ _,_ in nil}),
"=" : .equals
]
编辑:这是Operation enum
private enum Operation {
case constant(Double)
case nullOperation(() -> Double, String)
case unaryOperation((Double) -> Double, (String) -> String, (Double) -> String?)
case binaryOperation((Double,Double) -> Double, (String, String) -> String, (Double,Double) -> String?)
case equals
}
答案 0 :(得分:1)
编辑:在游乐场进行测试表明这不会帮助。我把它留在这里,以便其他人知道不要尝试它;)
&LT;猜&GT;
你能否尝试明确你的最后一个块的参数,例如
"÷" : .binaryOperation(/,{$0 + "÷" + $1}, { (_, denom: Double) -> String? in
return denom == 0 ? "Division by zero" : nil
}),
&LT; /猜测&GT;
答案 1 :(得分:0)
单独将每行添加到字典中,但速度并不快:)
private var operations: [String: Operation] = [:]
operations["rand"] = .nullOperation({Double(arc4random()) / Double(UInt32.max)}, "rand()")
operations["π"] = .constant(.pi)
operations["e"] = .constant(M_E)
operations["√"] = .unaryOperation(sqrt,{"√(" + $0 + ")"},{$0 < 0 ? "Sqrt of negative number" : nil})
operations["cos"] = .unaryOperation(cos,{"cos(" + $0 + ")"},{ _ in nil})
operations["∓"] = .unaryOperation({-$0},{"-(" + $0 + ")"},{ _ in nil})
operations["x²"] = .unaryOperation({ pow($0, 2) }, { "(" + $0 + ")²" },{ _ in nil})
operations["x³"] = .unaryOperation({ pow($0, 3) }, { "(" + $0 + ")³" },{ _ in nil})
operations["x⁻¹"] = .unaryOperation({ 1 / $0 }, { "(" + $0 + ")⁻¹" },{ _ in nil})
operations["sin"] = .unaryOperation(sin, { "sin(" + $0 + ")" },{ _ in nil})
operations["tan"] = .unaryOperation(tan, { "tan(" + $0 + ")" },{ _ in nil})
operations["sinh"] = .unaryOperation(sinh, { "sinh(" + $0 + ")" },{ _ in nil})
operations["cosh"] = .unaryOperation(cosh, { "cosh(" + $0 + ")" },{ _ in nil})
operations["tanh"] = .unaryOperation(tanh, { "tanh(" + $0 + ")" },{ _ in nil})
operations["ln"] = .unaryOperation(log, { "ln(" + $0 + ")" },{ _ in nil})
operations["log"] = .unaryOperation(log10, { "log(" + $0 + ")" },{ _ in nil})
operations["eˣ"] = .unaryOperation(exp, { "e^(" + $0 + ")" },{ _ in nil})
operations["10ˣ"] = .unaryOperation({ pow(10, $0) }, { "10^(" + $0 + ")" },{ _ in nil})
operations["x!"] = .unaryOperation(factorial, { "(" + $0 + ")!" },{ _ in nil})
operations["xʸ"] = .binaryOperation(pow, { $0 + "^" + $1 },{ _,_ in nil})
operations["+"] = .binaryOperation(+,{$0 + "+" + $1},{ _,_ in nil})
operations["−"] = .binaryOperation(-,{$0 + "-" + $1},{ _,_ in nil})
operations["÷"] = .binaryOperation(/,{$0 + "÷" + $1}, { $1 == Double(0) ? "Division by zero" : nil})
operations["×"] = .binaryOperation(*,{$0 + "*" + $1},{ _,_ in nil})
operations["="] = .equals