IP地址类

来源:互联网 发布:php 布尔型 编辑:程序博客网 时间:2024/05/16 23:55

问题及代码:

/* *copyright(c) 2014,烟台大学计算机学院 *All rights reserved *文件名称:test.cpp *作者:杨昊 *版本:v6.0 *时间;2016年4月10日 * *问题描述:Ip地址类 *输入描述:无 *程序输出:*/#include<iostream> using namespace std; class IP { private:     union     {         struct         {             unsigned char seg0;             unsigned char seg1;             unsigned char seg2;             unsigned char seg3;         };         unsigned int address;     }; public:     IP(int =0,int=0,int=0,int=0);     void showIP();    bool sameSubnet(const IP &ip, const IP &mark);     char whatKind(); }; IP::IP(int a,int b,int c,int d) {     seg0=a;     seg1=b;     seg2=c;     seg3=d; } //bool IP::sameSubnet(const IP &ip, const IP &mark) {     unsigned int i1, i2;     i1=address&mark.address;     i2=ip.address&mark.address;     return (i1==i2); } ///////////////////////////    void IP:: showIP()    {        int a=seg0;        int b=seg1;         int c=seg2;         int d=seg3;         cout<<a<<"."<<b<<"."<<c<<"."<<d<<endl;    } char  IP ::whatKind()     {         if(seg0<128)             return 'A';         else if(seg0<192)             return 'B';          else if(seg0<224)             return 'C';         else if(seg0<240)             return 'D';         else             return 'E';     } int main() {     IP ip1(202,194,116,97),ip2(202,194,119,102),mark(255,255,248,0);     cout<<"ip1:";     ip1.showIP();     cout<<"ip2:";     ip2.showIP();      if(ip1.sameSubnet(ip2,mark))          cout<<"两个IP在同一子网"<<endl;     else            cout<<"两个IP不在同一子网"<<endl;     cout<<"ip1属于"<<ip1.whatKind()<<"类网络"<<endl; }

0 0
原创粉丝点击