山科 STUST OJ Problem B: 編寫函數:String to Double (II) (Append Code)

來源:互聯網
上載者:User

標籤: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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.