Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <string>
- using namespace std;
- class IP
- {
- public:
- string FullIP;
- int adress[4];
- int IPadress[4];
- int submask[4];
- int netPrefix[4];
- int broadcastAdr[4];
- IP()
- {
- cout << "Podaj adres IP: ";
- cin >> FullIP;
- splitIP();
- IPadress[0] = adress[0];
- IPadress[1] = adress[1];
- IPadress[2] = adress[2];
- IPadress[3] = adress[3];
- cout << "Podaj maske podsieci: ";
- cin >> FullIP;
- splitIP();
- submask[0] = adress[0];
- submask[1] = adress[1];
- submask[2] = adress[2];
- submask[3] = adress[3];
- networkPrefix();
- cout<<"Adres calej podsieci: "<< niceFormat(netPrefix)<<endl;
- broadcastAdress();
- cout<<"Adres rozgloszeniowy: "<< niceFormat(broadcastAdr)<<endl;
- cout<<"Ilosc hostow w sieci: " << hostAmount(connect4(submask));
- }
- void splitIP()
- {
- char temp[3];
- temp[0] = '0';
- temp[1] = '0';
- temp[2] = '0';
- int counter=0;
- int counter2=0;
- for (unsigned int i = 0; i <= FullIP.size(); i++)
- {
- if(FullIP[i]!='.'&&i<FullIP.size())
- {
- temp[counter] = FullIP[i];
- counter++;
- }
- else
- {
- if(temp==0)
- adress[counter2] = 0;
- else if(counter==3)
- adress[counter2] = asInt(temp);
- else if(counter==2)
- {
- temp[2] = temp[1];
- temp[1] = temp[0];
- temp[0] = '0';
- adress[counter2] = asInt(temp);
- }
- else
- {
- temp[2] = temp[0];
- temp[1] = '0';
- temp[0] = '0';
- adress[counter2] = asInt(temp);
- }
- counter = 0;
- counter2++;
- temp[0] = '0';
- temp[1] = '0';
- temp[2] = '0';
- }
- }
- }
- int asInt(char n[3]) //konwersja string na int
- {
- int temp = 0;
- temp += (n[0] - '0') * 100; //od kodu ascii znaku cyfry adresu ip odejmujemy kod znaku '0' otrzymując w ten sposób liczby.
- temp += (n[1] - '0') * 10;
- temp += (n[2] - '0');
- return temp;
- }
- void networkPrefix()
- {
- string temp;
- netPrefix[0] = (IPadress[0]&submask[0]); //operacja logiczna AND | &
- netPrefix[1] = (IPadress[1]&submask[1]);
- netPrefix[2] = (IPadress[2]&submask[2]);
- netPrefix[3] = (IPadress[3]&submask[3]);
- }
- void broadcastAdress()
- {
- int temp[4];
- temp[0] = toDecimal(toBinaryWithInverse(submask[0]));
- temp[1] = toDecimal(toBinaryWithInverse(submask[1]));
- temp[2] = toDecimal(toBinaryWithInverse(submask[2]));
- temp[3] = toDecimal(toBinaryWithInverse(submask[3]));
- broadcastAdr[0] = temp[0] + netPrefix[0];
- broadcastAdr[1] = temp[1] + netPrefix[1];
- broadcastAdr[2] = temp[2] + netPrefix[2];
- broadcastAdr[3] = temp[3] + netPrefix[3];
- }
- string niceFormat(int *tab)
- {
- string temp;
- temp+=to_string(tab[0]);
- temp+='.';
- temp+=to_string(tab[1]);
- temp+='.';
- temp+=to_string(tab[2]);
- temp+='.';
- temp+=to_string(tab[3]);
- return temp;
- }
- string toBinaryWithInverse(int n)
- {
- string r;
- while(n!=0)
- {
- r=(n%2==0 ?"1":"0")+r; //tutaj odbywa się negacja, zero i jeden są na odwrót co daje nam negacje podczas konwersji | sama konwersja -> r=(n%2==0 ?"0":"1")+r;
- n/=2;
- }
- return r;
- }
- string toBinary(int n)
- {
- string r;
- while(n!=0)
- {
- r=(n%2==0 ?"0":"1")+r; //tutaj odbywa się negacja, zero i jeden są na odwrót co daje nam negacje podczas konwersji | sama konwersja -> r=(n%2==0 ?"0":"1")+r;
- n/=2;
- }
- return r;
- }
- int toDecimal(string a) {
- int temp=0;
- for (unsigned int i = 0; i<a.size(); i++) {
- temp+=(a[i]-'0') * pow(2, a.size()-i-1);
- }
- return temp;
- }
- string connect4(int *tab){
- string temp;
- temp+=toBinary(tab[0]);
- temp+=toBinary(tab[1]);
- temp+=toBinary(tab[2]);
- temp+=toBinary(tab[3]);
- return temp;
- }
- int hostAmount(string a) {
- int n=0;
- for (unsigned int i = 0; i< a.size(); i++) {
- if(a[i] == '0') n++;
- }
- return pow(2, n)-2;
- }
- };
- int main()
- {
- IP test;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement