Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- #include <cstring>
- using namespace std;
- 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");
- }
- string reverseString(const string hello)
- {
- string s(hello.begin(),hello.end());
- reverse (s.begin(), s.end());
- return s;
- }
- int StrToInt(string str)
- {
- int i = atoi(str.c_str());
- return i;
- }
- string IntToStr(int i)
- {
- ostringstream ss;
- ss << i;
- string str = ss.str();
- return str;
- }
- int Addr[4];
- 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 main()
- {
- string ip, mask;
- cin >> ip >> 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]);
- }
- //// sth get fuck up here.... or maybe not????
- 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;
- }
- }
- //and this mindfuck ends here....
- // 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];
- int Broadcast[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);
- }
- cout << Broadcast[0] << "." << Broadcast[1] << "." << Broadcast[2] << "." << Broadcast[3];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement