aca-tasks/task1/src/task1-randgen.cpp

64 lines
1.8 KiB
C++

#include <QRandomGenerator>
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <cmath>
#include <ranges>
auto main(int argc, char *argv[]) -> int{
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationName("Random dataset generator");
QCoreApplication::setApplicationVersion("1.0");
QCommandLineParser parser;
parser.setApplicationDescription("Generates random dataset files for mergesort algorithm testing");
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("destination", "Filename of where to place the generated data");
parser.addPositionalArgument("num_values", "The power of 10 for the number of values to generate");
parser.process(app);
const QStringList args = parser.positionalArguments();
if (args.length() != 2) {
parser.showHelp(-1);
}
const QString dest = args.at(0);
bool convOK;
const int pow_value = args.at(1).toInt(&convOK);
if (!convOK) {
parser.showHelp(-1);
}
const int num_values = std::pow(10, pow_value);
QTextStream print(stdout);
print << "Writing " << num_values << " values into " << dest << Qt::endl;
QFile file(dest);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
print << "Error opening file: " << file.errorString() << Qt::endl;
app.exit(-1);
return app.exec();
}
QTextStream out(&file);
for (int i: std::views::iota(0, num_values)) {
out << QString::number(QRandomGenerator::global()->generate()) << '\n';
}
file.flush();
QFileInfo finfo(file);
print << "Wrote " << num_values << " to " << dest << " with resulting size of " << (finfo.size() / 1000000) << " mb"
<< Qt::endl;
file.close();
app.exit(0);
return 0;
}