-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
149 lines (130 loc) · 2.88 KB
/
Source.cpp
File metadata and controls
149 lines (130 loc) · 2.88 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
#include <iostream>
#include <string>
using namespace std;
int * ActetIp = new int [4];
int * ActetMask = new int[4];
int * ActetSubmet = new int[4];
int * ActetHost = new int[4];
bool Calculator(int * arrIp, int * arrMask, int * arrNet, int * arrHost)
{
for (int i = 0; i < 4;i++)
{
arrNet[i] = arrIp[i] & arrMask[i];
arrHost[i] = arrIp[i] & ~arrMask[i];
}
return true;
}
string TransferDecimalToAny(long long int number, const unsigned int &base)
{
const char *DIGITS = "0123456789abcdefghijklmnopqrstuvwxyz";
bool FlagInverse = false;
string reversString, stringInBase;
if (base < 2 || base > 36)
{
std::cout << "Error: основание должно быть от 2 до 36." << std::endl;
return "-1";
}
if (number < 0)
{
FlagInverse = true;
number *= -1;
}
stringInBase += DIGITS[number % base];
number = number / base;
while (number)
{
stringInBase += DIGITS[number % base];
number = number / base;
}
if (FlagInverse == true)
reversString = "-";
for (int i = stringInBase.length() - 1; i >= 0; i--)
reversString += stringInBase[i];
return reversString;
}
bool CheckMask(int * arr)
{
bool flag = false;
for (int i = 0; i < 4; i++)
{
string str = TransferDecimalToAny(arr[i],2);
for (int x = 0; str[x] != '\0'; x++)
{
if (str[x] == '0')
{
flag = true;
}
if (str[x] == '1' && flag)
{
return false;
}
}
}
return true;
}
bool CharToULong(char* ip_,int *arr)
{
char *buff;
buff = new char[3];
int s = 0;
//буфер для хранения одного октета
for (int i = 0, j = 0;ip_[i] != '\0';i++, j++)
{
if (ip_[i] != '.') //если не точка
buff[j] = ip_[i]; // записать символ в буфер
if (ip_[i] == '.' || ip_[i + 1] == '\0')
{
if (ip_[i + 1] == '\0')
{
buff[j] = ip_[i];
buff[j + 1] = '\0';
}
else buff[j] = '\0';
// если следующий октет или последний
cout << atoi(buff) << endl;
if (atoi(buff)>=255) return NULL;
// елиоктетбольше 255 – ошибка
arr[s] = (int)atoi(buff);
s++;
//преобразовать и добавить к числу IP-адреса
j = -1;
};
}
return true;
}
void main()
{
char * myip = new char[255];
char * mymask = new char[255];
cout << "IP > ";
(cin >> myip).get();
// cout << endl;
cout << "MASK > ";
(cin >> mymask).get();
cout << endl;
if (!CharToULong(myip, ActetIp))
{
cout << "Error IP" << endl;
return;
}
if (!CharToULong(mymask, ActetMask))
{
cout << "Error MASK" << endl;
return;
}
if (!CheckMask(ActetMask))
{
cout << "Error mask" << endl;
return;
}
Calculator(ActetIp,ActetMask,ActetSubmet,ActetHost);
cout << "NETWORK ID ";
for (int i = 0;i < 4;i++)
cout << ActetSubmet[i] << ".";
cout << endl;
cout << "HOST ID ";
for (int i = 0;i < 4;i++)
cout << ActetHost[i] << ".";
cout << endl;
system("pause");
}