This commit is contained in:
jacekpoz 2024-03-22 12:36:34 +01:00
parent 345ce6bf81
commit d1249eb38b
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
7 changed files with 244 additions and 0 deletions

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

23
zad6/Cargo.lock generated Normal file
View file

@ -0,0 +1,23 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cc"
version = "1.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "zad6"
version = "0.1.0"
dependencies = [
"cc",
"libc",
]

10
zad6/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "zad6"
version = "0.1.0"
edition = "2021"
[dependencies]
libc = "0.2.153"
[build-dependencies]
cc = "1.0.90"

8
zad6/build.rs Normal file
View file

@ -0,0 +1,8 @@
extern crate cc;
fn main() {
cc::Build::new()
.file("src/mod_iter.c")
.shared_flag(true)
.compile("mod.a");
}

27
zad6/src/main.rs Normal file
View file

@ -0,0 +1,27 @@
#[repr(C)]
pub struct Result {
pub resultExists: libc::c_int,
pub n: libc::c_int,
pub m: libc::c_int,
}
extern "C" {
pub fn gcd(a: libc::c_uint, b: libc::c_uint) -> libc::c_uint;
pub fn factorial(n: libc::c_uint) -> libc::c_uint;
pub fn diophantine(a: libc::c_int, b: libc::c_int, c: libc::c_int) -> Result;
}
fn main() {
unsafe {
println!("gcd(a, b): {}", gcd(10, 5));
println!("factorial(a): {}", factorial(5));
let r = diophantine(1027, 712, 1);
if r.resultExists == 0 {
println!("dupa");
} else {
println!("diophantine(a, b, c): {}, {}", r.n, r.m);
}
}
}

87
zad6/src/mod_iter.c Normal file
View file

@ -0,0 +1,87 @@
#include <stdbool.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;
}
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;
}
typedef struct {
bool resultExists;
int n;
int m;
} Result;
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;
}

68
zad6/src/mod_rec.c Normal file
View file

@ -0,0 +1,68 @@
#include <stdbool.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);
}
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;
}
typedef struct {
bool resultExists;
int n;
int m;
} Result;
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;
}