From a539ef270a0c08420a2f65ed1badedf4d33652a8 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Fri, 22 Mar 2024 11:44:03 +0100 Subject: [PATCH] zad4 --- zad4/.ccls | 21 ++++++++++ zad4/include/wrapper.h | 18 ++++++++ zad4/iter/module.adb | 95 ++++++++++++++++++++++++++++++++++++++++++ zad4/rec/module.adb | 76 +++++++++++++++++++++++++++++++++ zad4/src/main.c | 21 ++++++++++ zad4/src/module.ads | 31 ++++++++++++++ zad4/zad4.gpr | 11 +++++ 7 files changed, 273 insertions(+) create mode 100644 zad4/.ccls create mode 100644 zad4/include/wrapper.h create mode 100644 zad4/iter/module.adb create mode 100644 zad4/rec/module.adb create mode 100644 zad4/src/main.c create mode 100644 zad4/src/module.ads create mode 100644 zad4/zad4.gpr diff --git a/zad4/.ccls b/zad4/.ccls new file mode 100644 index 0000000..9f91bd0 --- /dev/null +++ b/zad4/.ccls @@ -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 diff --git a/zad4/include/wrapper.h b/zad4/include/wrapper.h new file mode 100644 index 0000000..8b3531f --- /dev/null +++ b/zad4/include/wrapper.h @@ -0,0 +1,18 @@ +#ifndef _JPP_L1_Z4_WRAPPER_H +#define _JPP_L1_Z4_WRAPPER_H + +#include + +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 diff --git a/zad4/iter/module.adb b/zad4/iter/module.adb new file mode 100644 index 0000000..07f9af2 --- /dev/null +++ b/zad4/iter/module.adb @@ -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; diff --git a/zad4/rec/module.adb b/zad4/rec/module.adb new file mode 100644 index 0000000..a9eea03 --- /dev/null +++ b/zad4/rec/module.adb @@ -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; diff --git a/zad4/src/main.c b/zad4/src/main.c new file mode 100644 index 0000000..a63b857 --- /dev/null +++ b/zad4/src/main.c @@ -0,0 +1,21 @@ +#include + +#include +#include +#include + +int main(int argc, char *argv[]) { + if (argc != 4) { + fprintf(stderr, "usage: %s \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; +} diff --git a/zad4/src/module.ads b/zad4/src/module.ads new file mode 100644 index 0000000..208e41b --- /dev/null +++ b/zad4/src/module.ads @@ -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; diff --git a/zad4/zad4.gpr b/zad4/zad4.gpr new file mode 100644 index 0000000..66e2379 --- /dev/null +++ b/zad4/zad4.gpr @@ -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;