文章
84
粉丝
408
获赞
33
访问
877.9k
注意输出格式:十六进制不足两位要补零
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
const int M = 1024;
int main()
{
char line[M], tmp[20];
int ip[8];
int count = 0;
cin.getline(line, M);
int i = 0, head = 0, len = 0;
bool flag = true;//是否合法
while (line[i]) {
if (line[i] >= '0' && line[i] <= '9') {
len++;
}
else if (line[i] == '.') {
memset(tmp, 0, sizeof(tmp));
strncpy(tmp, line+head, len);
head = i + 1;
len = 0;
int num = atoi(tmp);
if (num < 0 || num > 255) {
flag = false;
break;
}
else {
ip[count++] = num;
if (count > 4) {
flag = false;
break;
}
}
}
else {
flag = false;
break;
}
i++;
}
memset(tmp, 0, sizeof(tmp));
strncpy(tmp, line+head, len);
int num = atoi(tmp);
if (num < 0 || num > 255) {
flag = false;
}
else {
ip[count++] = num;
if (count > 4) {
flag = false;...
登录后发布评论
暂无评论,来抢沙发