home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
CataniaB
/
teach-act
/
esempi
/
Comp-Sep
/
getsym.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-05-13
|
1KB
|
48 lines
#include <stdio.h>
#include <stdlib.h>
#include "getsym.h"
const char tab[NOFSYM] = {'+','-','*','/','%','=','(',')'};
symbol getsym()
{
symbol s;
static char c = ' ';
s.kind = NOTREC; /* pessimistic view! */
while (c != EOF && (c == ' ' || c == '\n' || c == '\t') )
c = getchar(); /* skip blank, new line and tabbing */
if (c == EOF) /* unexpected end of input */
{
printf("\n Error: unexpected end of file \n");
exit(EXIT_FAILURE);
}
if (c >= '0' && c <= '9') /* number */
{
s.kind = NUMBER;
s.value = 0;
do
s.value = s.value * 10 + c - '0';
while ((c = getchar()) != EOF && c >= '0' && c <= '9');
}
else /* operation or unrecognized symbol */
{
symkind i;
for (i = PLUS; i <= RPAR; i++)
if (tab[i] == c)
s.kind = i;
if (s.kind == NOTREC)
{
printf("\n Error: unrecognized character: %c \n",c);
exit(EXIT_FAILURE);
}
c = getchar(); /* input synchronization */
}
return s;
}