Finished exercise 5.2

This commit is contained in:
kdeng00
2021-02-27 23:49:24 -05:00
parent 6a955e3fa3
commit 49af8a805e
2 changed files with 106 additions and 15 deletions
+106
View File
@@ -0,0 +1,106 @@
/*
*
* Exercise 5-2. Write getfloat, the floating-point analog of getint. What type does getfloat
* return as its function value?
*
* Author: Kun Deng
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define BUFSIZE 100
#define SIZE 1024
int getch(void);
void ungetch(int);
int getfloat(float *ptr);
int main(void)
{
int n;
float array[SIZE];
for(n=0; n<SIZE && getfloat(&array[n]) != EOF; n++)
;
for(; n >= 0; n--)
{
printf("%f ",array[n]);
}
printf("\n");
return 0;
}
int getfloat(float *ptr)
{
int c,sign;
float power;
while(isspace(c = getch()))
;
if(!isdigit(c) && c != EOF && c != '+' && c != '-' && c != '.')
{
ungetch(c);
return 0;
}
sign = (c == '-') ? -1: 1;
if(c == '+' || c == '-')
{
c = getch();
}
for(*ptr = 0.0; isdigit(c); c = getch())
{
*ptr = 10.0 * (*ptr) + (c - '0');
}
if(c == '.')
{
c = getch();
}
for(power=1.0; isdigit(c); c = getch())
{
*ptr = 10.0 * (*ptr) + (c - '0'); /* fractional part */
power *= 10.0;
}
*ptr *= sign / power;
if(c != EOF)
{
ungetch(c);
}
return c;
}
char buf[BUFSIZE];
int bufp = 0;
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if(bufp >= BUFSIZE)
{
printf("ungetch: too many characters\n");
}
else
{
buf[bufp++] = c;
}
}