#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
main()
{
char buff[30],c,ch;
char key[5][10]={"int\0","char\0","float\0","main\0"};
FILE *fp;
int i,j,k,l,m;
clrscr();
fp=fopen("add.c","r");
while(1)
{
ch=fgetc(fp);
c=ch;
if(c==EOF)
break;
if(c=='+'||c=='-'||c=='*'||c=='/')
{
buff[0]=c;
c=fgetc(fp);
if(c=='+'||c=='-'||c=='=')
printf ("%c%c\t is operator\n",buff[0],c);
else
{
printf("%c is operator\n",buff[0]);
fseek(fp,0,-1);
}
}
else if(c=='=')
{
buff[0]=c;
c=fgetc(fp);
if(c=='=')
printf("%c%c\t is comparision\n",buff[0],c);
else
{
printf("%c is assignment\n",buff[0]);
fseek(fp,0,-1);
}
}
else if(isdigit(c))
{
i=0;
buff[i]=c;
c=fgetc(fp);
while (isdigit(c))
{
i++;
buff[i]=c;
c=fgetc(fp);
}
for(k=0;k<=i;k++)
{
printf("%c",buff[k]);
}
printf("\t is number\n");
fseek(fp,0,-1);
}
else if(isalnum(c))
{
m=0;
i=0;
do
{
buff[i]=c;
c=fgetc(fp);
i++;
}while(isalnum(c));
buff[i]='\0';
for(j=0;j<4;j++)
{
if(strcmp(buff,key[j])==0)
{
m=1;
printf(" %s is key word\n",buff);
break;
}
}
if(strcmp(buff,key[j])!=0)
{
printf(" %s is a identifier\n",buff);
}
fseek(fp,0,-1);
}
}
fclose(fp);
getch();
}
No comments:
Post a Comment