现在IPV4下用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此不需要用正号出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
现在需要你用程序来判断IP是否合法。
bool panDuan(int num){
if (num >= 0 && num <= 255)return true;
else return false;
}
void hwOJ(){
string ipv4 = "256.223.117.118";
int ipPool[4] = { 0 };
string tmp = "";
stringstream ss;
int ipd=0,k=0;
int len = ipv4.length();
bool f = true;
for (int i = 0; i <len; ++i){
if (ipv4[i] == '.'){ ss << tmp; ss >> ipd; ipPool[k++] = ipd; tmp.clear(); ss.clear(); }
else
{
tmp.push_back(ipv4[i]);
}
}
ss << tmp; ss >> ipd; ipPool[k++] = ipd;
for (int i = 0; i < 4; ++i){
if (panDuan(ipPool[i]) == false){ f = false; break; }
}
if (f)cout << "YES\n";
else cout << "NO\n";
}