mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add triangle-type/rust/function
This commit is contained in:
parent
791b1a7d52
commit
1d87185e58
7
challenges/triangle-type/solutions/rust/function/Cargo.lock
generated
Normal file
7
challenges/triangle-type/solutions/rust/function/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "rust_application"
|
||||
version = "1.0.0"
|
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "rust_application"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
@ -0,0 +1,3 @@
|
||||
# triangle-type/rust/function
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 4 January 2023.
|
46
challenges/triangle-type/solutions/rust/function/src/main.rs
Normal file
46
challenges/triangle-type/solutions/rust/function/src/main.rs
Normal file
@ -0,0 +1,46 @@
|
||||
use std::io;
|
||||
|
||||
pub struct Triangle {
|
||||
pub sides: [u32; 3],
|
||||
}
|
||||
|
||||
impl Triangle {
|
||||
pub fn sides_type(&self) -> String {
|
||||
if (self.sides[0] + self.sides[1] < self.sides[2])
|
||||
|| (self.sides[2] + self.sides[0] < self.sides[1])
|
||||
|| (self.sides[2] + self.sides[1] < self.sides[0])
|
||||
{
|
||||
return String::from("impossible");
|
||||
}
|
||||
if self.sides[0] == self.sides[1] && self.sides[1] == self.sides[2] {
|
||||
return String::from("equilateral");
|
||||
}
|
||||
if self.sides[0] == self.sides[1]
|
||||
|| self.sides[1] == self.sides[2]
|
||||
|| self.sides[2] == self.sides[0]
|
||||
{
|
||||
return String::from("isosceles");
|
||||
}
|
||||
return String::from("scalene");
|
||||
}
|
||||
|
||||
pub fn size(&self) -> usize {
|
||||
return self.sides.len();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut triangle = Triangle { sides: [0, 0, 0] };
|
||||
for index in 0..triangle.size() {
|
||||
let mut side = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut side)
|
||||
.expect("Failed to read `stdin` line.");
|
||||
let side: u32 = side
|
||||
.trim()
|
||||
.parse()
|
||||
.expect("Failed to convert `side` as an `u32`.");
|
||||
triangle.sides[index] = side;
|
||||
}
|
||||
println!("{}", triangle.sides_type());
|
||||
}
|
Loading…
Reference in New Issue
Block a user