This commit is contained in:
jacekpoz 2024-03-21 23:14:44 +01:00
parent 6aff3e3706
commit 2e06edb393
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
7 changed files with 301 additions and 0 deletions

1
zad2/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
target/

95
zad2/src/iter/module.adb Normal file
View file

@ -0,0 +1,95 @@
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package body Module is
function factorial(n : Natural) return Natural is
result : Natural := 1;
begin
for i in 1 .. n loop
result := result * i;
end loop;
return result;
end factorial;
function gcd(a : Natural; b : Natural) return Natural is
r : Natural;
aa : Natural := a;
bb : Natural := b;
begin
if a = 0 then
return b;
end if;
while bb > 0 loop
r := aa mod bb;
aa := bb;
bb := r;
end loop;
return aa;
end gcd;
function gcdExtended(a : Integer; b : Integer; x : out Integer; y : out Integer) return Integer is
x1 : Integer := 0;
y1 : Integer := 1;
q : Integer;
tempX, tempY, tempB : Integer;
aa : Integer := a;
bb : Integer := b;
begin
x := 1;
y := 0;
while bb /= 0 loop
q := aa / bb;
tempX := x - q * x1;
tempY := y - q * y1;
x := x1;
y := y1;
x1 := tempX;
y1 := tempY;
tempB := aa - q * bb;
aa := bb;
bb := tempB;
end loop;
return aa;
end gcdExtended;
function diophantine(a : Integer; b : Integer; c : Integer) return Result is
r : Result;
x0 : Integer := 0;
y0 : Integer := 0;
g : Integer := gcdExtended(abs a, abs b, x0, y0);
begin
r.n := 0;
r.m := 0;
r.resultExists := False;
if a = 0 and b = 0 then
return r;
end if;
if (c rem g) /= 0 then
return r;
end if;
x0 := x0 * (c / g);
y0 := y0 * (c / g);
if a < 0 then
x0 := -x0;
end if;
if b < 0 then
y0 := -y0;
end if;
r.n := x0;
r.m := y0;
r.resultExists := True;
return r;
end diophantine;
end Module;

24
zad2/src/main.adb Normal file
View file

@ -0,0 +1,24 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Module;
procedure Main is
use Module;
gcdResult : Natural;
factorialResult : Natural;
diophantineResult : Result;
begin
gcdResult := gcd(10, 5);
factorialResult := factorial(5);
diophantineResult := diophantine(1027, 712, 1);
Put_Line("gcd(a, b):" & Natural'Image(gcdResult));
Put_Line("factorial(a):" & Natural'Image(factorialResult));
if not diophantineResult.resultExists then
Put_Line("dupa");
else
Put_Line("diophantine(a, b, c): " & Integer'Image(diophantineResult.n) & "," & Integer'Image(diophantineResult.m));
end if;
end Main;

76
zad2/src/module.adb Normal file
View file

@ -0,0 +1,76 @@
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package body Module is
function factorial(n : Natural) return Natural is
begin
if (n = 0 or n = 1 ) then
return 1;
end if;
return n * factorial(n - 1);
end factorial;
function gcd(a : Natural; b : Natural) return Natural is
begin
if b = 0 then
return a;
end if;
return gcd(b, a mod b);
end gcd;
function gcdExtended(a : Integer; b : Integer; x : out Integer; y : out Integer) return Integer is
x1 : Integer;
y1 : Integer;
d : Integer;
begin
if b = 0 then
x := 1;
y := 0;
return a;
end if;
d := gcdExtended(b, a rem b, x1, y1);
x := y1;
y := x1 - y1 * (a / b);
return d;
end gcdExtended;
function diophantine(a : Integer; b : Integer; c : Integer) return Result is
r : Result;
x0 : Integer := 0;
y0 : Integer := 0;
g : Integer := gcdExtended(abs a, abs b, x0, y0);
begin
r.n := 0;
r.m := 0;
r.resultExists := False;
if a = 0 and b = 0 then
return r;
end if;
if (c rem g) /= 0 then
return r;
end if;
x0 := x0 * (c / g);
y0 := y0 * (c / g);
if a < 0 then
x0 := -x0;
end if;
if b < 0 then
y0 := -y0;
end if;
r.n := x0;
r.m := y0;
r.resultExists := True;
return r;
end diophantine;
end Module;

20
zad2/src/module.ads Normal file
View file

@ -0,0 +1,20 @@
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package Module is
type Result is record
resultExists : Boolean;
n : Integer;
m : Integer;
end record;
function factorial(n : Natural) return Natural;
function gcd(a : Natural; b : Natural) return Natural;
function gcdExtended(a : Integer; b : Integer; x : out Integer; y : out Integer) return Integer;
function diophantine(a : Integer; b : Integer; c : Integer) return Result;
end Module;

76
zad2/src/rec/module.adb Normal file
View file

@ -0,0 +1,76 @@
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package body Module is
function factorial(n : Natural) return Natural is
begin
if (n = 0 or n = 1 ) then
return 1;
end if;
return n * factorial(n - 1);
end factorial;
function gcd(a : Natural; b : Natural) return Natural is
begin
if b = 0 then
return a;
end if;
return gcd(b, a mod b);
end gcd;
function gcdExtended(a : Integer; b : Integer; x : out Integer; y : out Integer) return Integer is
x1 : Integer;
y1 : Integer;
d : Integer;
begin
if b = 0 then
x := 1;
y := 0;
return a;
end if;
d := gcdExtended(b, a rem b, x1, y1);
x := y1;
y := x1 - y1 * (a / b);
return d;
end gcdExtended;
function diophantine(a : Integer; b : Integer; c : Integer) return Result is
r : Result;
x0 : Integer := 0;
y0 : Integer := 0;
g : Integer := gcdExtended(abs a, abs b, x0, y0);
begin
r.n := 0;
r.m := 0;
r.resultExists := False;
if a = 0 and b = 0 then
return r;
end if;
if (c rem g) /= 0 then
return r;
end if;
x0 := x0 * (c / g);
y0 := y0 * (c / g);
if a < 0 then
x0 := -x0;
end if;
if b < 0 then
y0 := -y0;
end if;
r.n := x0;
r.m := y0;
r.resultExists := True;
return r;
end diophantine;
end Module;

9
zad2/zad2.gpr Normal file
View file

@ -0,0 +1,9 @@
project Zad2 is
type Impl_Type is ("iter", "rec");
Impl : Impl_Type := external ("impl", "iter");
for Main use ("main");
for Source_Dirs use ("src", "src/" & Impl);
for Object_Dir use "target";
end Zad2;