-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
211 lines (179 loc) · 5.57 KB
/
main.cpp
File metadata and controls
211 lines (179 loc) · 5.57 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
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
203
204
205
206
207
208
209
210
211
#include "Header.h"
int main(int argc, char* argv[]) {
char buf[50]; // áóôåð äëÿ ñòðîêè
string bufStr; // ñòðîêà äëÿ ïàðñèíãà
string operation; // òåêóùàÿ îïåðàöèÿ
string outFileName = "output.txt"; // âûõîäíîé ôàéë
string inFileName; // âõîäíîé ôàéë
Rtree myRtree; // R-tree
list<Node*> searchRes; // ðåçóëüòàòû ïîèñêà
MBR covering; // ïàðàìåòð ïîêðûòèÿ äëÿ çàïðîñà ê äåðåâó
Node* bufNode; // áóôåð äëÿ îòâåòà îò äåðåâà
int inputNum; // ïðàìåòð ïàðñèíãà
int separPos = 0; // òåêóùàÿ ïîçèöèÿ ðàçäåëèòåëÿ (ïðîáåëà)
int obj[5]; // îòïàðñåííûå çíà÷åíèÿ
if (argc == 1) {
cout << "wrong input, use -help for info" << endl;
return 0;
}
if (strcmp(argv[1], "-help") == 0) {
cout << "First arg - input file .txt" << endl << "Second arg - output file .txt (optional, default - output.txt)" << endl;
return 0;
}
inFileName = argv[1];
if (ifstream(inFileName.c_str()).good() == 0) {
cout << "input file is not existing" << endl;
return 0;
} else {
if (inFileName.find(".txt") != inFileName.length() - 4) {
cout << "input file is not .txt" << endl;
return 0;
}
}
if (argc == 3) {
outFileName = argv[2];
if (outFileName.find(".txt") != outFileName.length() - 4) {
cout << "output file is not .txt" << endl;
return 0;
}
}
ifstream inputFile(inFileName.c_str()); // âõîäíîé ïîòîê
ofstream outFile; // âûõîäíîé ïîòîê
outFile.open(outFileName.c_str());
// ÷èòàåì ïîñòîðî÷íî
while (!inputFile.eof()) {
inputFile.getline(buf, 50);
bufStr = string(buf);
bufNode = NULL;
// ïàðñèì ñòðîêó
separPos = bufStr.find(' ');
operation = bufStr.substr(0, separPos);
bufStr = bufStr.substr(separPos + 1, bufStr.length() - 1);
if (operation == "add" || operation == "searchByArea" || operation == "searchObject" || operation == "delete") {
Node* newN;
inputNum = 5;
if (operation == "searchByArea")
inputNum = 4;
try {
for (int i = 0; i < inputNum; i++) {
separPos = bufStr.find(' ');
obj[i] = stoi(bufStr.substr(0, separPos));
if (obj[i] < 0)
throw invalid_argument("error");
bufStr = bufStr.substr(separPos + 1, bufStr.length() - 1);
}
if (obj[0] >= obj[1] || obj[2] >= obj[3])
throw invalid_argument("error");
}
catch (exception const &e) {
outFile << "wrong input" << endl;
inputFile.close();
outFile.close();
return 0;
}
if (operation == "add") {
newN = new Node;
newN->type = OBJECT;
newN->covering.x1 = obj[0];
newN->covering.x2 = obj[1];
newN->covering.y1 = obj[2];
newN->covering.y2 = obj[3];
newN->data = obj[4];
newN->parent = NULL;
newN->childs = NULL;
myRtree.InsertObject(newN);
continue;
}
if (operation == "delete") {
covering.x1 = obj[0];
covering.x2 = obj[1];
covering.y1 = obj[2];
covering.y2 = obj[3];
outFile << "object" << " x1=" << covering.x1
<< " x2=" << covering.x2
<< " y1=" << covering.y1
<< " y2=" << covering.y2 << " data=" << obj[4];
if (myRtree.DeleteObject(covering, obj[4]) == true) {
outFile << " was successfully deleted" << endl << endl;
}
else {
outFile << " don't exist" << endl << endl;
}
continue;
}
if (operation == "searchByArea") {
covering.x1 = obj[0];
covering.x2 = obj[1];
covering.y1 = obj[2];
covering.y2 = obj[3];
myRtree.SearchByArea(covering, &searchRes);
outFile << "search in" << " x1=" << covering.x1
<< " x2=" << covering.x2
<< " y1=" << covering.y1
<< " y2=" << covering.y2 << " area results:" << endl;
if (searchRes.empty() == true) {
outFile << "\tno objects in this area" << endl << endl;
continue;
}
while (searchRes.empty() != true) {
bufNode = searchRes.back();
searchRes.pop_back();
outFile << '\t' << " x1=" << bufNode->covering.x1
<< " x2=" << bufNode->covering.x2
<< " y1=" << bufNode->covering.y1
<< " y2=" << bufNode->covering.y2 << " data=" << bufNode->data << endl;
}
outFile << endl;
continue;
}
if (operation == "searchObject") {
covering.x1 = obj[0];
covering.x2 = obj[1];
covering.y1 = obj[2];
covering.y2 = obj[3];
outFile << "search object" << " x1=" << covering.x1
<< " x2=" << covering.x2
<< " y1=" << covering.y1
<< " y2=" << covering.y2 << " data=" << obj[4] << " result:" << endl;
bufNode = myRtree.SearchObject(covering, obj[4]);
if (bufNode != NULL)
outFile << "\tobject is exists" << endl << endl;
else
outFile << "\tobject not found" << endl << endl;
continue;
}
}
if (operation == "print") {
myRtree.PrintRtree(outFile);
continue;
}
if (operation == "max") {
bufNode = myRtree.FindExtremumByArea(MAX);
if (bufNode == NULL) {
outFile << "no max, R-tree is empty" << endl << endl;
continue;
}
outFile << "max:" << " x1=" << bufNode->covering.x1
<< " x2=" << bufNode->covering.x2
<< " y1=" << bufNode->covering.y1
<< " y2=" << bufNode->covering.y2 << " data=" << bufNode->data << endl << endl;
continue;
}
if (operation == "min") {
bufNode = myRtree.FindExtremumByArea(MIN);
if (bufNode == NULL) {
outFile << "no min, R-tree is empty" << endl << endl;
continue;
}
outFile << "min:" << " x1=" << bufNode->covering.x1
<< " x2=" << bufNode->covering.x2
<< " y1=" << bufNode->covering.y1
<< " y2=" << bufNode->covering.y2 << " data=" << bufNode->data << endl << endl;
continue;
}
}
inputFile.close();
outFile.close();
cout << "Success!" << endl;
return 0;
}