home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
relop1.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-18
|
2KB
|
67 lines
/*
simple C++ program that uses logical expressions
this program uses the conditional expression to display
TRUE or FALSE messages, since C++ does not support the
BOOLEAN data type.
*/
#include <iostream.h>
const MIN_NUM = 30;
const MAX_NUM = 199;
const int TRUE = 1;
const int FALSE = 0;
main()
{
int i, j, k;
int flag1, flag2, in_range,
same_int, xor_flag;
cout << "Type first integer : "; cin >> i;
cout << "Type second integer : "; cin >> j;
cout << "Type third integer : "; cin >> k;
// test for range [MIN_NUM..MAX_NUM]
flag1 = i >= MIN_NUM;
flag2 = i <= MAX_NUM;
in_range = flag1 && flag2;
cout << "\n" << i << " is in the range "
<< MIN_NUM << " to " << MAX_NUM << " : "
<< ((in_range) ? "TRUE" : "FALSE");
// test if two or more entered numbers are equal
same_int = i == j || i == k || j == k;
cout << "\nat least two integers you typed are equal : "
<< ((same_int) ? "TRUE" : "FALSE");
// miscellaneous tests
cout << "\n" << i << " != " << j << " : "
<< ((i != j) ? "TRUE" : "FALSE");
cout << "\nNOT (" << i << " < " << j << ") : "
<< ((!(i < j)) ? "TRUE" : "FALSE");
cout << "\n" << i << " <= " << j << " : "
<< ((i <= j) ? "TRUE" : "FALSE");
cout << "\n" << k << " > " << j << " : "
<< ((k > j) ? "TRUE" : "FALSE");
cout << "\n(" << k << " = " << i << ") AND ("
<< j << " != " << k << ") : "
<< ((k == i && j != k) ? "TRUE" : "FALSE");
// NOTE: C++ does NOT support the logical XOR operator for
// boolean expressions.
// add numeric results of logical tests. Value is in 0..2
xor_flag = (k <= i) + (j >= k);
// if xor_flag is either 0 or 2 (i.e. not = 1), it is
// FALSE therefore interpret 0 or 2 as false.
xor_flag = (xor_flag == 1) ? TRUE : FALSE;
cout << "\n(" << k << " <= " << i << ") XOR ("
<< j << " >= " << k << ") : "
<< ((xor_flag) ? "TRUE" : "FALSE");
cout << "\n(" << k << " > " << i << ") AND("
<< j << " <= " << k << ") : "
<< ((k > i && j <= k) ? "TRUE" : "FALSE");
cout << "\n\n";
return 0;
}