/* *Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:main.cpp *作 者:张珩瑞 *完成日期:2016年4月6日 *版 本 号:v1.0 * *问题描述:分数类的雏形 输入描述:分数 *输出描述:分数的各种表现方式,化简 */ #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 s0,int s1,int s2,int s3) { seg0=s0; seg1=s1; seg2=s2; seg3=s3; } void IP::showIP() { cout<<int(seg0)<<"."<<int(seg1)<<"."<<int(seg2)<<"."<<int(seg3); } 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); } 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; return 0; }