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𝒊

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𝒊## 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值