gpt4 book ai didi

c - 按升序插入记录函数 - C 作业

转载 作者:太空宇宙 更新时间:2023-11-04 03:00:17 24 4
gpt4 key购买 nike

美好的一天,Stack Overflow。

我有一个家庭作业要在这个周末完成,但我遇到了一些问题。我们有一个结构“Record”(其中包含有关经销商汽车的信息),它根据 1)其品牌和 2)根据其型号年份放置在链接列表中的特定位置。

这是在最初构建列表时完成的,当在 Main 中调用“int insertRecordInAscendingOrder”函数时。在“insertRecordInAscendingOrder”中,调用了第三个函数“createRecord”,其中创建了链表。该函数然后转到函数“compareCars”以确定将哪些元素放在哪里。根据此函数返回的值,insertRecordInAscendingOrder 然后将记录放在它所属的位置。然后打印出该列表。任务还有很多,但我会在完成任务时跨过那座桥。

理想情况下,为了使分配被认为是正确的,链表必须按以下方式排序:

    Chevrolet 2012 25
Chevrolet 2013 10
Ford 2010 5
Ford 2011 3
Ford 2012 15
Honda 2011 9
Honda 2012 3
Honda 2013 12
Toyota 2009 2
Toyota 2011 7
Toyota 2013 20

来自一个文本文件,其中的数据按以下方式排序:

    Ford 2012 15
Ford 2011 3
Ford 2010 5
Toyota 2011 7
Toyota 2012 20
Toyota 2009 2
Honda 2011 9
Honda 2012 3
Honda 2013 12
Chevrolet 2013 10
Chevrolet 2012 25

请注意,“制造”字段的字母顺序优先,然后,型号年份从旧到新排列。

但是,该程序将其生成为最终列表:

    Chevrolet 2012 25
Chevrolet 2013 10
Honda 2011 9
Honda 2012 3
Honda 2013 12
Toyota 2009 2
Toyota 2011 7
Toyota 2012 20
Ford 2010 5
Ford 2011 3
Ford 2012 15

昨天我和一名研究生坐下来试图解决所有这些问题,但我们就是无法弄清楚为什么它将福特节点踢到列表的末尾。

这是代码。您会注意到,我在插入节点的每个实例中都包含了一个 printList 调用。这样,您就可以看到节点按“顺序”放置时发生了什么。它在 ANSI C99.所有函数调用都必须按照指定的方式进行,因此不幸的是,没有真正的方法可以通过创建更高效​​的算法来解决这个问题。

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

#define MAX_LINE 50
#define MAX_MAKE 20

typedef struct record
{
char *make;
int year;
int stock;
struct record *next;
} Record;

int compareCars(Record *car1, Record *car2);
void printList(Record *head);
Record* createRecord(char *make, int year, int stock);
int insertRecordInAscendingOrder(Record **head, char *make, int year, int stock);

int main(int argc, char **argv)
{
FILE *inFile = NULL;
char line[MAX_LINE + 1];
char *make, *yearStr, *stockStr;
int year, stock, len;
Record* headRecord = NULL;

/*Input and file diagnostics*/
if (argc!=2)
{
printf ("Filename not provided.\n");
return 1;
}

if((inFile=fopen(argv[1], "r"))==NULL)
{
printf("Can't open the file\n");
return 2;
}

/*obtain values for linked list*/
while (fgets(line, MAX_LINE, inFile))
{
make = strtok(line, " ");
yearStr = strtok(NULL, " ");
stockStr = strtok(NULL, " ");
year = atoi(yearStr);
stock = atoi(stockStr);
insertRecordInAscendingOrder(&headRecord,make, year, stock);

}


printf("The original list in ascending order: \n");
printList(headRecord);
}

/*use strcmp to compare two makes*/

int compareCars(Record *car1, Record *car2)
{
int compStrResult;
compStrResult = strcmp(car1->make, car2->make);
int compYearResult = 0;

if(car1->year > car2->year)
{
compYearResult = 1;
}
else if(car1->year == car2->year)
{
compYearResult = 0;
}
else
{
compYearResult = -1;
}

if(compStrResult == 0 )
{
if(compYearResult == 1)
{
return 1;
}
else if(compYearResult == -1)
{
return -1;
}
else
{
return compStrResult;
}
}
else if(compStrResult == 1)
{
return 1;
}
else
{
return -1;
}
}

int insertRecordInAscendingOrder(Record **head, char *make, int year, int stock)
{
Record *previous = *head;
Record *newRecord = createRecord(make, year, stock);
Record *current = *head;
int compResult;

if(*head == NULL)
{
*head = newRecord;
printf("Head is null, list was empty\n");
printList(*head);
return 1;
}
else if ( compareCars(newRecord, *head)==-1)
{
*head = newRecord;
(*head)->next = current;
printf("New record was less than the head, replacing\n");
printList(*head);
return 1;
}
else
{
printf("standard case, searching and inserting\n");
previous = *head;

while ( current != NULL &&(compareCars(newRecord, current)==1))
{
printList(*head);
previous = current;
current = current->next;
}

printList(*head);
previous->next = newRecord;
previous->next->next = current;
}
return 1;
}

/*creates records from info passed in from main via insertRecordInAscendingOrder.*/
Record* createRecord(char *make, int year, int stock)
{
printf("CreateRecord\n");
Record *theRecord;
int len;

if(!make)
{
return NULL;
}

theRecord = malloc(sizeof(Record));

if(!theRecord)
{
printf("Unable to allocate memory for the structure.\n");
return NULL;
}

theRecord->year = year;
theRecord->stock = stock;

len = strlen(make);
theRecord->make = malloc(len + 1);
strncpy(theRecord->make, make, len);
theRecord->make[len] = '\0';
theRecord->next=NULL;

return theRecord;
}

/*prints list. lists print.*/
void printList(Record *head)
{
int i;
int j = 50;
Record *aRecord;
aRecord = head;

for(i = 0; i < j; i++)
{
printf("-");
}
printf("\n");
printf("%20s%20s%10s\n", "Make", "Year", "Stock");

for(i = 0; i < j; i++)
{
printf("-");
}
printf("\n");

while(aRecord != NULL)
{
printf("%20s%20d%10d\n", aRecord->make, aRecord->year,
aRecord->stock);
aRecord = aRecord->next;
}
printf("\n");
}

命令行参数所需的文本文件可以以您喜欢的任何名称保存;以下是您需要的内容:

    Ford 2012 15
Ford 2011 3
Ford 2010 5
Toyota 2011 7
Toyota 2012 20
Toyota 2009 2
Honda 2011 9
Honda 2012 3
Honda 2013 12
Chevrolet 2013 10
Chevrolet 2012 25

在此先感谢您的帮助。我将继续亲自努力。

最佳答案

问题是

else if(compStrResult == 1)

您检查 strcmp 的特定返回值。规范仅说明在各种情况下结果是正数、负数或零,而且通常是第一个不同字符之间的差异。你应该检查

if (compStrResult > 0)

那里。

关于c - 按升序插入记录函数 - C 作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12876927/

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