📄 sh.c
字号:
#include "stdio.h"
#include "string.h"
#include "ctype.h"
void analzid(FILE *output,char *p)
{
int i=0;
int count=0;
if (isalpha(p[0]))
{if (strcmp(p,"if")==0) {fprintf(output,"(3,if)\n");}
else if(strcmp(p,"then")==0) {fprintf(output,"(4,then)\n");}
else if(strcmp(p,"else")==0) {fprintf(output,"(5,else)\n");}
else if(strcmp(p,"while")==0) {fprintf(output,"(6,while)\n");}
else if(strcmp(p,"do")==0) {fprintf(output,"(7,do)\n");}
else if(strcmp(p,"begin")==0) {fprintf(output,"(8,begin)\n");}
else if(strcmp(p,"end")==0) {fprintf(output,"(9,end)\n");}
else if(strcmp(p,"long")==0) {fprintf(output,"(10,long)\n");}
else if(strcmp(p,"switch")==0) {fprintf(output,"(11,switch)\n");}
else if(strcmp(p,"case")==0) {fprintf(output,"(12,case)\n");}
else if(strcmp(p,"enum")==0) {fprintf(output,"(13,enum)\n");}
else if(strcmp(p,"register")==0) {fprintf(output,"(14,register)\n");}
else if(strcmp(p,"typedef")==0) {fprintf(output,"(15,typedef)\n");}
else if(strcmp(p,"char")==0) {fprintf(output,"(16,char)\n");}
else if(strcmp(p,"extern")==0) {fprintf(output,"(17,extern)\n");}
else if(strcmp(p,"return")==0) {fprintf(output,"(18,return)\n");}
else if(strcmp(p,"union")==0) {fprintf(output,"(19,union)\n");}
else if(strcmp(p,"const")==0) {fprintf(output,"(20,const)\n");}
else if(strcmp(p,"float")==0) {fprintf(output,"(21,float)\n");}
else if(strcmp(p,"short")==0) {fprintf(output,"(22,short)\n");}
else if(strcmp(p,"unsigned")==0) {fprintf(output,"(23,unsigned)\n");}
else if(strcmp(p,"continue")==0) {fprintf(output,"(24,continue)\n");}
else if(strcmp(p,"for")==0) {fprintf(output,"(25,for)\n");}
else if(strcmp(p,"signed")==0) {fprintf(output,"(26,signed)\n");}
else if(strcmp(p,"void")==0) {fprintf(output,"(27,void)\n");}
else if(strcmp(p,"default")==0) {fprintf(output,"(28,default)\n");}
else if(strcmp(p,"goto")==0) {fprintf(output,"(29,goto)\n");}
else if(strcmp(p,"sizeof")==0) {fprintf(output,"(30,sizeof)\n");}
else if(strcmp(p,"volatile")==0) {fprintf(output,"(43,volatile)\n");}
else if(strcmp(p,"auto")==0) {fprintf(output,"(44,auto)\n");}
else if(strcmp(p,"double")==0) {fprintf(output,"(45,double)\n");}
else if(strcmp(p,"int")==0) {fprintf(output,"(46,int)\n");}
else if(strcmp(p,"struct")==0) {fprintf(output,"(47,struct)\n");}
else if(strcmp(p,"break")==0) {fprintf(output,"(48,break)\n");}
else if(strcmp(p,"static")==0) {fprintf(output,"(49,static)\n");}
else {fprintf(output,"(1,%s)\n",p);}
}
else
{for(;i<(int)strlen(p);i++) if(isdigit(p[i])) count++;
if (count==(int)strlen(p))
{fprintf(output,"(2,%s)\n",p);}
else
if (p[0]=='_'&&(isalpha(p[1])))
{fprintf(output,"(1,%s)\n",p);}
else {fprintf(output,"%s 未定义\n",p);}
}
}
void analzsy(FILE *outfile,char *p)
{
if (strcmp(p,"=")==0) {fprintf(outfile,"(37,=)\n");}
else if(strcmp(p,"+")==0) {fprintf(outfile,"(31,+)\n");}
else if(strcmp(p,"-")==0) {fprintf(outfile,"(32,-)\n");}
else if(strcmp(p,"*")==0) {fprintf(outfile,"(33,*)\n");}
else if(strcmp(p,"/")==0) {fprintf(outfile,"(34,/)\n");}
else if(strcmp(p,"(")==0) {fprintf(outfile,"(52,()\n");}
else if(strcmp(p,")")==0) {fprintf(outfile,"(53,))\n");}
else if(strcmp(p,"[")==0) {fprintf(outfile,"(55,[)\n");}
else if(strcmp(p,"]")==0) {fprintf(outfile,"(56,])\n");}
else if(strcmp(p,"{")==0) {fprintf(outfile,"(57,{)\n");}
else if(strcmp(p,"}")==0) {fprintf(outfile,"(58,})\n");}
else if(strcmp(p,"<<")==0) {fprintf(outfile,"(59,<<)\n");}
else if(strcmp(p,">>")==0) {fprintf(outfile,"(60,>>)\n");}
else if(strcmp(p,"'")==0) {fprintf(outfile,"(61,')\n");}
else if(strcmp(p,"#")==0) {fprintf(outfile,"(62,#)\n");}
else if(strcmp(p,".")==0) {fprintf(outfile,"(64,.)\n");}
else if(strcmp(p,"*")==0) {fprintf(outfile,"(33,*)\n");}
else if(strcmp(p,"/")==0) {fprintf(outfile,"(34,/)\n");}
else if(strcmp(p,"%")==0) {fprintf(outfile,"(40,%)\n");}
else if(strcmp(p,",")==0) {fprintf(outfile,"(64,,)\n");}
else if(strcmp(p,":")==0) {fprintf(outfile,"(65,:)\n");}
else if(strcmp(p,";")==0) {fprintf(outfile,"(54,;)\n");}
else if(strcmp(p,">")==0) {fprintf(outfile,"(36,>)\n");}
else if(strcmp(p,"<")==0) {fprintf(outfile,"(35,<)\n");}
else if(strcmp(p,">=")==0) {fprintf(outfile,"(39,>=)\n");}
else if(strcmp(p,"<=")==0) {fprintf(outfile,"(38,<=)\n");}
else if(strcmp(p,"==")==0) {fprintf(outfile,"(41,==)\n");}
else if(strcmp(p,"!=")==0) {fprintf(outfile,"(42,!=)\n");}
else if(strcmp(p," ")==0) ;
else if(strcmp(p,"\n")==0) ;
else {fprintf(outfile,"%s 未定义\n",p);}
}
void main()
{FILE *fp,*out;
int i=0,x=0,y=0;
int EA=0;
char ch,str[10000],idstr[10],systr[2];
if((fp=fopen("infile.txt","r"))==NULL)
{printf("Can not open infile!\n");
}
if((out=fopen("outfile.txt","w"))==NULL)
{printf("Can not open outfile!\n");
}
ch=fgetc(fp);
while(ch!=EOF) //将infile.txt中的字符全部存入数组
{str[i]=ch;str[i+1]='\0';i++;ch=fgetc(fp);}
i=0;
while(1)
{
if(str[i]=='@') break; //遇到@则结束
else
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')||
(str[i]>='0'&&str[i]<='9')||(str[i]=='_')) //判断标识符
{idstr[x]=str[i];idstr[x+1]='\0';x++;i++;EA=1;}
else
{
x=0;
if((strlen(idstr)!=0)&&(EA)) analzid(out,idstr);//将标识符输出到outfile.txt
idstr[x]='\0';
if(str[i]<'0'||(str[i]>'9'&&str[i]<'A')||
(str[i]>'Z'&&str[i]<'a')||str[i]>'z')//判断除数字、字符之外的其他符号
{
if(str[i]!='!'&&str[i]!='='&&str[i]!='<'&&str[i]!='>')
{
systr[y]=str[i];systr[y+1]='\0';analzsy(out,systr);i++;
}
else
if((str[i]=='!'&&str[i+1]=='=')||(str[i]=='='&&str[i+1]=='=')||
(str[i]=='>'&&str[i+1]=='=')||(str[i]=='<'&&str[i+1]=='=')||
(str[i]=='<'&&str[i+1]=='<')||(str[i]=='>'&&str[i+1]=='>'))//判断双目符号
{
systr[y]=str[i];systr[y+1]=str[i+1];systr[y+2]='\0';
analzsy(out,systr);i++;i++;//将双目符号输出
}
else
{
systr[y]=str[i];systr[y+1]='\0';analzsy(out,systr);i++;
}}}}
printf("-----全部结果已经存入outfile.txt文档-----\n");
fprintf(out,"--------完成--------");
fclose(fp);
fclose(out);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -