Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <stdio.h>
- #include <string.h>
- #include <bitset>
- #include <stdlib.h>
- using namespace std;
- int main()
- {
- string ip;
- string F[4];
- int L[4];
- cout<<"Insert IP address:"<<endl;
- cin>>ip;
- size_t x = ip.find(".");
- F[0] = ip.substr (0,x);
- size_t m = ip.find(".",x+1);
- F[1] = ip.substr (x+1,m-x-1);
- size_t h = ip.find(".",m+1);
- F[2] = ip.substr (m+1,h-m-1);
- size_t n = ip.find(".",h+1);
- F[3] = ip.substr (h+1,n-h-1);
- L[0]=stoi(F[0],nullptr,10);
- L[1]=stoi(F[1],nullptr,10);
- L[2]=stoi(F[2],nullptr,10);
- L[3]=stoi(F[3],nullptr,10);
- if(L[0]>=256|L[0]<=0)
- {
- cout<<"Invalid Address";
- return 0;
- }
- bitset<8> K(L[0]);
- bitset<8> K1(L[1]);
- bitset<8> K2(L[2]);
- bitset<8> K3(L[3]);
- cout<<"BINARY CODE:"<<K<<"."<<K1<<"."<<K2<<"."<<K3<<endl;
- string ch;
- cout<<"Insert address class:\n"<<"A\n"<<"B\n"<<"C\n";
- cin>>ch;
- bitset<8> SMA(255);
- bitset<8> SMA1(0);
- bitset<8> SMA2(0);
- bitset<8> SMA3(0);
- bitset<8> SMB(255);
- bitset<8> SMB1(255);
- bitset<8> SMB2(0);
- bitset<8> SMB3(0);
- bitset<8> SMC(255);
- bitset<8> SMC1(255);
- bitset<8> SMC2(255);
- bitset<8> SMC3(0);
- bitset<8>X;
- bitset<8>X1;
- bitset<8>X2;
- bitset<8>X3;
- cout<<"Insert number of host"<<endl;
- int N;
- cin>>N;
- bitset<32>SBN(N);
- string SBNstr=SBN.to_string();
- if(ch=="A")
- {
- X=K&SMA;
- X1=K1&SMA1;
- X2=K2&SMA2;
- X3=K3&SMA3;
- cout<<"LOGICAL AND:"<<X<<"."<<X1<<"."<<X2<<"."<<X3<<endl;
- size_t g = SBNstr.find("1");
- if (N>=16777214|L[0]>=126)
- {
- cout<<"Request error"<<endl;
- return 0;
- }
- string O=X.to_string();
- string O1=X1.to_string();
- string O2=X2.to_string();
- string O3=X3.to_string();
- string BYTE;
- BYTE=O+O1+O2+O3;
- bitset<32>BYTEbin(BYTE);
- string lor;
- bitset<32>lorx=BYTEbin|SBN;
- lor=lorx.to_string();
- string by=lor.substr (0,8);
- string by1=lor.substr (8,8);
- string by2=lor.substr (16,8);
- string by3=lor.substr (24,8);
- int mby;
- int mby1;
- int mby2;
- int mby3;
- mby=stoi(by,nullptr,2);
- mby1=stoi(by1,nullptr,2);
- mby2=stoi(by2,nullptr,2);
- mby3=stoi(by3,nullptr,2);
- cout<<mby<<"."<<mby1<<"."<<mby2<<"."<<mby3;
- }
- else if(ch=="B")
- {
- X=K&SMB;
- X1=K1&SMB1;
- X2=K2&SMB2;
- X3=K3&SMB3;
- cout<<"LOGICAL AND:"<<X<<"."<<X1<<"."<<X2<<"."<<X3<<endl;
- size_t g = SBNstr.find("1");
- if (N>=16777214|L[0]>=192|L[0]<=127)
- {
- cout<<"Request error"<<endl;
- return 0;
- }
- string O=X.to_string();
- string O1=X1.to_string();
- string O2=X2.to_string();
- string O3=X3.to_string();
- string BYTE;
- BYTE=O+O1+O2+O3;
- bitset<32>BYTEbin(BYTE);
- string lor;
- bitset<32>lorx=BYTEbin|SBN;
- lor=lorx.to_string();
- string by=lor.substr (0,8);
- string by1=lor.substr (8,8);
- string by2=lor.substr (16,8);
- string by3=lor.substr (24,8);
- int mby;
- int mby1;
- int mby2;
- int mby3;
- mby=stoi(by,nullptr,2);
- mby1=stoi(by1,nullptr,2);
- mby2=stoi(by2,nullptr,2);
- mby3=stoi(by3,nullptr,2);
- cout<<mby<<"."<<mby1<<"."<<mby2<<"."<<mby3;
- }
- else if(ch=="C")
- {
- X=K&SMC;
- X1=K1&SMC1;
- X2=K2&SMC2;
- X3=K3&SMC3;
- cout<<"LOGICAL AND:"<<X<<"."<<X1<<"."<<X2<<"."<<X3<<endl;
- size_t g = SBNstr.find("1");
- if (N>=16777214|L[0]>=256|L[0]<=191)
- {
- cout<<"Request error"<<endl;
- return 0;
- }
- string O=X.to_string();
- string O1=X1.to_string();
- string O2=X2.to_string();
- string O3=X3.to_string();
- string BYTE;
- BYTE=O+O1+O2+O3;
- bitset<32>BYTEbin(BYTE);
- string lor;
- bitset<32>lorx=BYTEbin|SBN;
- lor=lorx.to_string();
- string by=lor.substr (0,8);
- string by1=lor.substr (8,8);
- string by2=lor.substr (16,8);
- string by3=lor.substr (24,8);
- int mby;
- int mby1;
- int mby2;
- int mby3;
- mby=stoi(by,nullptr,2);
- mby1=stoi(by1,nullptr,2);
- mby2=stoi(by2,nullptr,2);
- mby3=stoi(by3,nullptr,2);
- cout<<mby<<"."<<mby1<<"."<<mby2<<"."<<mby3;
- }
- else
- {
- cout<<"Please reinsert the class";
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement