/*HEllo everybody. i would like to see your recommendations and your quotes.
how can i optimize this code?
what is wrong into the code?
what can it be improved?*/
//excuse me again.
/*23. 4.33 (Sides of a Triangle) Write a program that reads three
nonzero values and determines and prints whether they
could represent the sides of a triangle.
24. 4.34 (Sides of a Right Triangle) Write a program that reads three
nonzero integers and determines and prints whether they’re the
sides of a right triangle.*/
///Ejercicio 4.32; instrucciones de control; Encabezado de la clase;
///Luis Fernando Pinzon
///6/08/18
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
class Triangles{
public:
void decimalsTriangle()
{
double sideA;
double sideB;
double sideC;
cout << "Check wether the sides form a triangle" << endl;
cout << "join side A: ";
cin >> sideA;
cout << "join side B: ";
cin >> sideB;
cout << "join side c: ";
cin >> sideC;
cout << "\n";
///According to the theorem: inequality of the triangle
/// A + B > C
/// B + C > A
/// A + C > B
if(sideA + sideB > sideC)
{
if(sideB + sideC > sideA)
{
if(sideA + sideC > sideB)
{
cout << "the sides form a triangle. " << endl;
}
else
{
cout << "Sides A y C are less than side B, therefore it's not a triangle. " << endl;
}
}
else
{
cout << "Sides B y C are less than side A, therefore it's not a triangle. " << endl;
}
}
else
{
cout << "Sides A y B are less than side C, therefore it's not a triangle. " << endl;
}
}
void integerTriangles()
{
int sideA;
int sideB;
int sideC;
cout << "Check wether the sides form a triangle" << endl;
cout << "join side A: ";
cin >> sideA;
cout << "join side B: ";
cin >> sideB;
cout << "join side c: ";
cin >> sideC;
cout << "\n";
if(sideA != 0)
{
///check if it is a hypotenuse.
if(sideA > sideB)
{
if(sideA > sideC)
{
///check for The theorem of Pythagoras.
if((sideA * sideA) == ((sideC * sideC)+(sideB * sideB)))
{
cout << "it's a right triangle. " << endl;
}
else
{
cout << "it's not a right triangle. " << endl;
}
}
}
}
else
{
cout << "enter a numbre different from zero " << endl;
}
if(sideB != 0)
{
///check if it is a hypotenuse.
if(sideB > sideA)
{
if(sideB > sideC)
{
///check for The theorem of Pythagoras.
if((sideB * sideB) == ((sideC * sideC)+(sideA * sideA)))
{
cout << "it's a right triangle. " << endl;
}
else
{
cout << "it's not a right triangle. " << endl;
}
}
}
}
else
{
cout << "enter a numbre different from zero " << endl;
}
if(sideC != 0)
{
///check if it is a hypotenuse.
if(sideC > sideA)
{
if(sideC > sideB)
{
///check for The theorem of Pythagoras.
if((sideC * sideC) == ((sideA * sideA)+(sideB * sideB)))
{
cout << "it's a right triangle. " << endl;
}
else
{
cout << "it's not a right triangle. " << endl;
}
}
}
}
else
{
cout << "enter a numbre different from zero " << endl;
}
}
};
int main()
{
Triangles objectTriangles;
objectTriangles.integerTriangles();
return 0;
}
My main recommendation would be to NOT stuff plain old functions inside a class for no reason. And if you do then at least make them static. But I don't see the point of putting them in a class at all.
4.33 (Sides of a Triangle) Write a program that reads three nonzero values and determines and prints whether they could represent the sides of a triangle.
#include <iostream>
#include <algorithm>
int main()
{
double a, b, c ;
std::cout << "enter the three sides: " ;
std::cin >> a >> b >> c ;
std::cout << "the values " << a << ", " << b << " and " << c ;
if( a > 0 && b > 0 && c > 0 )
{
using std::swap ; // http://www.cplusplus.com/reference/algorithm/swap/if( a > b ) swap(a,b) ; // after this, b >= a
if( b > c ) swap(b,c) ; // after this, c >= b
// at his point, c is the largest side
// check if the triangle inequality holds. https://en.wikipedia.org/wiki/Triangle_inequalityif( c < (a+b) ) std::cout << " could be the sides of a triangle\n" ;
else std::cout << " can't be the sides of a triangle (violates the triangle inequality)\n" ;
}
else std::cout << " are not the sides of a triangle (all values are not positive)\n" ;
}
#include <iostream>
#include <algorithm>
usingnamespace std;
bool isTriangle( double a, double b, double c )
{
if ( a <= 0 || b <= 0 || c <= 0 ) returnfalse;
return max( { a, b, c } ) < 0.5 * ( a + b + c ); // Use <= to allow degenerate (zero-area) triangles
}
int main()
{
double a, b, c;
cout << "Test potential sides of a triangle\n";
cout << "Enter a b c: ";
cin >> a >> b >> c;
cout << "This " << ( isTriangle( a, b, c ) ? "could" : "could not" ) << " be a triangle\n";
}
(Triangle inequality rearranged for symmetry in a, b, c).
"My main recommendation would be to NOT stuff plain old functions inside a class for no reason. And if you do then at least make them static. But I don't see the point of putting them in a class at all."
im sorry, im reall learning how to program on c++ i dind´t know about that though. lo siento sir, i don't understand what do you mean about the old class functions,
JLBorges thanks mrs and last chances, i will check carefully your code to understand those ways for making an efficent code.