Определить маску подсети — C++

Разработать приложение, которое по заданному классу (А, В или С), количеству подсетей N и максимальному количеству компьютеров М в подсети определяет маску для разбиения на подсети и список возможных IP-адресов подсетей. Если разбиение на подсети невозможно, приложение должно выдавать соответствующее сообщение об ошибке.

высчитать маску подсети C++

<br />
#define _CRT_SECURE_NO_WARNINGS</p>
<p>#include &lt;stdio.h&gt;<br />
#include &lt;stdlib.h&gt;<br />
#include &lt;string.h&gt;<br />
#include &lt;ctype.h&gt;<br />
#include &lt;iostream&gt;</p>
<p>using namespace std;</p>
<p>typedef struct _TAddress<br />
{<br />
char IPclass;<br />
int a[4];<br />
int freeBits;<br />
int Nbits;<br />
int Mbits;<br />
}   TAddress;</p>
<p>void FillPow2(int pow2[])<br />
{<br />
int i = 0;<br />
pow2[0] = 1;<br />
while (++i&lt;25)<br />
pow2[i] = pow2[i - 1] * 2;<br />
}</p>
<p>int IsClass(char c)<br />
{<br />
char *alphabet = &quot;ABC&quot;;<br />
int i = strlen(alphabet);<br />
while (--i &gt;-1)<br />
if (c == alphabet[i])<br />
return 1;<br />
return 0;<br />
}</p>
<p>TAddress *CreateAddress(char cl)<br />
{<br />
TAddress *address = (TAddress*)malloc(sizeof(TAddress));<br />
address-&gt;IPclass = cl;<br />
address-&gt;a[0] = 255;<br />
address-&gt;a[1] = address-&gt;a[2] = address-&gt;a[3] = 0;</p>
<p>	if (cl == 'A')<br />
address-&gt;freeBits = 24;<br />
else<br />
{<br />
address-&gt;a[1] = 255;<br />
if (cl == 'B')<br />
address-&gt;freeBits = 16;<br />
else<br />
{<br />
address-&gt;a[2] = 255;<br />
address-&gt;freeBits = 8;<br />
}<br />
}<br />
address-&gt;Mbits = address-&gt;Nbits = -1;</p>
<p>	return address;<br />
}</p>
<p>void DeleteAddress(TAddress *address)<br />
{<br />
free(address);<br />
}</p>
<p>void PrintAddress(TAddress *address)<br />
{<br />
for (int i = 0; i&lt;4; i++)<br />
printf(&quot;%3d.&quot;, address-&gt;a[i]);<br />
printf(&quot;\n&quot;);<br />
}</p>
<p>void FillBits(TAddress *address, int N, int M, int pow2[])<br />
{<br />
int i;<br />
if (N)<br />
{<br />
for (i = 0; i&lt;25; i++)<br />
if (N &lt;= pow2[i] - 2)<br />
{<br />
address-&gt;Nbits = i;<br />
break;<br />
}<br />
for (i = 0; i&lt;25; i++)<br />
if (M &lt;= pow2[i] - 2)<br />
{<br />
address-&gt;Mbits = i;<br />
break;<br />
}<br />
}<br />
else<br />
{<br />
address-&gt;Nbits = 0;<br />
address-&gt;Mbits = address-&gt;freeBits;<br />
}<br />
}</p>
<p>int CheckBits(TAddress *address)<br />
{<br />
return address-&gt;Nbits + address-&gt;Mbits &lt;= address-&gt;freeBits &amp;&amp; address-&gt;Nbits &gt;= 0 &amp;&amp; address-&gt;Mbits&gt;0;<br />
}</p>
<p>int ai(int pow2[], int bits)<br />
{<br />
int i = 0, sum = pow2[7];<br />
while (++i &lt; bits)<br />
sum += pow2[7 - i];<br />
return sum;<br />
}</p>
<p>void CreateMask(TAddress *address, int pow2[])<br />
{<br />
if (!address-&gt;Nbits)<br />
return;<br />
if (address-&gt;IPclass == 'C')<br />
{<br />
address-&gt;a[3] = ai(pow2, address-&gt;Nbits);<br />
return;<br />
}<br />
if (address-&gt;IPclass == 'B')<br />
{<br />
if (address-&gt;Nbits &lt;= 8)<br />
{<br />
address-&gt;a[2] = ai(pow2, address-&gt;Nbits);<br />
return;<br />
}<br />
else<br />
{<br />
address-&gt;a[2] = 255;<br />
address-&gt;a[3] = ai(pow2, address-&gt;Nbits - 8);<br />
return;<br />
}<br />
}<br />
if (address-&gt;Nbits &lt;= 8)<br />
{<br />
address-&gt;a[1] = ai(pow2, address-&gt;Nbits);<br />
return;<br />
}<br />
else if (address-&gt;Nbits &lt;= 16)<br />
{<br />
address-&gt;a[1] = 255;<br />
address-&gt;a[2] = ai(pow2, address-&gt;Nbits - 8);<br />
return;<br />
}<br />
else<br />
{<br />
address-&gt;a[1] = address-&gt;a[2] = 255;<br />
address-&gt;a[3] = ai(pow2, address-&gt;Nbits - 16);<br />
return;<br />
}<br />
}</p>
<p>int main()<br />
{<br />
setlocale(LC_ALL, &quot;Russian&quot;);<br />
int pow2[25], N, M;<br />
FillPow2(pow2);<br />
char cl;</p>
<p>	do<br />
{<br />
printf(&quot;Класс сети:\t&quot;);<br />
cl = toupper(getc(stdin));<br />
getc(stdin);<br />
} while (!IsClass(cl));</p>
<p>	TAddress *address = CreateAddress(cl);<br />
printf(&quot;Класс стандарт маски:\t&quot;);<br />
PrintAddress(address);</p>
<p>	printf(&quot;\n\nКоличество подсетей N = &quot;);<br />
scanf(&quot;%d&quot;, &amp;N);<br />
printf(&quot;Максимально количество компьютеров M = &quot;);<br />
scanf(&quot;%d&quot;, &amp;M);<br />
getc(stdin);</p>
<p>	FillBits(address, N, M, pow2);<br />
printf(&quot;Free bits: %d\tBits for subs: %d\tBits for comps: %d&quot;, address-&gt;freeBits, address-&gt;Nbits, address-&gt;Mbits);<br />
if (CheckBits(address))<br />
{<br />
CreateMask(address, pow2);<br />
printf(&quot;\n\nМаска для данной подсети:\t&quot;);<br />
PrintAddress(address);<br />
}<br />
else<br />
printf(&quot;\n\n\Невозможно создать эту конфигурацию\n&quot;);</p>
<p>	DeleteAddress(address);<br />
getchar();<br />
return 0;<br />
}

Оставить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *