This commit is contained in:
jacekpoz 2024-03-22 11:44:03 +01:00
parent 89a4d917e2
commit a539ef270a
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
7 changed files with 273 additions and 0 deletions

21
zad4/.ccls Normal file
View file

@ -0,0 +1,21 @@
gcc
%c -std=c99
%h
-Iinclude
-Wall
-Wextra
-Wpedantic
-Wstrict-aliasing
-Wfloat-equal
-Wundef
-Wshadow
-Wpointer-arith
-Wcast-align
-Wstrict-prototypes
-Wstrict-overflow=5
-Wwrite-strings
-Wcast-qual
-Wswitch-default
-Wswitch-enum
-Wconversion
-Wunreachable-code

18
zad4/include/wrapper.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef _JPP_L1_Z4_WRAPPER_H
#define _JPP_L1_Z4_WRAPPER_H
#include <stdbool.h>
extern unsigned int gcd(unsigned int a, unsigned int b);
extern unsigned int factorial(unsigned int n);
typedef struct {
bool resultExists;
int n;
int m;
} Result;
extern Result diophantine(int a, int b, int c);
#endif // _JPP_L1_Z4_WRAPPER_H

95
zad4/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;

76
zad4/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;

21
zad4/src/main.c Normal file
View file

@ -0,0 +1,21 @@
#include <wrapper.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 4) {
fprintf(stderr, "usage: %s <a> <b> <c>\n", argv[0]);
exit(EXIT_FAILURE);
}
printf("gcd(a, b): %u\n", gcd(atol(argv[1]), atol(argv[2])));
printf("factorial(a): %u\n", factorial(atol(argv[1])));
Result r = diophantine(atol(argv[1]), atol(argv[2]), atol(argv[3]));
if (!r.resultExists)
printf("dupa\n");
else
printf("diophantine(a, b, c): %d, %d\n", r.n, r.m);
return 0;
}

31
zad4/src/module.ads Normal file
View file

@ -0,0 +1,31 @@
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package Module is
function gcd(a : Natural; b : Natural) return Natural
with
Export => True,
Convention => C,
External_Name => "gcd";
function factorial(n : Natural) return Natural
with
Export => True,
Convention => C,
External_Name => "factorial";
type Result is record
resultExists : Boolean;
n : Integer;
m : Integer;
end record
with Convention => C;
function diophantine(a : Integer; b : Integer; c : Integer) return Result
with
Export => True,
Convention => C,
External_Name => "diophantine";
end Module;

11
zad4/zad4.gpr Normal file
View file

@ -0,0 +1,11 @@
project Zad4 is
type Impl_Type is ("iter", "rec");
Impl : Impl_Type := external ("impl", "iter");
for Languages use ("ada", "c");
for Main use ("main.c");
for Source_Dirs use ("src", "include", Impl);
for Object_Dir use "target";
end Zad4;