华为OJ2417合法

//华为oj合法IP题目
#include
#include
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;


}

你可能感兴趣的:(华为OJ测试题)