FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-coding-exercise/integer-factorization/main.cpp at main · DoctorLai/cpp-coding-exercise · GitHub
DoctorLai
/
cpp-coding-exercise
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
3
Star
3
Code
Issues
0
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp-coding-exercise
/
integer-factorization
/
main.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
98 lines (89 loc) · 2.64 KB
Breadcrumbs
cpp-coding-exercise
/
integer-factorization
/
main.cpp
Copy path
File metadata and controls
98 lines (89 loc) · 2.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
*
* This program implements various algorithms for integer factorization,
* It takes integer(s) from the command line or a file and outputs their prime
* factorizations.
*
* Usage:
* ./integer-factorization [options] [integers...]
* Options:
* -h, --help Show this help message and exit
* -f, --file FILE Read integers from FILE, one per line
* -o, --output FILE Write output to FILE instead of stdout
*/
#
include
<
iostream
>
#
include
<
fstream
>
#
include
<
vector
>
#
include
<
string
>
#
include
<
getopt.h
>
#
include
"
factorization_algorithms.hpp
"
void
print_help
()
{
std::cout <<
"
Usage: ./integer-factorization [options] [integers...]
\n
"
<<
"
Options:
\n
"
<<
"
-h, --help Show this help message and exit
\n
"
<<
"
-f, --file FILE Read integers from FILE, one per line
\n
"
<<
"
-o, --output FILE Write output to FILE instead of stdout
\n
"
;
}
int
main
(
int
argc,
char
* argv[])
{
std::string input_file;
std::string output_file;
std::vector<
long
long
> numbers;
static
struct
option
long_options[] = {
{
"
help
"
, no_argument,
0
,
'
h
'
},
{
"
file
"
, required_argument,
0
,
'
f
'
},
{
"
output
"
, required_argument,
0
,
'
o
'
},
{
0
,
0
,
0
,
0
}};
int
opt;
while
((opt =
getopt_long
(argc, argv,
"
hf:o:
"
, long_options,
NULL
)) != -
1
) {
switch
(opt) {
case
'
h
'
:
print_help
();
return
0
;
case
'
f
'
:
input_file = optarg;
break
;
case
'
o
'
:
output_file = optarg;
break
;
default
:
print_help
();
return
1
;
}
}
//
Read numbers from file if specified
if
(!input_file.
empty
()) {
std::ifstream
infile
(input_file);
long
long
num;
while
(infile >> num) {
numbers.
push_back
(num);
}
}
//
Read numbers from command line arguments
for
(
int
index = optind; index < argc; index++) {
numbers.
push_back
(
std::stoll
(argv[index]));
}
//
Prepare output stream
std::ostream* out_stream = &std::cout;
std::ofstream outfile;
if
(!output_file.
empty
()) {
outfile.
open
(output_file);
out_stream = &outfile;
}
//
Factor each number and output the result
for
(
const
auto
& number : numbers) {
std::vector<
long
long
> factors;
factors =
trial_division
(number);
*out_stream <<
"
Factors of
"
<< number <<
"
:
"
;
for
(
const
auto
& factor : factors) {
*out_stream << factor <<
"
"
;
}
*out_stream <<
"
\n
"
;
}
if
(outfile.
is_open
()) {
outfile.
close
();
}
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL