-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExp5-netid-hostid-class.cpp
118 lines (110 loc) · 2.34 KB
/
Exp5-netid-hostid-class.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
// Show Net id, Host id and Network class of the address
#include <iostream>
#include <vector>
using namespace std;
string convertDecToBin(string str)
{
int x = stoi(str);
string bin;
return bin;
}
vector<int> extractNumbers(string str)
{
vector<int> arr;
int num = 0;
int i = 0;
while (i < str.length())
{
if (str[i] == '.')
{
arr.push_back(num);
num = 0;
}
else if ('0' <= str[i] and str[i] <= '9')
{
num = num * 10 + (str[i] - '0');
}
else
{
return {-1};
}
i++;
}
if (str[str.length() - 1] != '.')
{
arr.push_back(num);
}
return arr;
}
bool IsValidInput(vector<int> &nums)
{
if (nums.size() != 4)
return false;
for (int x : nums)
{
if (x > 255)
{
return false;
}
}
return true;
}
void IP_net_id(string str)
{
vector<int> nums = extractNumbers(str);
if (!IsValidInput(nums))
{
cout << "Invalid input\n";
return;
}
char ansClass;
string netId, hostId;
if (nums[0] <= 127)
{
ansClass = 'A';
netId = to_string(nums[0]);
hostId = to_string(nums[1]) + '.' + to_string(nums[2]) + '.' + to_string(nums[3]);
}
else if (nums[0] <= 191)
{
ansClass = 'B';
netId = to_string(nums[0]) + '.' + to_string(nums[1]);
hostId = to_string(nums[2]) + '.' + to_string(nums[3]);
}
else if (nums[0] <= 223)
{
ansClass = 'C';
netId = to_string(nums[0]) + '.' + to_string(nums[1]) + '.' + to_string(nums[2]);
hostId = to_string(nums[3]);
}
else if (nums[0] <= 239)
{
ansClass = 'D';
netId = to_string(nums[0]) + '.' + to_string(nums[1]) + '.' + to_string(nums[2]) + '.' + to_string(nums[3]);
hostId = "";
}
else
{
ansClass = 'E';
netId = to_string(nums[0]) + '.' + to_string(nums[1]) + '.' + to_string(nums[2]) + '.' + to_string(nums[3]);
hostId = "";
}
cout << "Class: " << ansClass << endl;
cout << "Netid: " << netId << endl;
cout << "HostId: " << hostId << endl;
}
int main()
{
string str;
char check = 'y';
while (true)
{
cout << "\nEnter IP address in the form of string: ";
cin >> str;
IP_net_id(str);
cout << "\nDo you want to enter more? (y/n): ";
cin >> check;
if (check == 'n' || check == 'N')
break;
}
}