?? cifa.c
字號:
#include<stdio.h>
#include<string.h>
#define N 100
char ch,A[N];
char *keyword[7]={"begin","and","if","then","else","while","do"};
int i=0;
char token[30];
int reserve(char B[])
{ int t, j;
for(j=0;j<7;j++)
if(!strcmp(B,keyword[j]))
{
switch(j)
{ case 0: t=1;
break;
case 1: t=2;
break;
case 2: t=3;
break;
case 3: t=4;
break;
case 4: t=5;
break;
case 5: t=6;
break;
case 6: t=7;
break;
default: printf("\n該字符串不是關鍵字");
}
break;
}
if(j==7) t=10;
return t;
}
char getch()
{
ch=A[i];
i++;
return ch;
}
void retract()
{i--;
}
void getbc()
{ int j=0;
while(ch==' '||ch=='\n')
{ j++;
getch();
}
if(j!=0&&ch=='#')
retract();
}
void concat()
{ char c[2];
c[0]=ch; c[1]='\0';
strcat(token,c);
}
void scaner()
{ int c;
token[0]='\0';
getbc();
if((ch<='z')&&(ch>='A'))
{
while(((ch>='A')&&(ch<='z'))||((ch<='9')&&(ch>='0')))
{ concat();
getch();
}
retract();
c=reserve(token);
if(c!=10)
printf("\n關鍵字 %d\t%s",c,token);
else
printf("\n標識符 10\t%s",token);
}
else
if((ch<='9')&&(ch>='0'))
{
while((ch<='9')&&(ch>='0'))
{ concat();
getch();
}
retract();
printf("\n數字 11\t %s",token);
}
else switch(ch)
{ case'+':printf("\n運算符 13\t+");
break;
case'-':printf("\n運算符 14\t-");
break;
case'*':printf("\n運算符 15\t*");
break;
case'/':printf("\n運算符 16\t/");
break;
case'<':getch();
if(ch=='=')
printf("\n運算符 17\t<=");
else if(ch=='>')
printf("\n運算符 18\t<>");
else
{ retract();
printf("\n運算符 19\t<");
}
break;
case':':getch();
if(ch=='=')
printf("\n運算符 22\t:=");
else
{retract();
printf("\n運算符 21\t:");
}
break;
case';':printf("\n運算符 23\t;\n");
break;
case'#':printf("\n程序結束");break;
default:printf("\nERROR");
}
}
void main()
{ int j=0;
printf("請輸入源程序:\n");
scanf("%c",&A[0]);
while(A[j]!='#')
{j++;
scanf("%c",&A[j]);
}
getch();
while(ch!='#')
{
scaner();
getch();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -