#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <locale>
#include <algorithm>
// Structure to hold file information
struct FileInfo {
std::string name;
std::string extension;
std::string date;
int size; // in bytes
};
// Function to generate a random file name
std::string generateFileName() {
const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string result;
for (int i = 0; i < 8; ++i) {
result += charset[rand() % (sizeof(charset) - 1)];
}
return result;
}
// Function to generate a random file extension
std::string generateFileExtension() {
const std::vector<std::string> extensions = {".txt", ".cpp", ".jpg", ".png", ".pdf"};
return extensions[rand() % extensions.size()];
}
// Function to generate a random date
std::string generateDate() {
int year = rand() % 24 + 2000; // Random year between 2000 and 2023
int month = rand() % 12 + 1; // Random month between 1 and 12
int day = rand() % 28 + 1; // Random day between 1 and 28
return std::to_string(year) + "-" + (month < 10 ? "0" : "") + std::to_string(month) + "-" + (day < 10 ? "0" : "") + std::to_string(day);
}
// Function to generate a random file size
int generateFileSize() {
return rand() % 10000 + 100; // Random size between 100 and 10000 bytes
}
// Function to generate a list of random files
std::vector<FileInfo> generateRandomFiles(int count) {
std::vector<FileInfo> files;
for (int i = 0; i < count; ++i) {
FileInfo file;
file.name = generateFileName();
file.extension = generateFileExtension();
file.date = generateDate();
file.size = generateFileSize();
files.push_back(file);
}
return files;
}
// Function to sort files by date
void sortFilesByDate(std::vector<FileInfo>& files) {
std::sort(files.begin(), files.end(), [](const FileInfo& a, const FileInfo& b) {
return a.date < b.date; // Compare dates lexicographically
});
}
// Function to print the list of files
void printFiles(const std::vector<FileInfo>& files) {
std::cout.imbue(std::locale(""));
for (const auto& file : files) {
std::cout << "Name: " << file.name << file.extension
<< ", Date: " << file.date
<< ", Size: " << file.size << " bytes\n";
}
}
int main() {
srand(time(0)); // Seed for random number generation
int fileCount = 10; // Number of files to generate
std::vector<FileInfo> files = generateRandomFiles(fileCount);
std::cout << "Files before sorting:\n";
printFiles(files);
sortFilesByDate(files);
std::cout << "\nFiles after sorting by date:\n";
printFiles(files);
}
/*
run:
Files before sorting:
Name: EoHztjtN.cpp, Date: 2006-03-27, Size: 8,117 bytes
Name: MeeqGswr.txt, Date: 2000-10-04, Size: 9,044 bytes
Name: qewFZaUH.txt, Date: 2001-11-17, Size: 9,392 bytes
Name: aogsVuxa.png, Date: 2023-07-24, Size: 777 bytes
Name: ntsaileE.png, Date: 2016-12-05, Size: 3,017 bytes
Name: iDhfHJtp.cpp, Date: 2011-09-09, Size: 875 bytes
Name: vGnCfAyx.pdf, Date: 2004-03-07, Size: 3,282 bytes
Name: bMTPBctI.pdf, Date: 2000-02-11, Size: 6,748 bytes
Name: VTeVmzBz.txt, Date: 2000-12-06, Size: 9,560 bytes
Name: DjQmWRAP.png, Date: 2006-12-21, Size: 8,280 bytes
Files after sorting by date:
Name: bMTPBctI.pdf, Date: 2000-02-11, Size: 6,748 bytes
Name: MeeqGswr.txt, Date: 2000-10-04, Size: 9,044 bytes
Name: VTeVmzBz.txt, Date: 2000-12-06, Size: 9,560 bytes
Name: qewFZaUH.txt, Date: 2001-11-17, Size: 9,392 bytes
Name: vGnCfAyx.pdf, Date: 2004-03-07, Size: 3,282 bytes
Name: EoHztjtN.cpp, Date: 2006-03-27, Size: 8,117 bytes
Name: DjQmWRAP.png, Date: 2006-12-21, Size: 8,280 bytes
Name: iDhfHJtp.cpp, Date: 2011-09-09, Size: 875 bytes
Name: ntsaileE.png, Date: 2016-12-05, Size: 3,017 bytes
Name: aogsVuxa.png, Date: 2023-07-24, Size: 777 bytes
*/