mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-11-14 07:10:48 -05:00
31 lines
818 B
Zig
31 lines
818 B
Zig
pub const TriangleError = error{Invalid};
|
|
|
|
pub const Triangle = struct {
|
|
a: f64,
|
|
b: f64,
|
|
c: f64,
|
|
|
|
fn isValid(a: f64, b: f64, c: f64) bool {
|
|
return (a > 0 and b > 0 and c > 0) and (a + b >= c and b + c >= a and a + c >= b);
|
|
}
|
|
|
|
pub fn init(a: f64, b: f64, c: f64) TriangleError!Triangle {
|
|
if (!isValid(a, b, c)) {
|
|
return error.Invalid;
|
|
}
|
|
|
|
return Triangle{ .a = a, .b = b, .c = c };
|
|
}
|
|
|
|
pub fn isEquilateral(self: Triangle) bool {
|
|
return self.a == self.b and self.b == self.c;
|
|
}
|
|
|
|
pub fn isIsosceles(self: Triangle) bool {
|
|
return self.a == self.b or self.a == self.c or self.b == self.c;
|
|
}
|
|
|
|
pub fn isScalene(self: Triangle) bool {
|
|
return self.a != self.b and self.a != self.c and self.b != self.c;
|
|
}
|
|
};
|