blob: a0e05593aaeaf6f31924b683d70b1308c11a146b (
plain) (
tree)
|
|
#include <stdio.h>
#include <ctype.h>
int getint(int *np);
char getch(void);
void ungetch(char input);
int
main(void)
{
int num;
getint(&num);
printf("%d\n", num);
return 0;
}
int
getint(int *np)
{
int input, sign;
/* skip space */
while (isspace(input = getch()))
;
if (!isdigit(input) && input != EOF && input != '+' && input != '-') {
ungetch(input);
return 0;
}
sign = (input == '-') ? -1 : 1;
if (input == '+' || input == '-')
while (isspace(input = getch()))
;
for (*np = 0; isdigit(input); input = getch())
*np = *np * 10 + input - '0';
*np *= sign;
if (input != EOF)
ungetch(input);
/* return last digit or EOF */
return input;
}
static char buff = -1;
char
getch(void)
{
char temp;
if (buff == -1)
return getchar();
temp = buff;
buff = -1;
return temp;
}
void
ungetch(char input)
{
if (buff == -1)
buff = input;
else
printf("Err: buffer is full\n");
}
|