patterncMinor
Triangle categorizer -- completely refactored and re-written
Viewed 0 times
categorizerwrittencompletelyrefactoredtriangleand
Problem
While reviewing a C question I started writing my own implementation and in no time I had a completely refactored completely different implementation. As the asker is learning I did not want to post it, instead I ask you for further review and possibilities of improvement.
#include
#include
int get_side(name) {
printf("Side-%d has value: ", name);
int value; scanf("%d", &value);
return value;
}
bool is_valid_triangle(side_1,side_2,side_3) {
bool all_positive = side_1 > 0 && side_2 > 0 && side_3 > 0;
bool correct_sized_sides = side_1 + side_2 > side_3 && \
side_2 + side_3 > side_1 && \
side_3 + side_1 > side_2;
return all_positive && correct_sized_sides;
}
bool is_equilater(side_1,side_2,side_3) {
return (side_1 == side_2 && side_2 == side_3);
}
bool is_isosceles(side_1,side_2,side_3) {
return (side_1 == side_2) || (side_2 == side_3) || (side_3 == side_1);
}
bool is_scalen(side_1,side_2,side_3) {
return ! is_isosceles(side_1,side_2,side_3);
}
const char * triangle_categorizer(side_1,side_2,side_3) {
if (is_equilater(side_1,side_2,side_3)) {
return "equilater";
}
if (is_isosceles(side_1,side_2,side_3)) {
return "isosceles";
}
if (is_scalen(side_1,side_2,side_3)) {
return "scalen";
}
}
int main() {
int side_1 = get_side(1);
int side_2 = get_side(2);
int side_3 = get_side(3);
if (! is_valid_triangle(side_1,side_2,side_3)) {
puts("The triangle you intered is invalid!");
return 1;
}
printf("The triangle you entered is %s.\n", \
triangle_categorizer(side_1,side_2,side_3));
return 0;
}Solution
A few simple comments:
Spelling:
Triangles are either Isosceles, Equilateral, or Scalene
Also instead of using string literals, replace them with macros like so:
I commend you on using long names in c, as many c-programmers use 1-2 letter names for things. I might suggest adding comments to complex code, such as mentioning the fact that you are using triangle inequality in your is_valid_triangle function
I would also suggest extending this program to work with non-integers, such as long longs, doubles and the like. In addition, it may be more helpful to a user to pass in coordinates instead of side lengths. That would also allow you to not check if a triangle is valid or not, as technically all 3 point pairs form a triangle (even if it is degenerate).
Spelling:
Triangles are either Isosceles, Equilateral, or Scalene
Also instead of using string literals, replace them with macros like so:
#define EQUILATERAL "equilateral"
#define ISOSCELES "isosceles"
#define SCALENE "scalene"I commend you on using long names in c, as many c-programmers use 1-2 letter names for things. I might suggest adding comments to complex code, such as mentioning the fact that you are using triangle inequality in your is_valid_triangle function
I would also suggest extending this program to work with non-integers, such as long longs, doubles and the like. In addition, it may be more helpful to a user to pass in coordinates instead of side lengths. That would also allow you to not check if a triangle is valid or not, as technically all 3 point pairs form a triangle (even if it is degenerate).
Code Snippets
#define EQUILATERAL "equilateral"
#define ISOSCELES "isosceles"
#define SCALENE "scalene"Context
StackExchange Code Review Q#84826, answer score: 5
Revisions (0)
No revisions yet.