编写函数atof(),能把输入的字符串转换成浮点数。要求能处理科学计算表达式,如:

来源:百度知道 编辑:UC知道 时间:2024/06/23 14:52:46
编写函数atof(),能把输入的字符串转换成浮点数。要求能处理科学计算表达式,如:
“123.45e-5”
详细一点,最好经过TC软件验证过的,就是TC的,不是VC什么的,要验证正确的!!!而且123.45e-5不过是一个例子,没有必要专门写出,最好有一个函数输入这一类的数~谢啦

#include <stdio.h>
float atof(char *c)
{
float f;
sscanf(c,"%f",&f);
return f;
}

int main(void)
{
char a[]="123.45e-5";
float b=atof(a);
printf("%f",b);
}

******************************************

//---------------------------------------------------------------------------

#include <stdio.h>
float atof(char *c)
{
float f=0,t=0,te=1;
int i,op=1,eop=0,ep=0,cf=0;;
for (i = 0; c[i]; i++) {
if (c[i]=='.') {
op=0;
}
if (c[i]=='e'||c[i]=='E') {
eop=1;
op=2;
}
if (eop&&c[i]=='-') {
cf=1;
}
else if (eop&&c[i]=='+') {
cf=0;
}
if (op==1&&c[i]>='0'&&c[i]<='9') {
f=f*10+(c[i]-'0');
}
else if(!op&&c[i]>