-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
84 lines (71 loc) · 2.18 KB
/
main.cpp
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
#include <assert.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
#include "dependency.h"
#include "processor.h"
using namespace std;
bool malformedInput = false;
string DEPENDS = "depends";
string ON = "on";
void parseLibraryStructure(
map<Dependency, vector<Dependency> >& dependencyGraph,
vector<string>& outputOrder) {
string libraryString;
while (getline(cin, libraryString)) {
int stringSize = libraryString.size();
if (stringSize == 0) break;
int i = 0;
int stringsFound = 0;
Dependency vertex;
while (i < stringSize) {
int j = i;
while (libraryString[j] != ' ') {
j++;
}
string substring = libraryString.substr(i, j - i);
if ((stringsFound < 1 || stringsFound > 2) &&
(substring == DEPENDS || substring == ON)) {
malformedInput = true;
}
if (stringsFound == 1 && substring != DEPENDS) {
malformedInput = true;
}
if (stringsFound == 2 && substring != ON) {
malformedInput = true;
}
if (i == 0) {
vertex = Dependency(substring);
outputOrder.push_back(substring);
} else if (substring != DEPENDS && substring != ON) {
Dependency child = Dependency(substring);
dependencyGraph[vertex].push_back(child);
}
i = j + 1;
stringsFound++;
if (malformedInput) break;
}
if (stringsFound < 4) {
malformedInput = true;
}
}
}
int main() {
freopen("textfiles/input.txt", "r", stdin);
freopen("textfiles/output.txt", "w", stdout);
map<Dependency, vector<Dependency> > dependencyGraph;
vector<string> outputOrder;
parseLibraryStructure(dependencyGraph, outputOrder);
if (malformedInput) {
cout << "Malformed Input detected!";
return 0;
}
Processor processor = Processor(dependencyGraph, outputOrder);
processor.process();
return 0;
}