-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.cpp
More file actions
executable file
·156 lines (126 loc) · 2.72 KB
/
testing.cpp
File metadata and controls
executable file
·156 lines (126 loc) · 2.72 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
// testing.cpp <Starter Code>
// <Your name>
//
// Adam T Koehler, PhD
// University of Illinois Chicago
// CS 251, Fall 2023
//
// Project Original Variartion By:
// Joe Hummel, PhD
// University of Illinois at Chicago
//
// This file is used for testing graph.h. We encourage you to use Google's
// test framework for this project, but it is not required (because we will
// not be grading the tests file).
//
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <fstream>
#include "graph.h"
using namespace std;
//
// buildGraph:
//
// Inputs the graph vertices and edges from the given file, building
// the graph g. File format:
// vertex
// vertex
// ...
// #
// src dest weight
// src dest weight
// ...
// #
//
void buildGraph(string filename, graph<string,int>& G)
{
ifstream file(filename);
string v;
if (!file.good())
{
cout << endl;
cout << "**Error: unable to open input file '" << filename << "'." << endl;
cout << endl;
return;
}
//
// Input vertices as single uppercase letters: A B C ... #
//
file >> v;
while (v != "#")
{
if (!G.addVertex(v))
cout << "**Error: unable to add vertex '" << v << "', why not?" << endl;
file >> v;
}
//
// Now input edges: Src Dest Weight ... #
//
string src, dest;
int weight;
file >> src;
while (src != "#")
{
file >> dest;
file >> weight;
if (!G.addEdge(src, dest, weight))
cout << "**Error: unable to add edge (" << src << "," << dest << "," << weight << "), why not?" << endl;
file >> src;
}
}
//
// outputGraph:
//
// Outputs graph g to the console.
//
void outputGraph(graph<string,int>& G)
{
vector<string> vertices = G.getVertices();
cout << "**Vertices: ";
for (string v : vertices)
{
cout << v << " ";
}
cout << endl;
cout << "**Edges: ";
for (string v : vertices)
{
set<string> neighbors = G.neighbors(v);
for (string n : neighbors)
{
int weight;
if (G.getWeight(v, n, weight))
{
cout << "(" << v << "," << n << "," << weight << ") ";
}
else
{
cout << "(" << v << "," << n << "," << "???" << ") ";
}
}
}
cout << endl;
}
int main()
{
graph<string,int> G;
string filename;
string startV;
cout << "Enter filename containing graph data> ";
cin >> filename;
cout << endl;
//
// Let's input the graph, and then output to see what we have:
//
buildGraph(filename, G);
outputGraph(G);
G.dump(cout);
//
// done:
//
return 0;
}