c programming language ___ 5_2.c

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 #define  BUG printf("here!bug!\n");
 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\n",intbeforepoint);
75     printf("intafterpoint=%d\n",intafterpoint);
76     printf("inte=%d\n",inte);
77     printf("length=%d\n",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\n",num[i]);
86     }
87     return 0;
88 }
5_2.c

 

posted on 2014-05-16 15:22 symons 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/symons1992/p/3732271.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值