华为OJ2417合法

来源:互联网 发布:mysql的触发器 编辑:程序博客网 时间:2024/05/18 01:01
//华为oj合法IP题目
#include<iostream>
#include<string>
using namespace std;
bool IsDigit(char Digit)//判断字符是都为数字
{
bool flag = false;
if (Digit >= '0'&&Digit <= '9')
{
flag = true;
}
return flag;
}
bool IsFormatValid(char *IP)
{
int DotCnt = 0;
while (*IP != '\0')
{
if (*IP == '.')
{
DotCnt++;
}
else if (!IsDigit(*IP))
{
return false;
}
IP++;
}
if (DotCnt == 3)
return true;
else
return false;
}
bool IsValueVaild(char *IP)
{
int n = 0;
while (*IP != '\0')
{
if (IsDigit(*IP))
{
n = n * 10 + *IP-'0';
}
else
{
if (n > 255)
return false;
n = 0;
}
IP++;
}
return true;
}
int main()
{
char s[30];
//string s;
while (cin >> s)
{
if (IsFormatValid(s) && IsValueVaild(s))
{
cout << "YES" << endl;
}
else
cout << "NO" << endl;
}
return 0;


}
0 0
原创粉丝点击