Code:
void input(int a[], int b[], char *);
//void printstep(int);
void bitsborrowed(int [], int *, int *, char);
void conversion(int [], int *, int *, int);
void dtob(int [], int [], int [], int, int, int, int);
int main(void)
{
int ipaddress[MAX_OCTECT];
int subnetmask[MAX_OCTECT];
int bitsborrow = 0;
int bitsremain = 0;
char addressclass;
input(ipaddress, subnetmask, &addressclass);
if(addressclass == 'n')
{
cout << "This is a class I do not support." << endl;
return 0;
}
cout << endl << addressclass << endl;
bitsborrowed(subnetmask, &bitsborrow, &bitsremain, addressclass);
cout << endl << endl << "bitsborrowed " << bitsborrow << " bitsremain " << bi
tsremain;
cout << endl << "These are the numbers for this subnet mask." << endl;
return 0;
}
void input(int ip[], int subnet[], char *adcl)
{
cout << "Please enter an ip address, using spaces between each octet. : ";
cin >> ip[0] >> ip[1] >> ip[2] >> ip[3];
cout << endl;
cout << "You entered: ";
for(int i = 0; i < 4; i++)
{
cout << ip[i];
if( i < 3)
cout << ".";
}
cout << endl;
cout << "Please enter the subnet mask using spaces between each octet. : ";
cin >> subnet[0] >> subnet[1] >> subnet[2] >> subnet[3];
cout << endl;
for(int j = 0; j < 4; j++)
{
cout << subnet[j];
if(j < 3)
cout << ".";
}
if(0 < ip[0] && 127 >= ip[0])
{
*adcl = 'a';
}
else if(128 <= ip[0] && ip[0] <= 191)
{
*adcl = 'b';
}
else if(192 <= ip[0] && 223 >= ip[0])
{
*adcl = 'c';
}
else
{
*adcl = 'n';
}
}
void bitsborrowed(int subnet[], int *bitsborrowed, int *bitsremaining, char aclass)
{
int octet = 0;
if(aclass == 'c')
{
conversion(subnet, bitsborrowed, bitsremaining, 1);
}
else if(aclass == 'b')
{
conversion(subnet, bitsborrowed, bitsremaining, 2);
}
else
{
conversion(subnet, bitsborrowed, bitsremaining, 3);
}
}
void conversion(int sn[], int *bb, int *br, int octets)
{
/*int oc1[] = {0,0,0,0,0,0,0,0};
int oc2[] = {0,0,0,0,0,0,0,0};
int oc3[] = {0,0,0,0,0,0,0,0};
*/
int oc1[8];
int oc2[8];
int oc3[8];
int tempbr = 0;
int tempr = 0;
if(octets == 1)
{
dtob(oc1,oc2,oc3,sn[3],0,0,1);
int i = 7;
while(oc1[i] == 1)
{
tempbr++;
i--;
}
tempr = 8 - tempbr;
*bb = tempbr;
*br = tempr;
}
if(octets == 2)
{
dtob(oc1,oc2,oc3,sn[3],sn[2],0,2);
int i = 7;
while(oc2[i] == 1)
{
tempbr++;
i--;
}
if(i == 0)
{
i = 7;
while(oc1[i] == 1)
{
tempbr++;
i--;
}
tempr = 16 - tempbr;
*bb = tempbr;
*br = tempr;
}
else
{
tempr = 16-tempbr;
*bb = tempbr;
*br = tempr;
}
}
if(octets == 3)
{
cout << "Made it." << endl;
cout << sn[3] << " " << sn[2] << " " << sn[1] << " " << endl;
for(int j = 0; j < 7; j++)
{
cout << oc1[j] << " " << oc2[j] << " " << oc3[j] << endl;
}
dtob(oc3, oc2, oc1, sn[3], sn[2], sn[1], 3);
cout << "Made it again." << endl;
int i = 7;
while(oc3[i] == 1)
{
tempbr++;
i--;
}
if(i == 0)
{
i = 7;
while(oc2[i] == 1)
{
tempbr++;
i--;
}
}
else
{
tempr = 16-tempbr;
*bb = tempbr;
*br = tempr;
}
if(i == 0)
{
i = 7;
while(oc1[i] == 1)
{
tempbr++;
i--;
}
tempr = 24 - tempbr;
*bb = tempbr;
*br = tempr;
}
}
}
void dtob(int oc3[], int oc2[], int oc1[], int third, int second, int first, int numo
c)
{
int remainder = 0;
int count = 0;
cout << "123";
if(numoc >= 1)
{
cout << "OK!" << third;
if(third != 0)
{
while(third != 1)
{
remainder = third % 2; //calculate the remainder
third = (int)(third / 2);
oc3[count++] = remainder;
}
oc3[count] = third;
cout << "First!";
}
else
{
for(int i = 0; i < 7; i++)
{
oc3[i] = 0;
}
cout << "FIRST!";
}
}
if(numoc >= 2)
{
count = 0;
while(second != 1)
{
remainder = second % 2; //calculate the remainder
second = (int)(second / 2);
oc2[count++] = remainder;
}
oc2[count] = second;
cout << "Second!";
}
if(numoc == 3)
{
count = 0;
while(first != 1)
{
remainder = first % 2;
first = (int)(first / 2);
oc1[count++] = remainder;
}
oc1[count] = first;
cout << "Third!";
}
}