捞一下求助
查看原帖
捞一下求助
759274
Stevehim楼主2022/10/28 14:48
#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
int n; // 需要进行连接的计算机数量
int server_num = 0; //服务器数量
string ip;
map<string, int>server;
/*
需要检查:
    1.".",":"的使用
    2.是否有前导零
    3.数字是否超限
*/

int checkDot(string s) { //该函数检查'.',':'的规范
//  cout << "checking dot..." << endl;
    int t_dot = 0;
    int len = s.length();
    for (int i = 0; i < len; i ++) {
        if (s[i] == '.') {
            t_dot++;
        } else if (s[i] == ':') {
            break;
        }
    }
    if (t_dot == 3) {
        return 1; //符合规范
    } else {
        return 0; //不符合规范
    }
}

int checkZero(string s) { //该函数用于检查是否有前导零
//  cout << "checking zero..." << endl;
    string temp = "";
    int i = 0;
    int sum = 0;
    for (int j = 0; j < 5; j++) {
        while (s[i] != '.' && s[i] != ':') { //还原每一个字串
            temp += s[i];
            i++;
        }
//      cout << "num" << j + 1  << " : " << temp << endl;
        if (temp.length() == 1) { //长度等于一了判断前导零没有必要
            sum++; //符合规范,计数器加一
        } else {
            if (temp[0] == '0') { //有前导零
                continue; //不符合规范,直接返回
            } else {
                sum++; //符合规范,计数器加一
            }
        }
        temp = ""; //初始化temp
        i++;
    }
    if (sum == 5) {
        return 1; //计数器等于子串个数,说明符合规范
    } else {
        return 0; //不符合,返回
    }

}

int checkEdge(string s) { //该函数用于检测每个字串数字大小是否符合规范
//  cout << "checking edge..." << endl;
    string temp = "";
    int sum = 0;
    int i = 0;
    for (int j = 0; j < 4; j++) {
        while (s[i] != '.' && s[i] != ':') { //还原每一个字串
            temp += s[i];
            i++;
        }
//      cout << "num" << j + 1 << " : " << temp << endl;
        int temp_1 = stoi(temp); //转换为整型
        if (temp_1 >= 0 && temp_1 <= 255) {
            sum++; //符合规范,计数器加一
        }
        temp = "";
        i++;
    }
    while (s[i] != '.' && s[i] != ':') { //还原端口字串
        temp += s[i];
        i++;
    }
    int temp_1 = stoi(temp);
    if (temp_1 >= 0 && temp_1 <= 65535) {
        sum++;
    }
    if (sum == 5) {
        return 1;
    } else {
        return 0;
    }
}

int checkIp(string ip) { //就是个中控台
    if (!checkDot(ip)) {
        return 0; //直接返回不符合规范
    }
    if (!checkZero(ip)) {
        return 0;
    }
    if (!checkEdge(ip)) {
        return 0;
    }
    return 1; //都符合规范
}

int check_if_server_in(string ip) {
    for (auto i = server.begin(); i != server.end(); i++) {
        if (i->first == ip) {
            return 0; //已经存在
        }
    }
    return 1; //没有存在
}
string t1;
string t2;

int main() {
    freopen("P7911_2.in", "r", stdin);
//  freopen("P")
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> t1;
//      getchar();
        cin >> t2;
//      getchar();
        if (!checkIp(t2)) {
            cout << "ERR" << endl;
            continue;
        }
        if (t1 == "Client") {
            int num = 0;
            for (auto i = server.begin(); i != server.end(); i++) {
                if (i->first == t2) {
                    cout << server[t2] << endl;
                } else {
                    num++;
                }
            }
            if (num == server_num) {
                cout << "FAIL" << endl;
            }
        } else {
            if (!check_if_server_in(t2)) {
                cout << "FAIL" << endl;
                continue;
            }
            server[t2] = i;
            server_num ++;
            cout << "OK" << endl;
        }
    }
    fclose(stdin);
    return 0;
}
2022/10/28 14:48
加载中...