From 26a7b3d447cf22514d60f0d0e2852aa522fcce25 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Fri, 22 Mar 2024 13:09:09 +0100 Subject: [PATCH] general improvements including rust arg taking, rust feature work and changing the name of the executable in ada --- zad2/zad2.gpr | 4 ++++ zad3/Cargo.toml | 4 ++++ zad3/src/main.rs | 21 ++++++++++----------- zad4/zad4.gpr | 4 ++++ zad5/zad5.gpr | 4 ++++ zad6/Cargo.toml | 4 ++++ zad6/build.rs | 10 +++++++++- zad6/src/main.rs | 18 +++++++++++++++--- 8 files changed, 54 insertions(+), 15 deletions(-) diff --git a/zad2/zad2.gpr b/zad2/zad2.gpr index 9e47a59..64ed43a 100644 --- a/zad2/zad2.gpr +++ b/zad2/zad2.gpr @@ -6,4 +6,8 @@ project Zad2 is for Source_Dirs use ("src", Impl); for Object_Dir use "target"; + package Builder is + for Executable ("main.adb") use "z2"; + end Builder; + end Zad2; diff --git a/zad3/Cargo.toml b/zad3/Cargo.toml index c565456..f7f50dc 100644 --- a/zad3/Cargo.toml +++ b/zad3/Cargo.toml @@ -2,3 +2,7 @@ name = "zad3" version = "0.1.0" edition = "2021" + +[features] +iter = [] +rec = [] diff --git a/zad3/src/main.rs b/zad3/src/main.rs index 2d9db80..503f7ad 100644 --- a/zad3/src/main.rs +++ b/zad3/src/main.rs @@ -6,29 +6,28 @@ use crate::module::{Module, ModuleIter, ModuleRec}; fn main() { let args = env::args().collect::>(); - if args.len() != 2 { - eprintln!("usage: {} " /* "*/, args[0]); + if args.len() != 4 { + eprintln!("usage: {} ", args[0]); exit(1); } - let r#impl = &args[1]; - // let a = args[2].parse::().expect("failed to parse a"); - // let b = args[3].parse::().expect("failed to parse b"); - // let c = args[4].parse::().expect("failed to parse c"); + let a = args[1].parse::().expect("failed to parse a"); + let b = args[2].parse::().expect("failed to parse b"); + let c = args[3].parse::().expect("failed to parse c"); let module: Box = - if r#impl == "iter" { + if cfg!(feature = "iter") { Box::new(ModuleIter {}) - } else if r#impl == "rec" { + } else if cfg!(feature = "rec") { Box::new(ModuleRec {}) } else { eprintln!("unknown module type"); exit(1); }; - println!("gcd(a, b): {}", module.gcd(10, 5)); - println!("factorial(a): {}", module.factorial(5)); - let r = module.diophantine(1027, 712, 1); + println!("gcd(a, b): {}", module.gcd(a as u64, b as u64)); + println!("factorial(a): {}", module.factorial(a as u64)); + let r = module.diophantine(a, b, c); print!("diophantine(a, b, c): "); if r.is_none() { println!("there's no solution"); diff --git a/zad4/zad4.gpr b/zad4/zad4.gpr index 66e2379..a8812b7 100644 --- a/zad4/zad4.gpr +++ b/zad4/zad4.gpr @@ -8,4 +8,8 @@ project Zad4 is for Source_Dirs use ("src", "include", Impl); for Object_Dir use "target"; + package Builder is + for Executable ("main.c") use "z4"; + end Builder; + end Zad4; diff --git a/zad5/zad5.gpr b/zad5/zad5.gpr index 35a6258..eb62e5d 100644 --- a/zad5/zad5.gpr +++ b/zad5/zad5.gpr @@ -8,4 +8,8 @@ project Zad5 is for Source_Dirs use ("src", Impl); for Object_Dir use "target"; + package Builder is + for Executable ("main.adb") use "z5"; + end Builder; + end Zad5; diff --git a/zad6/Cargo.toml b/zad6/Cargo.toml index 2993348..e34f79f 100644 --- a/zad6/Cargo.toml +++ b/zad6/Cargo.toml @@ -8,3 +8,7 @@ libc = "0.2.153" [build-dependencies] cc = "1.0.90" + +[features] +iter = [] +rec = [] diff --git a/zad6/build.rs b/zad6/build.rs index f8602ff..4659dcb 100644 --- a/zad6/build.rs +++ b/zad6/build.rs @@ -1,8 +1,16 @@ extern crate cc; fn main() { + let module = + if cfg!(feature = "iter") { + "src/mod_iter.c" + } else if cfg!(feature = "rec") { + "src/mod_rec.c" + } else { + panic!("unknown module type") + }; cc::Build::new() - .file("src/mod_iter.c") + .file(module) .shared_flag(true) .compile("mod.a"); } diff --git a/zad6/src/main.rs b/zad6/src/main.rs index 24d3ad7..b63f4d3 100644 --- a/zad6/src/main.rs +++ b/zad6/src/main.rs @@ -1,3 +1,5 @@ +use std::{env, process::exit}; + #[repr(C)] pub struct Result { pub resultExists: libc::c_int, @@ -15,9 +17,19 @@ extern "C" { fn main() { unsafe { - println!("gcd(a, b): {}", gcd(10, 5)); - println!("factorial(a): {}", factorial(5)); - let r = diophantine(1027, 712, 1); + let args = env::args().collect::>(); + if args.len() != 4 { + eprintln!("usage: {} ", args[0]); + exit(1); + } + + let a = args[1].parse::().expect("failed to parse a"); + let b = args[2].parse::().expect("failed to parse b"); + let c = args[3].parse::().expect("failed to parse c"); + + println!("gcd(a, b): {}", gcd(a as u32, b as u32)); + println!("factorial(a): {}", factorial(a as u32)); + let r = diophantine(a, b, c); print!("diophantine(a, b, c): "); if r.resultExists == 0 { println!("there's no solution");