Swift實(shí)現(xiàn)復(fù)數(shù)計(jì)算器
本文實(shí)例為大家分享了Swift實(shí)現(xiàn)復(fù)數(shù)計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
Swift使用笛卡爾和極坐標(biāo)進(jìn)行轉(zhuǎn)換的復(fù)數(shù)計(jì)算器
import Foundation
struct Complex{
? ? //實(shí)部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))
? ? ? ??
? ? }
? ? //笛卡爾轉(zhuǎn)極坐標(biāo)
? ? 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)
? ? ? ? }
? ? }
? ? //極坐標(biāo)轉(zhuǎn)笛卡爾
? ? 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))??"
? ? ? ? ? ? } else if parameter.img < 0 {
? ? ? ? ? ? ? ? return "\(Double(round(parameter.real * 1000)/1000))-\(-(Double(round(parameter.img * 1000)/1000)))??"
? ? ? ? ? ? ? ?} else {
? ? ? ? ? ? ? ? return "\(Double(round(parameter.img * 1000)/1000))"
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? //計(jì)算前進(jìn)行格式轉(zhuǎn)換
? ? public mutating func ChangeAdd(AddNum:(mod:Double,arg:Double)){
? ? ? ? ChangePolar = FirstPolar as (Double,Double)
? ? ? ? //初始值通過set進(jìn)行了轉(zhuǎn)換
? ? ? ? let x = ChangePolar
? ? ? ? //用x進(jìn)行一個(gè)存儲(chǔ)
? ? ? ? ChangePolar = AddNum as (Double,Double)
? ? ? ? let y = ChangePolar
? ? ? ? Add(x: x, y: y)
? ? ? ? //調(diào)用Add方法,進(jìn)行運(yùn)算
? ? }
? ? public mutating func ChangeSubtract(SubNum:(mod:Double,arg:Double)){
? ? ? ? ChangePolar = FirstPolar as (Double,Double)
? ? ? ? let x = ChangePolar
? ? ? ? ChangePolar = SubNum as (Double,Double)
? ? ? ? let y = ChangePolar
? ? ? ? Subtract(x: x, y: y)
? ? }
? ? public mutating func ChangeMultiply(MulNum:(mod:Double,arg:Double)){
? ? ? ? ChangePolar = FirstPolar as (Double,Double)
? ? ? ? let x = ChangePolar
? ? ? ? ChangePolar = MulNum as (Double,Double)
? ? ? ? let y = ChangePolar
? ? ? ? Multiply(x: x, y: y)
? ? }
? ? public mutating func ChangeDiv(DivNum:(mod:Double,arg:Double)){
? ? ? ? Divide(x: FirstPolar, y: DivNum)
? ? }
? ??
? ? //加減乘除方法
? ? public func Add(x:(real:Double,img:Double),y:(real:Double,img:Double)){
? ? ? ? let answer = (x.real + y.real,x.img + y.img)
? ? ? ? print(judgment(parameter: answer))
? ? }
? ? public func Subtract(x:(real:Double,img:Double),y:(real:Double,img:Double)){
? ? ? ? let answer = (x.real - y.real,x.img - y.img)
? ? ? ? print(judgment(parameter: answer))
? ? }
? ? public func Multiply(x:(real:Double,img:Double),y:(real:Double,img:Double)){
? ? ? ? let answer = (x.real * y.real - x.img * y.img,x.real * y.real + x.img * y.img)
? ? ? ? print(judgment(parameter: answer))
? ? }
? ? public mutating func Divide(x:(mod:Double,arg:Double),y:(mod:Double,arg:Double)){
? ? ? ? let answer = ((x.mod / y.mod),(x.arg - y.arg))
// ? ? ? ChangePolar = answer
? ? ? ? print(judgment(parameter: answer))
? ? }
? ?}以及對(duì)應(yīng)的測(cè)試
var test = Complex(PolarNumber: (mod: 10.63, arg: 0.852))//7,8 //加法 test.ChangeAdd(AddNum: (mod: 2.2361, arg: 1.107))//8.0+10.0?? //減法 test.ChangeSubtract(SubNum:(mod: 2.2361, arg: 1.107))//5.999+6.0?? //乘法 test.ChangeMultiply(MulNum: (mod: 2.2361, arg: 1.107))//-8.997+23.001?? //除法 test.ChangeDiv(DivNum:(mod: 2.2361, arg: 1.107))//4.754-0.255??##?
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SwiftUI 中創(chuàng)建反彈動(dòng)畫的實(shí)現(xiàn)
這篇文章主要介紹了SwiftUI 中創(chuàng)建反彈動(dòng)畫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Swift Extension擴(kuò)展得使用詳細(xì)介紹
在swift中,extension與Objective-C的category有點(diǎn)類似,但是extension比起category來說更加強(qiáng)大和靈活,它不僅可以擴(kuò)展某種類型或結(jié)構(gòu)體的方法,同時(shí)它還可以與protocol等結(jié)合使用,編寫出更加靈活和強(qiáng)大的代碼2022-09-09
Swift實(shí)現(xiàn)表格視圖單元格單選(1)
這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)表格視圖單元格單選,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
swift中defer幾個(gè)簡(jiǎn)單的使用場(chǎng)景詳解
在Swift 2.0中,Apple提供了defer關(guān)鍵字,讓我們可以實(shí)現(xiàn)同樣的效果,這篇文章主要介紹了關(guān)于swift中defer幾個(gè)簡(jiǎn)單的使用場(chǎng)景的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用defer具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧。2018-03-03

