永发信息网

一个错误。两个警告

答案:1  悬赏:80  手机版
解决时间 2021-02-23 06:07
// 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));
}
}
}
}
最佳答案
#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));
            }
        }
    }
}没有语法错误了,没有仔细看功能
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
十七八岁了一般女生都会有情窦初开的感觉吧
【军神阅读答案】阅读《军神》文章及答案
直线xtanπ/5+y=0倾斜角为
文梅山庄怎么去啊,有知道地址的么
济宁贵和屈臣氏刷贵和的购物卡不?
关干小动物之间的作文
慈云路/朝阳路(路口)我想知道这个在什么地方
网易抽奖中金条?这是真的嘛?
高分跪求热门小马全集(日语版)
全国高考中考时间是统一的吗
什么叫眼影散粉?是让眼影快速定形的吗?还有
寻仙电工全攻略!?
鹏翔殡葬服务站怎么去啊,有知道地址的么
急问:2009年5月9日用天干地支表示是什么年啊
木耳和鸭肉能一起吃吗
推荐资讯
胸闷,上不来气,心电彩超都正常,不知怎么回
合肥什么工作适合带孩子的家长:什么工作适合
房产证是70年产权,但土地使用权是30-40年这
父母买棺材回家,听别人说女儿要送贺礼吗
2016年贷款30万10年每月还多少
歌唱明星刀郎出生在什么地方
DO you have any brothers?的 同义句--------
我的iphone4震动键失灵怎么办
板电地址有知道的么?有点事想过去
招商银行房贷多久可以提前还款
广州诚杰顾问有限公司怎么样?
美之金典女子会所这个地址在什么地方,我要处
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?