-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangleTypeEnumDEMO.cpp
More file actions
103 lines (89 loc) · 3.08 KB
/
Copy pathTriangleTypeEnumDEMO.cpp
File metadata and controls
103 lines (89 loc) · 3.08 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//************************************************************************
// Author: Rolando Carreon
// Date: mar 25 2025
// Language: C++
// Assignment: TriangleTypeEnumDEMO
// Description: classify a triangle by it's type, based on the length
// of the sides provided by the user
//************************************************************************
#include<iostream>
using namespace std;
// define enumerations
enum TriangleType{ SCALENE, ISOSCELES, EQUILATERAL, NO_TRIANGLE };
// FUNCTION PROTYPES
// calling triangle shape will assign a triangle Type
TriangleType triangleShape(double, double, double);
void printShape(TriangleType);
int main()
{
// declare local variables
double lenSide1, lenSide2, lenSide3;
TriangleType myTriangle;
// prompt for and get the length of the 3 sides from the user
cout << "Enter the lengths of the three sides of a triangle. ";
cin >> lenSide1 >> lenSide2 >> lenSide3;
cout << endl;
// get the triangle's type
// u could take this line out if u place this insdie of PRINTSHAPE
myTriangle = triangleShape(lenSide1, lenSide2, lenSide3);
// display the triangle shape
cout << "The shape of the triangle is: ";
// example of efficient from line 36
// printShape( triangleShape(lenSide1, lenSide2, lenSide3 );
printShape(myTriangle);
cout << endl;
return 0;
}
// function definitions
// take the 3 side lengths of a triangle, determine if the sides will
// form a triangle, and return the type of triangle formed.
// Scalene triangle - all 3 sides are different lengths,
// Isosceles Triangle - 2 sides are the same length,
// Equilateral triangle - all three sides are the same length.
TriangleType triangleShape(double sideA, double sideB, double sideC)
{
// check to see if all the sides are the same length - equilateral
if(sideA == sideB && sideB == sideC)
return EQUILATERAL;
// check if the side lenghts form a triangle
else if ( sideA + sideB > sideC &&
sideA + sideC > sideB &&
sideB + sideC > sideA)
{
// check for two sides of the same length - Isosclees
if ( sideA == sideB ||
sideB == sideC ||
sideA == sideC )
return ISOSCELES;
// all sides are different lengths - Scalene
else
return SCALENE;
}
else
return NO_TRIANGLE;
}
// definition will take a TriagnleType enumeration and display the
// triangle's shape
// switch works as just umbers butits preferred to use names for grade
void printShape(TriangleType triangle)
{
// switch on the TriangleType passed in
switch(triangle)
{
case SCALENE:
cout << "Scalene\n";
break;
case ISOSCELES:
cout << "Isosceles\n";
break;
case EQUILATERAL:
cout << "Equilateral\n";
break;
case NO_TRIANGLE:
cout << "The side lengths dont form a triangle.\n";
break;
default:
cout << "I dont know what shape that is.\n";
break;
}
}