篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift enum automata for swift相关的知识,希望对你有一定的参考价值。
//playground to make integer automatan
//instruction for math machine
enum Instruction {
case op(String)
case int(Int)
}
enum Math {
static let opers = [
"+":{(a:Int, b:Int) in a + b},
"-":{(a:Int, b:Int) in a - b},
"*":{(a:Int, b:Int) in a * b},
"/":{(a:Int, b:Int) in a / b},
"%":{(a:Int, b:Int) in a % b}
]
static let transitions = [
"+":{(amount:Int) in Math.state(amount:amount, oper:"+")},
"-":{(amount:Int) in Math.state(amount:amount, oper:"-")},
"*":{(amount:Int) in Math.state(amount:amount, oper:"*")},
"/":{(amount:Int) in Math.state(amount:amount, oper:"/")},
"%":{(amount:Int) in Math.state(amount:amount, oper:"%")}
]
case state(amount:Int, oper:String)
init(amount:Int, op:String) {
self = Math.transitions[op]!(amount)
}
mutating func input(code:Instruction) {
switch code {
case .op(let oper):
switch self {
case state(let amount, _):
self = Math.state(amount: amount, oper: oper)
}
case .int(let int):
switch self {
case state(let amount,let oper):
self = Math.state(amount: Math.opers[oper]!(amount, int), oper: oper)
}
}
}
}
以上是关于swift enum automata for swift的主要内容,如果未能解决你的问题,请参考以下文章