2013-03-31 44 views
0

我在努力将结构的内容与变量进行比较。 array1中有10个结构体,其中包含变量值和计数。我需要查看每个值变量,直到找到与tempVal匹配的值,然后增加相应的计数值,然后搜索结束。如果没有找到,函数将返回-1。将结构值与变量进行比较

我有以下代码运行良好,但不工作,我有一种感觉,它可能是错误的strcmp行,但我不知道。欢呼任何输入。

int valCheck(char *tempVal){ 

    int j; 
    for(j=0;j<10;j++){ 
     if(strcmp(array1[j].value, tempVal) == 0){ 
      array1[j].count++; //increment its count 
      break; 

     } 
    } 
} 

被修改满:

#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 

struct values 
{ 
    char value[64]; 
    int count; 
}; 

struct values array1[100]; 

//check if value exists in array 
int valCheck(char *tempVal) 
{ 
    int j; 
    for(j=0;j<10;j++) 
    { 
     if(strcmp(array1[j].value, tempVal) == 0) 
     { 
      array1[j].count++; //increment its count 
      //return j; // <== return index of found element 
     } 
    } 
    return -1; 
} 

int main() 
{ 
    FILE * input; 
    int i; 
    char tempVal[] ="hello"; 
    input = fopen("random.txt","r"); 

    for (i=0;i<=10;i++) //go through the 10 elements in text file 
    { 
     //scan each word into a temporary variable 
     // **** NOT COMPLETE, JUST USING TEMPWORD ASSIGNED FOR NOW 

     int checkedVal = valCheck(&tempVal); 

     //if wordCheck returns -1 add the word to the array 
     //otherwise do nothing as a duplicate has appeared 
     if(checkedVal == -1){ 
      fscanf(input, "%s",array1[i].value); 
     } 

     printf("WORD %i: %s ",i,array1[i].value); 
     printf(" COUNT IS: %i", array1[i].count); 
     printf("\n"); 
    } 

    fclose(input); 
    return 0; 
} 
+0

A **返回值**可能是有保证的,您不同意吗?如果这应该向调用者指出'valCheck()'找到了值并修改了它的计数,并且更重要的是,如果这是您用于“这不起作用”的说法的基础,那么您可能会*希望通过'return'语句实际传达该事实,而不是EAX寄存器中的某个随机值。 – WhozCraig

+1

请定义*不起作用*。还要将'array1 []'结构的定义添加到问题中。 – wildplasser

+1

从你的代码中,没有人可以说出什么是错的。什么是array1?什么是tempVal –

回答

2

假设以下几点:

  • 你的阵列被适当地分配(无论是在叠层或动态地)是在-至少-10-元件宽。
  • 您的结构成员value是有效的char *或定长char[n]缓冲区。
  • 您的结构的value成员的字符串数据引用正确以null结尾。
  • tempVal引用的字符串数据是有效的,并且正确地以null结尾。
  • 你知道strcmp()比较大小写敏感
  • 你评论阵列中的约100个元素,并且该代码只检查第一(10)元素是故意

我的水晶球告诉我,最终你需要有你的功能,实际上返回的东西。

int valCheck(char *tempVal) 
{ 
    int j; 
    for(j=0;j<10;j++) 
    { 
     if(strcmp(array1[j].value, tempVal) == 0) 
     { 
      array1[j].count++; //increment its count 
      return j; // <== return index of found element 
     } 
    } 
    return -1; // <== return -1 per your description of failure. 
} 

注:您的编译器,配有体面的警告的检查,会很容易地发现缺的回报此代码。留意这些警告。同样,请仔细检查此答案顶部的项目符号列表中的所有内容,以便确定您正在进行的操作是正确的。


编辑更新,以反映样本字典建立OP。

以下是我认为这可能会被称为。希望它可以帮助你。我换了几件事情:

  • 作出的字典有点大(256)
  • 处理命令行来获取文件名。使我更容易测试。
  • 只处理时报告最后的摘要而不是每个词。

我希望它有帮助。

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

struct values 
{ 
    char value[64]; 
    int count; 
}; 

// global array. 
struct values array1[256]; 
int n_words = 0; 

