文章
2
粉丝
489
获赞
4
访问
23.9k
 
#include<stdio.h>
void main()
{char s[100];
int i=0,t=0,c=0,x[10],b=0;
gets(s);
while(s[i]!='\0')
    {if(s[i]!='.'&&s[i]<'0'||s[i]>'9') break;
     else if(s[i]=='.') c++;
    i++;}
  if(s[i]=='\0'&&c==3&&s[i-1]!='.'&&s[0]!='.')
    {i=0;
   while(s[i]!='\0')
    {if(s[i]=='.') if(t>255) {printf("Error");break;}
     else {i++;t=0;}
   t=t*10+s[i]-'0';
    i++;}
if(s[i]=='\0') if(t>255) printf("Error");
else {i=0;t=0;
      while(s[i]!='\0')
     {if(s[i]=='.') {x[b]=t;b++;i++;t=0;}
      t=t*10+s[i]-'0';
      i++;}
     printf("0x%02X%02X%02X%02X",x[0],x[1],x[2],t);
    &nbs...
登录后发布评论
#include<stdio.h>
void main()
{char s[15];
int i=0,t=0,b=0,x[3];
gets(s);
while(s[i]!='\0')
{if(s[i]!='.'&&s[i]<'0'||s[i]>'9') break;
if(s[i]=='.'&&s[i-1]!='.'&&s[i+1]!='.') b++;//‘.’的位置确定为固定的排法
i++;}
if(s[i]=='\0'&&b==3&&s[i-1]!='.'&&s[0]!='.')//排除数字和‘.’之外的字符>以及‘.’的位置。
{i=0;
while(s[i]!='\0')
{if(s[i]=='.') {if(t>255) {printf("Error");break;}//将字符串里的四组数字提出与255比较;
else {i++;t=0;}}
t=t*10+s[i]-'0';
i++;}
if(t>255) printf("Error");
else {i=t=b=0;
while(s[i]!='\0')
{if(s[i]=='.') {x[b]=t;b++;i++;t=0;}
t=t*10+s[i]-'0';
i++;}
printf("0x%02X%02X%02X%02X",x[0],x[1],x[2],t);
}
}
else printf("Error");
}
数据不一定是纯数字