00000000000000000000000000000000 255.255.255.255
0.0.0.0 11111111111111111111111111111111
#include<cstdio> #include<cstdlib> #include<cstring> using namespace std; int ar[10]; void count(int n){ int k=0,e=1; for(int j=n-1;j>=0;--j){ k+=ar[j]*e;e=e*10; } int ip[10],i=0; memset(ip,0,sizeof(ip)); while(k){ ip[i++]=k%2; k=k/2; } for(i=7;i>=0;--i) printf("%d",ip[i]); } int main() { int d,i,j,n,l,k; char s[35]; while(scanf("%s",s)!=EOF){ l=strlen(s);n=0; if(strchr(s,'.')){ for(i=0;i<l;++i){ if(s[i]=='.'){ count(n); memset(ar,0,sizeof(ar)); n=0;continue; } ar[n++]=s[i]-'0'; } count(n); printf("\n"); } else { d=0;k=128; for(i=0;i<l;++i){ n+=(s[i]-'0')*k;k/=2;d++; if(d==8){ if(i==l-1){ printf("%d",n);continue; } printf("%d.",n); n=0;k=128;d=0; } } printf("\n"); } } return 0; }
原文地址:http://blog.csdn.net/r1986799047/article/details/43486385