Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1.
- #include <iostream>
- int main() {
- const int size = 8;
- int arr[] = { 14, 33, 27, 10, 35, 19, 42, 44 };
- for (int i = 1; i < size; i++)
- {
- int key = arr[i];
- int j = i - 1;
- std::cout << "Start swapping" << std::endl;
- while (j >= 0 && arr[j] > key)
- {
- arr[j + 1] = arr[j];
- j = j - 1;
- for (int k = 0; k < size; ++k) {
- std::cout << arr[k] << " ";
- }
- std::cout << std::endl;
- }
- arr[j + 1] = key;
- std::cout << "After loop number" << i << " - ";
- for (int k = 0; k < size; ++k) {
- std::cout << arr[k] << " ";
- }
- std::cout << std::endl;
- }
- for (int k = 0; k < size; k++) {
- std::cout << arr[k] << " ";
- }
- }
- 2. Nuyn tvic qani hat ka array-um
- #include <iostream>
- int main() {
- const int size = 9;
- int arr[] = { 14, 14, 14, 10, 14, 19, 42, 19, 19 };
- for (int i = 1; i < size; i++)
- {
- int key = arr[i];
- int j = i - 1;
- /* Move elements of arr[0..i-1], that are
- greater than key, to one position ahead
- of their current position */
- while (j >= 0 && arr[j] > key)
- {
- arr[j + 1] = arr[j];
- j = j - 1;
- }
- arr[j + 1] = key;
- }
- for (int k = 0; k < size; k++) {
- std::cout << arr[k] << " ";
- }
- std::cout << std::endl;
- int max_count = 1;
- for (int i = 0; i < size - 1; ++i) // i == size - 1
- {
- int count = 1;
- while (arr[i] == arr[i + 1]) // arr[size - 1] == arr[size]
- {
- ++count;
- ++i;
- }
- std::cout << arr[i] << " count is " << count << std::endl;
- if (count > max_count)
- {
- max_count = count;
- }
- }
- if (arr[size - 1] != arr[size - 2])
- {
- std::cout << arr[size - 1] << " count is " << 1 << std::endl;
- }
- std::cout << max_count << std::endl;
- }
- 3. OOP C++
- #include <iostream>
- #include <string>
- class Color {
- private:
- std::string mColor;
- public:
- Color(const std::string& color = "Black")
- : mColor(color)
- {
- }
- void set_color(const std::string& color)
- {
- mColor = color;
- }
- std::string get_color() const
- {
- return mColor;
- }
- };
- class Vehicle {
- protected:
- int mWheelsCount;
- Color mColor;
- int mMaxSpeed;
- public:
- Vehicle()
- {
- mWheelsCount = 0;
- mMaxSpeed = 0;
- }
- Vehicle(int wheelsCount, const Color& color, int maxSpeed);
- // function overloading
- void set_color(const std::string& color)
- {
- mColor.set_color(color);
- }
- void set_color(const Color& color)
- {
- mColor = color;
- }
- Color get_color() const
- {
- return mColor;
- }
- int get_max_speed() const
- {
- return mMaxSpeed;
- }
- int wheels_count() const
- {
- return mWheelsCount;
- }
- };
- Vehicle::Vehicle(int wheelsCount, const Color& color, int maxSpeed)
- : mWheelsCount(wheelsCount)
- , mColor(color)
- , mMaxSpeed(maxSpeed)
- {
- }
- class VehicleWithPlate : public Vehicle {
- protected:
- std::string mLicensePlate; // 22SD457
- public:
- VehicleWithPlate() = default;
- VehicleWithPlate(int wheelsCount, const Color& color, int maxSpeed,
- const std::string& lincensePlate)
- : Vehicle(wheelsCount, color, maxSpeed)
- , mLicensePlate(lincensePlate)
- {
- }
- };
- class Bicycle : public Vehicle {
- };
- class Bus : public VehicleWithPlate {
- protected:
- int mSeetCount;
- int mNumber; // 47
- int mPriority;
- public:
- Bus(int wheelsCount, const Color& color, int maxSpeed,
- const std::string& lincensePlate, int seetCount, int number)
- : VehicleWithPlate(wheelsCount, color, maxSpeed, lincensePlate)
- , mSeetCount(seetCount)
- , mNumber(number)
- {
- mPriority = 0;
- if (color.get_color() == "Black")
- {
- mPriority = 1;
- }
- }
- };
- class Car : public VehicleWithPlate {
- private:
- int mFloorCount;
- public:
- Car()
- {
- mFloorCount = 0;
- }
- Car(int wheelsCount, const Color& color, int maxSpeed,
- const std::string& lincensePlate, int floorCount)
- : VehicleWithPlate(wheelsCount, color, maxSpeed, lincensePlate)
- , mFloorCount(floorCount)
- {
- }
- friend std::istream& operator>>(std::istream& is, Car& c);
- };
- std::istream& operator>>(std::istream& is, Car& c)
- {
- // is == std::cin
- is >> c.mWheelsCount >> c.mMaxSpeed >> c.mLicensePlate >> c.mFloorCount;
- return is;
- }
- int main()
- {
- /*
- int wheelsCount;
- Color color("Black");
- int maxSpeed;
- std::string lincensePlate;
- int floorCount;
- std::cin >> wheelsCount >> maxSpeed >> lincensePlate >> floorCount;
- Car c(wheelsCount, color, maxSpeed, lincensePlate, floorCount);
- */
- Car car;
- std::cin >> car;
- Color c("Yellow");
- car.set_color(c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement