将字符转换为数值(有指数)我的程序错在哪里??
#include<stdio.h>#include<stdlib.h>
double main(void)
{
int i,j=0;
double t;
char a[20];
double Atof(char *s);
printf("输入一串字符");
scanf("%s",a);
for(i=0;i<100;i++)
{ while(a[i]!='\0')
{
if(a[i]>='0'&&a[i]<='9'||a[i]=='.'||a[i]=='e'||a[i]=='E'||a[i]=='-'||a[i]=='+')
{
a[j]=a[i];
j++;
}
}
a[j]='\0';
break;
}
t=Atof(a);
printf("%lf",t);
system("pause");
return 0;
}
double Atof(char *s)
{
int p=0,f=0,a=0,w=1,e=0,i;
double b=0,sum,c=0.0;
while(*s!='\0'&&*s!='e'&&*s!='E')
{
if(*s=='-')
{
p=1;
}
else if(*s=='+')
;
else if(*s>='0'&&*s<='9')
{
a*=10;
a=a+(*s-'0');
}
else
;
s++;
}
while(*s!='\0'&&*s!='e'&&*s!='E')
{
if(*s>='0'&&*s<='9')
{
c*=0.1;
b+=(*s-'0')*c;
}
else
;
s++;
}
while(*s!='\0'&&*s!='.')
{
if(*s>='0'&&*s<='9')
{
e*=10;
e=e+(*s-'0');
}
}
for(i=0;i<e;i++)
w=w*10;
sum=p*(a+b)*w;
return sum;
}