Skip to content

Commit

Permalink
Exerc 5_02.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsm-lisper committed May 27, 2024
1 parent a06cada commit 1cfa954
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 0 deletions.
5 changes: 5 additions & 0 deletions chapter_5.pointers_arrays/5_02.getfloat/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BINARY=getfloat

LFLAGS=-lm

include ../../Makefile.exerc
25 changes: 25 additions & 0 deletions chapter_5.pointers_arrays/5_02.getfloat/getch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# include <stdio.h>

# define BUFSIZE 128

int buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */


/* getch: get a (possibly pushed-back) character */
int getch (void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}


/* ungetch: push character back on input */
void ungetch (int c)
{
if (bufp >= BUFSIZE) {
printf("ungetch: too many characters\n");
} else {
buf[bufp++] = c;
}
}

9 changes: 9 additions & 0 deletions chapter_5.pointers_arrays/5_02.getfloat/getch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ifndef GETCH_H
# define GETCH_H

/* getch: get a (possibly pushed-back) character */
int getch (void);
/* ungetch: push character back on input */
void ungetch (int c);

# endif
50 changes: 50 additions & 0 deletions chapter_5.pointers_arrays/5_02.getfloat/getfloat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# include <stdio.h>
# include <ctype.h>
# include <math.h>
# include "getch.h"

/*
getfloat: get next floating-point number from input into *pn
return:
- 0 when it's not a number
- EOF when read ok and EOF reached
- positive integer when read ok
*/
int getfloat (float *pn)
{
int c, sign;
unsigned int d;

while (isspace(c = getch())) /* skip white space */
;
if (! isdigit(c) && c != EOF && c != '+' && c != '-') {
ungetch(c); /* it is not a number */
return 0;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-') {
if (! isdigit(c = getch()) && c != '.') {
ungetch(c);
ungetch(sign ? '+' : '-');
return 0;
}
}
*pn = 0;
while (isdigit(c)) {
*pn = 10 * *pn + (c - '0');
c = getch();
}
if (c == '.') {
d = 0;
while (isdigit(c = getch())) {
*pn = 10 * *pn + (c - '0');
d++;
}
*pn /= pow(10.0, d);
}
if (*pn != 0)
*pn *= sign;
if (c != EOF)
ungetch(c);
return c;
}
7 changes: 7 additions & 0 deletions chapter_5.pointers_arrays/5_02.getfloat/getfloat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ifndef GETFLOAT_H
# define GETFLOAT_H

/* getfloat: get next floating-point number from input into *pn */
int getfloat (float *pn);

# endif
19 changes: 19 additions & 0 deletions chapter_5.pointers_arrays/5_02.getfloat/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# include <stdio.h>
# include "getfloat.h"

int main ()
{
int r;
float n;

for (r = getfloat(&n); r != EOF && r != 0; r = getfloat(&n)) {
printf("float read: %f\n", n);
}
if (r == EOF) {
printf("(EOF)\n");
} else {
printf("not a number\n");
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 10 222
4 changes: 4 additions & 0 deletions chapter_5.pointers_arrays/5_02.getfloat/tests/0_pos_ints.tout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
float read: 1.000000
float read: 10.000000
float read: 222.000000
(EOF)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-1024 -0 +256 ++1000
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
float read: -1024.000000
float read: 0.000000
float read: 256.000000
not a number
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-100 -2
+5 -3 0


123456
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
float read: -100.000000
float read: -2.000000
float read: 5.000000
float read: -3.000000
float read: 0.000000
float read: 123456.000000
(EOF)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1 10.55 222.098694
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
float read: 0.100000
float read: 10.550000
float read: 222.098694
(EOF)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-.0501 +10. -123.33300 +.0 -. --
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
float read: -0.050100
float read: 10.000000
float read: -123.333000
float read: 0.000000
float read: 0.000000
not a number
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0.4 -100.000999

-.01
+3333

-a.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
float read: 0.400000
float read: -100.000999
float read: -0.010000
float read: 3333.000000
not a number

0 comments on commit 1cfa954

Please sign in to comment.