SoFunction
Updated on 2025-04-10

Swift implements complex calculator

This article shares the specific code of Swift to implement the plural calculator for your reference. The specific content is as follows

Swift's complex calculator using Cartesian and polar coordinates for conversion

import Foundation
struct Complex{
    //Real real    public var real : Double = 0
    //Image 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))
        
    }
    //Cartesian to polar coordinates    public var ChangeCartesian :(mod:Double,arg:Double){
        get{
            return polar
        }
        set(CartesianNum){
             = sqrt(( * )+( * ))
             = atan2(, )
        }
    }
    //Polar coordinates Cartesian    public var ChangePolar : (real:Double,img:Double){
        get{
            return Cartesian
        }
        set(PolarNum){
             =  * cos()
             =  * sin()
        }
    }
    //judge    public func judgment(parameter:(real:Double,img:Double))->String {
        
            if  > 0 {
                return "\(Double(round( * 1000)/1000))+\(Double(round( * 1000)/1000))𝒊"
            } else if  < 0 {
                return "\(Double(round( * 1000)/1000))-\(-(Double(round( * 1000)/1000)))𝒊"
               } else {
                return "\(Double(round( * 1000)/1000))"
               }
           }
    //Convert format before calculation    public mutating func ChangeAdd(AddNum:(mod:Double,arg:Double)){
        ChangePolar = FirstPolar as (Double,Double)
        //The initial value is converted by set        let x = ChangePolar
        // Use x to make a storage        ChangePolar = AddNum as (Double,Double)
        let y = ChangePolar
        Add(x: x, y: y)
        //Call Add method to perform operations    }
    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)
    }
    
    //Additional, subtraction, multiplication and division method    public func Add(x:(real:Double,img:Double),y:(real:Double,img:Double)){
        let answer = ( + , + )
        print(judgment(parameter: answer))
    }
    public func Subtract(x:(real:Double,img:Double),y:(real:Double,img:Double)){
        let answer = ( - , - )
        print(judgment(parameter: answer))
    }
    public func Multiply(x:(real:Double,img:Double),y:(real:Double,img:Double)){
        let answer = ( *  -  * , *  +  * )
        print(judgment(parameter: answer))
    }
    public mutating func Divide(x:(mod:Double,arg:Double),y:(mod:Double,arg:Double)){
        let answer = (( / ),( - ))
//       ChangePolar = answer
        print(judgment(parameter: answer))
    }
   }

And the corresponding test

var test = Complex(PolarNumber: (mod: 10.63, arg: 0.852))//7,8
//addition(AddNum: (mod: 2.2361, arg: 1.107))//8.0+10.0𝒊
//Subtraction(SubNum:(mod: 2.2361, arg: 1.107))//5.999+6.0𝒊
//multiplication(MulNum: (mod: 2.2361, arg: 1.107))//-8.997+23.001𝒊
//division(DivNum:(mod: 2.2361, arg: 1.107))//4.754-0.255𝒊## 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.