float expdcm(int y) {
int i;float a;
a=1.0;if (y > 0) for (i=0;i < y;i++) a*=10.0;
if (y < 0) for (i=0;i > y;i--) a/=10.0;
return a;
}
int cvalue(char *str,float *a,int *intflg) {
int c,ist,err; int ix,ie; int ictr1,ictr2; float dx;
- a=0.0;*intflg=ON;
ie=0;ist=0;err=0;
ictr1=0;ictr2=0;
while ((c=(*str++)) != '\0') {
if (c >= '0' && c <= '9') { //only 0-9 and "."
ix=c-'0';dx=ix; //dx = the int.
if (ist >= 0 && ist <= 2) {
*a=*a*10.0+dx; //a is added by the current int to be equivalent value.
if (ictr1 != 0 || c != '0') ictr1++; //0 comes at the beginin' --> ictr1++.
ist=2; //ist = 2
}
else if (ist >= 3 && ist <= 4) { //After the DOT.
ictr2++;
*a=*a+dx*expdcm(-ictr2);
ist=4;
intflg=OFF;
}
else { //exception
err=3;*a=0.0;
return err;
}
}
else if (c == '.') {
if (ist >= 0 && ist <= 2) {
ist=3;
*intflg=OFF;
}
else { //if the DOT appear twice.
err=7;*a=0.0;
return err;
}
}
else { //exception.
err=1;*a=0.0;
return err;
}
}
if ((ist >= 2 && ist <= 4)) {
- a=*a*expdcm(ie);
err=0;
if ((*a-(int)(*a)) != 0.0) *intflg=OFF;
return err;
}
else {
err=2;*a=0.0;
return err;
}
} =============================================================================== Purpose: Change a string to a float number ===============================================================================// int value(char *str,float *a) {
int intflg;
return cvalue(str,a,&intflg);
}