How to Classify a Triangle

Asked

Viewed 753 times

4

I want to classify a triangle as to the sides, being that a triangle with all equal sides is designated Equilateral, with all different sides being designated Escaleno and if it has only two equal sides, it is called Isosceles.

I tried to do it this way:

void HeaderClass::ClassificarUmTriangulo() {
int x1, x2, x3, y1, y2, y3, a, b, c;

cout << "Coordenadas do ponto1 (x): ";
cin >> x1;

cout << "Coordenadas do ponto1 (y): ";
cin >> y1;

cout << "Coordenadas do ponto2 (x): ";
cin >> x2;

cout << "Coordenadas do ponto2 (y): ";
cin >> y2;

cout << "Coordenadas do ponto3 (x): ";
cin >> x3;

cout << "Coordenadas do ponto3 (y): ";
cin >> y3;

Sleep(SLEEP_1);

a = sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)));
b = sqrt(((x3 - x2)*(x3 - x2)) + ((y3 - y2)*(y3 - y2)));
c = sqrt(((x1 - x3)*(x1 - x3)) + ((y1 - y3)*(y1 - y3)));

if (a == b && b == c) {
    system("cls");
    cout << "Triangulo equilatero." << endl;
}

Managing to at least use the condition if to get a triangle equilatro, but still not able to get the remaining ones, what will be the way to, using the conditions if, else if and else

1 answer

6


Just do this:

if (a == b && b == c) {
    cout << "Triangulo equilatero." << endl;
}
else if(a == b || b == c){
    cout << "Triangulo isosceles." << endl;
}
else
    cout << "Triangulo escaleno." << endl;

Can you understand why?

Basically the algorithm is as follows:

If the three sides are equal (a=b e b=c) then is equilateral, otherwise if at least one pair is identical (a=b ou inclusivo b=c) so it is isosceles, otherwise it is scalene).

  • But, that else if(a == b || b == c) won’t cause bugs or anything like that ?

  • No. I’ll edit the question.

  • I was thinking of something more complicated, and it turned out to be that simple. I thought something like this: else if( a == b && *a não é igual a c etc..)

  • 2

    @André I think you need to take a closer look at the structure if (condição_1) (...) else if (condição_2) (...) else (...).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.