-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetfloat.c
More file actions
44 lines (38 loc) · 898 Bytes
/
Copy pathgetfloat.c
File metadata and controls
44 lines (38 loc) · 898 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <ctype.h>
#include <stdio.h>
#include <math.h>
#include "polish_calc/getch.c"
int getfloat(float *);
int main(){
float ans[100];
int i = 0;
while(getfloat(&ans[i++]) != EOF);
printf("%.8g", *ans);
}
int getfloat(float *pn){
int c, sign;
while (isspace(c = getch()));
if(!isdigit(c) && c != EOF && c != '+' && c != '-'){
ungetch(c);
return 0;
}
sign = (c == '-') ? -1 : 1;
if(c == '+' || c == '-')
c = getch();
for(*pn = 0; isdigit(c); c = getch())
*pn = 10 * *pn + (c - '0');
if(c == '.'){
c = getch();
for(int i = 1; isdigit(c); c = getch()){
float denom = 1;
for(int j = 0; j < i; j++)
denom *= 10;
*pn += (c - '0') / denom;
i++;
}
}
*pn *= sign;
if(c != EOF)
ungetch(c);
return c;
}