//check if value exists in array 
int valCheck(const char *tempVal) 
{ 
    int j; 
    for(j=0;j<10;j++) 
    { 
     if(strcmp(array1[j].value, tempVal) == 0) 
     { 
      array1[j].count++; //increment its count 
      return j; 
     } 
    } 
    return -1; 
} 

int main(int argc, char *argv[]) 
{ 
    FILE * input = NULL; 
    char tempVal[64]; 
    int i=0; 

    if (argc < 2) 
    { 
     printf("Must specify a filename.\n"); 
     return EXIT_FAILURE; 
    } 

    // open file 
    input = fopen(argv[1],"r"); 
    if (!input) 
    { 
     perror("Failed to open file."); 
     return EXIT_FAILURE; 
    } 

    // read all strings from the file one at a time. 
    while (fscanf(input, "%64s", tempVal) == 1) 
    { 
     int i = valCheck(tempVal); 
     if (i == -1) 
     { 
      if (n_words < sizeof(array1)/sizeof(array1[0])) 
      { 
       strcpy(array1[n_words].value, tempVal); 
       array1[n_words].count = 1; 
       i = n_words++; 
      } 
      else 
      { // error. no more space in dictionary 
       printf("No more space to add word: %s\n", tempVal); 
      } 
     } 

    } 
    fclose(input); 

    // summary report 
    for (i=0;i<n_words;++i) 
     printf("WORD %i: %s, COUNT IS: %i\n", i, array1[i].value, array1[i].count); 

    return EXIT_SUCCESS; 
} 

输入

hello my name is dave hello I am dave hi 

输出

WORD 0: hello, COUNT IS: 2 
WORD 1: my, COUNT IS: 1 
WORD 2: name, COUNT IS: 1 
WORD 3: is, COUNT IS: 1 
WORD 4: dave, COUNT IS: 2 
WORD 5: I, COUNT IS: 1 
WORD 6: am, COUNT IS: 1 
WORD 7: hi, COUNT IS: 1 

作业

毕竟,我让你确定为什么下面的代码也可以工作,但是不使用临时缓冲区来完成(反正)。

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

struct values 
{ 
    char value[64]; 
    int count; 
}; 

// global array. 
#define MAX_WORDS 256 
struct values array1[MAX_WORDS] = {{{0},0}}; 
int n_words = 0; 

//check if value exists in array 
int valCheck(const char *tempVal) 
{ 
    int j; 
    for(j=0; j< n_words; j++) 
    { 
     if(strcmp(array1[j].value, tempVal) == 0) 
     { 
      array1[j].count++; //increment its count 
      return j; 
     } 
    } 
    return -1; 
} 

int main(int argc, char *argv[]) 
{ 
    FILE * input = NULL; 
    int i=0; 

    if (argc < 2) 
    { 
     printf("Must specify a filename.\n"); 
     return EXIT_FAILURE; 
    } 

    // open file 
    input = fopen(argv[1],"r"); 
    if (!input) 
    { 
     perror("Failed to open file."); 
     return EXIT_FAILURE; 
    } 

    // read all strings from the file one at a time. 
    while (n_words < MAX_WORDS && 
      fscanf(input, "%64s", array1[n_words].value) == 1) 
    { 
     if (valCheck(array1[n_words].value) == -1) 
     { 
      array1[n_words].count = 1; 
      ++n_words; 
     } 
    } 
    fclose(input); 

    // summary report 
    for (i=0;i<n_words;++i) 
     printf("WORD %i: %s, COUNT IS: %i\n", i, array1[i].value, array1[i].count); 

    return 0; 
} 

输出与以前相同。

+0

嗯,它不会出现我的比较代码太错了,所以我必须在别处做一个问题。我非常肯定所有这些假设都会被满足: - 由结构的值成员引用的字符串数据正确地以null结尾。 - 由tempVal引用的字符串数据是有效的,并且正确地以null结尾。我已经编辑了OP,如果有帮助 – user2209254

+0

@ user2209254您更新的帖子将*总是*返回-1。返回j是有原因的:表示* success *(即一个非 - ( - 1)的返回值,你可以很容易地返回0.任何事都可以做,只要它不是(-1) – WhozCraig

+0

ah对不起,忘记了!我确实尝试过返回0,但是它只做了一次循环,然后停下来,文件中只包含了随机的用空格分隔的单词。“你好我的名字是dave hello我是dave hi “ - 非常随意! – user2209254