-
Notifications
You must be signed in to change notification settings - Fork 0
/
philo.cpp
90 lines (81 loc) · 2.23 KB
/
philo.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
85
86
87
88
89
90
//philo.cpp
//This contains the graph
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main(){
string word, prev_word;
vector<string> marked;
vector<string> heads; //first words inputed
int count = 0; //keep track of # of words inputed (34 will be piped in per round)
//graph is a map where key is word and value is list of its neighbors
map<string, vector<string> > graph;
prev_word = " ";
while(cin >> word) {
count++; //increment number of words inputed
bool found = 0;
cout << word << endl;
//add word to graph
if(prev_word != " ") {
graph[prev_word].push_back(word);
} else {
heads.push_back(word);
}
prev_word = word;
//check if it's in marked
for(auto it1 = marked.begin(); it1 != marked.end(); it1++) {
if(*it1 == word) {
found = 1;
break;
}
}
if(found == 0) { //not already in marked
marked.push_back(word);
} else {
cout << "\tInfinite loop can be entered." << endl;
//ignore rest of input
for(int i = count; count < 34; count++) {
cin >> word;
}
count = 0;
prev_word = " ";
}
}
//remove the heads from the marked (so we can output formatting to file)
for(auto mk = marked.begin(); mk != marked.end(); mk++) {
for(auto hd = heads.begin(); hd != heads.end(); hd++) {
if(*mk == *hd) { //we found a head
*mk = " ";
break;
}
}
}
//output to file
ofstream outfile ("graph.dot");
if(outfile.is_open()) {
outfile << "digraph G {" << endl;
//first, output formatting
outfile << "{" << endl;
//output heads
for(auto it = heads.begin(); it != heads.end(); it++) {
outfile << *it << " [style=filled fontcolor=black fillcolor=darkolivegreen3]" << endl;
}
//output all other nodes
for(auto mk1 = marked.begin(); mk1 != marked.end(); mk1++) {
if(*mk1 == " ") continue;
outfile << *mk1 << " [style=filled fontcolor=white fillcolor=gray40]" << endl;
}
outfile << "}" << endl;
//output graph
for(auto it2 = graph.begin(); it2 != graph.end(); it2++) {
for(auto it3 = it2->second.begin(); it3 != it2->second.end(); it3++) {
outfile << it2->first << " -> " << *it3 << endl;
}
}
outfile << "}" << endl;
}
return 0;
}