zoukankan      html  css  js  c++  java
  • 山科 STUST OJ Problem B: 编写函数:String to Double (II) (Append Code)

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

    另外还有一点是注意浮点数存在-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
    ", strToDouble(s));
    41     return 0;
    42 }
  • 相关阅读:
    C# 异常处理
    UIElement 的DesiredSize 和 RenderSize
    删除集合中满足条件的元素
    C# 中的Property
    C# readonly 与 const
    C# 实例化类的执行顺序
    C# volatile 与 lock
    双重检查加锁机制
    定位问题
    EBS部分表整理
  • 原文地址:https://www.cnblogs.com/Wade-/p/6189175.html
Copyright © 2011-2022 走看看