// CalculateSubnet.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include"winsock2.h"
#include <iostream>
#pragma comment( lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
if(argc != 3)
{
printf("Usage: CalculateSubnet netaddr netmask\r\nExample: CalculateSubnet 192.168.0.0 255. 255.255.0");
return 1;
}
bool IsValidIP(char* ip)
{
std::string sip = ip;
int pos = (int)sip.find_first_of(".");
if(pos == 0)
return false;
std::string s1 = sip.substr(0,pos);
sip = sip.substr(pos+1,sip.length() -pos);
if(s1.length() > 3)
return false;
for(int i = 0;i<(int)s1.length();i++)
{
int c = s1.c_str()[i];
if(!isdigit(c))
return false;
}
int a = atoi(s1.c_str());
if(a<1 || a>255)
return false;
pos = (int)sip.find_first_of(".");
std::string s2 = sip.substr(0,pos);
sip = sip.substr(pos + 1,sip.length() - pos);
if(s2.length() > 3)
return false;
for(int i = 0;i<(int)s2.length();i++)
{
int c = s2.c_str()[i];
if(!isdigit(c))
return false;
}
a = atoi(s2.c_str());
if(a>255)
return false;
pos = (int)sip.find_first_of(".");
std::string s3 = sip.substr(0,pos);
sip = sip.substr(pos + 1,sip.length() - pos);
if(s3.length() > 3)
return false;
for(int i = 0 ;i<(int)s3.length();i++)
{
int c = s3.c_str()[i];
if(!isdigit(c))
return false;
}
a = atoi(s3.c_str());
if(a>255)
return false;
pos = (int)sip.find_first_of(".");
std::string s4 = sip.substr(0,pos);
sip = sip.substr(pos + 1,sip.length() - pos);
if(s4.length() > 3)
return false;
for(int i = 0;i<(int)s4.length();i++)
{
int c = s4.c_str()[i];
if(!isdigit(c))
return false;
}
a = atoi(s4.c_str());
if(a>254)
return true;
}
if(!IsValidIP ( argv[1] ) );
{
printf("%s is not a valid ip.\n",argv[1]);
return 1;
if (!IsValidIP(argv[2]));
{
printf("%s is not a valid ip.\n",argv[2]);
return 1;
printf("netaddr:%s\n",argv[1]);
printf("netmask:%s\n",argv[2]);
unsigned long lnetaddr = ntohl(inet_addr(argv[1]));
unsigned long lnetmask = ntohl(inet_addr(argv[2]));
unsigned long l_first_netaddr = lnetaddr & lnetmask;
unsigned long l_broadcast = lnetaddr | ~lnetmask;
long num = l_broadcast - l_first_netaddr- 1;
printf("Number of valid IPS: %d\n\n",num);
printf("IPs in subnet: \n===============\n");
for(unsigned long i = l_first_netaddr+1;i< l_broadcast;i++)
{
in_addr IPAddr;
IPAddr.S_un.S_addr = ntohl(i);
printf("%s\n",inet_ntoa(IPAddr));
}
}
}
}
一个错误。两个警告
答案:1 悬赏:80 手机版
解决时间 2021-02-23 06:07
- 提问者网友:斑駁影
- 2021-02-22 20:48
最佳答案
- 五星知识达人网友:摆渡翁
- 2021-02-22 21:06
#include"winsock2.h"
#include <iostream>
#pragma comment( lib,"ws2_32.lib")
bool IsValidIP(char* ip)
{
std::string sip = ip;
int pos = (int)sip.find_first_of(".");
if(pos == 0)
return false;
std::string s1 = sip.substr(0,pos);
sip = sip.substr(pos+1,sip.length() -pos);
if(s1.length() > 3)
return false;
for(int i = 0;i<(int)s1.length();i++)
{
int c = s1.c_str()[i];
if(!isdigit(c))
return false;
}
int a = atoi(s1.c_str());
if(a<1 || a>255)
return false;
pos = (int)sip.find_first_of(".");
std::string s2 = sip.substr(0,pos);
sip = sip.substr(pos + 1,sip.length() - pos);
if(s2.length() > 3)
return false;
for(int i = 0;i<(int)s2.length();i++)
{
int c = s2.c_str()[i];
if(!isdigit(c))
return false;
}
a = atoi(s2.c_str());
if(a>255)
return false;
pos = (int)sip.find_first_of(".");
std::string s3 = sip.substr(0,pos);
sip = sip.substr(pos + 1,sip.length() - pos);
if(s3.length() > 3)
return false;
for(int i = 0 ;i<(int)s3.length();i++)
{
int c = s3.c_str()[i];
if(!isdigit(c))
return false;
}
a = atoi(s3.c_str());
if(a>255)
return false;
pos = (int)sip.find_first_of(".");
std::string s4 = sip.substr(0,pos);
sip = sip.substr(pos + 1,sip.length() - pos);
if(s4.length() > 3)
return false;
for(int i = 0;i<(int)s4.length();i++)
{
int c = s4.c_str()[i];
if(!isdigit(c))
return false;
}
a = atoi(s4.c_str());
if(a>254)
return true;
return false;
}
int _tmain(int argc, char** argv)
{
if(argc != 3)
{
printf("Usage: CalculateSubnet netaddr netmask\r\nExample: CalculateSubnet 192.168.0.0 255. 255.255.0");
return 1;
}
if(!IsValidIP ( argv[1] ) )//one
{
printf("%s is not a valid ip.\n",argv[1]);
return 1;
if (!IsValidIP(argv[2]))//; //
{
printf("%s is not a valid ip.\n",argv[2]);
return 1;
printf("netaddr:%s\n",argv[1]);
printf("netmask:%s\n",argv[2]);
unsigned long lnetaddr = ntohl(inet_addr(argv[1]));
unsigned long lnetmask = ntohl(inet_addr(argv[2]));
unsigned long l_first_netaddr = lnetaddr & lnetmask;
unsigned long l_broadcast = lnetaddr | ~lnetmask;
long num = l_broadcast - l_first_netaddr- 1;
printf("Number of valid IPS: %d\n\n",num);
printf("IPs in subnet: \n===============\n");
for(unsigned long i = l_first_netaddr+1;i< l_broadcast;i++)
{
in_addr IPAddr;
IPAddr.S_un.S_addr = ntohl(i);
printf("%s\n",inet_ntoa(IPAddr));
}
}
}
}没有语法错误了,没有仔细看功能
#include <iostream>
#pragma comment( lib,"ws2_32.lib")
bool IsValidIP(char* ip)
{
std::string sip = ip;
int pos = (int)sip.find_first_of(".");
if(pos == 0)
return false;
std::string s1 = sip.substr(0,pos);
sip = sip.substr(pos+1,sip.length() -pos);
if(s1.length() > 3)
return false;
for(int i = 0;i<(int)s1.length();i++)
{
int c = s1.c_str()[i];
if(!isdigit(c))
return false;
}
int a = atoi(s1.c_str());
if(a<1 || a>255)
return false;
pos = (int)sip.find_first_of(".");
std::string s2 = sip.substr(0,pos);
sip = sip.substr(pos + 1,sip.length() - pos);
if(s2.length() > 3)
return false;
for(int i = 0;i<(int)s2.length();i++)
{
int c = s2.c_str()[i];
if(!isdigit(c))
return false;
}
a = atoi(s2.c_str());
if(a>255)
return false;
pos = (int)sip.find_first_of(".");
std::string s3 = sip.substr(0,pos);
sip = sip.substr(pos + 1,sip.length() - pos);
if(s3.length() > 3)
return false;
for(int i = 0 ;i<(int)s3.length();i++)
{
int c = s3.c_str()[i];
if(!isdigit(c))
return false;
}
a = atoi(s3.c_str());
if(a>255)
return false;
pos = (int)sip.find_first_of(".");
std::string s4 = sip.substr(0,pos);
sip = sip.substr(pos + 1,sip.length() - pos);
if(s4.length() > 3)
return false;
for(int i = 0;i<(int)s4.length();i++)
{
int c = s4.c_str()[i];
if(!isdigit(c))
return false;
}
a = atoi(s4.c_str());
if(a>254)
return true;
return false;
}
int _tmain(int argc, char** argv)
{
if(argc != 3)
{
printf("Usage: CalculateSubnet netaddr netmask\r\nExample: CalculateSubnet 192.168.0.0 255. 255.255.0");
return 1;
}
if(!IsValidIP ( argv[1] ) )//one
{
printf("%s is not a valid ip.\n",argv[1]);
return 1;
if (!IsValidIP(argv[2]))//; //
{
printf("%s is not a valid ip.\n",argv[2]);
return 1;
printf("netaddr:%s\n",argv[1]);
printf("netmask:%s\n",argv[2]);
unsigned long lnetaddr = ntohl(inet_addr(argv[1]));
unsigned long lnetmask = ntohl(inet_addr(argv[2]));
unsigned long l_first_netaddr = lnetaddr & lnetmask;
unsigned long l_broadcast = lnetaddr | ~lnetmask;
long num = l_broadcast - l_first_netaddr- 1;
printf("Number of valid IPS: %d\n\n",num);
printf("IPs in subnet: \n===============\n");
for(unsigned long i = l_first_netaddr+1;i< l_broadcast;i++)
{
in_addr IPAddr;
IPAddr.S_un.S_addr = ntohl(i);
printf("%s\n",inet_ntoa(IPAddr));
}
}
}
}没有语法错误了,没有仔细看功能
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