func caculate(s:String)->Double{ let exp:NSExpression = NSExpression(format:s) let result:Double = exp.expressionValueWithObject(nil,context:nil) as? Double ?? 0 return result } caculate("(1+1)*2") caculate("1+1*2") caculate("1+1") caculate("(3+1)*2") caculate("1/0") caculate("1/0.0") caculate("sqrt(2.99)") caculate("log(2)") caculate("abs(-1)") caculate("exp(3)") caculate("ln(exp(3))") caculate("random()")
沒有留言:
張貼留言