1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-11-09 22:08:58 +01:00

feat(solutions): add triangle-type/rust/function

This commit is contained in:
Divlo 2023-01-04 17:59:38 +01:00
parent 791b1a7d52
commit 1d87185e58
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
4 changed files with 62 additions and 0 deletions

View 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"

View File

@ -0,0 +1,6 @@
[package]
name = "rust_application"
version = "1.0.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,3 @@
# triangle-type/rust/function
Created by [@Divlo](https://github.com/Divlo) on 4 January 2023.

View 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());
}