📄 shiyan.c
字号:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#define MAXSIZE 100
#define N 50
void init(char *ch)
{ char newc[N];
newc[0]=NULL;
strcpy(ch,newc);}
void scan(char *ch)
{ char *k1="if";
char *k2="then";
char *k3="else";
char *k4="while";
char *k5="do";
char *k6="+";
char *k7="-";
char *k8="*";
char *k9="/";
char *k10=">";
char *k11="<";
char *k12="=";
char *k13="(";
char *k14=")";
int judge;
int i=1,k;
int answer=0;
if(strcmp(ch,k1)==0||strcmp(ch,k2)==0||strcmp(ch,k3)==0||strcmp(ch,k4)==0||strcmp(ch,k5)==0)
printf("keywords\t%s\n",ch);
else if (isalpha(ch[0])!=0)
printf("IDN\t%s\n",ch);
{ if(strcmp(ch,k6)==0||strcmp(ch,k7)==0||strcmp(ch,k8)==0||strcmp(ch,k9)==0||strcmp(ch,k10)==0||
strcmp(ch,k11)==0||strcmp(ch,k12)==0||strcmp(ch,k13)==0||strcmp(ch,k14)==0)
{printf("operator\t%s\n",ch);}
else
{ if(isdigit(ch[0])==2)
{if(strlen(ch)>1)
{if(strlen(ch)>2)
{if(ch[1]=='x')
{i=2;judge=1;
while(ch[i]!=NULL)
{if(isxdigit(ch[i])==0)
{ judge=0;}
i++; }
if(judge==1)
{for(i=2;i<strlen(ch);i++)
{if(ch[i]>='0'&&ch[i]<='9')
{k=ch[i]-'0';}
else
{k=ch[i]-'W';}
answer=answer+k*pow(16,strlen(ch)-i-1);}
printf("INT16\t%s\t%d\n",ch,answer);}
else printf("Input error %s is not a hexadecimal number!\n",ch) ;
}
}
if(ch[0]=='0')
{i=1;judge=1;
while(ch[i]!=NULL)
{if(isdigit(ch[i])==0)
{judge=0;}
i++;}
if(judge==1)
{
for(i=0;i<strlen(ch);i++)
{if(ch[i]>='8'){judge=3;break;}
k=ch[i]-'0';answer=answer+k*pow(8,strlen(ch)-i-1);}
if(judge==3)printf("Input error %s is not a octal number !\n",ch);
else printf("INT8\t%s\t%d\n",ch,answer);}}
else
{i=1;judge=1;
while(ch[i]!=NULL)
{if(isxdigit(ch[i])==0)
{judge=0;}
i++;}
if(judge==1)
{printf("INT10\t%s\n",ch);}}}
if(strlen(ch)==1)
{printf("INT10\t%s\n",ch);}}
/*else{printf("IDN\t%s\n",ch);}}}} */
}} }
main()
{ char token[MAXSIZE];
char pretoken[MAXSIZE];
char aftoken[N];
/* 用来存放分割后的各个字符串 */
char ch[2];
/* 预处理过程和分割字符串的过程中起到存放字符的作用,然后再用字符连接函数strcat添加新字符到字符串中 */
int i=0,j=0;
ch[1]='\0';
printf("Please input:");
gets(token);
while(token[i]==' ')
{ i++;}
while(token[i]!=NULL)
{ token[j]=token[i];
i++;
j++; }
token[j]=NULL;
i=0;
while(token[i]!=NULL)
{ ch[0]=token[i];
if(ch[0]==' ')
{strcat(pretoken,ch);
j=1;
while(token[i+j]==' ')
{j++;}
i=i+j-1;}
else{strcat(pretoken,ch);}
i++; }
/* 以上三个while,用于预处理,去掉多余的空格 */
i=0;
while(pretoken[i]!=NULL)
{ ch[0]=pretoken[i];
if(ch[0]=='+'||ch[0]=='-'||ch[0]=='*'||ch[0]=='/'||ch[0]=='>'||ch[0]=='<'||ch[0]=='='||ch[0]=='('||ch[0]==')')
{ if(strlen(aftoken)!=0)
{ scan(aftoken);
init(aftoken);
strcat(aftoken,ch);
scan(aftoken);
init(aftoken);}
else
{strcat(aftoken,ch);
scan(aftoken);
init(aftoken);}
i++;
continue;}
if(ch[0]==' ')
{if(strlen(aftoken)!=0)
{scan(aftoken);
/*init(aftoken); */
}}
else
{strcat(aftoken,ch);}
i++;}
/*把单个单词分出来,转入专门的识别处理程序*/
if(strlen(aftoken)!=0)
{scan(aftoken);}
getch();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -