CodePudding user response:
Untreated abnormal input, only supplies the reference:# include & lt; Stdio. H>
# include & lt; String. H>
# include & lt; Stdlib. H>
/* handle IP string */
Unsigned int handler (char * STR)
{
Unsigned int the rs=0;
Char * p;
P=strtok (STR, ". ");
While (p!=NULL)
{
Rs=(rs & lt; <8) + atoi (p);
P=strtok (NULL, "");
}
Return the rs;
}
/* print binary */
Void print (unsigned int rs)
{
If (rs & gt; 0)
{
Print (rs/2);
Printf (" % d ", the rs % 2);
}
}
Int main (void)
{
Unsigned int the rs=0;
Char IP [16]={0};
The scanf (" % s ", IP);
/* handle IP string */
Rs=handler (IP);
/* print decimal */
Printf (" % u \ n ", rs);
/* print binary */
Print (rs);
return 0;
}
CodePudding user response:
With array, fix, conform to the requirements,# include & lt; Stdio. H>
# include & lt; String. H>
# include & lt; Stdlib. H>
/* handle IP string */
Unsigned int aton (const char STR [])
{
Unsigned int the rs=0;
Char * p=STR (char *);
P=strtok (p, "");
While (p!=NULL)
{
Rs=(rs & lt; <8) + atoi (p);
P=strtok (NULL, "");
}
Return the rs;
}
/* print binary */
Void print (unsigned int rs)
{
If (rs & gt; 0)
{
Print (rs/2);
Printf (" % d ", the rs % 2);
}
}
Int main (void)
{
Unsigned int the rs=0;
Char IP [16]={0};
The scanf (" % s ", IP);
/* handle IP string */
Rs=aton (IP);
/* print decimal */
Printf (" % u \ n ", rs);
/* print binary */
Print (rs);
return 0;
}
CodePudding user response: