weidiezeng


私信TA

用户名:weidie

访问量:2563

签 名:

等  级
排  名 22973
经  验 601
参赛次数 0
文章发表 5
年  龄 0
在职情况 学生
学  校 五邑大学
专  业

  自我简介:

解题思路:把字符串用.隔开,判断是否为数字,不是返回false;如果是运用atoi函数转为int并判断是大于0小于255,是返回true。

注意事项:巧妙地运用了atoi函数

参考代码:

#include<iostream>

#include<string>

#include<cstdlib>//atoi头文件 


using namespace std;


bool judge(string str)

{

string str1;

int i=0;

while(i<str.length())

{

 

while(str[i]!='.'&&i<str.length())

{

if(str[i]>='0'&&str[i]<='9')

{

str1+=str[i];

}

else

{

return false;

}

 

i++;

}


int num; 

num=atoi(str1.c_str());//string 转num 

if(num<0||num>255)

{

return false;

}

str1="";//把string清空 

i++;//越过. 

}

return true;


}

int main()

{

string str;

while(cin>>str)

if(judge(str)==true)

cout<<"Y"<<endl;

else

cout<<'N'<<endl; 

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区