Advertisement
pablo7890

ip broadcast address

Mar 20th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <cstdio>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <cstring>
  8.  
  9. using namespace std;
  10.  
  11. int BinToDec(const char *binary)
  12. {
  13.     int len, dec = 0, i, exp;
  14.  
  15.     len = strlen(binary);
  16.     exp = len - 1;
  17.  
  18.     for (i = 0; i < len; i++, exp--)
  19.         dec += binary[i] == '1' ? pow(2, exp) : 0;
  20.     return dec;
  21. }
  22.  
  23. string DecToBin(int decimal)
  24. {
  25.     return decimal ==
  26.         0 ? "" : DecToBin(decimal / 2) + ((decimal % 2) ? "1" : "0");
  27. }
  28.  
  29. string reverseString(const string hello)
  30. {
  31.     string s(hello.begin(),hello.end());
  32.     reverse (s.begin(), s.end());
  33.     return s;
  34. }
  35.  
  36. int StrToInt(string str)
  37. {
  38.     int i = atoi(str.c_str());
  39.     return i;
  40. }
  41.  
  42. string IntToStr(int i)
  43. {
  44.     ostringstream ss;
  45.     ss << i;
  46.     string str = ss.str();
  47.     return str;
  48. }  
  49.  
  50. int Addr[4];
  51. void ConvertFromAddr(string ip)
  52. {
  53.     stringstream s(ip);
  54.     int a, b, c, d;
  55.     char ch;
  56.     s >> a >> ch >> b >> ch >> c >> ch >> d;
  57.  
  58.     Addr[0] = a;
  59.     Addr[1] = b;
  60.     Addr[2] = c;
  61.     Addr[3] = d;
  62. }
  63.  
  64. int main()
  65. {
  66.     string ip, mask;
  67.     cin >> ip >> mask;
  68.     // IP start
  69.     ConvertFromAddr(ip); // del dots and array address
  70.     string binIp[4];
  71.     int binIntIp[4];
  72.     for (int i = 0; i<4; i++)
  73.     {
  74.         // fill missing zeros
  75.         int lengthAddr = floor(log10(abs(Addr[i]))) + 1;
  76.         if (lengthAddr<8)
  77.         {
  78.             for (int j=0; j<8-lengthAddr;j++)
  79.                 binIp[i] += "0";
  80.         }
  81.         // add binary value of certain 8 bits
  82.         binIp[i] += DecToBin(Addr[i]);
  83.         // convert string to int
  84.         binIntIp[i] = StrToInt(binIp[i]);
  85.     }
  86.     //// sth get fuck up here....  or maybe not????
  87.     int arrayBinIp[4][8];
  88.     for (int j=0; j<4; j++)
  89.     {
  90.         for (int i = 7; i >= 0; i--)
  91.         {
  92.             // every number goes to its field
  93.             arrayBinIp[j][i] = binIntIp[j] % 10;
  94.             binIntIp[j] /= 10;
  95.         }
  96.     }
  97.     //and this mindfuck ends here....
  98.     // IP end
  99.     // mask start
  100.     ConvertFromAddr(mask);
  101.     string binMask[4];
  102.     int binIntMask[4];
  103.     for (int i = 0; i<4; i++)
  104.     {
  105.         int lengthAddr = floor(log10(abs(Addr[i]))) + 1;
  106.         if (lengthAddr<8)
  107.         {
  108.             for (int j=0; j<8-lengthAddr;j++)
  109.                 binMask[i] += "0";
  110.         }
  111.         binMask[i] += DecToBin(Addr[i]);
  112.         binIntMask[i] = StrToInt(binMask[i]);
  113.     }
  114.     int arrayBinMask[4][8];
  115.     for (int j=0; j<4; j++)
  116.     {
  117.         for (int i = 7; i >= 0; i--)
  118.         {
  119.             arrayBinMask[j][i] = binIntMask[j] % 10;
  120.             binIntMask[j] /= 10;
  121.         }
  122.     }
  123.     // mask end
  124.     // let's compare them...
  125.     for (int i=0;i<4;i++)
  126.     {
  127.      for (int j=0;j<8;j++)
  128.      {
  129.        if (arrayBinMask[i][j]==0)
  130.        {
  131.          // arrayBinIp became the broadcast address
  132.         arrayBinIp[i][j]=1;
  133.        }
  134.      }
  135.     }
  136.     string binBroadcast[4];
  137.     int Broadcast[4];
  138.     for (int i = 0;i<4;i++)
  139.     {
  140.      for (int j =0;j<8; j++)
  141.      {
  142.        binBroadcast[i] += arrayBinIp[i][j]+48;
  143.      }
  144.  
  145.     char *cstr = new char[binBroadcast[i].length() + 1];
  146. strcpy(cstr, binBroadcast[i].c_str());
  147.  
  148.   Broadcast[i]=BinToDec(cstr);
  149.     }
  150.    
  151.     cout << Broadcast[0] << "." <<  Broadcast[1] << "." <<  Broadcast[2] << "." <<  Broadcast[3];
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement