zoukankan      html  css  js  c++  java
  • 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日--

  • 相关阅读:
    Delphi中WebBrowser自动填表模板
    对TMemoryStream的一些改进(用到了LockFile)
    用Delphi画圆角Panel的方法(使用CreateRoundRectRgn创造区域,SetWindowRgn显示指定区域)
    Delphi5的System.pas只有11514行
    《MFC游戏开发》笔记八 游戏特效的实现(二):粒子系统
    Delphi动态申请数组内存的方法(不使用SetLength,采用和C相似的方式)
    Delphi的类型转换 good
    New 和 GetMem 的不同之处
    XML SelectSingleNode的使用 根据节点属性获取该节点
    ADO面板上的控件简介
  • 原文地址:https://www.cnblogs.com/heyang78/p/13056022.html
Copyright © 2011-2022 走看看