網站首頁 編程語言 正文
本文實例為大家分享了Swift實現復數計算器的具體代碼,供大家參考,具體內容如下
Swift使用笛卡爾和極坐標進行轉換的復數計算器
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))?" ? ? ? ? ? ? } 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))" ? ? ? ? ? ? ? ?} ? ? ? ? ? ?} ? ? //計算前進行格式轉換 ? ? public mutating func ChangeAdd(AddNum:(mod:Double,arg:Double)){ ? ? ? ? ChangePolar = FirstPolar as (Double,Double) ? ? ? ? //初始值通過set進行了轉換 ? ? ? ? let x = ChangePolar ? ? ? ? //用x進行一個存儲 ? ? ? ? ChangePolar = AddNum as (Double,Double) ? ? ? ? let y = ChangePolar ? ? ? ? Add(x: x, y: y) ? ? ? ? //調用Add方法,進行運算 ? ? } ? ? 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)) ? ? } ? ?}
以及對應的測試
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?##?
原文鏈接:https://blog.csdn.net/papay_Ida/article/details/109282365
- 上一篇:WPF基本控件介紹_基礎應用
- 下一篇:C語言互換最大值與最小值的位置_C 語言
相關推薦
- 2022-11-01 Android串口通訊SerialPort的使用詳情_Android
- 2022-04-24 C語言時間函數之mktime和difftime詳解_C 語言
- 2022-12-07 C語言內存分布與heap空間分別詳細講解_C 語言
- 2022-12-13 Python使用Matplotlib繪制三維散點圖詳解流程_python
- 2023-04-17 Python魔術方法深入分析講解_python
- 2022-04-12 Qt實現實時鼠標繪制圖形_C 語言
- 2022-03-31 nginx平滑重啟和平滑升級的圖文教程_nginx
- 2023-05-07 pytest多重斷言的實現_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支