This commit is contained in:
jacekpoz 2024-03-22 11:54:51 +01:00
parent a539ef270a
commit eba793b938
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
6 changed files with 244 additions and 0 deletions

21
zad5/.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

19
zad5/include/mod.h Normal file
View file

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

82
zad5/iter/mod.c Normal file
View file

@ -0,0 +1,82 @@
#include "../include/mod.h"
#include <stddef.h>
#include <stdlib.h>
unsigned int factorial(unsigned int n) {
unsigned int result = 1;
for (size_t i = 1; i <= n; ++i) {
result *= i;
}
return result;
}
unsigned int gcd(unsigned int a, unsigned int b) {
if (a == 0) return b;
size_t r;
while (b > 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
inline int extended_gcd(int a, int b, int *x, int *y) {
*x = 1;
*y = 0;
int x1 = 0;
int y1 = 1;
int q;
int temp_x, temp_y, temp_b;
while (b != 0) {
q = a / b;
temp_x = *x - q * x1;
temp_y = *y - q * y1;
*x = x1;
*y = y1;
x1 = temp_x;
y1 = temp_y;
temp_b = a - q * b;
a = b;
b = temp_b;
}
return a;
}
Result diophantine(int a, int b, int c) {
Result r = {
.resultExists = false,
.n = 0,
.m = 0,
};
if (a == 0 && b == 0)
return r;
int x0 = 0;
int y0 = 0;
int g = extended_gcd(abs(a), abs(b), &x0, &y0);
if (c % g)
return r;
x0 *= c / g;
y0 *= c / g;
if (a < 0) x0 = -x0;
if (b < 0) y0 = -y0;
r.resultExists = true;
r.n = x0;
r.m = y0;
return r;
}

63
zad5/rec/mod.c Normal file
View file

@ -0,0 +1,63 @@
#include "../include/mod.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
unsigned int factorial(unsigned int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
unsigned int gcd(unsigned int a, unsigned int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
inline int extended_gcd(int a, int b, int *x, int *y) {
if (b == 0) {
*x = 1;
*y = 0;
return a;
}
int x1, y1;
int d = extended_gcd(b, a % b, &x1, &y1);
*x = y1;
*y = x1 - y1 * (a / b);
return d;
}
Result diophantine(int a, int b, int c) {
Result r = {
.resultExists = false,
.n = 0,
.m = 0,
};
if (a == 0 && b == 0)
return r;
int x0 = 0;
int y0 = 0;
int g = extended_gcd(abs(a), abs(b), &x0, &y0);
if (c % g)
return r;
x0 *= c / g;
y0 *= c / g;
if (a < 0) x0 = -x0;
if (b < 0) y0 = -y0;
r.resultExists = true;
r.n = x0;
r.m = y0;
return r;
}

48
zad5/src/main.adb Normal file
View file

@ -0,0 +1,48 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Interfaces.C; use Interfaces.C;
procedure Main is
function gcd(a : Natural; b : Natural) return Natural
with
Import => True,
Convention => C,
External_Name => "gcd";
function factorial(n : Natural) return Natural
with
Import => 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
Import => True,
Convention => C,
External_Name => "diophantine";
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;

11
zad5/zad5.gpr Normal file
View file

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