欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Swift實(shí)現(xiàn)復(fù)數(shù)計(jì)算器

 更新時(shí)間:2022年01月27日 09:53:06   作者:正在爬的程序猿  
這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)復(fù)數(shù)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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))
? ? }
? ?}

以及對應(yīng)的測試

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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SwiftUI 中創(chuàng)建反彈動(dòng)畫的實(shí)現(xiàn)

    SwiftUI 中創(chuàng)建反彈動(dòng)畫的實(shí)現(xiàn)

    這篇文章主要介紹了SwiftUI 中創(chuàng)建反彈動(dòng)畫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Swift讀取App的版本信息與PCH文件詳解

    Swift讀取App的版本信息與PCH文件詳解

    這篇文章主要介紹了Swift讀取App的版本信息與PCH文件的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • Swift Extension擴(kuò)展得使用詳細(xì)介紹

    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)

    Swift實(shí)現(xiàn)表格視圖單元格單選(1)

    這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)表格視圖單元格單選,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 深入理解Swift中的變量與常量

    深入理解Swift中的變量與常量

    本文主要是介紹Swift中最常用的常量和變量,將從“變量常量的定義”、"如何聲明變量常量"、“變量和常量的命名”,"變量常量的本質(zhì)區(qū)別"四個(gè)方面入手,重點(diǎn)介紹變量和常量的使用以及區(qū)別,希望大家在閱讀完本文后都可以熟練使用它們。有需要的朋友們下面來一起學(xué)習(xí)吧。
    2017-01-01
  • Swift進(jìn)階教程Mirror反射示例詳解

    Swift進(jìn)階教程Mirror反射示例詳解

    這篇文章主要為大家介紹了Swift進(jìn)階教程Mirror反射示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Swift UIButton使用教程

    Swift UIButton使用教程

    這篇文章主要介紹了Swift UIButton的使用方法,幫助大家更好的理解和學(xué)習(xí)swift編程,感興趣的朋友可以了解下
    2020-09-09
  • Swift3遷移至Swift4可能遇到的問題小結(jié)

    Swift3遷移至Swift4可能遇到的問題小結(jié)

    每當(dāng)看到新的編程語言我總是會(huì)有相當(dāng)大的興趣,所以下面這篇文章主要給大家介紹了關(guān)于Swift3遷移至Swift4可能遇到的問題,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • swift中defer幾個(gè)簡單的使用場景詳解

    swift中defer幾個(gè)簡單的使用場景詳解

    在Swift 2.0中,Apple提供了defer關(guān)鍵字,讓我們可以實(shí)現(xiàn)同樣的效果,這篇文章主要介紹了關(guān)于swift中defer幾個(gè)簡單的使用場景的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用defer具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧。
    2018-03-03
  • 淺談swift 4.0中private所發(fā)生的變化

    淺談swift 4.0中private所發(fā)生的變化

    Swift 4是蘋果計(jì)劃于2017年秋季推出的最新版本,其主要重點(diǎn)是提供與Swift 3代碼的源兼容性,并努力實(shí)現(xiàn)ABI穩(wěn)定性。下面這篇文章主要給大家介紹了關(guān)于swift 4.0中private所發(fā)生的一些變化,需要的朋友可以參考下。
    2017-12-12

最新評論