You've already forked book-exercises
functions, loops and control flow
This commit is contained in:
6
functions/Cargo.toml
Normal file
6
functions/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "functions"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
71
functions/src/main.rs
Normal file
71
functions/src/main.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
// fn main() {
|
||||
// print_labelled_measurements(5, 'h');
|
||||
// }
|
||||
//
|
||||
// fn print_labelled_measurements(value: i32, unit_label: char) {
|
||||
// println!("The measurement is: {value}{unit_label}");
|
||||
// }
|
||||
//
|
||||
// // a variable
|
||||
//
|
||||
use std::io::{self};
|
||||
|
||||
fn main() {
|
||||
println!(
|
||||
"welcome to our rudimentary calculator!\nwe can do two things:\n1.factorial.2.square\nwhich one do you want?\n\t"
|
||||
);
|
||||
loop {
|
||||
let mut input = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("couldn't read line!");
|
||||
|
||||
let input: i32 = input.trim().parse().expect("numbers only");
|
||||
|
||||
if input > 2 || input < 0 {
|
||||
println!("aye choose from the options foo");
|
||||
break;
|
||||
}
|
||||
|
||||
println!("\nalright and what number to operate on?\n\t");
|
||||
|
||||
let mut number = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut number)
|
||||
.expect("couldn't read line!");
|
||||
|
||||
let number: i32 = number
|
||||
.trim()
|
||||
.parse()
|
||||
.expect("aye foo enter a numberrr quit playin");
|
||||
|
||||
if input == 1 {
|
||||
println!("\nthe factorial of {number} is {}", factorial(number));
|
||||
break;
|
||||
} else {
|
||||
println!("\nthe square of {number} is {}", square(number));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fn square(x: i32) -> i32 {
|
||||
return x * x;
|
||||
}
|
||||
|
||||
//TODO: fix factorial function after you figure out loops
|
||||
//NOTE: we have implemented factorial in the the loops sub-directory
|
||||
fn factorial(x: i32) -> i32 {
|
||||
let mut rev = x - 1;
|
||||
let mut result = x;
|
||||
loop {
|
||||
result *= rev;
|
||||
rev -= 1;
|
||||
|
||||
if rev < 3 {
|
||||
break result * 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
loops/Cargo.toml
Normal file
6
loops/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "loops"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
6
loops/christmas/Cargo.toml
Normal file
6
loops/christmas/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "christmas"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
42
loops/christmas/src/main.rs
Normal file
42
loops/christmas/src/main.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
//this probably can be made better
|
||||
fn main() {
|
||||
let cardinals: [&str; 12] = [
|
||||
"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth",
|
||||
"tenth", "eleventh", "twelfth",
|
||||
];
|
||||
|
||||
let lines: [&str; 12] = [
|
||||
"A partridge in a pear tree",
|
||||
"Two turtle doves",
|
||||
"Three French hens",
|
||||
"Four colly birds",
|
||||
"Five gold rings",
|
||||
"Six geese a-laying",
|
||||
"Seven swans a-swimming",
|
||||
"Eight maids a-milking",
|
||||
"Nine drummers drumming",
|
||||
"Ten pipers piping",
|
||||
"Eleven ladies dancing",
|
||||
"Twelve fiddlers fiddling",
|
||||
];
|
||||
|
||||
for mut i in 1..13 {
|
||||
println!("The {} day of Christmas,", cardinals[i - 1]);
|
||||
println!("My true love sent to me");
|
||||
|
||||
while i != 0 {
|
||||
while i != 0 {
|
||||
if i == 1 {
|
||||
println!("{}.\n", lines[i - 1]);
|
||||
} else {
|
||||
if i == 2 {
|
||||
println!("{}, and", lines[i - 1]);
|
||||
} else {
|
||||
println!("{},", lines[i - 1]);
|
||||
}
|
||||
}
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
loops/fibonacci/Cargo.toml
Normal file
6
loops/fibonacci/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "fibonacci"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
29
loops/fibonacci/src/main.rs
Normal file
29
loops/fibonacci/src/main.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use std::io;
|
||||
|
||||
fn fib(x: i32) -> i32 {
|
||||
if x == 1 {
|
||||
return 0;
|
||||
} else if x == 2 {
|
||||
return 1;
|
||||
} else {
|
||||
return fib(x - 1) + fib(x - 2);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("welcome to the fibo party!");
|
||||
println!("which fibonacci number would you like to see?");
|
||||
|
||||
let mut input = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("error storing user input");
|
||||
|
||||
let input: i32 = input
|
||||
.trim()
|
||||
.parse()
|
||||
.expect("error converting to number: did you enter a non-number?");
|
||||
|
||||
println!("fibonacci number {input} is {}", fib(input));
|
||||
}
|
||||
17
loops/src/main.rs
Normal file
17
loops/src/main.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
fn main() {
|
||||
let fac: i32 = factorial(5);
|
||||
println!("{fac}");
|
||||
}
|
||||
|
||||
fn factorial(x: i32) -> i32 {
|
||||
let mut rev = x - 1;
|
||||
let mut result = x;
|
||||
loop {
|
||||
result *= rev;
|
||||
rev -= 1;
|
||||
|
||||
if rev == 1 {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
6
loops/tempverter/Cargo.toml
Normal file
6
loops/tempverter/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "tempverter"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
49
loops/tempverter/src/main.rs
Normal file
49
loops/tempverter/src/main.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
println!("welcome to tempverter!");
|
||||
println!("please enter your temp:");
|
||||
|
||||
let mut input = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("error storing user input");
|
||||
|
||||
let input: i32 = input
|
||||
.trim()
|
||||
.parse()
|
||||
.expect("error storing temperature - did you enter a number?");
|
||||
|
||||
println!("DEBUG: you entered {input}");
|
||||
|
||||
println!("is this in C or F?");
|
||||
|
||||
let mut input_unit = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut input_unit)
|
||||
.expect("error storing user input");
|
||||
|
||||
let input_unit: char = input_unit
|
||||
.trim()
|
||||
.parse()
|
||||
//NOTE: it won't panic when you enter a number
|
||||
//because char is an unsigned 32-bit integer
|
||||
//so it will akshually panic when you enter a negative number
|
||||
.expect("error storing temperature unit - did you enter a char?");
|
||||
|
||||
if input_unit == 'c' || input_unit == 'C' {
|
||||
println!("{input}C is {}F", converter(input, 1));
|
||||
} else if input_unit == 'f' || input_unit == 'F' {
|
||||
println!("{input}F is {}C", converter(input, 2));
|
||||
}
|
||||
}
|
||||
|
||||
fn converter(input: i32, choice: i32) -> i32 {
|
||||
if choice == 1 {
|
||||
return 32 + ((9 * input) / 5);
|
||||
} else {
|
||||
return ((input - 32) / 9) * 5;
|
||||
}
|
||||
}
|
||||
6
variables/Cargo.toml
Normal file
6
variables/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "variables"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
56
variables/src/main.rs
Normal file
56
variables/src/main.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
// fn main() {
|
||||
// let mut x = 5;
|
||||
// println!("The value of x is: {x}");
|
||||
// x = 6;
|
||||
// println!("The value of x is: {x}");
|
||||
// }
|
||||
|
||||
// fn main() {
|
||||
// // let tup: (i32, f64) = (500, 2.91);
|
||||
// let tup = (500, 2.91);
|
||||
// let t: bool = true;
|
||||
// let c: char = '✅';
|
||||
//
|
||||
// println!("t is {t}\nc is {c}\nand tup is {:?}\n\n\n", tup);
|
||||
//
|
||||
// // let (x: i32, y: f64, z) = tup;
|
||||
// let (x, y) = tup;
|
||||
// println!("the second value is: {}", y);
|
||||
// println!("the second value is: {}", tup.1);
|
||||
// }
|
||||
|
||||
fn main() {
|
||||
let months = ["jan", "feb", "mar", "apr", "may", "jun"];
|
||||
let ratings: [i32; 3] = [1, 2, 3];
|
||||
let mut employees = ["do kwon", "sbf", "tabasco", "kyle"];
|
||||
|
||||
println!("employees are: {:?}", employees);
|
||||
|
||||
let mut employee_of_the_month: (&str, &str, i32) = ("jan", "do kwon", 2);
|
||||
|
||||
let (x, y, z) = employee_of_the_month;
|
||||
println!(
|
||||
"\nin {x}, {y} was the employee of the month with a rating of {}",
|
||||
employee_of_the_month.2
|
||||
);
|
||||
|
||||
println!("\ncaroline replaced kyle");
|
||||
employees[3] = "caroline";
|
||||
|
||||
println!("employees are: {:?}", employees);
|
||||
|
||||
employee_of_the_month = (months[1], employees[1], ratings[2]);
|
||||
|
||||
let (x, y, z) = employee_of_the_month;
|
||||
println!("\nin {x}, {y} was the employee of the month with a rating of {z}");
|
||||
|
||||
employee_of_the_month = (months[2], employees[2], ratings[1]);
|
||||
|
||||
let (x, y, z) = employee_of_the_month;
|
||||
println!("\nin {x}, {y} was the employee of the month with a rating of {z}");
|
||||
|
||||
employee_of_the_month = (months[3], employees[3], ratings[0]);
|
||||
|
||||
let (x, y, z) = employee_of_the_month;
|
||||
println!("\nin {x}, {y} was the employee of the month with a rating of {z}");
|
||||
}
|
||||
Reference in New Issue
Block a user