hozer

VULab1cpp

Sep 18th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct range{
  5.     int first, second;
  6.  
  7.     void input(){cout << "Input first second: "; cin >> first >> second;}
  8.     int check(){ return first < second ? 1 : 0; }
  9.     void output(int in_range){cout << (in_range == 0 ? "Out off range" : "In range") << "[" << first << ";" << second <<").\n";}
  10.     int is_in_range(int pnt){return (pnt >= first && pnt < second) ? 1 : 0;}
  11.     static void rangePrintUnion(range ob1, range ob2);
  12. };
  13.  
  14. void range::rangePrintUnion(range ob1, range ob2)
  15. {
  16.     int i;
  17.     range u;
  18.  
  19.     if (ob1.is_in_range(ob2.first) && ob1.is_in_range(ob2.second)) {
  20.         u = ob1;
  21.     }
  22.     else if (ob2.is_in_range(ob1.first) && ob2.is_in_range(ob1.second))
  23.     {
  24.         u = ob2;
  25.     }
  26.     else if (ob1.is_in_range(ob2.second)) {
  27.         u.first = ob2.first;
  28.         u.second = ob1.second;
  29.     }
  30.     else if (ob1.is_in_range(ob2.first))
  31.     {
  32.         u.first = ob1.first;
  33.         u.second = ob2.second;
  34.     }
  35.     else
  36.     {
  37.         u = ob2;
  38.         for (i = ob1.first; i < ob1.second; i++) cout << i << " ";
  39.     }
  40.  
  41.     for (i = u.first; i < u.second; i++) cout << i << " ";
  42.     cout << endl;
  43. }
  44.  
  45. void main()
  46. {
  47.     int t;
  48.     range ob1, ob2;
  49.  
  50.     ob1.input();
  51.     ob2.input();
  52.  
  53.     if (!ob1.check()) cout << "Range [" << ob1.first << ";" << ob1.second << ") is incorrect.\n";
  54.     if (!ob2.check()) cout << "Range [" << ob2.first << ";" << ob2.second << ") is incorrect.\n";
  55.  
  56.     cout << "input num: "; cin >> t;
  57.  
  58.     ob1.output(ob1.is_in_range(t));
  59.     ob2.output(ob2.is_in_range(t));
  60.  
  61.     ob1.rangePrintUnion(ob1, ob2);
  62.  
  63.     cin.ignore();
  64.     cin.get();
  65. }
Advertisement
Add Comment
Please, Sign In to add comment