|
| 1 | +/** |
| 2 | + * This program implements various algorithms for integer factorization, |
| 3 | + * It takes integer(s) from the command line or a file and outputs their prime |
| 4 | + * factorizations. |
| 5 | + * |
| 6 | + * Usage: |
| 7 | + * ./integer-factorization [options] [integers...] |
| 8 | + * Options: |
| 9 | + * -h, --help Show this help message and exit |
| 10 | + * -f, --file FILE Read integers from FILE, one per line |
| 11 | + * -o, --output FILE Write output to FILE instead of stdout |
| 12 | + */ |
| 13 | + |
| 14 | +#include <iostream> |
| 15 | +#include <fstream> |
| 16 | +#include <vector> |
| 17 | +#include <string> |
| 18 | +#include <getopt.h> |
| 19 | +#include "factorization_algorithms.hpp" |
| 20 | + |
| 21 | +void |
| 22 | +print_help() |
| 23 | +{ |
| 24 | + std::cout << "Usage: ./integer-factorization [options] [integers...]\n" |
| 25 | + << "Options:\n" |
| 26 | + << " -h, --help Show this help message and exit\n" |
| 27 | + << " -f, --file FILE Read integers from FILE, one per line\n" |
| 28 | + << " -o, --output FILE Write output to FILE instead of stdout\n"; |
| 29 | +} |
| 30 | + |
| 31 | +int |
| 32 | +main(int argc, char* argv[]) |
| 33 | +{ |
| 34 | + std::string input_file; |
| 35 | + std::string output_file; |
| 36 | + std::vector<long long> numbers; |
| 37 | + |
| 38 | + static struct option long_options[] = { |
| 39 | + {"help", no_argument, 0, 'h'}, |
| 40 | + {"file", required_argument, 0, 'f'}, |
| 41 | + {"output", required_argument, 0, 'o'}, |
| 42 | + {0, 0, 0, 0}}; |
| 43 | + |
| 44 | + int opt; |
| 45 | + while ((opt = getopt_long(argc, argv, "hf:o:", long_options, NULL)) != -1) { |
| 46 | + switch (opt) { |
| 47 | + case 'h': |
| 48 | + print_help(); |
| 49 | + return 0; |
| 50 | + case 'f': |
| 51 | + input_file = optarg; |
| 52 | + break; |
| 53 | + case 'o': |
| 54 | + output_file = optarg; |
| 55 | + break; |
| 56 | + default: |
| 57 | + print_help(); |
| 58 | + return 1; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Read numbers from file if specified |
| 63 | + if (!input_file.empty()) { |
| 64 | + std::ifstream infile(input_file); |
| 65 | + long long num; |
| 66 | + while (infile >> num) { |
| 67 | + numbers.push_back(num); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Read numbers from command line arguments |
| 72 | + for (int index = optind; index < argc; index++) { |
| 73 | + numbers.push_back(std::stoll(argv[index])); |
| 74 | + } |
| 75 | + |
| 76 | + // Prepare output stream |
| 77 | + std::ostream* out_stream = &std::cout; |
| 78 | + std::ofstream outfile; |
| 79 | + if (!output_file.empty()) { |
| 80 | + outfile.open(output_file); |
| 81 | + out_stream = &outfile; |
| 82 | + } |
| 83 | + |
| 84 | + // Factor each number and output the result |
| 85 | + for (const auto& number : numbers) { |
| 86 | + std::vector<long long> factors; |
| 87 | + factors = trial_division(number); |
| 88 | + *out_stream << "Factors of " << number << ": "; |
| 89 | + for (const auto& factor : factors) { |
| 90 | + *out_stream << factor << " "; |
| 91 | + } |
| 92 | + *out_stream << "\n"; |
| 93 | + } |
| 94 | + if (outfile.is_open()) { |
| 95 | + outfile.close(); |
| 96 | + } |
| 97 | + return 0; |
| 98 | +} |
0 commit comments