Advertisement
pablo7890

final ip calc

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