2014-10-02 55 views
0

我想要执行一个代码来搜索txt文件,并返回不同单词的数量以及它们出现在文本上的次数。 我试图做到这一点,但我在比较从输入中读取的单词和已经阅读的单词时遇到了问题。所以我正在做一个代码,如果是新的话,就会在单词向量中增加一个单词,如果不是,则单词计数增加1。但是当我比较这些单词时,即使它们不是,它们也没有说明它们是平等的。 通过为例:TXT填充有:从文本输入中统计不同单词

test test test. 
test test test. 

( “测试”。从 “测试”=/=)。它返回7个不同的单词,其中3个为空,“3测试”和1个“测试”。 。这应该返回2个单词并在测试中计数为4,在测试中计数为2。

任何人都可以看到我的代码有什么问题吗?

#define MAX_PALAVRAS 1024 
#define MAX_TAM_PALAVRA 32 

typedef struct ocorrencia_ { 
    char palavra[MAX_TAM_PALAVRA]; 
    int pos; 
    int num_ocorrencias; 
}ocorrencia; 

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdbool.h> 


int main (int argc, char * argv[]){ 
    ocorrencia palavras[MAX_PALAVRAS]; 
    int i,palavras_diferentes=0,palavra_atual=0; 
    char aux[MAX_TAM_PALAVRA]; 
    bool nova_palavra=true; 
    for (i=0;i<MAX_PALAVRAS;i++){ 
     palavras[i].pos=-1; 
     palavras[i].num_ocorrencias=0; 
    } 
    FILE * D = fopen("input.txt","r"); 
    while (!feof(D)){ 
     char aux2[MAX_TAM_PALAVRA]; 
     fscanf(D,"%s",aux); 
     for (i=0;i<palavras_diferentes;i++){ 
      if (strcmp(palavras[palavras_diferentes].palavra,aux)==0){ 
       nova_palavra=false; 
       break; 
      } 
      palavra_atual++; 
     } 
     if (nova_palavra){ 
      strcpy(palavras[palavra_atual].palavra,aux); 
      palavras_diferentes++; 
     } 
     palavras[palavra_atual].num_ocorrencias++; 
     printf("%s\n",palavras[palavra_atual].palavra); 
    } 
    fclose (D); 
    printf("diferent words=%i\n",palavras_diferentes); 
    printf("success!\n"); 
    return (EXIT_SUCCESS); 
} 

感谢您的服用或时间阅读或试图帮助!

+1

你可能已经忘记了在while循环的开始设置'palavra_atual'至0 ... – francis 2014-10-02 17:09:21

+0

它解决了NULL问题,但仍然有7个不同的单词......现在3个“睾丸”。和4个“睾丸”。 – 2014-10-02 17:22:24

+1

您可能还需要添加'nova_palavra = true;'。你可能需要测试'fscanf'的返回值。有些人喜欢'if(fscanf(D,“%s”,aux)== 1){...}' – francis 2014-10-02 17:25:18

回答

1

按照我的意见,这里有一些变化,可以帮助你:

- 设置palavra_atual为0,nova_palavratrue在while循环的开始。

- 测试的fscanf回归,加上类似于if(fscanf(D,"%s",aux)==1){...}

- 测试所有的话! if (strcmp(palavras[i].palavra,aux)==0)

这里去代码:

#define MAX_PALAVRAS 1024 
#define MAX_TAM_PALAVRA 32 

typedef struct ocorrencia_ { 
    char palavra[MAX_TAM_PALAVRA]; 
    int pos; 
    int num_ocorrencias; 
}ocorrencia; 

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdbool.h> 


int main (int argc, char * argv[]){ 
    ocorrencia palavras[MAX_PALAVRAS]; 
    int i,palavras_diferentes=0,palavra_atual=0; 
    char aux[MAX_TAM_PALAVRA]; 
    bool nova_palavra=true; 
    for (i=0;i<MAX_PALAVRAS;i++){ 
     palavras[i].pos=-1; 
     palavras[i].num_ocorrencias=0; 
    } 
    FILE * D = fopen("input.txt","r"); 
    while (!feof(D)){ 
     palavra_atual=0; 
     nova_palavra=true; 
     char aux2[MAX_TAM_PALAVRA]; 
     if(fscanf(D,"%s",aux)==1){ 
      for (i=0;i<palavras_diferentes;i++){ 
       if (strcmp(palavras[i].palavra,aux)==0){ 
        nova_palavra=false; 
        break; 
       } 
       palavra_atual++; 
      } 
      if (nova_palavra==true){ 
       printf("new word %d %s\n",palavra_atual,aux); 
       strcpy(palavras[palavra_atual].palavra,aux); 
       palavras_diferentes++; 
      } 
      palavras[palavra_atual].num_ocorrencias++; 
      printf("%s\n",palavras[palavra_atual].palavra); 
     } 
    } 
    fclose (D); 
    printf("diferent words=%i\n",palavras_diferentes); 
    printf("success!\n"); 
    return (EXIT_SUCCESS); 
} 

您将ispunct()被interesseted的ctypes.hhere

相关问题