//
// ViewController.swift
// learn2
// Created by myhaspl on 16/1/19.
// Copyright (c) 2016年 myhaspl. All rights reserved.
///Volumes/myhaspl/book/swift算法/src/2/learn2/learn2/Base.lproj/Main.storyboard
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view,if already loaded.
}
}
private func compute(n:Int)->String{
//计算阶乘,如果计算的参数大于30,则进行近似计算
var result:Double = 1
if n<30 {
for i:Int in 1 ... n {
result = result * Double(i)
}
}
else {
var result1:Double = sqrt(2 * M_PI * Double(n))
var result2 :Double = pow(Double(n)/M_E,Double(n))
result = result1 * result2
}
return "结果"+String(stringInterpolationSegment:result)
}
@IBOutlet weak var Number: NSTextField!
@IBOutlet weak var ComputeResult: NSTextField!
@IBAction func jqCom(sender: AnyObject) {
if let n:Int=self.Number.stringValue.toInt() {
self.ComputeResult.stringValue=compute(n)
}
}
}