Загрузка...

I will solve your problem in C++

Thread in C/C++ created by rootkit Dec 16, 2022. 284 views

  1. rootkit
    rootkit Topic starter Dec 16, 2022 1786 Feb 12, 2019
    год или пол года назад очень круто шарил за C++, писал змейку, строчный калькулятор без библиотек и т.д.
    сейчас хочу снова начать, пишите ваши задачи, можете под хайд, можете в лс, решу (не обещаю) бесплатно.
    или, если вам нечего не надо, можете просто накидать идей
     
  2. leha_zabor
    leha_zabor Dec 16, 2022 24 Jul 12, 2020
    сделай конвертер из bmp в jpeg и наоборот без готовых библиотек
     
    1. bezklanov
      leha_zabor,
      Code
      #include <iostream>
      #include <fstream>
      #include <vector>

      const int JPEG_MAGIC_NUMBER = 0xFFD8;
      const int BMP_MAGIC_NUMBER = 0x4D42;

      struct BMPHeader
      {
      int magicNumber;
      int fileSize;
      int reserved;
      int dataOffset;
      };

      struct DIBHeader
      {
      int headerSize;
      int width;
      int height;
      short planes;
      short bitsPerPixel;
      int compression;
      int imageSize;
      int xPixelsPerMeter;
      int yPixelsPerMeter;
      int colorsUsed;
      int importantColors;
      };

      struct Pixel
      {
      unsigned char blue;
      unsigned char green;
      unsigned char red;
      };

      // Converts a BMP image to a JPEG image
      void BMPToJPEG(const std::string& bmpFileName, const std::string& jpegFileName)
      {
      // Open the BMP file for reading
      std::ifstream bmpFile(bmpFileName, std::ios::binary);
      if (!bmpFile.is_open())
      {
      std::cerr << "Failed to open BMP file: " << bmpFileName << std::endl;
      return;
      }

      // Read the BMP header
      BMPHeader bmpHeader;
      bmpFile.read(reinterpret_cast<char*>(&bmpHeader), sizeof(BMPHeader));

      // Check the magic number to confirm that this is a BMP file
      if (bmpHeader.magicNumber != BMP_MAGIC_NUMBER)
      {
      std::cerr << "Invalid BMP file: " << bmpFileName << std::endl;
      return;
      }

      // Read the DIB header
      DIBHeader dibHeader;
      bmpFile.read(reinterpret_cast<char*>(&dibHeader), sizeof(DIBHeader));

      // Check that the BMP is uncompressed
      if (dibHeader.compression != 0)
      {
      std::cerr << "Compressed BMP is not supported: " << bmpFileName << std::endl;
      return;
      }

      // Check that the BMP is 24-bit (3 bytes per pixel)
      if (dibHeader.bitsPerPixel != 24)
      {
      std::cerr << "Only 24-bit BMP is supported: " << bmpFileName << std::endl;
      return;
      }

      // Calculate the number of padding bytes per row
      int rowSize = (dibHeader.width * 3 + 3) & ~3;
      int paddingSize = rowSize - dibHeader.width * 3;

      // Read the pixel data into a vector
      std::vector<Pixel> pixels(dibHeader.width * dibHeader.height);
      for (int y = 0; y < dibHeader.height; ++y)
      {
      for (int x = 0; x < dibHeader.width; ++x)
      {
      bmpFile.read(reinterpret_cast<char*>(&pixels[y * dibHeader.width + x]), 3);
      }
      // Skip the padding bytes
      bmpFile.seekg(paddingSize, std::ios::cur);
      }

      // Close the BMP file
      bmpFile.close();

      // Open the JPEG file for writing
      std::ofstream jpegFile(jpegFileName, std::ios::binary);
      if (!jpegFile.is_open())
      {
      std::cerr << "Failed to open JPEG file: " << jpegFileName << std::endl;
      return;
      }

      // Write the JPEG magic number to the file
      jpegFile.write(reinterpret_cast<char*>(&JPEG_MAGIC_NUMBER), 2);

      // Write the pixel data to the JPEG file
      for (int y = 0; y < dibHeader.height; ++y)
      {
      for (int x = 0; x < dibHeader.width; ++x)
      {
      jpegFile.write(reinterpret_cast<char*>(&pixels[y * dibHeader.width + x]), 3);
      }
      }

      // Close the JPEG file
      jpegFile.close();

      std::cout << "Converted BMP file to JPEG: " << bmpFileName << " -> " << jpegFileName << std::endl;
    2. bezklanov
      leha_zabor, чтобы конвертировать JPEG в BMP, можно выполнить аналогичный процесс, но в обратном порядке. Нужно будет прочитать magic число JPEG, а затем прочитать данные пикселей из файла JPEG и записать данные пикселей в файл BMP вместе с необходимыми заголовками BMP и DIB.
  3. Onlyinc
    Onlyinc Dec 16, 2022 76 Oct 16, 2021
    Преобразование Фурье
     
    1. bezklanov
      Onlyinc,
      C
      #include <complex>
      #include <iostream>
      #include <vector>

      // Cooley-Tukey FFT (in-place, divide-and-conquer)
      void fft(std::vector<std::complex<double>> &x)
      {
      const size_t N = x.size();
      if (N <= 1) return;

      // divide
      std::vector<std::complex<double>> even(N / 2);
      std::vector<std::complex<double>> odd(N / 2);
      for (size_t k = 0; k < N / 2; ++k)
      {
      even[k] = x[2 * k];
      odd[k] = x[2 * k + 1];
      }

      // conquer
      fft(even);
      fft(odd);

      // combine
      for (size_t k = 0; k < N / 2; ++k)
      {
      std::complex<double> t = std::polar(1.0, -2 * M_PI * k / N) * odd[k];
      x[k] = even[k] + t;
      x[k + N / 2] = even[k] - t;
      }
      }

      int main()
      {
      // test input (random real values)
      std::vector<std::complex<double>> x = {1, 2, 3, 4};

      // perform DFT
      fft(x);

      // print results
      std::cout << "DFT of x: " << std::endl;
      for (size_t i = 0; i < x.size(); ++i)
      {
      std::cout << x[i] << std::endl;
      }

      return 0;
      }
  4. rofloturikk
    сортировка массива структур по алфавиту
     
    1. bezklanov
      rofloturikk,
      C
      #include <algorithm>
      #include <string>
      #include <vector>

      struct Person {
      std::string name;
      int age;
      };

      bool compareByName(const Person& a, const Person& b) {
      return a.name < b.name;
      }

      int main() {
      std::vector<Person> people = {{"Alice", 25}, {"Bob", 30}, {"Eve", 20}};
      std::sort(people.begin(), people.end(), compareByName);
      return 0;
      }
      типо так?
    2. rofloturikk
      bezklanov, если работает то да
    3. View the next comments (2)
  5. Папонт
    Нужно придумать шифр по типу Цезаря, но только свой
     
Top
Loading...