www.pudn.com > fengzzzz.rar > fengzzzz.c
#include "stdio.h"
#include "ctype.h"
#include "string.h"
#include "math.h"
#include "stdlib.h"
char keywords[32][12]={
{'a','u','t','o','\0'},
{'b','r','e','a','k','\0'},
{'c','a','s','e','\0'},
{'c','h','a','r','\0'},
{'c','o','n','s','t','\0'},
{'c','o','n','t','i','n','u','e','\0'},
{'d','e','f','a','u','l','t','\0'},
{'d','o','\0'},
{'d','o','u','b','l','e','\0'},
{'e','l','s','e','\0'},
{'e','n','u','m','\0'},
{'e','x','t','e','r','n','\0'},
{'f','l','o','a','t','\0'},
{'f','o','r','\0'},
{'g','o','t','o','\0'},
{'i','f','\0'},
{'i','n','t','\0'},
{'l','o','n','g','\0'},
{'r','e','g','i','s','t','e','r','\0'},
{'r','e','t','u','r','n','\0'},
{'s','h','o','r','t','\0'},
{'s','i','g','n','e','d','\0'},
{'s','m','i','z','e','o','f','\0'},
{'s','t','a','t','i','c','\0'},
{'s','t','r','u','c','t','\0'},
{'s','w','i','t','c','h','\0'},
{'t','y','p','e','d','e','f','\0'},
{'u','n','i','o','n','\0'},
{'u','n','s','i','g','n','e','d','\0'},
{'v','o','i','d','\0'},
{'v','o','l','a','t','i','l','e','\0'},
{'w','h','i','l','e','\0'}
};
void main()
{char filename[30];
char ch;
int ch1=1;
FILE * fp;
char str[12];
int length;
float number;
do{ch1=1;
printf("请输入你的文件全称名(包括路径):");
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
{printf("该文件不存在!\n");
ch1=0;}
}while(!ch1);//确定输入正确的文件名//
ch=fgetc(fp);
while(ch!=EOF)
{ch1=(int)ch;
if(isspace(ch1))
{ ch=fgetc(fp);
ch1=(int)ch;
continue;
} //空格,回车换行的判断//
if(isalpha(ch1))
{length=0;
while(isalpha(ch1)||isdigit(ch1)||(ch=='_'))
{str[length]=ch;
length++;
ch=fgetc(fp);
ch1=(int)ch;
}
str[length]='\0';
if(compare(str))
{ printf("%s\t关键字\n",str);
}
else
{
printf("%s\t标识符\n",str);
}
continue;
}
//标识符的判断//
if(isdigit(ch1))
{length=0;
while(isdigit(ch1)||(ch=='.'))
{str[length]=ch;
length++;
ch=fgetc(fp);
ch1=(int)ch;
}
str[length]='\0';
printf("%s\t数字常量\n",str);
continue;
}
//数字常量的判断//
if((ch=='*')||(ch=='/'))
{
printf("%c\t数字运算符\n",ch);
ch=fgetc(fp);
ch1=(int)ch;
continue;
}
//乘除的判断
if((ch=='+')||(ch=='-'))
{ printf("%c",ch);
ch=fgetc(fp);
ch1=(int)ch1;
if((ch=='+')||(ch=='='))
{ printf("%c\t自增运算符\n",ch);
ch=fgetc(fp);
ch1=(int)ch;
}
else{
if((ch=='-')||(ch=='='))
{ printf("%c\t自减运算符\n",ch);
ch=fgetc(fp);
ch1=(int)ch;
}
else
{printf("\t加减运算符\n",ch);}
}
}
//加减运算符的判断//
if(ch=='=')
{printf("%c",ch);
ch=fgetc(fp);
ch1=(int)ch;
if(ch=='=')
{printf("%c\t逻辑运算符\n",ch);
ch=fgetc(fp);
ch1=(int)ch;
}
else
{
printf("\t数字运算符\n");
}
continue;
}/* "=="的判断 */
if((ch=='>')||(ch=='<'))
{ printf("%c",ch);
ch=fgetc(fp);
ch1=(int)ch;
if((ch=='='))
{ printf("%c\t逻辑运算符\n",ch);
ch=fgetc(fp);
ch1=(int)ch;
}
else
{
printf("\t逻辑运算符\n");
}
continue;
}
/* "<,>,<=,>="的判断 */
else
{ printf("%c\t界符\n",ch);
ch=fgetc(fp);
ch1=(int)ch;
}
}
//文件的执行
fclose(fp);
getch();
}
int compare( char str[10])//关键字的判断,是则返回1//
{int i;
int flag=0;
for(i=0;i<32;i++)
{if(strcmp(str,keywords[i])==0)
{flag=1;
break;}
}
return flag;
}