Advertisement
Leeen

ЯИМП 5

May 16th, 2019
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 KB | None | 0 0
  1. #include <iostream>
  2. #include "Color.h"
  3. #include "TransColor.h"
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     setlocale(LC_ALL, "RUS"); //переводим консоль в русскую кодировку
  9.     Color ultraBlue = Color(0, 0, 255);
  10.     Color userColor;
  11.     cin >> userColor;
  12.  
  13.     if (userColor == ultraBlue)
  14.         cout << "Это ультра голубой цвет!!!" << endl;
  15.     else
  16.         cout << "Это не ультра голубой цвет(((" << endl;
  17.  
  18.     system("pause");
  19. }
  20.  
  21. #include <iostream>
  22. using namespace std;
  23. class Color {
  24.  
  25. protected:
  26.     int R, G, B;
  27.     bool correct_color_flag(int x) {
  28.         return x >= 0 && x <= 255;
  29.     }
  30.  
  31. public:
  32.     Color() {
  33.         R = 0;
  34.         G = 0;
  35.         B = 0;
  36.     }
  37.  
  38.     Color(int r, int g, int b) {  //конструктор инициализации
  39.  
  40.         if (!correct_color_flag(r)) throw "incorrect RED component value";
  41.         if (!correct_color_flag(g)) throw "incorrect GREEN component value";
  42.         if (!correct_color_flag(b)) throw "incorrect BLUE component value";
  43.  
  44.         R = r;
  45.         G = g;
  46.         B = b;
  47.     }
  48.  
  49.     Color setRedColor(int x) {
  50.         if (correct_color_flag(x))
  51.             R = x;
  52.         else
  53.             throw "incorrect RED component value";
  54.  
  55.         return *this;
  56.     }
  57.  
  58.     Color setGreenColor(int x) {
  59.         if (correct_color_flag(x))
  60.             G = x;
  61.         else
  62.             throw "incorrect GREEN component value";
  63.  
  64.         return *this;
  65.     }
  66.  
  67.     Color setBlueColor(int x) {
  68.         if (correct_color_flag(x))
  69.             B = x;
  70.         else
  71.             throw "incorrect BLUE component value";
  72.  
  73.         return *this;
  74.     }
  75.  
  76.     Color(const Color& tmp) { //конструктор копирования
  77.  
  78.         R = tmp.R;
  79.         G = tmp.G;
  80.         B = tmp.B;
  81.     }
  82.  
  83.     ~Color() { //деструктор
  84.  
  85.     }
  86.  
  87.     Color& operator = (Color obj) {
  88.         R = obj.R;
  89.         G = obj.G;
  90.         B = obj.B;
  91.         return *this;
  92.     }
  93.  
  94.  
  95.  
  96.     bool operator > (Color obj) {
  97.         return R + G + B > obj.R + obj.G + obj.B;
  98.     }
  99.  
  100.  
  101.     bool operator < (Color obj) {
  102.         return R + G + B < obj.R + obj.G + obj.B;
  103.     }
  104.  
  105.  
  106.     bool operator != (Color obj) {
  107.         return (R != obj.R || G != obj.G || B != obj.B);
  108.     }
  109.  
  110.  
  111.     bool operator >= (Color obj) {
  112.         return R + G + B >= obj.R + obj.G + obj.B;
  113.     }
  114.  
  115.     bool operator <= (Color obj) {
  116.         return R + G + B <= obj.R + obj.G + obj.B;
  117.     }
  118.  
  119.     bool operator == (Color obj) {
  120.         return (R == obj.R && G == obj.G && B == obj.B);
  121.     }
  122.  
  123.     friend istream& operator >> (istream& in, Color& obj) {
  124.  
  125.         cout << "Enter values for the color components" << endl;
  126.         int x;
  127.  
  128.         cout << "Enter RED component ";
  129.         in >> x;
  130.         obj.setRedColor(x);
  131.  
  132.         cout << endl << "Enter GREEN component ";
  133.         in >> x;
  134.         obj.setGreenColor(x);
  135.  
  136.         cout << endl << "Enter BLUE component ";
  137.         in >> x;
  138.         obj.setBlueColor(x);
  139.  
  140.         return in;
  141.     }
  142.     friend ostream& operator << (ostream& out, Color& obj) {
  143.         out << "Color components" << endl;
  144.         out << "RED component " << obj.R << endl << "GREEN component " << obj.G << endl << "BLUE component " << obj.B << endl;
  145.  
  146.         return out;
  147.     }
  148.  
  149.     friend Color& operator+ (Color c1, Color c2) {
  150.         int R = c1.R + c2.R;
  151.         int G = c1.G + c2.G;
  152.         int B = c1.B + c2.B;
  153.         if (R > 255) R = 255;
  154.         if (G > 255) G = 255;
  155.         if (B > 255) B = 255;
  156.         Color c3(R, G, B);
  157.         return c3;
  158.     }
  159.     friend Color& operator- (Color c1, Color c2) {
  160.         int R = c1.R - c2.R;
  161.         int G = c1.G - c2.G;
  162.         int B = c1.B - c2.B;
  163.         if (R < 0) R = 0;
  164.         if (G < 0) G = 0;
  165.         if (B < 0) B = 0;
  166.         Color c3(R, G, B);
  167.         return c3;
  168.     }
  169.  
  170. };
  171.  
  172. #include <iostream>
  173. #include "Color.h"
  174. using namespace std;
  175. class TransColor : Color {
  176.  
  177. public:
  178.     enum ColorType
  179.     {
  180.         RGB,
  181.         CMY
  182.     };
  183.     /*TransColor tc = TransColor(100, 100, 100, TransColor::CMY);*/
  184.     TransColor(int fComponent, int sComponent, int tComponent, ColorType type) : Color(fComponent, sComponent, tComponent) {
  185.         curentType = type;
  186.     }
  187.     TransColor(const TransColor& tc) : Color(tc.R, tc.G, tc.B) {
  188.         curentType = tc.curentType;
  189.     }
  190.     ~TransColor() { Color::~Color(); }
  191.     TransColor() { Color::Color(); }
  192.  
  193.     TransColor toColorType(ColorType newType) {
  194.         if (curentType != newType) {
  195.             R = 255 - R;
  196.             G = 255 - G;
  197.             B = 255 - B;
  198.             curentType = newType;
  199.         }
  200.         return *this;
  201.     }
  202.  
  203.     ColorType getColorType() {
  204.         return curentType;
  205.     }
  206.  
  207. private:
  208.     ColorType curentType;
  209. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement