0

我正在努力将结构的内容与变量进行比较。array1 中有 10 个结构,变量 value 和 count。我需要遍历每个值变量,直到找到一个匹配的 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;
}
4

1 回答 1

2

假设如下:

  • 您的数组已正确分配(在堆栈上或动态)为至少 10 个元素宽。
  • 您的结构成员value是有效的char *或固定长度的char[n]缓冲区。
  • 结构成员引用的字符串数据value正确地以空值结尾。
  • 引用的字符串数据tempVal有效且正确地以空值结尾。
  • 您知道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;
}

输出与之前相同。

于 2013-03-31T12:31:43.500 回答