#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() % 21 + 2000; // Random year between 2000 and 2020
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 size
void sortFilesBySize(std::vector<FileInfo>& files) {
std::sort(files.begin(), files.end(), [](const FileInfo& a, const FileInfo& b) {
return a.size < b.size;
});
}
// 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);
sortFilesBySize(files);
std::cout << "\nFiles after sorting by size:\n";
printFiles(files);
}
/*
run:
Files before sorting:
Name: JgJGvjqv.pdf, Date: 2008-12-13, Size: 5,816 bytes
Name: BMEIrjyV.pdf, Date: 2004-04-27, Size: 1,555 bytes
Name: tcVKZEQI.jpg, Date: 2019-04-08, Size: 8,096 bytes
Name: kEegQhSW.png, Date: 2008-06-19, Size: 5,045 bytes
Name: PAeewZgr.png, Date: 2010-08-02, Size: 4,095 bytes
Name: jrhqbTwH.txt, Date: 2014-10-17, Size: 4,453 bytes
Name: EgUupKxV.txt, Date: 2007-03-07, Size: 1,706 bytes
Name: HCjNRUGA.pdf, Date: 2016-01-14, Size: 7,831 bytes
Name: WYGqlkxf.txt, Date: 2007-10-03, Size: 3,450 bytes
Name: HxiejQis.txt, Date: 2009-01-02, Size: 1,366 bytes
Files after sorting by size:
Name: HxiejQis.txt, Date: 2009-01-02, Size: 1,366 bytes
Name: BMEIrjyV.pdf, Date: 2004-04-27, Size: 1,555 bytes
Name: EgUupKxV.txt, Date: 2007-03-07, Size: 1,706 bytes
Name: WYGqlkxf.txt, Date: 2007-10-03, Size: 3,450 bytes
Name: PAeewZgr.png, Date: 2010-08-02, Size: 4,095 bytes
Name: jrhqbTwH.txt, Date: 2014-10-17, Size: 4,453 bytes
Name: kEegQhSW.png, Date: 2008-06-19, Size: 5,045 bytes
Name: JgJGvjqv.pdf, Date: 2008-12-13, Size: 5,816 bytes
Name: HCjNRUGA.pdf, Date: 2016-01-14, Size: 7,831 bytes
Name: tcVKZEQI.jpg, Date: 2019-04-08, Size: 8,096 bytes
*/