求指出错误之处,可 不要代码
这是我的代码:
#include <string>
using namespace std;
struct node {
int type;
string ad;
} a[1005];
bool isAdCorrect(string ad) {
int onep = 0;
int twop = 0;
int nlen = 0;
int cnt = 1;
int sum = 0;
for (int i = ad.length() - 1; i >= 0; i--) {
if (ad[i] >= '0' && ad[i] <= '9') {
nlen++;
sum += cnt * (ad[i] - '0');
cnt *= 10;
} else if (ad[i] == '.') {
if (ad[i+1] == '0' && nlen > 1) {
return false;
}
if (sum > 255) {
return false;
}
if (twop < 1) {
return false;
}
onep++;
nlen = 0;
sum = 0;
cnt = 1;
} else if (ad[i] == ':') {
if (ad[i+1] == '0' && nlen > 1) {
return false;
}
if (sum > 65535) {
return false;
}
if (onep > 0) {
return false;
}
twop++;
nlen = 0;
sum = 0;
cnt = 1;
} else {
return false;
}
}
if (onep != 3 || twop != 1) {
return false;
}
return true;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
string op;
cin >> op;
if (op == "Server") {
a[i].type = 0;
cin >> a[i].ad;
if (!isAdCorrect(a[i].ad)) {
cout << "ERR" << endl;
continue;
}
bool flag = true;
for (int j = 1; j < i; j++) {
if (a[j].type == 0 && a[j].ad == a[i].ad) {
cout << "FAIL" << endl;
flag = false;
break;
}
}
if (!flag) {
continue;
}
cout << "OK" << endl;
} else if (op == "Client") {
a[i].type = 1;
cin >> a[i].ad;
if (!isAdCorrect(a[i].ad)) {
cout << "ERR" << endl;
continue;
}
bool flag = false;
for (int j = 1; j < i; j++) {
if (a[j].type == 0 && a[j].ad == a[i].ad) {
cout << j << endl;
flag = true;
break;
}
}
if (flag) {
continue;
}
cout << "FAIL" << endl;
} else {
a[i].type = -1;
cin >> a[i].ad;
}
}
return 0;
}