年年月月日日无bug 2023-12-09 12:41 采纳率: 54.5%
浏览 0

c语言写的分词器不报错但不输出


#include<stdio.h>
struct tokens{
    char value[128];
    char kind[64];
}token_list[128];
int token_list_p = 0;
void token(char input[128]){
    int p = 0;
    while(p < 127){
        if((input[p]>='a' && input[p]<='z') || (input[p]>='A' && input[p]<='Z')){
            char word[128];
            int p2 = 0;
            while((input[p]>='a' && input[p]<='z') || (input[p]>='A' && input[p]<='Z')){
                word[p2] = input[p];
                p2++;
                p++;
            }
            token_list[token_list_p] = (struct tokens){word,"string"};
            token_list_p++;
            continue;            
        }
        if(input[p] >='0' && input[p]<='9'){
            int p2 = 0;
            char word[128];
            while(input[p] >='0' && input[p]<='9'){
                word[p2] = input[p];
                p2++;
                p++;
            }
            token_list[token_list_p] = (struct tokens){word,"string"};
            token_list_p++;
            continue;
        }
    }
}
int main(){
    char input[128];
    scanf("%s",input);
    token(input);
    printf("%s",token_list[0].kind);
    int a;
    scanf("%d",&a);
    return 0;
}

用c写的token,输入后却不显示

  • 写回答

3条回答 默认 最新

  • &春风有信 2023-12-09 13:58
    关注

    首先,你的代码中存在一些逻辑错误。在 token 函数中,你使用两个 while 循环来提取单词,但是外层的 while 循环条件是 p < 127,这意味着当遇到一个单词或一个数字的结束时,你将会跳出循环,不再继续检查后面的字符。这可能会导致你的代码无法正确处理一些多字符的单词或数字。

    其次,你在 main 函数中只打印了 token_list[0].kind,但是并没有对数组中的每个元素进行遍历,所以可能没有输出任何内容。

    让我来修复这些问题:

    #include<stdio.h>
    #include<string.h>
    
    struct tokens{
        char value[128];
        char kind[64];
    }token_list[128];
    int token_list_p = 0;
    
    void token(char input[128]){
        int p = 0;
        while(p < 127){
            if((input[p]>='a' && input[p]<='z') || (input[p]>='A' && input[p]<='Z')){
                char word[128];
                int p2 = 0;
                while((input[p]>='a' && input[p]<='z') || (input[p]>='A' && input[p]<='Z')){
                    word[p2] = input[p];
                    p2++;
                    p++;
                }
                word[p2] = '\0'; // 添加字符串结束标志
                token_list[token_list_p] = (struct tokens){word,"string"};
                token_list_p++;
            }
            else if(input[p] >='0' && input[p]<='9'){
                char word[128];
                int p2 = 0;
                while(input[p] >='0' && input[p]<='9'){
                    word[p2] = input[p];
                    p2++;
                    p++;
                }
                word[p2] = '\0'; // 添加字符串结束标志
                token_list[token_list_p] = (struct tokens){word,"number"};
                token_list_p++;
            }
            else{ // 如果不是字母或数字,则跳过该字符
                p++;
            }
        }
    }
    
    int main(){
        char input[128];
        scanf("%s",input);
        token(input);
        // 遍历数组并打印每个元素的kind字段
        for(int i = 0; i < token_list_p; i++){
            printf("%s\n", token_list[i].kind);
        }
        return 0;
    }
    
    

    这个修复版本的代码会遍历整个 token_list 数组,并打印每个元素的 kind 字段。这样你就可以看到输出的结果了。同时,我也添加了字符串结束标志 \0 来正确处理多字符的单词或数字。

    评论

报告相同问题?

问题事件

  • 创建了问题 12月9日

悬赏问题

  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费
  • ¥15 kafka无法正常启动(只启动了一瞬间会然后挂了)
  • ¥15 Workbench中材料库无法更新,如何解决?
  • ¥20 如何推断此服务器配置