分享一个Swift语言的逆波兰表达式计算器的实现
2015-09-18 23:15
639 查看
//
// CalculatorBrain.swift
// Calculator
//
// Created by 王卓 on 15/6/10.
// Copyright (c) 2015年 王卓. All rights reserved.
//
import Foundation
class CalculatorBrain {
private enum Op:CustomStringConvertible
{
case Operand(Double)
case UnaryOperation(String,Double->Double)
case BinaryOperation(String,(Double,Double)->Double)
var description:String{
switch self{
case .Operand(let Operand):
return "\(Operand)"
case .UnaryOperation(let symbol, _):
return "\(symbol)"
case .BinaryOperation(let symbol, _):
return "\(symbol)"
}
}
}
private var opStack=[Op]()
private var knowsOps=[String:Op]()
init() {
func learnOp(op:Op){
knowsOps[op.description]=op
}
// ***重点***
// ***闭包***
// switch operation {
// //1最复杂的形式
// case "×":performOperation2({(Double op1,Double op2)->Double in return op1*op2})
// //2
// case "÷":performOperation2({(Double op1,Double op2)->Double in op2/op1})
// //3
// case "+":performOperation2({(op1,op2)->Double in op1+op2})
// //4
// case "−":performOperation2({$1-$0})
// //其他形式performoperation(){$1-$0}
// case "√":performOperation1(){sqrt($0)}
// default:break
// }
//将操作符传入
knowsOps["×"]=Op.BinaryOperation("×",
*)
knowsOps["÷"]=Op.BinaryOperation("÷",
{$1/$0})
learnOp(Op.BinaryOperation("+",
+));
//knowsOps["+"]=Op.BinaryOperation("+", +)
knowsOps["−"]=Op.BinaryOperation("−",
{$1-$0})
//将sqrt函数作为参传入
knowsOps["√"]=Op.UnaryOperation("√", sqrt)
}
private func evalucate(ops:[Op])->(result:Double?,remainOps:[Op]){
if !ops.isEmpty{
var remainOps=ops
let op=remainOps.removeLast()
switch op{
case .Operand(let operand):
return (operand,remainOps)
case .UnaryOperation(_, let operation):
let OperandEvalucate = evalucate(remainOps)
if let operand=OperandEvalucate.result{
return (operation(operand),OperandEvalucate.remainOps)
}
case .BinaryOperation(_, let operation):
let op1Evalucate = evalucate(remainOps)
if let operand1=op1Evalucate.result{
let op2Evalucate = evalucate(op1Evalucate.remainOps)
if let operand2=op2Evalucate.result{
return(operation(operand1,operand2),op2Evalucate.remainOps)
}
}
}
}
return(nil,ops)
}
func evalucate()->Double?{
let (result,remainder)=evalucate(opStack)
print("\(opStack)
= \(result) with \(remainder)
left over")
return result
}
func pushOperand(operand:Double)->Double?{
opStack.append(Op.Operand(operand))
return evalucate()
}
func performOperation(Operation:String)->Double?{
if let operation = knowsOps[Operation]{
opStack.append(operation)
}
return evalucate()
}
}
// CalculatorBrain.swift
// Calculator
//
// Created by 王卓 on 15/6/10.
// Copyright (c) 2015年 王卓. All rights reserved.
//
import Foundation
class CalculatorBrain {
private enum Op:CustomStringConvertible
{
case Operand(Double)
case UnaryOperation(String,Double->Double)
case BinaryOperation(String,(Double,Double)->Double)
var description:String{
switch self{
case .Operand(let Operand):
return "\(Operand)"
case .UnaryOperation(let symbol, _):
return "\(symbol)"
case .BinaryOperation(let symbol, _):
return "\(symbol)"
}
}
}
private var opStack=[Op]()
private var knowsOps=[String:Op]()
init() {
func learnOp(op:Op){
knowsOps[op.description]=op
}
// ***重点***
// ***闭包***
// switch operation {
// //1最复杂的形式
// case "×":performOperation2({(Double op1,Double op2)->Double in return op1*op2})
// //2
// case "÷":performOperation2({(Double op1,Double op2)->Double in op2/op1})
// //3
// case "+":performOperation2({(op1,op2)->Double in op1+op2})
// //4
// case "−":performOperation2({$1-$0})
// //其他形式performoperation(){$1-$0}
// case "√":performOperation1(){sqrt($0)}
// default:break
// }
//将操作符传入
knowsOps["×"]=Op.BinaryOperation("×",
*)
knowsOps["÷"]=Op.BinaryOperation("÷",
{$1/$0})
learnOp(Op.BinaryOperation("+",
+));
//knowsOps["+"]=Op.BinaryOperation("+", +)
knowsOps["−"]=Op.BinaryOperation("−",
{$1-$0})
//将sqrt函数作为参传入
knowsOps["√"]=Op.UnaryOperation("√", sqrt)
}
private func evalucate(ops:[Op])->(result:Double?,remainOps:[Op]){
if !ops.isEmpty{
var remainOps=ops
let op=remainOps.removeLast()
switch op{
case .Operand(let operand):
return (operand,remainOps)
case .UnaryOperation(_, let operation):
let OperandEvalucate = evalucate(remainOps)
if let operand=OperandEvalucate.result{
return (operation(operand),OperandEvalucate.remainOps)
}
case .BinaryOperation(_, let operation):
let op1Evalucate = evalucate(remainOps)
if let operand1=op1Evalucate.result{
let op2Evalucate = evalucate(op1Evalucate.remainOps)
if let operand2=op2Evalucate.result{
return(operation(operand1,operand2),op2Evalucate.remainOps)
}
}
}
}
return(nil,ops)
}
func evalucate()->Double?{
let (result,remainder)=evalucate(opStack)
print("\(opStack)
= \(result) with \(remainder)
left over")
return result
}
func pushOperand(operand:Double)->Double?{
opStack.append(Op.Operand(operand))
return evalucate()
}
func performOperation(Operation:String)->Double?{
if let operation = knowsOps[Operation]{
opStack.append(operation)
}
return evalucate()
}
}
相关文章推荐
- Swift 第八课属性与方法
- 让你真正明白cinder与swift、glance的区别
- 《从零开始学Swift》学习笔记(Day 14)——字符串的插入、删除和替换
- Swift 2.0学习笔记(Day 14)——字符串的插入、删除和替换
- Swift学习笔记-基础语法1
- Benchmarking Performance of OpenStack Swift Configurations
- swift 日历练习
- Swift - 控制流的不同
- iOS 8 Metal Swift教程 :开始学习
- Swift - 按钮(UIButton)的用法
- SWIFT学习笔记02
- Swift 基础知识
- swift开发笔记4 - 设置tabbar选中颜色
- Swift 2.0 下面向协议的MVVM架构实践
- The Swift Programming Language中国完整版
- swift开发笔记3 - 设置tableview背景图片
- swift开发笔记2-通过pushViewController跳转后 隐藏后退按钮
- 关东升的iOS实战系列图书 《iOS实战:传感器卷(Swift版)》已经上市
- Swift学习之十四:闭包(Closures) .
- swift 从零开始