Advertisement
Qrist

Sorted Algorithms C++

Jul 25th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 KB | None | 0 0
  1. 1.
  2.  
  3. #include <iostream>
  4.  
  5. int main() {
  6.     const int size = 8;
  7.     int arr[] = { 14, 33, 27, 10, 35, 19, 42, 44 };
  8.     for (int i = 1; i < size; i++)
  9.     {
  10.         int key = arr[i];
  11.         int j = i - 1;        
  12.         std::cout << "Start swapping" << std::endl;
  13.         while (j >= 0 && arr[j] > key)
  14.         {
  15.             arr[j + 1] = arr[j];
  16.             j = j - 1;
  17.             for (int k = 0; k < size; ++k) {
  18.                 std::cout << arr[k] << " ";
  19.             }
  20.             std::cout << std::endl;
  21.         }
  22.         arr[j + 1] = key;
  23.         std::cout << "After loop number" << i << " - ";
  24.         for (int k = 0; k < size; ++k) {
  25.             std::cout << arr[k] << " ";
  26.         }
  27.         std::cout << std::endl;
  28.     }
  29.     for (int k = 0; k < size; k++) {
  30.         std::cout << arr[k] << " ";
  31.     }
  32. }
  33.  
  34. 2. Nuyn tvic qani hat ka array-um
  35.  
  36. #include <iostream>
  37. int main() {
  38.     const int size = 9;
  39.     int arr[] = { 14, 14, 14, 10, 14, 19, 42, 19, 19 };
  40.     for (int i = 1; i < size; i++)
  41.     {
  42.         int key = arr[i];
  43.         int j = i - 1;
  44.         /* Move elements of arr[0..i-1], that are
  45.         greater than key, to one position ahead
  46.         of their current position */
  47.         while (j >= 0 && arr[j] > key)
  48.         {
  49.             arr[j + 1] = arr[j];
  50.             j = j - 1;
  51.         }
  52.         arr[j + 1] = key;
  53.     }
  54.     for (int k = 0; k < size; k++) {
  55.         std::cout << arr[k] << " ";
  56.     }
  57.     std::cout << std::endl;
  58.     int max_count = 1;
  59.     for (int i = 0; i < size - 1; ++i) // i == size - 1
  60.     {
  61.         int count = 1;
  62.         while (arr[i] == arr[i + 1]) // arr[size - 1] == arr[size]
  63.         {
  64.             ++count;
  65.             ++i;
  66.         }
  67.         std::cout << arr[i] << " count is " << count << std::endl;
  68.         if (count > max_count)
  69.         {
  70.             max_count = count;
  71.         }
  72.     }
  73.     if (arr[size - 1] != arr[size - 2])
  74.     {
  75.         std::cout << arr[size - 1] << " count is " << 1 << std::endl;
  76.     }
  77.     std::cout << max_count << std::endl;
  78. }
  79.  
  80. 3. OOP C++
  81.  
  82. #include <iostream>
  83. #include <string>
  84. class Color {
  85. private:
  86.     std::string mColor;
  87. public:
  88.     Color(const std::string& color = "Black")
  89.         : mColor(color)
  90.     {
  91.     }
  92.     void set_color(const std::string& color)
  93.     {
  94.         mColor = color;
  95.     }
  96.     std::string get_color() const
  97.     {
  98.         return mColor;
  99.     }
  100. };
  101. class Vehicle {
  102. protected:
  103.     int mWheelsCount;
  104.     Color mColor;
  105.     int mMaxSpeed;
  106. public:
  107.     Vehicle()
  108.     {
  109.         mWheelsCount = 0;
  110.         mMaxSpeed = 0;
  111.     }
  112.     Vehicle(int wheelsCount, const Color& color, int maxSpeed);
  113.     // function overloading
  114.     void set_color(const std::string& color)
  115.     {
  116.         mColor.set_color(color);
  117.     }
  118.     void set_color(const Color& color)
  119.     {
  120.         mColor = color;
  121.     }
  122.     Color get_color() const
  123.     {
  124.         return mColor;
  125.     }
  126.     int get_max_speed() const
  127.     {
  128.         return mMaxSpeed;
  129.     }
  130.     int wheels_count() const
  131.     {
  132.         return mWheelsCount;
  133.     }
  134. };
  135. Vehicle::Vehicle(int wheelsCount, const Color& color, int maxSpeed)
  136.     : mWheelsCount(wheelsCount)
  137.     , mColor(color)
  138.     , mMaxSpeed(maxSpeed)
  139. {
  140. }
  141. class VehicleWithPlate : public Vehicle {
  142. protected:
  143.     std::string mLicensePlate; // 22SD457
  144. public:
  145.     VehicleWithPlate() = default;
  146.     VehicleWithPlate(int wheelsCount, const Color& color, int maxSpeed,
  147.         const std::string& lincensePlate)
  148.         : Vehicle(wheelsCount, color, maxSpeed)
  149.         , mLicensePlate(lincensePlate)
  150.     {
  151.     }
  152. };
  153. class Bicycle : public Vehicle {
  154. };
  155. class Bus : public VehicleWithPlate {
  156. protected:
  157.     int mSeetCount;
  158.     int mNumber; // 47
  159.     int mPriority;
  160. public:
  161.     Bus(int wheelsCount, const Color& color, int maxSpeed,
  162.         const std::string& lincensePlate, int seetCount, int number)
  163.         : VehicleWithPlate(wheelsCount, color, maxSpeed, lincensePlate)
  164.         , mSeetCount(seetCount)
  165.         , mNumber(number)
  166.     {
  167.         mPriority = 0;
  168.         if (color.get_color() == "Black")
  169.         {
  170.             mPriority = 1;
  171.         }
  172.     }
  173. };
  174. class Car : public VehicleWithPlate {
  175. private:
  176.     int mFloorCount;
  177. public:
  178.     Car()
  179.     {
  180.         mFloorCount = 0;
  181.     }
  182.     Car(int wheelsCount, const Color& color, int maxSpeed,
  183.         const std::string& lincensePlate, int floorCount)
  184.         : VehicleWithPlate(wheelsCount, color, maxSpeed, lincensePlate)
  185.         , mFloorCount(floorCount)
  186.     {
  187.     }
  188.     friend std::istream& operator>>(std::istream& is, Car& c);
  189. };
  190. std::istream& operator>>(std::istream& is, Car& c)
  191. {
  192.     // is == std::cin
  193.     is >> c.mWheelsCount >> c.mMaxSpeed >> c.mLicensePlate >> c.mFloorCount;
  194.     return is;
  195. }
  196. int main()
  197. {
  198.     /*
  199.     int wheelsCount;
  200.     Color color("Black");
  201.     int maxSpeed;
  202.     std::string lincensePlate;
  203.     int floorCount;
  204.     std::cin >> wheelsCount >> maxSpeed >> lincensePlate >> floorCount;
  205.     Car c(wheelsCount, color, maxSpeed, lincensePlate, floorCount);
  206.     */
  207.     Car car;
  208.     std::cin >> car;
  209.     Color c("Yellow");
  210.     car.set_color(c);
  211. }
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement