-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.cpp
202 lines (144 loc) · 4.62 KB
/
Utility.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "Utility.h"
/*********************************************************************/
inline bool digitOrSpace(char c) {
return (c == ' ' || (c >= '0' && c <= '9'));
}
/*********************************************************************/
/* This Function checks that the string contains only legal characters
*/
bool checkIfValidChar(string& line, bool checkWithMinus) {
for (int i = 0; i < line.size(); i++) {
if (!(digitOrSpace(line[i]) || (checkWithMinus && line[i] == '-'))) {
char temp = line[i];
line = "invalid character inserted: '";
line += temp;
line += "'";
line += " is an invalid input. \n";
return false;
}
}
return true;
}
/*********************************************************************/
bool checkFileOpened(ifstream& input, ofstream& output, string& line) {
if (input.fail() || output.fail())
{
line = "Error occured while trying to open the files";
return false;
}
return true;
}
/*********************************************************************/
/* This Function checks that the current line is not an empty line.
* And sets line as the line that was inserted.
*/
bool checkEmptyLine(ifstream& input, string& line) {
if (!getline(input, line))
{
line = "empty line. invalid input \n";
return false;
}
return true;
}
/*********************************************************************/
/* This Function checks that the current line is legal according to the expected input,
* And assigns the value to num_of_vertices.
*/
bool checkLegalLine_NumOfVertices(string& line, int& num_of_vertices) {
num_of_vertices = 0;
if (!checkIfValidChar(line, DoNotCheckWithMinus)) {
return false;
}
num_of_vertices = std::stoi(line);
if (num_of_vertices <= 0) {
line = "number of vertices in a graph can't be a non-positive number. invalid input\n";
return false;
}
return true;
}
/*********************************************************************/
/* This Function checks that the current line is legal according to the expected input,
* And assigns the value to num_of_edges.
*/
bool checkLegalLine_NumOfEdges(string& line, int& num_of_edges) {
num_of_edges = 0;
if (!checkIfValidChar(line, DoNotCheckWithMinus)) {
return false;
}
num_of_edges = std::stoi(line);
if (num_of_edges < 0) {
line = "number of edges in a graph can't be a negative number. invalid input\n";
return false;
}
return true;
}
/*********************************************************************/
/* This Function checks that the current line is legal according to the expected input,
* And assigns the value to ver1, ver2 & ver3.
*
* Expected format of a line: "{int} {int} {int}"
* ASSUMPTION: there are no duplicate edges {v1,v2} & {v1,v2} or {v2,v1}
*/
bool checkLegalLine_WeightedEdge(string& line, int num_of_vertices, int& ver1, int& ver2, int& weight) {
ver1 = ver2 = weight = 0;
if (!checkIfValidChar(line, CheckWithMinus)) {
return false;
}
int check = sscanf(line.c_str(), "%d %d %d", &ver1, &ver2, &weight);
if (check != 3) {
line = "wrong input format. invalid input \n";
return false;
}
if (!(ver1 >= 1 && ver1 <= num_of_vertices && ver2 >= 1 && ver2 <= num_of_vertices && ver1 != ver2)) {
line = "invalid edge. invalid input \n";
return false;
}
return true;
}
/*********************************************************************/
/* This Function checks that the current line is legal according to the expected input,
* And assigns the value to ver1 and ver2.
*
* Expected format of a line: "{int} {int}"
*/
bool checkLegalLine_RemovedEdge(string& line, Graph* g, int& ver1, int& ver2) {
ver1 = ver2 = 0;
if (!checkIfValidChar(line, DoNotCheckWithMinus)) {
return false;
}
int dump;
int check = sscanf(line.c_str(), "%d %d %d", &ver1, &ver2, &dump);
if (check != 2) {
line = "wrong input format. invalid input \n";
return false;
}
if (!(ver1 >= 1 && ver1 <= g->get_Num_of_Vertices() && ver2 >= 1 && ver2 <= g->get_Num_of_Vertices() && ver1 != ver2)) {
line = "invalid edge. invalid input \n";
return false;
}
// if edge inserted wasn't in the graph:
if (!(g->IsAdjacent(ver1, ver2))) {
line = "Edge (";
line += ver1;
line += ", ";
line += ver2;
line += ") wasn't in graph. invalid input \n";
return false;
}
return true;
}
/*********************************************************************/
void deleteEverything(ifstream& input, ofstream& output, Graph*& g1, Graph*& g2, Graph*& g3)
{
if (input.is_open())
input.close();
if (output.is_open())
output.close();
if (!g1)
delete g1;
if (!g2)
delete g2;
if (!g3)
delete g3;
}
/*********************************************************************/