1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <stdio.h>
// Reads up to max doubles separated by commas from the given string.
int readDoubles(const char *s, double *points, int max) {
int size = 0;
double f;
int pos = 0;
if (max <= 0 || sscanf(s, "%lf%n", &f, &pos) != 1)
return 0;
points[size++] = f;
while (sscanf(s += pos, " ,%lf%n", &f, &pos) == 1) {
if (size >= max) break;
points[size++] = f;
}
return size;
}
int main() {
double points[8];
int size = readDoubles(" 1.2 , 3.4,5.6, 7.8 ", points,
sizeof points/sizeof *points);
for (int i = 0; i < size; i++)
printf("%f\n", points[i]);
return 0;
}
|