Swift

关注公众号 jb51net

关闭
首页 > 软件编程 > Swift > Swift复数计算器

Swift实现复数计算器

作者:正在爬的程序猿

这篇文章主要为大家详细介绍了Swift实现复数计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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𝒊## 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文