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