54列志华

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#include<stdio.h>
#include<string.h>
#define MAX 100
int n=0,i=0;
char *keyshuru[6]={"begin","if","then","while","do","end"};

void KEYword(char shuru[MAX])
{
	int k=0,key=0;
	char token[MAX];
    while(shuru[i]!='#')
    {    
    while((shuru[i]>='a'&&shuru[i]<='z') || (shuru[i]>='A'&&shuru[i]<='Z'))
    {
        if((shuru[i]>='a'&&shuru[i]<='z') || (shuru[i]>='A'&&shuru[i]<='Z'))
        {
              token[n]=shuru[i];
            n++;
            token[n]='\0';
            i++;            
        }

    for(k=0;k<6;k++)
    {
        key=0;
        if(strcmp(keyshuru[k],token)==0)
        {
            printf("%s   %d\n",keyshuru[k],k+1);
            key=1;
            break;
        }
    }    }
    if(key==0)
    {
            printf("%s   10\n",token);
    }
    key=1;
    i=i+1;
    n=0;
    }
}
void character(char shuru[MAX])
{
    int p=0,sign=0;
    for(p=0;p<MAX;p++)
    {
        switch(shuru[p])
        {
        case '+':
            printf("+    13\n");
            break;
        case '-':
            printf("-    14\n");
            break;
        case '*':
            printf("*    15\n");
            break;
        case '/':
            printf("/    16\n");
            break;
        case ':':
            if(shuru[p+1]=='=')
            {
                printf(":=   18\n");
                p++;
            }
            else
                printf(":    17\n");
            break;
        case '<':
            if(shuru[p+1]=='=')
            {
                printf("<=     21\n");
                sign=1;
                p++;
            }
            else if(shuru[p+1]=='>')
            {
                printf("<>     22\n");
                sign=1;
                p++;
            }
            else if(sign==0)
                printf("<    20\n");
            break;
        case '>':
            if(shuru[p+1]=='=')
            {
                printf(">=    24\n");
                p++;
            }
            else
                printf(">    23\n");
            break;
        case '=':
            printf("=    25\n");
            break;
        case ';':
            printf(";    26\n");
            break;
        case '(':
            printf("(    27\n");
            break;
        case ')':
            printf(")    28\n");
            break;
        case '#':
            printf("#    29\n");
            break;
        }
    }
}

void number(char shuru[MAX])
{
    int p=0,j=0,t=0,key=0;
    char num[MAX];
    while(shuru[p]!='#')
    {
        key=0;
    while(shuru[p]>='0' && shuru[p]<='9')
    {
        num[j]=shuru[p];
        p++;
        j++;
        key=1;
    }
    if(key==1)
    {
        for(t=0;t<j;t++)
        {
            printf("%c",num[t]);
        }
        printf("    11\n");
        j=0;
    }
    p++;
    }

}

main()
{
	char a;
	char shuru[MAX];

    int p=0,j=0,m=0;

    printf("请输入源程序,#键为结束标志:");
    do{
        scanf("%c",&a);
        shuru[p]=a;
        p++;
    }while(a!='#');
   KEYword(shuru);
    character(shuru);
    number(shuru);
}




  

posted on 2015-09-21 18:08  liezh  阅读(278)  评论(1编辑  收藏  举报