1. C++词法与规则
2.数据类型
3. 常量
4.变量
5. 运算符
6.实战
using namespace std;
using namespace cv;
typedef int INT;
int main()
{
//定义要用的变量
unsigned long dwSize = 0;
MIB_IPADDRTABLE* pTable = (MIB_IPADDRTABLE*)malloc(sizeof(MIB_IPADDRTABLE));
//获取本机的IP地址列表,存放在pTable所指向的空间中中
if (GetIpAddrTable(pTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
{
free(pTable); //释放申请的内存空间
pTable = (MIB_IPADDRTABLE*)malloc(dwSize);
}
if (GetIpAddrTable(pTable, &dwSize, 0) != 0) //如果获取地址列表失败,则结束整个程序
{
cout << "获取地址表失败";
exit(-1);
}
for (INT i = 0; i < (int)pTable->dwNumEntries;i++) {
//输出选择的IP地址
cout<<"\n"<<"Interface Index["<<i<<"]"<< pTable->table[i].dwIndex<<endl;
unsigned long ip =pTable->table[i].dwAddr;
cout<<"\tIP Address["<<i<<"]:"<<" \t"<<ip<<endl;
cout << "\t原始存储形式值:" << hex << ip << endl;
cout << "\t点分十进制形式:" << dec
<< (ip & 0xff) << '.'
<< ((ip >> 8) & 0xff) << '.'
<< ((ip >> 16) & 0xff) << '.'
<< ((ip >> 24) & 0xff) << endl;
//输出选择的子网掩码
unsigned long mask = pTable->table[i].dwMask;
cout << "\t子网掩码[" << i << "]:" << " \t" << mask << endl;
cout << "\t原始存储形式值:" << hex << mask << endl;
cout << "\t点分十进制形式:" << dec
<< (mask & 0xff) << '.'
<< ((mask >> 8) & 0xff) << '.'
<< ((mask >> 16) & 0xff) << '.'
<< ((mask >> 24) & 0xff) << endl;
//计算主机号并显示
cout << "\t本机主机号:" << endl;
unsigned long HostAdd = ~mask & ip;
cout << '\t' << (HostAdd & 0xff) << '.'
<< ((HostAdd >> 8) & 0xff) << '.'
<< ((HostAdd >> 16) & 0xff) << '.'
<< ((HostAdd >> 24) & 0xff) << endl;
//计算广播地址并显示
unsigned long broadAdd = (mask & ip) | ~mask;
cout << "\t所在网络广播地址:" << endl;
cout << '\t' << (broadAdd & 0xff) << '.'
<< ((broadAdd >> 8) & 0xff) << '.'
<< ((broadAdd >> 16) & 0xff) << '.'
<< ((broadAdd >> 24) & 0xff) << endl;
}
return 0;
}
结果:
|