This repository has been archived on 2024-01-18. You can view files and clone it, but cannot push or open issues or pull requests.
Turner-CS4308-Homework_4/complexcalculator.adb
2023-10-25 10:52:17 -04:00

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;