#include <stdio.h>
#include <string.h>
int recognizechar(char c) //识别字符种类
{
if(c>='0'&&c<='9')
return 0;
else if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))
return 1;
else if((c=='+')||(c=='-')||(c=='*')||(c=='/')||(c=='!')||(c=='=')||(c=='>')||(c=='<'))
return 4;
else if(c==' ')
return 3;
else if(c==','||c==';'||c=='{'||c=='}'||c=='('||c==')')
return 5;
else return 10;
}
void judgestring(char *str) //? 识别保留字:if、int、for、while、do、return、break、continue;单词种别码为1。
{
char strInt[]={'i','n','t','/0','/0','/0','/0','/0','/0','/0'};
char strIf[]={'i','f','/0','/0','/0','/0','/0','/0','/0','/0'};
char strFor[]={'f','o','/r','/0','/0','/0','/0','/0','/0','/0'};
char strWhile[]={'w','h','i','l','e','/0','/0','/0','/0','/0'};
char strDo[]={'d','o','/0','/0','/0','/0','/0','/0','/0','/0'};
char strReturn[]={'i','f','/0','/0','/0','/0','/0','/0','/0','/0'};
char strBreak[]={'b','r','e','a','k','/0','/0','/0','/0','/0'};
char strElse[]={'e','l','s','e','/0','/0','/0','/0','/0','/0'};
if(!(strcmp(str,strInt)&&strcmp(str,strIf)&&strcmp(str,strFor)&&
strcmp(str,strWhile)&&strcmp(str,strDo)&&strcmp(str,strReturn)&&
strcmp(str,strBreak)&&strcmp(str,strElse)))
{
printf("(1, %s )",str);
for(int i=0;i<10;i++)
str[i]='/0';
printf("/n");
}else{
printf("(2, %s )",str);
for(int i=0;i<10;i++)
str[i]='/0';
printf("/n");
}
}
void stringclean(char *str) //将字符串清空
{
for(int i=0;i<10;i++)
str[i]='/0';
}
int main()
{
FILE *fp;
fp=fopen("C://c.txt","a+");//文件地址在C盘
int formword=0;
int stringword=0;
int spaceword=0;
int numberword=0;
char nextline='/n';
char caculate[10]={'/0'};
char str[10]={'/0'};
while(!feof(fp))
{
//putchar(ch);
char ch=fgetc(fp);
char str1[]={ch,'/0'};
char temp[]={'/0','/0','/0','/0','/0','/0','/0','/0','/0','/0'};
int i=recognizechar(ch);
if((i!=4)&&strcmp(caculate,temp))
{
printf("(4, %s )",caculate);
stringclean(caculate);
numberword=0;
spaceword=0;
printf("%c",nextline);
}
if(i==0)
{
if(!strcmp(str,temp))
{
strcat(str,str1);
numberword++;
}
else strcat(str,str1);
spaceword=0;
}
else if(i==1)
{
numberword=0;
strcat(str,str1);
spaceword=0;
}
else if(i==4)
{
if(strcmp(str,temp))
{ if(numberword)
{
printf("(3, %s )",str);
stringclean(str);
numberword=0;
printf("%c",nextline);
}
else
judgestring(str);
}
numberword=0;
spaceword=0;
strcat(caculate,str1);
}
else if(i==3)
{
if(strcmp(str,temp))
{
if(numberword)
{
printf("(3, %s )",str);
stringclean(str);
printf("%c",nextline);
}else
{
judgestring(str);
}
}
}
else if(i==5)
{
if(strcmp(str,temp))
{
if(numberword)
{
printf("(3, %s )",str);
stringclean(str);
numberword=0;
printf("%c",nextline);
}
else
judgestring(str);
}
printf("(5, %c )",ch);
printf("%c",nextline);
}
}
return 0;
}