我正在按照刚刚为 Swift 发布的 CS193p 讲座创建一个计算器应用程序。如何将 Pi 既作为操作数 & 又作为操作来实现?
I'm creating a calculator app by following the CS193p lectures just released for Swift. How can I implement Pi as both an operand & and operation?
我正在努力完成讲座中发布的作业,但当然......我被困在第一个作业上,我讨厌在无法解决问题的情况下继续前进。
我在将 π 作为操作 和 和操作数时遇到问题。我让它作为一个操作工作(它所做的只是 return π 的值)。例如。 ' π Enter π Enter + ' 结果是 6.28... 但是如果我说 ' π Enter π 输入 + π 输入 ÷ ' 结果是 1.0 而应该是 2.0
我搜索了 Whosebug,除了 Objective-C 解决方案外,一无所获。
感谢任何帮助。
这是当您点击操作时 ViewController 中触发的操作:
@IBAction func operate(sender: UIButton) {
let operand = sender.currentTitle!
if (userIsInTheMiddleOfTypingANumber){
enter()
}
if let operation = sender.currentTitle{
if let result = brain.performOperation(operation){
displayValue = result
} else {
displayValue = 0
}
}
}
这是模型中的代码:
private enum Op: Printable {
case Operand(Double)
case UnaryOperator(String, Double -> Double)
case BinaryOperator(String, (Double, Double) -> Double)
case ConstantOperator (String, Double)
var description: String {
get {
switch self {
case .Operand(let operand):
return "\(operand)"
case .UnaryOperator(let symbol, _ ):
return symbol
case .BinaryOperator(let symbol, _ ):
return symbol
case .ConstantOperator(let symbol, _ ):
return symbol
}
}
}
}
func performOperation(symbol: String) -> Double? {
if let operation = knownOps[symbol]{
opStack.append(operation)
}
return evaluate()
}
private var opStack = [Op]()
private var knownOps = [String:Op]()
init(){
func learnOp(op: Op){
knownOps[op.description] = op
}
learnOp(Op.BinaryOperator("✕", *))
//knownOps["✕"] = Op.BinaryOperator("✕", *)
knownOps["÷"] = Op.BinaryOperator("÷") { / [=12=]}
knownOps["+"] = Op.BinaryOperator("+", +)
knownOps["−"] = Op.BinaryOperator("−") { - [=12=]}
knownOps["√"] = Op.UnaryOperator("√", sqrt)
learnOp(Op.UnaryOperator("cos", cos))
learnOp(Op.UnaryOperator("sin", sin))
learnOp(Op.ConstantOperator("∏", M_PI))
}
private func evaluate(ops: [Op]) -> (result: Double?, remainingOps: [Op]){
if !ops.isEmpty {
var remainingOps = ops
let op = remainingOps.removeLast() //get the first op off the stack
switch op {
case .Operand(let operand):
return (operand, remainingOps)
case .UnaryOperator(_, let operation):
let operationEvaluation = evaluate(remainingOps)
if let operand = operationEvaluation.result {
return(operation(operand), operationEvaluation.remainingOps)
}
case .BinaryOperator(_ , let operation):
let op1Eval = evaluate(remainingOps)
if let op1 = op1Eval.result{
let op2Eval = evaluate(op1Eval.remainingOps)
if let op2 = op2Eval.result {
return (operation(op1, op2), op2Eval.remainingOps)
}
}
case .ConstantOperator(_, let value):
return(value, remainingOps)
}
}
return(nil, ops)
}
func evaluate() -> Double? {
let (result, remainder) = evaluate(opStack)
println("\(opStack) = \(result) with \(remainder) left over.")
return result
}
注意:鉴于教师希望我们遵循的结构,我正在尝试完成作业的必要任务。我在这里的尝试(尝试添加 ‖ 作为运算符)在某些情况下有效,但并非全部...
以下是作业中关于此任务的内容:
"The value of π is available via the expression M_PI. E.g. let x = M_PI. You can think of π as an operand or you can think of it as an operation (i.e. a new kind of operation that takes no arguments off the stack but returns a value). Up to you. But, either way, it’d be nice to be able to add other constants to your Calculator with a minimum of code."
我尝试了操作数方法,但我打破了 MVC 范式,所以我停止了。
如果您按 π 并输入,那么您实际上是将 pi 放入 opStack 两次。所以你实际上是在做 π π π π + π π ÷。那有意义吗?为了将 pi 添加到自身,您需要推动 π π +。如果你想把 pi 加在一起然后除以 pi,那就是 π π + π ÷。
我正在努力完成讲座中发布的作业,但当然......我被困在第一个作业上,我讨厌在无法解决问题的情况下继续前进。
我在将 π 作为操作 和 和操作数时遇到问题。我让它作为一个操作工作(它所做的只是 return π 的值)。例如。 ' π Enter π Enter + ' 结果是 6.28... 但是如果我说 ' π Enter π 输入 + π 输入 ÷ ' 结果是 1.0 而应该是 2.0
我搜索了 Whosebug,除了 Objective-C 解决方案外,一无所获。 感谢任何帮助。
这是当您点击操作时 ViewController 中触发的操作:
@IBAction func operate(sender: UIButton) {
let operand = sender.currentTitle!
if (userIsInTheMiddleOfTypingANumber){
enter()
}
if let operation = sender.currentTitle{
if let result = brain.performOperation(operation){
displayValue = result
} else {
displayValue = 0
}
}
}
这是模型中的代码:
private enum Op: Printable {
case Operand(Double)
case UnaryOperator(String, Double -> Double)
case BinaryOperator(String, (Double, Double) -> Double)
case ConstantOperator (String, Double)
var description: String {
get {
switch self {
case .Operand(let operand):
return "\(operand)"
case .UnaryOperator(let symbol, _ ):
return symbol
case .BinaryOperator(let symbol, _ ):
return symbol
case .ConstantOperator(let symbol, _ ):
return symbol
}
}
}
}
func performOperation(symbol: String) -> Double? {
if let operation = knownOps[symbol]{
opStack.append(operation)
}
return evaluate()
}
private var opStack = [Op]()
private var knownOps = [String:Op]()
init(){
func learnOp(op: Op){
knownOps[op.description] = op
}
learnOp(Op.BinaryOperator("✕", *))
//knownOps["✕"] = Op.BinaryOperator("✕", *)
knownOps["÷"] = Op.BinaryOperator("÷") { / [=12=]}
knownOps["+"] = Op.BinaryOperator("+", +)
knownOps["−"] = Op.BinaryOperator("−") { - [=12=]}
knownOps["√"] = Op.UnaryOperator("√", sqrt)
learnOp(Op.UnaryOperator("cos", cos))
learnOp(Op.UnaryOperator("sin", sin))
learnOp(Op.ConstantOperator("∏", M_PI))
}
private func evaluate(ops: [Op]) -> (result: Double?, remainingOps: [Op]){
if !ops.isEmpty {
var remainingOps = ops
let op = remainingOps.removeLast() //get the first op off the stack
switch op {
case .Operand(let operand):
return (operand, remainingOps)
case .UnaryOperator(_, let operation):
let operationEvaluation = evaluate(remainingOps)
if let operand = operationEvaluation.result {
return(operation(operand), operationEvaluation.remainingOps)
}
case .BinaryOperator(_ , let operation):
let op1Eval = evaluate(remainingOps)
if let op1 = op1Eval.result{
let op2Eval = evaluate(op1Eval.remainingOps)
if let op2 = op2Eval.result {
return (operation(op1, op2), op2Eval.remainingOps)
}
}
case .ConstantOperator(_, let value):
return(value, remainingOps)
}
}
return(nil, ops)
}
func evaluate() -> Double? {
let (result, remainder) = evaluate(opStack)
println("\(opStack) = \(result) with \(remainder) left over.")
return result
}
注意:鉴于教师希望我们遵循的结构,我正在尝试完成作业的必要任务。我在这里的尝试(尝试添加 ‖ 作为运算符)在某些情况下有效,但并非全部...
以下是作业中关于此任务的内容: "The value of π is available via the expression M_PI. E.g. let x = M_PI. You can think of π as an operand or you can think of it as an operation (i.e. a new kind of operation that takes no arguments off the stack but returns a value). Up to you. But, either way, it’d be nice to be able to add other constants to your Calculator with a minimum of code."
我尝试了操作数方法,但我打破了 MVC 范式,所以我停止了。
如果您按 π 并输入,那么您实际上是将 pi 放入 opStack 两次。所以你实际上是在做 π π π π + π π ÷。那有意义吗?为了将 pi 添加到自身,您需要推动 π π +。如果你想把 pi 加在一起然后除以 pi,那就是 π π + π ÷。