-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·298 lines (271 loc) · 5.55 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·298 lines (271 loc) · 5.55 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <fstream>
using namespace std ;
struct Node{
string data ;
bool priority ;
bool flag ;
Node *left ;
Node *right ;
Node *parent;
};
Node* root = NULL;
Node* create_node(string,Node*);
Node* insert(Node*,Node*);
Node* search_nodeReturn(string);
vector<string> input();
vector<string> sort();
bool include(string,string);
bool match(string,string);
string search(string);
int nodeCount(Node*);
int pNodeCount(Node*);
void treeInit(Node*);
void deletion(string);
void delNode(Node*,string);
Node* insert(Node* node , string value , int level , Node* parent){
if(node==NULL){
node = create_node(value,parent);
if(value.size()>level){
node->priority = true ;
}
}else{
if(value.size()==level&&node->priority){
string tmp = node->data ;
node -> data = value ;
node -> priority = false ;
value = tmp ;
}else if(value.size()>node->data.size()&&node->priority&&include(node->data,value)){
string tmp = node->data ;
node -> data = value ;
node -> priority = true ;
value = tmp ;
}
value.c_str();
int p_bit = value[level];
if(p_bit==48){
value.assign(value);
node->left = insert(node->left,value,++level,node);
}else{
value.assign(value);
node->right = insert(node->right,value,++level,node);
}
}
return node ;
}
Node* create_node(string value,Node* parent){
Node* n = (Node*)malloc(sizeof(Node));
n -> data = value ;
n -> priority = false ;
n -> flag = false ;
n -> left = NULL ;
n -> right = NULL ;
n -> parent = parent ;
return n ;
}
vector<string> input(){
vector<string> ip ;
string line ;
ifstream fp("address.txt");
while(getline(fp,line)){
ip.push_back(line);
}
fp.close();
int size = ip.size();
for(int i = 0 ; i < size ; i ++){
int length = ip[i].size();
int index = i ;
for(int j =i ;j<size;j++){
if(ip[j].size()>length){
index = j ;
length = ip[j].size();
}
}
if(i!=index)swap(ip[i],ip[index]);
}
return ip ;
}
bool include(string origin , string in ){
int size = origin.size();
bool result = true ;
origin.c_str();
in.c_str();
for(int i = 0 ; i < size ; i++){
if(origin[i]!=in[i]){
result = false ;
break ;
}
}
return result ;
}
Node* search_nodeReturn(string ip){
string bit = ip ;
Node* node = root ;
Node* cur = node ;
int level = 0 ;
int p_bit ;
bit.c_str();
while(node!=NULL){
if(match(node->data,ip)){
cur = node ;
if(node->priority)break;
}
p_bit = bit[level];
if(p_bit==48){
node = node->left ;
}else{
node = node->right ;
}
level ++ ;
}
return cur ;
}
string search(string ip){
string BMP = "*" ;
string bit = ip ;
Node* node = root ;
int level = 0 ;
int p_bit ;
bit.c_str() ;
while(node!=NULL){
if(match(node->data,ip)){
BMP = node->data ;
if(node->priority)break;
}
p_bit = bit[level];
if(p_bit==48){
node = node->left ;
}else{
node = node->right ;
}
level ++ ;
}
return BMP ;
}
bool match(string node , string ip){
bool result = true ;
int length = node.size();
node.c_str();
ip.c_str();
for(int i = 0 ; i < length ; i ++){
if(node[i]!=ip[i]){
result = false ;
break ;
}
}
return result ;
}
int nodeCount(Node* cur){
int count = 1 ;
cout<<cur->data<<endl;
if(cur->left!=NULL&&!cur->left->flag){
count += nodeCount(cur->left) ;
}
if(cur->right!=NULL&&!cur->right->flag){
count += nodeCount(cur->right) ;
}
cur->flag = true ;
return count ;
}
void treeInit(Node* cur){
if(cur->left!=NULL&&cur->left->flag){
treeInit(cur->left) ;
}
if(cur->right!=NULL&&cur->right->flag){
treeInit(cur->right) ;
}
cur->flag = false ;
}
int pNodeCount(Node* cur){
int count = 0 ;
if(cur->left!=NULL&&!cur->left->flag){
count += pNodeCount(cur->left);
}
if(cur->right!=NULL&&!cur->right->flag){
count += pNodeCount(cur->right);
}
cur->flag=true ;
if(cur->priority){
return count+1 ;
}else{
return count ;
}
}
void deletion(string prefix){
string result = search(prefix);
if(result == prefix){
Node* node = search_nodeReturn(prefix);
delNode(node,prefix);
}
}
void delNode(Node* node ,string prefix){
Node* parent = node -> parent ;
if(node->left==NULL&&node->right==NULL){
if(parent->left!=NULL&&parent->left->data==prefix){
parent -> left = NULL ;
}else{
parent -> right = NULL ;
}
}else{
if(node->left!=NULL){
node->data = node->left->data ;
node->priority = node->left->priority ;
delNode(node->left,node->left->data);
}else{
node->data = node->right->data ;
node->priority = node->right->priority;
delNode(node->right,node->right->data);
}
}
}
int main(){
char Inputip[16];
char *period=".";
char *pch;
vector<int> v;
fstream file;
file.open("InputIP.txt",ios::out);
cout<< "Input IP Address"<<endl;
cin >> Inputip;
//Seperate IP by . into 4 groups
pch = strtok(Inputip,period);
while(pch !=NULL){
v.push_back(atoi(pch));
pch = strtok(NULL,period);
}
for(int i = 0;i< v.size();i++){
//cout<<v[i]<<endl;
}
//set Mask
int mask = 1 << 8*sizeof(int)-1;
char c;
//turn IP XXX.XXX.XXX.XXX into bit
for( int i = 0 ; i < v.size();i++){
for(int j = 1; j <=8*sizeof(int) ; j++){
if(j > 24){
c=putchar(v[i] & mask ?'1':'0');
file<<c;
}
v[i] <<= 1;
}
}
file.close();
cout<<endl;
vector<string> prefix = input();
int size = prefix.size();
for(int i = 0 ; i < size ; i++){
root = insert(root,prefix[i],0,NULL);
}
string searchIp ;
ifstream fp("InputIP.txt") ;
getline(fp,searchIp);
fp.close();
cout<<"LMP:"<<search(searchIp)<<endl;
//deletion("00");
//int totalNode = nodeCount(root);
return 0 ;
}