gpt4 book ai didi

c - C 统计文本文件数量时发生数据丢失

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

我尝试创建一个函数来从文本文件中获取数字并计算不重叠且超过特定数字的数字。我将把它放入链接列表中。计算一个与创建链表不重叠的数字是一件好事,但是计算超过一定数字的数字就存在问题。结果比原来的数字要少。在Excel中,应该有一些数字,但在我编写的C程序函数中,它们小于该数字。

void GetData(headNode* rheadnode)
{
int dataType;
int newData;
FILE* fp = NULL;
fp = fopen("input.txt", "r");
if (fp != NULL) {

while (fscanf(fp, "%d", &newData) != EOF)
{
dataType = InsertNode(rheadnode, newData);

if (newData > 5000)
Data_morethan5000_Count++;

switch (dataType)
{
case 0:
break;
case 1:
NodeCount++;
}
}
fclose(fp);
}
}

上面的代码是整个程序代码的一部分。我正在使用一种方法从 input.txt 文件中获取一个数字,将其放入 newData 变量中,如果 Data_morethan5000_Count 超过 5000,则增加 Data_morethan5000_Count 的值。所以结果应该是45460。然而,C程序输出的结果值为45432。我想知道哪里发生了数据丢失。以下是完整代码。

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

typedef struct Node {
int key;
struct Node* link;
} listNode;

typedef struct Head {
struct Node* head;
}headNode;


int NodeCount = 0;
int Data_morethan5000_Count = 0;

headNode* initialize(headNode* rheadnode);
int DeleteList(headNode* rheadnode);
void GetData(headNode* rheadnode);
int InsertNode(headNode* rheadnode, int key);
void PrintResult(headNode* rheadnode);

int main()
{
int key;
headNode* headnode = NULL;

headnode = initialize(headnode);
GetData(headnode);
PrintResult(headnode);

DeleteList(headnode);

return 0;
}

headNode* initialize(headNode* rheadnode) {
headNode* temp = (headNode*)malloc(sizeof(headNode));
temp->head = NULL;
return temp;
}

int DeleteList(headNode* rheadnode) {
listNode* p = rheadnode->head;

listNode* prev = NULL;
while (p != NULL) {
prev = p;
p = p->link;
free(prev);
}
free(rheadnode);
return 0;
}


void GetData(headNode* rheadnode)
{
int dataType;
int newData;
FILE* fp = NULL;
fp = fopen("input.txt", "r");
if (fp != NULL) {

while (fscanf(fp, "%d", &newData) != EOF)
{
dataType = InsertNode(rheadnode, newData);

if (newData > 5000)
Data_morethan5000_Count++;

switch (dataType)
{
case 0:
break;
case 1:
NodeCount++;
}
}
fclose(fp);
}
}

int InsertNode(headNode* rheadnode, int key) {

listNode* search, * previous;
listNode* node = (listNode*)malloc(sizeof(listNode));

node->key = key;

search = rheadnode->head;
previous = NULL;
while (search != NULL)
{
if (node->key < search->key)
{
previous = search;
search = search->link;
}
else if (node->key == search->key)
return 0;
else
break;
}
if (previous == NULL)
{
node->link = rheadnode->head;
rheadnode->head = node;
}
else
{
node->link = search;
previous->link = node;
}
return 1;
}

void PrintResult(headNode* rheadnode) {
/*
The total number of nodes: 10011
More than 5000 values: 45460
Execution time: 1.234567 sec
*/
printf("The total number of nodes: %d\n", NodeCount);
printf("More than 5000 values: %d\n", Data_morethan5000_Count);
printf("Execution time: sec");
}

最佳答案

关于:我想知道哪里发生了数据丢失。

没有数据丢失。相反,存在一些重复的数据(键)值。

当存在重复时,节点数不增加。

关于c - C 统计文本文件数量时发生数据丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58155640/

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