-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathIO.cc
More file actions
110 lines (96 loc) · 2.64 KB
/
IO.cc
File metadata and controls
110 lines (96 loc) · 2.64 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
#ifndef IO_CC_
#define IO_CC_
#include <iostream>
#include "clients.cc"
#include "../first_half/ST.cc"
// Упражнение 20.9
template <class Graph, class Edge>
class IO {
public:
static void show(const Graph &G);
static void showEdges(const Graph &G);
static void showAdj(const Graph &G);
static void scanEZ(Graph &G);
static void scan(Graph &G);
private:
static bool isTerminateInput(int deep = 2);
};
template <class Graph, class Edge>
void IO<Graph, Edge>::show(const Graph &G) {
for (int s = 0; s < G.V(); s++) {
cout.width(2);
cout << s << ": ";
typename Graph::adjIterator A(G, s);
for (Edge *e = A.beg(); !A.end(); e = A.nxt()) {
cout.width(2);
cout << e->other(s);
e->wt(cout) << " -> ";
}
cout << endl;
}
cout << endl;
}
template <class Graph, class Edge>
void IO<Graph, Edge>::showEdges(const Graph &G) {
const vector<Edge *> vec = edges<Graph, Edge>(G);
for (typename vector<Edge *>::const_iterator p = vec.begin(); p != vec.end(); ++p) {
(*p)->show();
}
}
template <class Graph, class Edge>
void IO<Graph, Edge>::showAdj(const Graph &G) {
const int DEFAULT_WIDTH = 5;
for (int i = 0; i < DEFAULT_WIDTH; i++) cout << ' ';
cout << " |";
for (int i = 0; i < G.V(); i++) {
cout.width(DEFAULT_WIDTH);
cout << i;
}
cout << endl;
for (int i = 0; i <= G.V(); i++) {
for (int j = 0; j < DEFAULT_WIDTH; j++) cout << '-';
}
cout << "--" << endl;
for (int v = 0; v < G.V(); v++) {
cout.width(DEFAULT_WIDTH);
cout << v << " |";
for (int w = 0; w < G.V(); w++) {
cout << " ";
G.edge(v, w) ? G.edge(v, w)->wt(cout) : (cout << " *");
}
cout << endl;
}
cout << endl;
}
template <class Graph, class Edge>
void IO<Graph, Edge>::scanEZ(Graph &G) {
int v, w;
double wt;
while (!isTerminateInput()) {
cin >> v >> w >> wt;
if (v < 0 || v >= G.V() || w < 0 || w >= G.V()) {
cerr << "Out of range" << endl;
continue;
}
G.insert(new Edge(v, w, wt));
}
}
template <class Graph, class Edge>
void IO<Graph, Edge>::scan(Graph &G) {
string v, w;
double wt;
ST st;
while (!isTerminateInput()) {
cin >> v >> w;
G.insert(new Edge(st.index(v), st.index(w), wt));
}
}
template <class Graph, class Edge>
bool IO<Graph, Edge>::isTerminateInput(int deep) {
if (deep == 0) return true;
char c = cin.get();
if (c == '\n') return isTerminateInput(deep - 1);
else cin.putback(c);
return false;
}
#endif