C:把算术表达式分成Token

代码:

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef enum{
    TT_NUMBER,
    TT_ADD,
    TT_SUB,
    TT_MUL,
    TT_DIV,
    TT_EOL
}TokenType;

typedef struct{
    TokenType type;
    char text[255];
}Token;

static int pos=0;
static char* line;

void getToken(Token *token){
    char arr[255];
    int index=0;
    arr[index]='';

    while(line[pos]!=''){    
        if(line[pos]=='+'){
            if(strlen(arr)>0){
                strcpy(token->text,arr);
                index=0;
                arr[index]='';
                token->type=TT_NUMBER;
                
                return;
            }

            token->text[0]='+';
            token->text[1]='';
            token->type=TT_ADD;
            pos++;
            return;
        }else if(line[pos]=='-'){
            if(strlen(arr)>0){
                strcpy(token->text,arr);
                index=0;
                arr[index]='';
                token->type=TT_NUMBER;
                return;
            }

            token->text[0]='-';
            token->text[1]='';
            token->type=TT_SUB;
            pos++;
            return;
        }else if(line[pos]=='*'){
            if(strlen(arr)>0){
                strcpy(token->text,arr);
                index=0;
                arr[index]='';
                token->type=TT_NUMBER;
                return;
            }

            token->text[0]='*';
            token->text[1]='';
            token->type=TT_MUL;
            pos++;
            return;
        }else if(line[pos]=='/'){
            if(strlen(arr)>0){
                strcpy(token->text,arr);
                index=0;
                arr[index]='';
                token->type=TT_NUMBER;
                return;
            }

            token->text[0]='/';
            token->text[1]='';
            token->type=TT_DIV;
            pos++;
            return;
        }else if(line[pos]=='
'){
            if(strlen(arr)>0){
                strcpy(token->text,arr);
                index=0;
                arr[index]='';
                token->type=TT_NUMBER;
                return;
            }

            token->text[0]='';
            token->type=TT_EOL;
            pos++;
            return;
        }else{
            arr[index]=line[pos];
            index++;
            arr[index]='';
            pos++;
        }        
    }

    if(strlen(arr)>0){
        strcpy(token->text,arr);
        index=0;
        arr[index]='';
        token->type=TT_NUMBER;
        return;
    }
}

char* getTokenTypeDesc(Token *token){
    char* arr;
    arr = (char *)malloc(100); 

    if(token->type==0){
        strcpy(arr,"Num");
    }else if(token->type==1){
        strcpy(arr,"Add");
    }else if(token->type==2){
        strcpy(arr,"Sub");
    }else if(token->type==3){
        strcpy(arr,"Mul");
    }else if(token->type==4){
        strcpy(arr,"Div");
    }

    return arr;
}

void parse(){
    Token token;
    for(;;){
        getToken(&token);

        if(token.type==TT_EOL){
            break;
        }else{
            printf("%d %s %s
",token.type, getTokenTypeDesc(&token) ,token.text);
        }
    }
}



int _tmain(int argc, _TCHAR* argv[])
{
    char buf[1024];

    while(fgets(buf,1024,stdin)!=NULL){
        pos=0;
        line=buf;
        parse();
    }
    return 0;
}

运行结果:

1*2+3/4-5+6
0 Num 1
3 Mul *
0 Num 2
1 Add +
0 Num 3
4 Div /
0 Num 4
2 Sub -
0 Num 5
1 Add +
0 Num 6
1+2
0 Num 1
1 Add +
0 Num 2

--2020年6月6日--

原文地址:https://www.cnblogs.com/heyang78/p/13056022.html