我讲一下我想到的 看看有没有遗漏
不是 3 个 .
或者不是 1 个 :
数字超过范围
数字自然溢出了并刚好进 0 ~ 255 内
前导零 (排除了单个 0 的情况)
相邻符号
然后 WA 75
code:
#include <bits/stdc++.h>
using namespace std;
//int ip1, ip2, ip3, ip4, port;
class Address {
public:
int ip1, ip2, ip3, ip4, port;
};
class Format {
private:
int stringToInt(string s) {
int res = 0, len = s.size();
for (int i = 0; i < len; i++) {
res = res * 10 + (s[i] - '0');
}
return res;
}
bool checkIp(int ip) {
if (0 <= ip && ip <= 255) return true;
return false;
}
bool checkPort(int port) {
if (0 <= port && port <= 65535) return true;
return false;
}
public:
bool checkAddr(string s) {
string split[10];
int cur = 0, len = s.size();
int c1 = 0, c2 = 0;
for (int i = 0; i < len; i++) {
if (s[i] == '.') {
cur++;
c1++;
} else if (s[i] == ':') {
cur++;
c2++;
} else {
split[cur] += s[i];
}
if (cur > 4) return false;
}
// cout << endl << c1 << endl << c2 << endl;
if (c1 != 3 || c2 != 1) return false;
Address res;
for (int i = 0; i < 5; i++) {
if (split[i].size() > 1) {
if (split[i][0] == '0') return false;
}
if (split[i].size() > 6) return false;
if (split[i].size() == 0) return false;
}
res.ip1 = stringToInt(split[0]);
res.ip2 = stringToInt(split[1]);
res.ip3 = stringToInt(split[2]);
res.ip4 = stringToInt(split[3]);
res.port = stringToInt(split[4]);
return (checkIp(res.ip1)
&& checkIp(res.ip2)
&& checkIp(res.ip3)
&& checkIp(res.ip4)
&& checkPort(res.port));
}
Address parseAddr(string s) {
string split[10];
int cur = 0, len = s.size();
for (int i = 0; i < len; i++) {
if (s[i] == '.' || s[i] == ':') {
cur++;
} else {
split[cur] += s[i];
}
}
Address res;
res.ip1 = stringToInt(split[0]);
res.ip2 = stringToInt(split[1]);
res.ip3 = stringToInt(split[2]);
res.ip4 = stringToInt(split[3]);
res.port = stringToInt(split[4]);
return res;
}
} format;
class Network {
private:
map<string, int> servers;
public:
int create(string addr, int id) {
if (format.checkAddr(addr) == false) return 0;
if (servers[addr] != 0) return 1;
servers[addr] = id;
return 2;
}
int connect(string addr) {
return servers[addr];
}
} network;
int n;
string s, add;
int main() {
// scanf("%d.%d.%d.%d:%d", &ip1, &ip2, &ip3, &ip4, &port);
// printf("%d %d %d %d %d", ip1, ip2, ip3, ip4, port);
// cout << instance.stringToInt("123123");
// Address a = instance.parseAddr("1.117.1.17:11117");
// cout << a.ip1 << " " << a.ip2 << " " << a.ip3 << " " << a.ip4 << " " << a.port << endl;
// cout << format.checkAddr("88.112.9.7:4294967300");
// network.servers["123.123.123.1:1"] = 1;
// cout << network.servers["123.123.123.1:2"];
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s >> add;
if (s == "Server") {
int res = network.create(add, i);
if (res == 0) {
printf("ERR\n");
} else if (res == 1) {
printf("FAIL\n");
} else if (res == 2) {
printf("OK\n");
}
} else if (s == "Client") {
if (format.checkAddr(add) == false) {
printf("ERR\n");
} else {
int res = network.connect(add);
if (res == 0) {
printf("FAIL\n");
} else {
printf("%d\n", res);
}
}
}
}
}