gpt4 book ai didi

c - 在链表 C 中查找最高销量

转载 作者:太空宇宙 更新时间:2023-11-04 05:52:32 25 4
gpt4 key购买 nike

<分区>

我是新来的,请耐心等待我解释我的问题

我正在尝试创建一个函数来查找销售额最高的那一天。我通过从包含以下内容的文件中读取数据来执行此操作:

date, item, count

我正在使用一个链接列表,它过滤链接列表中的所有销售结构(它们按日期排序)

这是我的结构:

typedef struct sales {
int amount;
char *date;
struct stockItem *item;
struct sales *next;
} sales;

下面是查找哪一天销售额最高的代码:

sales *sFirstNode = NULL;
sales *sLastNode = NULL;

int findHighestVolumeSales() {
char *previousDate = NULL;
struct sales *tmpPtr = sFirstNode;
struct sales *tmpNxt = sFirstNode->next;
char *currentDate = tmpPtr->date;
int tmp=0;
int mostSales;
while(tmpNxt != NULL){
while(tmpNxt != tmpPtr){
if(currentDate == previousDate){
tmp = tmpPtr->amount + tmp;
}
else{
tmp = tmpPtr->amount;
}
if(tmp > mostSales){
mostSales = tmp;
printf("%d\n", mostSales);
}
previousDate = tmpPtr->date;
tmpPtr = tmpPtr->next;
}
tmpPtr = sFirstNode;
tmpNxt = tmpNxt->next;
}
printf("%d\n", mostSales);
return mostSales;
}

函数返回的值似乎是地址的值,而不是 mostSales 本身。

我得到:

6421718

值很大,但没有这个大。我确定我的代码有问题

编辑:

int findHighestVolumeSales() {
char *previousDate = NULL;
struct sales *tmpPtr = sFirstNode;
struct sales *tmpNxt = sFirstNode->next;
char *currentDate = NULL;
int tmp=0;
int mostSales =0;
while(tmpNxt != NULL){
currentDate = tmpPtr->date;
while(tmpNxt != tmpPtr){
// printf("%d \n", mostSales);
if(currentDate == previousDate){
tmp = tmpPtr->amount + tmp;
}
else{
tmp = tmpPtr->amount;
}
if(tmp > mostSales){
mostSales = tmp;
printf("%d\n", mostSales);
}
previousDate = tmpPtr->date;
tmpPtr = tmpPtr->next;
}
tmpPtr = sFirstNode;
tmpNxt = tmpNxt->next;
}
printf("%d\n", mostSales);
return mostSales;// Place holder
}

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