gpt4 book ai didi

c - 链表中传递的数据错误

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

假设我输入以下内容:

maximum weight number of containers the ship can carry: 10
maximum weight:5
filename: containerlist.txt

输出:

Weight: 1.82 Shipping Price:2537.76  <---- total exceed 5
Weight: 2.98 Shipping Price:5526.86
Weight: 6.22 Shipping Price:8444.84
Weight: 6.77 Shipping Price:4698.15

Total weight: 5.96 <----- Wrong

我试图将我的体重打印在下方

totalweight = totalweight + cnr_getWeight(node_getData(current));

看看我得到了什么值,结果是 2.98 和 2.98。

typedef struct Node {
void *data;
struct Node *next;
} Node;

typedef struct{
char *id;
double weight;
double shippingPrice;
}Container;

typedef struct{
Node *head;
Node *tail;
Node *iterator;
int size;
} List;

/* Main */

int main()
{
int maxCnr;
int file = 0;
double maxWeight;
char filename[50];
char choice;
FILE *input;
List *list = lst_new();

printf("********************** WELCOME TO SHIPMANAGER **********************\n\n");
printf("Please enter the maximum number of containers the ship can carry: ");
scanf("%i", &maxCnr);

printf("\nPlease enter the maximum weight the ship can carry: ");
scanf("%lf", &maxWeight);

do {
printf("\nEnter the filename (.txt): ");
scanf("%s", &filename);

input = fopen(filename, "r");
if (input == NULL)
{
file = 0;
printf("File does not exist. \n");
}
else
{
printf("File located.\n");
file = 1;
}
}while(file == 0);

readContainer(list,filename);
mxCnr(&list, maxCnr);
mxWeight(&list, maxWeight);
printContainers(list);
}
/* ensure that the container does not exceed the maximum number */

void mxCnr(List **list, int mCnr)
{
Container *tmpContainer;
List *max = lst_new();
int i = 0;

for (tmpContainer = lst_first(*list); i<mCnr; tmpContainer = lst_next(*list))
{
Node *previous = NULL;
Node *current = max->head;

if (max->size == 0)
lst_add(max, tmpContainer);
else
{
Node *newNode = node_new(tmpContainer, NULL);
current = max->head;
while (node_getNext(current) != NULL)
{
previous = current;
current = node_getNext(current);
}

if (previous == NULL)
{
node_setNext(newNode, max->head);
max->head = newNode;
}
else
{
max->tail->next = newNode;
max->tail = newNode;
}
max->size++;
}
i++;
}
lst_delete(*list);
*list = max;
}

/* function to calculate the total weight and to ensure that the weight does not exceed the maximum weight */

void mxWeight(List **list, double mWeight)
{
Container *tmpContainer;
List *maxW = lst_new();
double totalweight= 0;
printf("maxweight :%lf ", mWeight);

for (tmpContainer = lst_first(*list); totalweight<=mWeight; tmpContainer = lst_next(*list))
{
Node *current = maxW->head;
Node *previous = NULL;


if (maxW->size == 0)
{
lst_add(maxW, tmpContainer);

}
else
{
Node *newNode = node_new(tmpContainer, NULL);
current = maxW->head;
while (node_getNext(current) != NULL && totalweight <= mWeight)
{
previous = current;
current = node_getNext(current);
totalweight = totalweight + cnr_getWeight(node_getData(current));
printf("%lf ", cnr_getWeight(node_getData(current)));
}

if (previous == NULL)
{
node_setNext(newNode, maxW->head);
maxW->head = newNode;
}
else
{
maxW->tail->next = newNode;
maxW->tail = newNode;
}
maxW->size++;
}

}
printf("Total weight: %lf", totalweight);
lst_delete(*list);
*list = maxW;
}

最佳答案

如果您不想超过总重量,那么您需要在执行总重量之前进行检查。 (更简单...)

 while (node_getNext(current) != NULL && totalweight<= mWeight)
{
Node *old_previous = previous;
previous = current;
current = node_getNext(current);
totalweight = totalweight + cnr_getWeight(node_getData(current));
printf("%lf ", cnr_getWeight(node_getData(current)));
if (totalweight > mWeight){
totalweight -= cnr_getWeight(node_getData(current));
current = previous;
previous = old_previous;
break;
}
}

关于c - 链表中传递的数据错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18253290/

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