Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <cstring>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- int Addr[4];
- int Host[4];
- int Broadcast[4];
- int BinToDec(const char *binary)
- {
- int len, dec = 0, i, exp;
- len = strlen(binary);
- exp = len - 1;
- for (i = 0; i < len; i++, exp--)
- dec += binary[i] == '1' ? pow(2, exp) : 0;
- return dec;
- }
- string DecToBin(int decimal)
- {
- return decimal ==
- 0 ? "" : DecToBin(decimal / 2) + ((decimal % 2) ? "1" : "0");
- }
- int StrToInt(string str)
- {
- int i = atoi(str.c_str());
- return i;
- }
- void ConvertFromAddr(string ip)
- {
- stringstream s(ip);
- int a, b, c, d;
- char ch;
- s >> a >> ch >> b >> ch >> c >> ch >> d;
- Addr[0] = a;
- Addr[1] = b;
- Addr[2] = c;
- Addr[3] = d;
- }
- int GetHost(string ip, string mask)
- {
- int IPAddr[4], MaskAddr[4];
- ConvertFromAddr(ip);
- for (int i = 0; i < 4; i++)
- {
- IPAddr[i] = Addr[i];
- }
- ConvertFromAddr(mask);
- for (int i = 0; i < 4; i++)
- {
- MaskAddr[i] = Addr[i];
- }
- for (int i = 0; i < 4; i++)
- {
- Host[i] = IPAddr[i] & MaskAddr[i];
- }
- }
- int GetBroadcast(string ip, string mask)
- {
- // IP start
- ConvertFromAddr(ip); // del dots and array address
- string binIp[4];
- int binIntIp[4];
- for (int i = 0; i < 4; i++)
- {
- // fill missing zeros
- int lengthAddr = floor(log10(abs(Addr[i]))) + 1;
- if (lengthAddr < 8)
- {
- for (int j = 0; j < 8 - lengthAddr; j++)
- binIp[i] += "0";
- }
- // add binary value of certain 8 bits
- binIp[i] += DecToBin(Addr[i]);
- // convert string to int
- binIntIp[i] = StrToInt(binIp[i]);
- }
- int arrayBinIp[4][8];
- for (int j = 0; j < 4; j++)
- {
- for (int i = 7; i >= 0; i--)
- {
- // every number goes to its field
- arrayBinIp[j][i] = binIntIp[j] % 10;
- binIntIp[j] /= 10;
- }
- }
- // IP end
- // mask start
- ConvertFromAddr(mask);
- string binMask[4];
- int binIntMask[4];
- for (int i = 0; i < 4; i++)
- {
- int lengthAddr = floor(log10(abs(Addr[i]))) + 1;
- if (lengthAddr < 8)
- {
- for (int j = 0; j < 8 - lengthAddr; j++)
- binMask[i] += "0";
- }
- binMask[i] += DecToBin(Addr[i]);
- binIntMask[i] = StrToInt(binMask[i]);
- }
- int arrayBinMask[4][8];
- for (int j = 0; j < 4; j++)
- {
- for (int i = 7; i >= 0; i--)
- {
- arrayBinMask[j][i] = binIntMask[j] % 10;
- binIntMask[j] /= 10;
- }
- }
- // mask end
- // let's compare them...
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 8; j++)
- {
- if (arrayBinMask[i][j] == 0)
- {
- // arrayBinIp became the broadcast address
- arrayBinIp[i][j] = 1;
- }
- }
- }
- string binBroadcast[4];
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 8; j++)
- {
- binBroadcast[i] += arrayBinIp[i][j] + 48;
- }
- char *cstr = new char[binBroadcast[i].length() + 1];
- strcpy(cstr, binBroadcast[i].c_str());
- Broadcast[i] = BinToDec(cstr);
- }
- }
- int main()
- {
- int n;
- cin >> n;
- for (int q = 0; q < n; q++)
- {
- string ip, mask;
- cin >> ip >> mask;
- // ###HOST###
- GetHost(ip, mask);
- // ###BROADCAST###
- GetBroadcast(ip, mask);
- // ###OUTPUT###
- cout << Host[0] << "." << Host[1] << "." << Host[2] << "." << Host[3];
- cout << " ";
- cout << Broadcast[0] << "." << Broadcast[1] << "." << Broadcast[2] <<
- "." << Broadcast[3] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement