home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / relop1.cpp < prev    next >
C/C++ Source or Header  |  1993-03-18  |  2KB  |  67 lines

  1. /*
  2.    simple C++ program that uses logical expressions
  3.    this program uses the conditional expression to display
  4.    TRUE or FALSE messages, since C++ does not support the
  5.    BOOLEAN data type.
  6. */
  7.  
  8. #include <iostream.h>
  9.  
  10. const MIN_NUM = 30;
  11. const MAX_NUM = 199;                              
  12. const int TRUE = 1;
  13. const int FALSE = 0;
  14.  
  15. main()
  16. {
  17.     int i, j, k;
  18.     int flag1, flag2, in_range,
  19.         same_int, xor_flag;
  20.  
  21.     cout << "Type first  integer : "; cin >> i;
  22.     cout << "Type second integer : "; cin >> j;
  23.     cout << "Type third  integer : "; cin >> k;
  24.  
  25.     // test for range [MIN_NUM..MAX_NUM]
  26.     flag1 = i >= MIN_NUM;
  27.     flag2 = i <= MAX_NUM;
  28.     in_range = flag1 && flag2;
  29.     cout << "\n" << i << " is in the range "
  30.          << MIN_NUM << " to " << MAX_NUM << " : "
  31.          << ((in_range) ? "TRUE" : "FALSE");
  32.  
  33.     // test if two or more entered numbers are equal
  34.     same_int = i == j || i == k || j == k;
  35.     cout << "\nat least two integers you typed are equal : "
  36.          << ((same_int) ? "TRUE" : "FALSE");
  37.  
  38.     // miscellaneous tests
  39.     cout << "\n" << i << " != " << j << " : "
  40.          << ((i != j) ? "TRUE" : "FALSE");
  41.     cout << "\nNOT (" << i << " < " << j << ") : "
  42.          << ((!(i < j)) ? "TRUE" : "FALSE");
  43.     cout << "\n" << i << " <= " << j << " : "
  44.          << ((i <= j) ? "TRUE" : "FALSE");
  45.     cout << "\n" << k << " > " << j << " : "
  46.          << ((k > j) ? "TRUE" : "FALSE");
  47.     cout << "\n(" << k << " = " << i << ") AND (" 
  48.          << j << " != " << k << ") : "
  49.          << ((k == i && j != k) ? "TRUE" : "FALSE");
  50.  
  51.     // NOTE: C++ does NOT support the logical XOR operator for
  52.     // boolean expressions.
  53.     // add numeric results of logical tests.  Value is in 0..2
  54.     xor_flag = (k <= i) + (j >= k);
  55.     // if xor_flag is either 0 or 2 (i.e. not = 1), it is
  56.     // FALSE therefore interpret 0 or 2 as false.
  57.     xor_flag = (xor_flag == 1) ? TRUE : FALSE;
  58.     cout << "\n(" << k << " <= " << i << ") XOR (" 
  59.          << j << " >= " << k << ") : "
  60.          << ((xor_flag) ? "TRUE" : "FALSE");
  61.     cout << "\n(" << k << " > " << i << ") AND(" 
  62.          << j << " <= " << k << ") : "
  63.          << ((k > i && j <= k) ? "TRUE" : "FALSE");
  64.     cout << "\n\n";
  65.     return 0;
  66. }
  67.