谁能帮忙好好看看,帮忙改一下?谢谢啦!
#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;
int k=0;
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=stoll(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 pos){
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){
for(int i=1;i<=k;i++){
if(a[i].num==s){
return a[i].pos;
}
}
return -1;
}
int main(){
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);
if(p==-1){
cout<<"FAIL"<<endl;
}
else{
cout<<p<<endl;
}
}
}
return 0;
}