1 #include <stdio.h> 2 #include <ctype.h> 3 #define BUG printf("here!bug! "); 4 5 int getfloat(float *pn){ 6 int c; 7 int t; 8 int intbeforepoint=0; 9 int intafterpoint=0; 10 int intafterpoint_lenth=0; 11 int inte; 12 int sign,esign; 13 int ppoint; // 1 means there have point ,otherwise no 14 float res=0; 15 16 while( isspace(c=getch()) ) 17 ; 18 if(!isdigit(c)&&c!=EOF&&c!='+'&&c!='-'){ 19 ungetch(); 20 return 0; 21 } 22 sign=(c=='-')?-1:1; 23 if(c=='+'||c=='-') 24 c=getch(); 25 for(intbeforepoint=0;isdigit(c);c=getch()) 26 intbeforepoint=10*intbeforepoint+(c-'0'); 27 ppoint=0; 28 if(c=='.'){ 29 ppoint=1; 30 c=getch(); 31 for(intafterpoint=0;isdigit(c);c=getch()){ 32 intafterpoint=10*intafterpoint+(c-'0'); 33 intafterpoint_lenth++; 34 } 35 t=intafterpoint; 36 while(t%10==0){ 37 intafterpoint_lenth--; 38 t=t/10; 39 } 40 } 41 if(c=='e'||c=='E'){ 42 c=getch(); 43 esign=1; 44 if(c=='+'||c=='-'){ 45 esign= (c=='+')?1:-1; 46 c=getch(); 47 } 48 while( c=='0'){ 49 c=getch(); 50 } 51 for(inte=0;isdigit(c);c=getch()) 52 inte=10*inte+(c-'0'); 53 } 54 BUG; 55 if(c!=EOF) 56 ungetch(c); 57 res=1.0*intbeforepoint; 58 t=intafterpoint_lenth; 59 while(t--) 60 res*=10; 61 res+=intafterpoint; 62 t=intafterpoint_lenth; 63 while(t--) 64 res/=10; 65 if(esign==-1){ 66 while(inte--) 67 res/=10; 68 }else if(esign==1){ 69 while(inte--) 70 res*=10; 71 } 72 res*=sign; 73 *pn=res; 74 printf("intbeforepoint=%d ",intbeforepoint); 75 printf("intafterpoint=%d ",intafterpoint); 76 printf("inte=%d ",inte); 77 printf("length=%d ",intafterpoint_lenth); 78 return c; 79 } 80 int main(){ 81 float num[10]; 82 int i; 83 for(i=0;i<10;++i){ 84 getfloat(&num[i]); 85 printf("%lf ",num[i]); 86 } 87 return 0; 88 }