临时保存,回头测试 //判断一个IP地址是否合法,耗时2h void main(void) { char s[16]; memset(s,'/0',16); int IP[5]={0,0,0,0}; int i=0,j=0,count=0; cout<<"Please input an IP address/n"; cin>>s; int StrLenth=strlen(s);//15 //cout<<StrLenth<<endl; if(StrLenth>15) {//长度最大为15 cout<<"Invalid address/n"; exit(0); } else { for(i=0;i<StrLenth;i++) { if (((s[i]>'0'&&s[i]<'9')||(s[i]=='.'))&&!strstr(s,".."))//都是数字或者点号,而且没有连点 { //转换为数值 if(s[i]=='.') { count++; j++; } else IP[j]=IP[j]*10+(s[i]-'0'); } else { cout<<"Invalid address/n"; exit(0); } } if (count>3) {//分段太多 cout<<"Invalid address/n"; exit(0); } //cout<<IP[0]<<'.'<<IP[1]<<'.'<<IP[2]<<'.'<<IP[3]<<endl; for (j=0;j<4;j++ ) {//判断取值范围 if(IP[j]>255) { cout<<"Invalid address/n"; exit(0); } } cout<<"Valid address/n"; } }