Swift复数计算器
正在爬的程序猿 人气:0Swift使用笛卡尔和极坐标进行转换的复数计算器
import Foundation struct Complex{ //实部real public var real : Double = 0 //虚部img public var img : Double = 0 public var polar :(mod:Double,arg:Double) = (0,0) public var Cartesian :(real:Double,img:Double) = (0,0) public var FirstPolar :(mod:Double,arg:Double) = (0,0) init(PolarNumber:(mod:Double,arg:Double)) { FirstPolar = PolarNumber ChangePolar = PolarNumber as (Double,Double) // print(judgment(parameter: ChangePolar)) } //笛卡尔转极坐标 public var ChangeCartesian :(mod:Double,arg:Double){ get{ return polar } set(CartesianNum){ polar.mod = sqrt((CartesianNum.mod * CartesianNum.mod)+(CartesianNum.arg * CartesianNum.arg)) polar.arg = atan2(CartesianNum.arg, CartesianNum.mod) } } //极坐标转笛卡尔 public var ChangePolar : (real:Double,img:Double){ get{ return Cartesian } set(PolarNum){ Cartesian.real = PolarNum.real * cos(PolarNum.img) Cartesian.img = PolarNum.real * sin(PolarNum.img) } } //判断 public func judgment(parameter:(real:Double,img:Double))->String { if parameter.img > 0 { return "\(Double(round(parameter.real * 1000)/1000))+\(Double(round(parameter.img * 1000)/1000))
加载全部内容
- 猜你喜欢
- 用户评论