报stoi的错?这不是<string>类型的函数吗?
查看原帖
报stoi的错?这不是<string>类型的函数吗?
541224
FQY_0307楼主2024/8/9 18:46

在线等调,谢谢!!!

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int N=1005;
struct node{
    string num;    //成功建立连接的服务机地址串
    int pos;       //服务机的编号
}a[N];
int n;
string op,ad;
bool check(string s){    //是否合法?
    if(s.size()>21){
        return 0;
    }
    //合法格式为  a.b.c.d:e
    string r="";
    string w="";
    int c=0;   //统计是第几个数字
    for(int i=0;i<s.size();i++){
        if(isdigit(s[i])){
            w+=s[i];
            if(i==s.size()-1||!isdigit(s[i++])){
                c++;
                if(c>5||w.size()>5){
                    return 0;
                }
                if(w!="0" && w[0]=='0'){
                    return 0;
                }
                int x=stoi(w);
                if(c<=4&&x>255 || c==5&&x>65535){
                    return 0;
                }
                r+='0';
                w="";
            }
        }
        else{
            r+=s[i];
        }
    }
    if(r!="0.0.0.0:0"){
        return 0;
    }
    return 1;
}
//检验服务器能否成建立连接
string server(string s,int k){
    for(int i=1;i<=k;i++){
        if(a[i].num==s){
            return "FAIL";
        }
    }
    a[++k].num=s;
    a[k].pos=pos;
    return "OK";
}
//看客户机能否找到已经建立联系的服务机
int client(string s,int k){
    for(int i=1;i<=k;i++){
        if(a[i].num==s){
            return a[i].pos;
        }
    }
    return -1;
}
int main(){
    string tt;
    while(cin>>tt){
        cout<<check(tt)<<endl;
    }
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>op>>ad;
        if(!check(ad)){    //检查合法性
            cout<<"ERR"<<endl;
            continue;
        }
        if(op=="Server"){
            cout<<server(ad,i)<<endl;
        }
        else{
            int p=client(ad,i);
            if(p==-1){
                cout<<"FAIL"<<endl;
            }
            else{
                cout<<p<<endl;
            }
        }
    }
    return 0;
}
2024/8/9 18:46
加载中...