51 lines
1.6 KiB
Ada
51 lines
1.6 KiB
Ada
with Ada.Text_IO;
|
|
|
|
package body ComplexCalculator is
|
|
|
|
-- Adds two complex numbers
|
|
function Add(First, Second : ComplexNumber) return ComplexNumber is
|
|
Result : ComplexNumber;
|
|
|
|
begin
|
|
Result.Real := First.Real + Second.Real;
|
|
Result.Imaginary := First.Imaginary + Second.Imaginary;
|
|
return Result;
|
|
|
|
end Add;
|
|
|
|
-- Subtracts to complex Numbers
|
|
function Sub(First, Second : ComplexNumber) return ComplexNumber is
|
|
Result : ComplexNumber;
|
|
|
|
begin
|
|
Result.Real := First.Real - Second.Real;
|
|
Result.Imaginary := First.Imaginary - Second.Imaginary;
|
|
return Result;
|
|
|
|
end Sub;
|
|
|
|
-- Divides two complex numbers
|
|
function Divide(First, Second : ComplexNumber) return ComplexNumber is
|
|
Result : ComplexNumber;
|
|
Bottom : Float; -- improved readability & writability due to it repeating
|
|
begin
|
|
Bottom := (First.Real ** 2) + (Second.Imaginary ** 2);
|
|
|
|
Result.Real := ((First.Real * Second.Real) + (First.Imaginary * Second.Imaginary)) / Bottom;
|
|
Result.Imaginary := ((First.Imaginary * Second.Real) - (First.Real * Second.Imaginary)) / Bottom;
|
|
return Result;
|
|
|
|
end Divide;
|
|
|
|
-- Multiply two complex numbers
|
|
function Multiply(First, Second : ComplexNumber) return ComplexNumber is
|
|
Result : ComplexNumber;
|
|
|
|
begin
|
|
Result.Real := (First.Real * Second.Real) - (First.Imaginary - Second.Imaginary);
|
|
Result.Imaginary := (First.Real * Second.Imaginary) + (First.Imaginary * Second.Real);
|
|
return Result;
|
|
|
|
end Multiply;
|
|
end ComplexCalculator;
|