gpt4 book ai didi

将结构体值与变量进行比较

转载 作者:行者123 更新时间:2023-11-30 14:20:09 25 4
gpt4 key购买 nike

我正在努力将结构的内容与变量进行比较。 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) 个元素

我的 Crystal 球告诉我,最终你需要让你的函数真正返回一些东西。

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.
}

注意:您的编译器配备了不错的警告检查功能,可以轻松发现这种缺少返回的代码。注意这些警告。同样,请仔细检查此答案顶部项目符号列表中的所有内容,以确保您要使用的内容是正确的。

<小时/>

编辑更新以反射(reflect) 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;
}

输出与之前相同。

关于将结构体值与变量进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15729245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com