码迷,mamicode.com
首页 > 移动开发 > 详细

山科 STUST OJ Problem B: 编写函数:String to Double (II) (Append Code)

时间:2016-12-17 11:43:03      阅读:433      评论:0      收藏:0      [点我收藏+]

标签:str   end   std   null   存在   define   span   int   ++   

这道题没啥别的毛病,我的错误在于看不懂题。

另外还有一点是注意浮点数存在-0

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 #include <math.h>
 4 #define MAX_STR_LEN 10 + 5
 5 double strToDouble(char str[]){
 6     int state=0;
 7     double sum1 = 0.0;
 8     double sum2 = 0.0;
 9     int k=1;
10     int digited=0;
11     int fuhao=1;
12     if(!str[0])return 0;
13     for(int i=0;str[i];i++){
14         if(digited==0&&str[i]==-)
15         {
16             digited=1;
17             fuhao = -1;
18         }
19         if(digited==0&&str[i]==+){
20             digited=1;
21             fuhao = 1;      
22         }
23         if(isdigit(str[i])){
24             digited=1;
25             if(!state)sum1=10*sum1 +str[i]-0;
26             else sum2+=(pow(0.1,k++))*(str[i]-0);
27         }
28         else if(str[i]==.){
29             digited=1;
30             state=1;
31         }
32     }
33     return fuhao*(sum1 + sum2);
34 }
35  
36 int main()
37 {
38     char s[MAX_STR_LEN];
39     while(gets(s) != NULL)
40         printf("%lg\n", strToDouble(s));
41     return 0;
42 }

 

山科 STUST OJ Problem B: 编写函数:String to Double (II) (Append Code)

标签:str   end   std   null   存在   define   span   int   ++   

原文地址:http://www.cnblogs.com/Wade-/p/6189175.html

(0)
(2)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!