📄 good.c
字号:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
/*FILE *output;*/
struct keyword
{
char str[10];
int type;
int value;
}kword[32]={
{"outo",0,1},{"break",0,2},{"case",0,3},{"char",0,4},
{"const",0,5},{"continue",0,6},{"default",0,7},{"do",0,8},
{"double",0,9},{"else",0,10}, {"enum",0,11},{"extern",0,12},
{"float",0,13},{"for",0,14},{"goto",0,15},{"if",0,16},
{"int",0,17},{"long",0,18},{"register",0,19},{"return",0,20},
{"short",0,21},{"signed",0,22},{"sizeof",0,23},{"static",0,24},
{"struct",0,25},{"switch",0,26},{"typedef",0,7},{"union",0,28},
{"unsigned",0,29},{"void",0,30},{"volatile",0,31},{"while",0,32}
};
struct seperate
{
char ch;
int type;
int value;
}sep[13]={
{'#',1,1},{'(',1,2},{')',1,3},{'[',1,4},
{']',1,5},{'\'',1,6},{';',1,7},{':',1,8},
{'{',1,9},{'}',1,10},{',',1,11},{'\\',1,12},
{'\"',1,13}
};
struct calculate
{
char str[4];
int type;
int value;
}cal[36]={
{"!",2,1},{"%",2,2},{"^",2,3},{"&",2,4},
{"*",2,5},{"-",2,6},{"+",2,7},{"=",2,8},
{"~",2,9},{"|",2,10},{"<",2,11},{">",2,12},
{"||",2,13},{"/",2,14},{"?",2,15},{"+=",2,16},
{"-=",2,17},{"*=",2,18},{"/=",2,19},{"%=",2,20},
{"<<=",2,21},{">>=",2,22},{"&=",2,23},{"^=",2,24},
{"|=",2,25},{"->",2,26},{"++",2,27},{"--",2,28},
{">>",2,29},{"<<",2,30},{"<=",2,31},{">=",2,32},
{"!=",2,33},{"==",2,34},{"&&",2,35},{".",2,36}
};
/* struct id
{ 标识符
char str[10];
int type;
int value;*/
/*==========================主函数区=========================*/
main()
{
FILE *input,*output;
int i;
char ch,pre,tem, token[256]="";
if ((input=fopen("c:\\intext.txt","r"))==NULL)
return 0;
if ((output=fopen("c:\\outtext.txt","w"))==NULL)
return 0;
while(!feof(input))
{
ch=fgetc(input);
while((ch==' '||ch=='\r'||ch=='\n')&&!feof(input))/*过滤空格*/
ch=fgetc(input);
if(isalpha(ch)) /*字母的情况 */
{
while((isalnum(ch)||ch=='_')&&!feof(input))
{
token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
ch=fgetc(input);
}
if(!feof(input))
fseek(input,-1,1);
for(i=0;i<32;i++)
if(strcmp(token,kword[i].str)==0)
{
printf(" <%s,%d>",kword[i].str,kword[i].type);
fprintf(output," <%s,%d>\n",kword[i].str,kword[i].type);
break;
}
if(i==32)
printf(" <%s,3>",token);
fprintf(output," <%s,3>\n",token);
token[0]='\0';
}
else if(isdigit(ch)) /*数字的情况*/
{
while(isdigit(ch)&&!feof(input))
{
token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
ch=fgetc(input);
}
if(ch!='.')
{
if(ch=='e'||ch=='E')
{
/* */token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
ch=fgetc(input);
while(isdigit(ch)&&!feof(input))
{
token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
ch=fgetc(input);
}
}
fseek(input,-1,1);
printf( "<%s,40>",token);
fprintf(output,"<%s,40>\n",token);
token[0]='\0';
}
else
{
/* */token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
ch=fgetc(input);
while(isdigit(ch)&&!feof(input))
{
token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
ch=fgetc(input);
}
if(ch=='e'||ch=='E')
{
/* */token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
ch=fgetc(input);
while(isdigit(ch)&&!feof(input))
{
token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
ch=fgetc(input);
}
}
fseek(input,-1,1);
printf(" <%s,41>",token);
fprintf(output," <%s,41>\n",token);
token[0]='\0';
}
}
else if(isseperate(ch)) /* 分隔符的情况*/
{
if(ch=='\'')
{
ch=fgetc(input);
token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
ch=fgetc(input);
token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
ch=fgetc(input);
if(ch!='\'')
{
token[strlen(token)-1]='\0';
fseek(input,-1,1);
}
printf(" <%s,42>",token);
fprintf(output," <%s,42>\n",token);
token[0]='\0';
}
else if(ch=='\"')
{
ch=fgetc(input);
while(ch!='\"'&&!feof(input))
{
/**/ token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
ch=fgetc(input);
}
fseek(input,-1,1);
printf(" <%s,43>",token);
fprintf(output," <%s,43>\n",token);
ch=fgetc(input);
token[0]='\0';
}
else
{
for(i=0;i<13;i++)
if(ch==sep[i].ch)
printf(" <%c,%d>", sep[i].ch,sep[i].type);
fprintf(output," <%c,%d>\n", sep[i].ch,sep[i].type);
}
}
else if(iscal(ch))
{
token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
pre=ch;
ch=fgetc(input);
if(ch==pre||ch=='=')
{
token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
if(ch==pre&&(ch=='<'||ch=='>'))
{
ch=fgetc(input);
if(ch=='=')
{
token[strlen(token)+1]='\0';
token[strlen(token)]=ch;
}
else
fseek(input,-1,1);
}
for(i=0;i<36;i++)
{
if(strcmp(token,cal[i].str)==0)
{
printf(" <%s,%d>",cal[i].str,cal[i].type);
fprintf(output," <%s,%d>\n",cal[i].str,cal[i].type);
token[0]='\0';
break;
}
}
if (i==36)
printf("it's a wrong calcurate");
}
else if((pre=='/')&&(ch=='*'))
{
pre=fgetc(input);
ch=fgetc(input);
while(!(pre=='*'&&ch=='/'))
{
pre=ch;
ch=fgetc(input);
}
ch=fgetc(input);
}
else
{
if (!feof(input))
fseek(input,-1,1);
for(i=0;i<36;i++)
{
if(strcmp(token,cal[i].str)==0)
{
printf(" <%s,%d>",cal[i].str,cal[i].type);
fprintf(output," <%s,%d>\n",cal[i].str,cal[i].type);
token[0]='\0';
break;
}
}
}
/* ch=fgetc(input);
printf("result=%c", ch);*/
}
}
fclose(input);
getchar();
}
int isseperate(char ch)/*判断是否分割符*/
{
if(ch=='#'||ch=='('||ch==')'||ch=='\''||
ch=='['||ch==']'||ch=='\"'||ch==';'||
ch=='{'||ch=='}'||ch==','||ch=='\\'||ch==':')
return 1;
return 0;
}
int iscal(char ch)/*判断是否运算符*/
{
if(ch=='!'||ch=='%'||ch=='^'||ch=='&'||ch=='*'||
ch=='-'||ch=='+'||ch=='='||ch=='~'||ch=='/'||
ch=='.'||ch=='<'||ch=='>'||ch=='|'||ch=='?')
return 1;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -