Advertisement
J3st3rs_j0k3

5 pizda

Dec 28th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int check(int a){
  6.     return a < 0 || a > 255;
  7. }
  8. char * int_bin(int a){
  9.     char* ans = (char*)malloc(9 * sizeof(char));
  10.     for (int i = 0; i < 8; ++i){
  11.         ans = '0';
  12.     }
  13.     for (int i = 0; i < 8; ++i){
  14.         int c1 = (a >> (7 - i));
  15.         int c2 = c1 % 2;
  16.         if (c2>0)
  17.             ans[i] = '1';
  18.     }
  19.     ans[8] = '\n';
  20.     return ans;
  21. }
  22. int main() {
  23.     int a1, a2, a3, a4;
  24.     scanf("%d.%d.%d.%d", &a1, &a2, &a3, &a4);
  25.     int b1, b2, b3, b4;
  26.     scanf("%d.%d.%d.%d", &b1, &b2, &b3, &b4);
  27.     if(check(a1) || check(a2) || check(a3) || check(a4)){
  28.         printf("wrong IP");
  29.         return 0;
  30.     }
  31.     else if (check(b1) || check(b2) || check(b3) || check(b4)){
  32.         printf("wrong MASK");
  33.         return 0;
  34.     }
  35.     char * mask = "";
  36.     mask = int_bin(b1);
  37.     strcat(mask, int_bin(b2));
  38.     strcat(mask, int_bin(b3));
  39.     strcat(mask, int_bin(b4));
  40.     int was0 = 0;
  41.     for (int i = 0; i <32; ++i){
  42.         if (mask[i]=='1' && !was0)
  43.             continue;
  44.         else if (mask[i] == '0')
  45.             was0 = 1;
  46.         else{
  47.             printf("wrong MASK");
  48.             return 0;
  49.         }    
  50.     }
  51.     printf("%d.%d.%d.%d", a1 & b1, a2 & b2, a3 & b3, a4 & b4);
  52.        
  53.        
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement