努力吧,
编写的程序,不能改变世界,却可以改变自己...
#include <stdio.h> int main() { double sum = 0.0; //数据的和 double tmp; //当前数据 double flag; //小数位标识 char *p, *str = "a5.7dfg2.3.4df"; //测试字符串 for (p = str; *p;) { while (*p && ('0' >= *p || '9' <= *p)) { p++; } tmp = 0.0, flag = 0.1; while (*p && '.' != *p && '0' <= *p && '9' >= *p) { tmp *= 10, tmp += *p - '0'; p++; } if (*p && '.' == *p) { p++; while (*p && '0' <= *p && '9' >= *p) { tmp += flag * (*p - '0'); flag /= 10, p++; } } sum += tmp; } printf("%lf", sum); return 0; }