Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <bitset>
  6. #include <stdlib.h>
  7.  
  8. using namespace std;
  9.  
  10.  
  11.  
  12.  
  13.  
  14. int main()
  15. {
  16.  
  17.  
  18. string ip;
  19. string F[4];
  20. int L[4];
  21.  
  22.  
  23.  
  24.  
  25. cout<<"Insert IP address:"<<endl;
  26.  
  27. cin>>ip;
  28.  
  29.  
  30.  
  31.  
  32. size_t x = ip.find(".");
  33. F[0] = ip.substr (0,x);
  34. size_t m = ip.find(".",x+1);
  35.  
  36. F[1] = ip.substr (x+1,m-x-1);
  37. size_t h = ip.find(".",m+1);
  38. F[2] = ip.substr (m+1,h-m-1);
  39. size_t n = ip.find(".",h+1);
  40. F[3] = ip.substr (h+1,n-h-1);
  41.  
  42.  
  43. L[0]=stoi(F[0],nullptr,10);
  44. L[1]=stoi(F[1],nullptr,10);
  45. L[2]=stoi(F[2],nullptr,10);
  46. L[3]=stoi(F[3],nullptr,10);
  47.  
  48. if(L[0]>=256|L[0]<=0)
  49. {
  50. cout<<"Invalid Address";
  51. return 0;
  52. }
  53.  
  54.  
  55.  
  56.  
  57. bitset<8> K(L[0]);
  58. bitset<8> K1(L[1]);
  59. bitset<8> K2(L[2]);
  60. bitset<8> K3(L[3]);
  61. cout<<"BINARY CODE:"<<K<<"."<<K1<<"."<<K2<<"."<<K3<<endl;
  62.  
  63. string ch;
  64.  
  65. cout<<"Insert address class:\n"<<"A\n"<<"B\n"<<"C\n";
  66. cin>>ch;
  67.  
  68. bitset<8> SMA(255);
  69. bitset<8> SMA1(0);
  70. bitset<8> SMA2(0);
  71. bitset<8> SMA3(0);
  72. bitset<8> SMB(255);
  73. bitset<8> SMB1(255);
  74. bitset<8> SMB2(0);
  75. bitset<8> SMB3(0);
  76. bitset<8> SMC(255);
  77. bitset<8> SMC1(255);
  78. bitset<8> SMC2(255);
  79. bitset<8> SMC3(0);
  80.  
  81. bitset<8>X;
  82. bitset<8>X1;
  83. bitset<8>X2;
  84. bitset<8>X3;
  85.  
  86. cout<<"Insert number of host"<<endl;
  87. int N;
  88. cin>>N;
  89. bitset<32>SBN(N);
  90. string SBNstr=SBN.to_string();
  91.  
  92.  
  93.  
  94. if(ch=="A")
  95. {
  96. X=K&SMA;
  97. X1=K1&SMA1;
  98. X2=K2&SMA2;
  99. X3=K3&SMA3;
  100.  
  101. cout<<"LOGICAL AND:"<<X<<"."<<X1<<"."<<X2<<"."<<X3<<endl;
  102.  
  103. size_t g = SBNstr.find("1");
  104. if (N>=16777214|L[0]>=126)
  105. {
  106. cout<<"Request error"<<endl;
  107. return 0;
  108.  
  109. }
  110. string O=X.to_string();
  111. string O1=X1.to_string();
  112. string O2=X2.to_string();
  113. string O3=X3.to_string();
  114. string BYTE;
  115. BYTE=O+O1+O2+O3;
  116.  
  117. bitset<32>BYTEbin(BYTE);
  118. string lor;
  119. bitset<32>lorx=BYTEbin|SBN;
  120. lor=lorx.to_string();
  121.  
  122. string by=lor.substr (0,8);
  123. string by1=lor.substr (8,8);
  124. string by2=lor.substr (16,8);
  125. string by3=lor.substr (24,8);
  126.  
  127. int mby;
  128. int mby1;
  129. int mby2;
  130. int mby3;
  131.  
  132. mby=stoi(by,nullptr,2);
  133. mby1=stoi(by1,nullptr,2);
  134. mby2=stoi(by2,nullptr,2);
  135. mby3=stoi(by3,nullptr,2);
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. cout<<mby<<"."<<mby1<<"."<<mby2<<"."<<mby3;
  143. }
  144. else if(ch=="B")
  145. {
  146. X=K&SMB;
  147. X1=K1&SMB1;
  148. X2=K2&SMB2;
  149. X3=K3&SMB3;
  150.  
  151. cout<<"LOGICAL AND:"<<X<<"."<<X1<<"."<<X2<<"."<<X3<<endl;
  152.  
  153. size_t g = SBNstr.find("1");
  154. if (N>=16777214|L[0]>=192|L[0]<=127)
  155. {
  156. cout<<"Request error"<<endl;
  157. return 0;
  158.  
  159.  
  160. }
  161. string O=X.to_string();
  162. string O1=X1.to_string();
  163. string O2=X2.to_string();
  164. string O3=X3.to_string();
  165. string BYTE;
  166. BYTE=O+O1+O2+O3;
  167.  
  168. bitset<32>BYTEbin(BYTE);
  169. string lor;
  170. bitset<32>lorx=BYTEbin|SBN;
  171. lor=lorx.to_string();
  172.  
  173. string by=lor.substr (0,8);
  174. string by1=lor.substr (8,8);
  175. string by2=lor.substr (16,8);
  176. string by3=lor.substr (24,8);
  177.  
  178. int mby;
  179. int mby1;
  180. int mby2;
  181. int mby3;
  182.  
  183. mby=stoi(by,nullptr,2);
  184. mby1=stoi(by1,nullptr,2);
  185. mby2=stoi(by2,nullptr,2);
  186. mby3=stoi(by3,nullptr,2);
  187.  
  188. cout<<mby<<"."<<mby1<<"."<<mby2<<"."<<mby3;
  189.  
  190.  
  191.  
  192. }
  193.  
  194. else if(ch=="C")
  195. {
  196. X=K&SMC;
  197. X1=K1&SMC1;
  198. X2=K2&SMC2;
  199. X3=K3&SMC3;
  200.  
  201. cout<<"LOGICAL AND:"<<X<<"."<<X1<<"."<<X2<<"."<<X3<<endl;
  202.  
  203. size_t g = SBNstr.find("1");
  204. if (N>=16777214|L[0]>=256|L[0]<=191)
  205. {
  206. cout<<"Request error"<<endl;
  207. return 0;
  208.  
  209.  
  210. }
  211. string O=X.to_string();
  212. string O1=X1.to_string();
  213. string O2=X2.to_string();
  214. string O3=X3.to_string();
  215. string BYTE;
  216. BYTE=O+O1+O2+O3;
  217.  
  218. bitset<32>BYTEbin(BYTE);
  219. string lor;
  220. bitset<32>lorx=BYTEbin|SBN;
  221. lor=lorx.to_string();
  222.  
  223. string by=lor.substr (0,8);
  224. string by1=lor.substr (8,8);
  225. string by2=lor.substr (16,8);
  226. string by3=lor.substr (24,8);
  227.  
  228. int mby;
  229. int mby1;
  230. int mby2;
  231. int mby3;
  232.  
  233. mby=stoi(by,nullptr,2);
  234. mby1=stoi(by1,nullptr,2);
  235. mby2=stoi(by2,nullptr,2);
  236. mby3=stoi(by3,nullptr,2);
  237.  
  238. cout<<mby<<"."<<mby1<<"."<<mby2<<"."<<mby3;
  239. }
  240. else
  241. {
  242. cout<<"Please reinsert the class";
  243. return 0;
  244. }
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement