-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangle.cpp
57 lines (46 loc) · 1.39 KB
/
triangle.cpp
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
// Triangle validity tester
// Compile: g++ -std=c++11 triangle.cpp -o triangle
// includes
// --------------------------------------------------------------------------
#include <iostream>
#include <cmath>
using namespace std;
// functions
// --------------------------------------------------------------------------
int triangle_validator(double a, float b, short c) {
int failure = 1;
if ((a == 0)||(b == 0)||(c == 0)) {
return(failure);
} else if ((a < 0)||(b < 0)) {
return(failure);
} else {
if ( (abs(a) + abs(b) > abs(c)) && \
(abs(a) + abs(c) >= abs(b)) && \
(abs(c) + abs(b) > abs(a)) ) {
return(0);
} else {
return(failure);
}
}
}
int usage() {
cerr << "triangle <a-side-length> <b-side-length> <c-side-length> usage:" << endl;
cerr << " triangle validity tester, where <?-side-length> is length of a triangle side." << endl << endl;
cerr << " returns ecode of 0 / 1 / 2 with meaning: valid / invalid / input error" << endl;
}
int main(int argc, char* argv[]) {
double a_length;
float b_length;
short c_length;
if( !(argc >= 4) ) {
usage();
return(2);
}
// parse the input triangle side lengths
a_length = stod(argv[1]);
b_length = stof(argv[2]);
c_length = stol(argv[3]);
// determine whether the triangle is valid (can be constructed)
return(triangle_validator(a_length, b_length, c_length));
}
// eof