More polishing

This commit is contained in:
Kreyren 2020-01-24 17:23:00 +00:00
parent e854a29491
commit 2108b8b22b
5 changed files with 44 additions and 31 deletions

2
Cargo.lock generated
View File

@ -6,7 +6,7 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rustlang-fibonnaci"
name = "fibonnaci_kreyren"
version = "0.1.0"
dependencies = [
"die 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -1,5 +1,5 @@
[package]
name = "rustlang-fibonnaci"
name = "fibonnaci_kreyren"
version = "0.1.0"
authors = ["Kreyren <KostWarCZE@RiXotStudio.cz>"]
edition = "2018"

28
src/bin/main.rs Normal file
View File

@ -0,0 +1,28 @@
use std::io;
use std::string::String;
use die::Die;
use fibonnaci_kreyren::fib;
macro_rules! fixme {
($msg:expr) => ( println!("FIXME: {}", $msg);)
}
fn main() {
#[allow(non_snake_case)] // Makes it more readable
let mut userInput = String::new();
// Process userInput
io::stdin().read_line(&mut userInput)
.die_code("Unexpected input 'fixme_input_value' has been parsed", 256);
let n: f64 = userInput.trim_end_matches("\n").parse()
.die_code("Invalid argument 'fixme_argument' has been parsed", 2);
// FIXME: Optimize better for numbers greater then 41
if n > 41.0 {
fixme!("Function 'fib' takes too long to process numbers greater then 41");
}
// Output
println!("{}", fib(n));
}

14
src/lib.rs Normal file
View File

@ -0,0 +1,14 @@
use die::die;
// Fibonnaci sequence
pub fn fib(n: f64) -> f64 {
if n == 0.0 {
return 0.0;
} else if n == 1.0 {
return 1.0;
} else if n > 1.0 {
return fib(n-1.0) + fib(n-2.0);
} else {
die!(256; "Unexpected happend while processing number '{}'", n);
}
}

View File

@ -1,29 +0,0 @@
use die::Die;
use die::die;
use std::io;
use std::string::String;
// Fibonnaci sequence
fn fib(n: f64) -> f64 {
if n == 0.0 {
return 0.0;
} else if n == 1.0 {
return 1.0;
} else if n > 1.0 {
return fib(n-1.0) + fib(n-2.0);
} else {
die!(256; "Unexpected happend while processing number '{}'", n);
}
}
fn main() {
#[allow(non_snake_case)]
let mut userInput = String::new();
// Process userInput
io::stdin().read_line(&mut userInput).die_code("Unexpected input 'fixme_input_value' has been parsed", 256);
let n: f64 = userInput.trim_end_matches("\n").parse().die_code("Invalid argument 'fixme_argument' has been parsed", 2);
// Output
println!("{}", fib(n));
}