-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a06cada
commit 1cfa954
Showing
18 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BINARY=getfloat | ||
|
||
LFLAGS=-lm | ||
|
||
include ../../Makefile.exerc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 10 222 |
4 changes: 4 additions & 0 deletions
4
chapter_5.pointers_arrays/5_02.getfloat/tests/0_pos_ints.tout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
1 change: 1 addition & 0 deletions
1
chapter_5.pointers_arrays/5_02.getfloat/tests/1_pos_neg_err.tin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-1024 -0 +256 ++1000 |
4 changes: 4 additions & 0 deletions
4
chapter_5.pointers_arrays/5_02.getfloat/tests/1_pos_neg_err.tout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
chapter_5.pointers_arrays/5_02.getfloat/tests/2_pos_neg_whitespace.tin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-100 -2 | ||
+5 -3 0 | ||
|
||
|
||
123456 |
7 changes: 7 additions & 0 deletions
7
chapter_5.pointers_arrays/5_02.getfloat/tests/2_pos_neg_whitespace.tout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
1 change: 1 addition & 0 deletions
1
chapter_5.pointers_arrays/5_02.getfloat/tests/3_pos_neg_floats.tin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.1 10.55 222.098694 |
4 changes: 4 additions & 0 deletions
4
chapter_5.pointers_arrays/5_02.getfloat/tests/3_pos_neg_floats.tout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
1 change: 1 addition & 0 deletions
1
chapter_5.pointers_arrays/5_02.getfloat/tests/4_pos_neg_err_floats.tin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-.0501 +10. -123.33300 +.0 -. -- |
6 changes: 6 additions & 0 deletions
6
chapter_5.pointers_arrays/5_02.getfloat/tests/4_pos_neg_err_floats.tout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
chapter_5.pointers_arrays/5_02.getfloat/tests/5_pos_neg_floats_whitespace_err.tin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
0.4 -100.000999 | ||
|
||
-.01 | ||
+3333 | ||
|
||
-a. |
5 changes: 5 additions & 0 deletions
5
chapter_5.pointers_arrays/5_02.getfloat/tests/5_pos_neg_floats_whitespace_err.tout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |