-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
179 lines (158 loc) · 4.59 KB
/
main.cpp
File metadata and controls
179 lines (158 loc) · 4.59 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <fstream>
#include "src/include.h"
// Command 1: [Execution file] -a [Algorithm] [Given input] [Output parameter(s)]
// Command 2: [Execution file] -a [Algorithm] [Input size] [Input order] [Output parameter(s)]
// Command 3: [Execution file] -a [Algorithm] [Input size] [Output parameter(s)]
// Command 4: [Execution file] -c [Algorithm 1] [Algorithm 2] [Given input]
// Command 5: [Execution file] -c [Algorithm 1] [Algorithm 2] [Input size] [Input order]
bool isValidAlgorithm(const std::string &algorithm)
{
auto sort_algorithm = algorithmMap.find(algorithm);
return sort_algorithm != algorithmMap.end();
}
bool isValidInputOrder(const std::string &input_order)
{
auto data_type = dataTypeMap.find(input_order);
return data_type != dataTypeMap.end();
}
bool isValidGivenInput(const std::string &given_input)
{
std::ifstream inFile;
inFile.open(given_input);
if (inFile.is_open())
{
inFile.close();
return true;
}
return false;
}
bool isValidOutputParam(const std::string &output_parameter)
{
auto output_param = outputParamMap.find(output_parameter);
return output_param != outputParamMap.end();
}
int main(int argc, char *argv[])
{
if (argc < 5)
{
std::cerr << "Invalid command.\n";
return 1;
}
std::string mode = argv[1];
// algorithm mode
if (mode == "-a")
{
std::string algorithm = argv[2];
if (!isValidAlgorithm(algorithm))
{
std::cerr << "Invalid command.\n";
return 1;
}
if (!isdigit(argv[3][0]))
{
std::string given_input = argv[3];
std::string output_parameter = argv[4];
if (!isValidGivenInput(given_input) || !isValidOutputParam(output_parameter))
{
std::cerr << "Invalid command.\n";
return 1;
}
// command 1
Command1(algorithm, given_input, output_parameter);
}
else
{
int input_size = std::stoi(argv[3]);
std::string output_parameter = "";
std::string input_order = "";
if (argc > 5)
{
input_order = argv[4];
output_parameter = argv[5];
if (!isValidInputOrder(input_order) || !isValidOutputParam(output_parameter))
{
std::cerr << "Invalid command.\n";
return 1;
}
}
else
{
output_parameter = argv[4];
if (!isValidOutputParam(output_parameter))
{
std::cerr << "Invalid command.\n";
return 1;
}
}
if (!input_order.empty())
{
// command 2
Command2(algorithm, input_size, input_order, output_parameter);
}
else
{
// command 3
Command3(algorithm, input_size, output_parameter);
}
}
}
// comparision mode
else if (mode == "-c")
{
std::string algorithm1 = argv[2];
std::string algorithm2 = argv[3];
if (!isValidAlgorithm(algorithm1) || !isValidAlgorithm(algorithm2))
{
std::cerr << "Invalid command.\n";
return 1;
}
int input_size = 0;
std::string given_input = "";
std::string input_order = "";
if (!isdigit(argv[4][0]))
{
given_input = argv[4];
if (!isValidGivenInput(given_input))
{
std::cerr << "Invalid command.\n";
return 1;
}
}
else
{
if (argc > 5)
{
input_size = std::stoi(argv[4]);
input_order = argv[5];
if (!isValidInputOrder(input_order))
{
std::cerr << "Invalid command.\n";
return 1;
}
}
else
{
std::cerr << "Invalid command.\n";
return 1;
}
}
if (input_size == 0)
{
// command 4
Command4(algorithm1, algorithm2, given_input);
}
else
{
// command 5
Command5(algorithm1, algorithm2, input_size, input_order);
}
}
else
{
std::cerr << "Invalid command.\n";
return 1;
}
std::cout << "Success!\n";
return 0;
}