#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#define N 50
#define M 500
char *a[32]={"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","while","volatile"};
char *b[13]={"&&","||","++","--","+","-","*","/","=",">=","<=","==","!="};
char *c[10]={"[","]","{","}","(",")",",",";","'"};
char scmp(char *str)
{
int i;
for(i=0;i<32;i++)
{if(strcmp(str,a[i])==0)
return '1';
}
if(isdigit(str[0]))
return '3';
for(i=0;i<13;i++)
{if(strcmp(str,b[i])==0)
return '4';
}
for(i=0;i<10;i++)
{
if(strcmp(str,c[i])==0)
return '5';
}
if(!isalnum(str[0]))
return '5';
else return '2';
}
void main(void)
{
FILE *in,*out,*fp;
char ch,cha,strtoken[N],*word[M];
int i=0,j=0,k;
if((in=fopen("e:\\source.txt","r+"))==NULL)
{
printf("source.txt can't open or doesn't exist...\n");
getch();exit(0);
}
if((out=fopen("e:\\output.txt","w+"))==NULL)
{
printf("output file can not open...\n");
getch();exit(0);
}
while(!feof(in))
{
ch=fgetc(in);
if(isalpha(ch))
{
while(isalnum(ch)&&(i<N))
{
strtoken[i++]=ch;
ch=fgetc(in);
}
if(i>=N)
{printf("the string is too long...\n");
getch();exit(0);
}
strtoken[i]='\0';
word[j++]=(char *)malloc(sizeof(char)*(strlen(strtoken)+1));
strcpy(word[j-1],strtoken);
i=0;
fseek(in,-1L,1);
}
else if(isdigit(ch))
{
while(isdigit(ch)&&(i<N))
{
strtoken[i++]=ch;
ch=fgetc(in);
}
if(i>=N)
{
printf("the digit is too long...\n");
getch();exit(0);
}
strtoken[i]='\0';
word[j++]=(char *)malloc(sizeof(char)*(strlen(strtoken)+1));
strcpy(word[j-1],strtoken);
i=0;
fseek(in,-1L,1);
}
else if(!isalnum(ch))
{
if(ch!='\n'&&ch!=' ')
{
if(ch=='>'||ch=='<'||ch=='!')
{
if((cha=fgetc(in))=='=')
{
strtoken[i++]=ch;
strtoken[i++]=cha;strtoken[i]='\0';
word[j++]=(char *)malloc(sizeof(char)*3);
strcpy(word[j-1],strtoken);
i=0;
}
else
{
strtoken[i++]=ch;strtoken[i]='\0';
word[j++]=(char *)malloc(sizeof(char)*2);
strcpy(word[j-1],strtoken);
i=0;
fseek(in,-1L,1);
}
}
else if(ch=='+'||ch=='-'||ch=='&'||ch=='|'||ch=='=')
{
if((cha=fgetc(in))==ch)
{
strtoken[i++]=ch;strtoken[i++]=cha;strtoken[i]='\0';
word[j++]=(char *)malloc(sizeof(char)*3);
strcpy(word[j-1],strtoken);
i=0;
}
else
{
strtoken[i++]=ch;strtoken[i]='\0';
word[j++]=(char *)malloc(sizeof(char)*2);
strcpy(word[j-1],strtoken);
i=0;
fseek(in,-1L,1);
}
}
else
{
strtoken[i++]=ch;strtoken[i]='\0';
word[j++]=(char *)malloc(sizeof(char)*2);
strcpy(word[j-1],strtoken);
i=0;
}
}
}
}
for(i=0;i<j-1;i++)
{
puts(word[i]);
cha=scmp(word[i]);
fputc('(',out);
fputc(cha,out);
fputc(',',out);
fputs(word[i],out);
fputc(')',out);
fputc('\n',out);
}
getch();
return;
}