|
|
|
|
|
|
|
return (fabs(angle1 + angle2 + angle3 - 180.0) < 0.00000001);
} |
|
|
|
|
|
|
|
|
The following program fragment shows how function IsTriangle might be called: |
|
|
|
|
|
|
|
|
cin >> angleA >> angleB >> angleC;
if (IsTriangle(angleA, angleB, angleC)) // Function call
cout << The three angles form a valid triangle.;
else
cout << Those angles do not form a triangle.; |
|
|
|
|
|
|
|
|
The If statement is much easier to understand with the function than it would be if the entire condition were coded directly. When a conditional test is at all complicated, a Boolean function is in order. |
|
|
|
|
|
|
|
|
The C++ standard library provides a number of helpful Boolean functions that let you test the contents of char variables. To use them, you #include the header file ctype.h. Here are some of the available functions; Appendix C contains a more complete list. |
|
|
|
|
| Header File | Function | Function Type | Function Value | | <ctype.h> | isalpha(ch) | | Nonzero, if ch is a letter (A,Z, az);
0, otherwise | | <ctype.h> | isalnum(ch) | | Nonzero, if ch is a letter or a digit (AZ, az, 09);
0, otherwise | | <ctype.h> | isdigit(ch) | | Nonzero, if ch is a digit (09);
0, otherwise | | <cytpe.h> | islower(ch) | | Nonzero, if ch is a lowercase letter (az);
0, otherwise | | <ctype.h> | isspace(ch) | |  |
|
|
|
|
Nonzero, if ch is a whitespace character (blank, newline, tab, carriage return, form feed); |
|
|
|
| | <ctype.h> | isupper(ch) | | Nonzero, if ch is an uppercase letter (AZ);
0, otherwise |
|
|
|
|
|
|
Although Boolean is not a built-in data type in C++, the is functions behave like Boolean functions. They return an int value that is nonzero (true) or zero (false). These functions are convenient to use and make programs more readable. For example, the test |
|
|
|
|
|