1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
#include <algorithm>
void do_sort( int& a, int& b, int& c )
{
if( a > b ) std::swap(a,b) ;
if( b > c ) std::swap(b,c) ;
if( a > b ) std::swap(a,b) ;
}
bool is_triangle( int a, int b, int c )
{
if( a <= 0 || b <= 0 || c <= 0 ) return false ;
do_sort( a, b, c ) ;
return c < (a+b) ;
}
int count_equal( int a, int b, int c ) { return (a==b) + (b==c) + (c==a) ; }
bool is_equilateral( int a, int b, int c ) { return count_equal(a,b,c) == 3 ; }
bool is_scalene( int a, int b, int c ) { return count_equal(a,b,c) == 0 ; }
bool is_right_angled( int a, int b, int c )
{
do_sort( a, b, c ) ;
return ( a*a + b*b ) == (c*c) ;
}
int main ()
{
int a, b, c ;
std::cout << "enter the sides of an integer triangle: " ;
std::cin >> a >> b >> c ;
std::cout << "\n( " << a << ", " << b << ", " << c << " ): " ;
if( is_triangle(a,b,c) )
{
if( is_equilateral(a,b,c) ) std::cout << "equilateral triangle\n" ;
else if( is_scalene(a,b,c) )
{
std::cout << "scalene" ;
if( is_right_angled(a,b,c) ) std::cout << ", right angled" ;
std::cout << " triangle\n" ;
}
else std::cout << "isosceles triangle\n" ;
}
else std::cerr << "the sides do not form a (non-degenerate) triangle\n" ;
}
|