feat: solve 5-2

This commit is contained in:
Flinner Yuu 2022-07-18 07:48:47 +03:00
parent 428a0b4287
commit 2d127ac61a
Signed by: flinner
GPG Key ID: 95CE0DA7F0E58CA6
1 changed files with 45 additions and 0 deletions

45
chapter-5/5-2.c Normal file
View File

@ -0,0 +1,45 @@
#include <ctype.h>
#include <stdio.h>
#define SIZE 100
int getfloat(double *pf) {
int c, sign, dec;
while (isspace(c = getchar())) /* ignore whitespace */
;
if (c != EOF && c != '.' && c != '+' && c != '-' && !isdigit(c)) {
ungetc(c, stdin); // not a number
return c;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-')
c = getchar();
for (*pf = 0; isdigit(c); c = getchar()) {
*pf = 10 * *pf + (c - '0');
}
if (c != '.' && c != EOF) {
ungetc(c, stdin); // not a number after decimal
return c;
}
if (c == '.') {
c = getchar();
for (dec = 1; isdigit(c); c = getchar(), dec++)
*pf = *pf + (double)(c - '0') / (10 * dec);
}
*pf *= sign;
if (c != EOF)
ungetc(c, stdin);
return c;
}
int main() {
int n;
double array[SIZE];
for (n = 0; n < SIZE && getfloat(&array[n]) != EOF; n++)
;
printf("%lf", array[0]);
}