您的位置:首页 > 其它

第六周第三项目——IP地址

2016-04-05 18:39 302 查看
01./*Copyright (c)2016,烟台大学计算机与控制工程学院
02.*All rights reserved.
03.*文件名称:main.cpp
04.*作    者:田志伟
05.*完成日期:2016年4月4日
06.*版 本 号:v1.0
07.*
08.*问题描述:判断IP地址的类型是否属于同一子网
09.
10. 输入描述:
11.*输出描述:IP地址的类型
12.*/
13.
14.#include <iostream>
15.using namespace std;
16.class IP
17.{
18.private:
19.    union
20.    {
21.        struct
22.        {
23.            unsigned char seg0;
24.            unsigned char seg1;
25.            unsigned char seg2;
26.            unsigned char seg3;
27.        };
28.        unsigned int address;
29.    };
30.public:
31.    IP(int=0,int=0,int=0,int=0);
32.    void showIP();
33.    bool sameSubnet(const IP&ip,const IP&mark);
34.    char whatKind();
35.
36.};
37.IP::IP(int s0,int s1,int s2,int s3)
38.{
39.    seg0=s0;
40.    seg1=s1;
41.    seg2=s2;
42.    seg3=s3;
43.}
44.
45.void IP::showIP()
46.{
47.    cout<<int(seg0)<<"."<<int(seg1)<<"."<<int(seg2)<<"."<<int(seg3);
48.}
49.bool IP::sameSubnet(const IP &ip, const IP &mark)
50.{
51.
52.    unsigned int i1, i2;
53.    i1=address&mark.address;
54.    i2=ip.address&mark.address;
55.    return (i1==i2);
56.}
57.char IP::whatKind()
58.{
59.
60.    if(seg0<128)
61.        return 'A';
62.    else if(seg0<192)
63.        return 'B';
64.    else if(seg0<224)
65.        return 'C';
66.    else if(seg0<240)
67.        return 'D';
68.    else
69.        return 'E';
70.}
71.int main()
72.{
73.    IP ip1(202,194,116,97), ip2(202,194,119,102), mark(255,255,248,0);
74.    cout<<"ip1: ";
75.    ip1.showIP();
76.    cout<<"ip2: ";
77.    ip2.showIP();
78.    if(ip1.sameSubnet(ip2,mark))
79.        cout<<"两个IP在同一子网"<<endl;
80.    else
81.        cout<<"两个IP不在同一子网"<<endl;
82.    cout<<"ip1属于"<<ip1.whatKind()<<"类网络"<<endl;
83.    return 0;
84.}

运行程序:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: