# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# include<math.h>
int main()
{
char *c;
float transform(char *p);
float t;
c=(char *)malloc(80*sizeof(char));
scanf("%s",c);
t=transform(c);
printf("The %s is equal to the %f!",c,t);
system("pause");
}
float transform(char *p)
{
float intransform(char *p);
float fractransform(char *p);
int i,mark,count;
float sum;
char *intpart,*fracpart;
intpart=(char *)malloc(20*sizeof(char)),fracpart=(char *)malloc(20*sizeof(char));
i=0,mark=0,count=0;
do
{
if(*(p+i)!='.'&&mark==0) *(intpart+i)=*(p+i),count++;
else if(*(p+i)=='.') mark=1,*(intpart+i)='\0';
else *(fracpart+i-strlen(intpart)-1)=*(p+i);
i++;
}while(*(p+i-1)!='\0');
if(mark==0) *(fracpart)='\0';
sum=intransform(intpart)+fractransform(fracpart);
return sum;
}
float intransform(char *p)
{
int lettertrans(char c);
float pow(float x,int m),sum;
int n,i;
n=strlen(p);
sum=0;
for(i=0;i<n;i++)
sum=sum+lettertrans(*(p+i))*pow(16,n-i-1);
return sum;
}
int lettertrans(char c)
{
int r;
if(c>='0'&&c<='9') r=c-48;
else if(c>='A'&&c<='F') r=10+c-65;
else r=-1;
return r;
}
float fractransform(char *p)
{
int lettertrans(char c);
float pow(float x,int m),sum;
int n,i;
n=strlen(p);
sum=0;
for(i=0;i<n;i++)
sum=sum+lettertrans(*(p+i))*pow(16,-i-1);
return sum;
}
字符串形式的十六进制转化为十进制数字!