簡體   English   中英

將struct值與變量進行比較

[英]Comparing struct values against variable

我正在努力將結構的內容與變量進行比較。 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;
}

假設如下:

  • 您的數組已正確分配(在堆棧上或動態分配)至少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;
}

輸出與以前相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM