🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # vlzware's solution

## to Triangle in the C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Determine if a triangle is equilateral, isosceles, or scalene.

An equilateral triangle has all three sides the same length.

An isosceles triangle has at least two sides the same length. (It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)

A scalene triangle has all sides of different lengths.

## Note

For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side. See Triangle Inequality.

## Dig Deeper

The case where the sum of the lengths of two sides equals that of the third is known as a degenerate triangle - it has zero area and looks like a single line. Feel free to add your own code/tests to check for degenerate triangles.

## Getting Started

Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the `test` task.

``````make test
``````

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

## Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### test_triangle.c

``````#include "vendor/unity.h"
#include "../src/triangle.h"

void setUp(void)
{
}

void tearDown(void)
{
}

void test_equilateral_is_true_if_all_sides_are_equal(void)
{
triangle_t sides = { 2, 2, 2 };
TEST_ASSERT_TRUE(is_equilateral(sides));
}

void test_equilateral_is_false_if_any_side_is_unequal(void)
{
TEST_IGNORE();               // delete this line to run test
triangle_t sides = { 2, 3, 2 };
TEST_ASSERT_FALSE(is_equilateral(sides));
}

void test_equilateral_is_false_if_all_sides_zero(void)
{
TEST_IGNORE();
triangle_t sides = { 0, 0, 0 };
TEST_ASSERT_FALSE(is_equilateral(sides));
}

void test_equilateral_sides_may_be_floats(void)
{
TEST_IGNORE();
triangle_t sides = { 0.5, 0.5, 0.5 };
TEST_ASSERT_TRUE(is_equilateral(sides));
}

void test_isosceles_is_true_if_last_two_sides_are_equal(void)
{
TEST_IGNORE();
triangle_t sides = { 3, 4, 4 };
TEST_ASSERT_TRUE(is_isosceles(sides));
}

void test_isosceles_is_true_if_first_two_sides_are_equal(void)
{
TEST_IGNORE();
triangle_t sides = { 4, 4, 3 };
TEST_ASSERT_TRUE(is_isosceles(sides));
}

void test_isosceles_is_true_if_first_and_last_sides_are_equal(void)
{
TEST_IGNORE();
triangle_t sides = { 4, 3, 4 };
TEST_ASSERT_TRUE(is_isosceles(sides));
}

void test_equilateral_triangles_are_also_isosceles(void)
{
TEST_IGNORE();
triangle_t sides = { 4, 4, 4 };
TEST_ASSERT_TRUE(is_isosceles(sides));
}

void test_isosceles_is_false_if_no_sides_are_equal(void)
{
TEST_IGNORE();
triangle_t sides = { 2, 3, 4 };
TEST_ASSERT_FALSE(is_isosceles(sides));
}

void test_isosceles_is_false_if_two_sides_equal_and_violate_inequality(void)
{
TEST_IGNORE();
triangle_t sides = { 1, 1, 3 };
TEST_ASSERT_FALSE(is_isosceles(sides));
}

void test_isosceles_sides_may_be_floats(void)
{
TEST_IGNORE();
triangle_t sides = { 0.5, 0.4, 0.5 };
TEST_ASSERT_TRUE(is_isosceles(sides));
}

void test_scalene_is_true_if_no_sides_are_equal(void)
{
TEST_IGNORE();
triangle_t sides = { 5, 4, 6 };
TEST_ASSERT_TRUE(is_scalene(sides));
}

void test_scalene_is_false_if_all_sides_are_equal(void)
{
TEST_IGNORE();
triangle_t sides = { 4, 4, 4 };
TEST_ASSERT_FALSE(is_scalene(sides));
}

void test_scalene_is_false_if_two_sides_are_equal(void)
{
TEST_IGNORE();
triangle_t sides = { 4, 4, 3 };
TEST_ASSERT_FALSE(is_scalene(sides));
}

void test_scalene_is_false_if_no_sides_equal_and_violate_inequality(void)
{
TEST_IGNORE();
triangle_t sides = { 7, 3, 2 };
TEST_ASSERT_FALSE(is_scalene(sides));
}

void test_scalene_sides_may_be_floats(void)
{
TEST_IGNORE();
triangle_t sides = { 0.5, 0.4, 0.6 };
TEST_ASSERT_TRUE(is_scalene(sides));
}

int main(void)
{
UnityBegin("test/test_triangle.c");

RUN_TEST(test_equilateral_is_true_if_all_sides_are_equal);
RUN_TEST(test_equilateral_is_false_if_any_side_is_unequal);
RUN_TEST(test_equilateral_is_false_if_all_sides_zero);
RUN_TEST(test_equilateral_sides_may_be_floats);
RUN_TEST(test_isosceles_is_true_if_last_two_sides_are_equal);
RUN_TEST(test_isosceles_is_true_if_first_two_sides_are_equal);
RUN_TEST(test_isosceles_is_true_if_first_and_last_sides_are_equal);
RUN_TEST(test_equilateral_triangles_are_also_isosceles);
RUN_TEST(test_isosceles_is_false_if_no_sides_are_equal);
RUN_TEST(test_isosceles_is_false_if_two_sides_equal_and_violate_inequality);
RUN_TEST(test_isosceles_sides_may_be_floats);
RUN_TEST(test_scalene_is_true_if_no_sides_are_equal);
RUN_TEST(test_scalene_is_false_if_all_sides_are_equal);
RUN_TEST(test_scalene_is_false_if_two_sides_are_equal);
RUN_TEST(test_scalene_is_false_if_no_sides_equal_and_violate_inequality);
RUN_TEST(test_scalene_sides_may_be_floats);

UnityEnd();
return 0;
}``````

### src/triangle.c

``````#include "triangle.h"

int check_t(triangle_t t);

int is_equilateral(triangle_t t)
{
if (
t.a == t.b &&
t.a == t.c
)
return check_t(t);
return 0;
}

int is_isosceles(triangle_t t)
{
if (
t.a == t.b ||
t.a == t.c ||
t.b == t.c
)
return check_t(t);
return 0;
}

int is_scalene(triangle_t t)
{
if (
t.a != t.b &&
t.a != t.c &&
t.b != t.c
)
return check_t(t);
return 0;
}

int check_t(triangle_t t)
{
return
(t.a > 0 && t.b > 0 && t.c > 0) &&
(t.a < t.b + t.c) &&
(t.b < t.a + t.c) &&
(t.c < t.a + t.b);
}``````

### src/triangle.h

``````#ifndef TRIANGLE_H
#define TRIANGLE_H

typedef struct {
double a;
double b;
double c;
} triangle_t;

int is_equilateral(triangle_t triangle);
int is_isosceles(triangle_t triangle);
int is_scalene(triangle_t triangle);

#endif``````