gpt4 book ai didi

c - C 中数据结构的排序数组

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

我的程序应该读取一个文件,存储该文件的记录,然后对每个记录进行排序。我正在使用插入排序算法对记录进行排序,但在使其工作时遇到一些问题。

例如,如果 theext 文件看起来像这样

string one; string one; string one; 1
string two; string two; string two; 2
string three; stringh three; string three; 3

我需要将第三条记录作为第一条记录,将第二条记录作为第二条记录,将第一条记录作为最后一条记录。由于 3>2>1。

我认为插入排序算法是正确的,因为我用一个简单的数组测试了它并且它有效,但我很难将它实现到我的程序中。我得到的错误是insertionSort’从整数中生成指针而不进行强制转换,我认为这是因为我正在使用数据结构来存储从文件扫描的数据。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100

struct Element
{
char one[100];
char two[100];
char three[100];
int st;
};

void insertionSort(int arr[]);
void printArray(int arr[]);


int main() {


int i;
struct Element elements[MAXLEN];


FILE * fpointer = fopen("clients.txt", "r");

char buffer[1024]; // Define a really big string to hold the line of text in
char *field;
int field_number;


while(fgets(buffer,1024,fpointer))
{
field_number=0;
field=strtok(buffer,";");
while(field)
{
switch(field_number)
{
case 0:
strcpy(elements[i].one,field);
break;
case 1:
strcpy(elements[i].two,field);
break;
case 2:
strcpy(elements[i].three,field);
break;
case 3:
elements[i].st=atoi(field);
break;
}
field=strtok(NULL,";"); // Get next field
field_number++;
}
i++; // Move the index for elements to the next one
}


insertionSort(elements[MAXLEN].st);
printArray(elements[MAXLEN].st);

fclose(fpointer);
return 0;

}


void insertionSort(int arr[])
{
struct Element elements[MAXLEN];
int i, key, j;
for (i = 1; i < 10; i++)
{
key = elements[i].st;
j = i-1;


while (j >= 0 && elements[j].st > key)
{
elements[j+1].st = elements[j].st;
j = j-1;
}
elements[j+1].st = key;
}

}


void printArray(int arr[])
{
int i;
for (i=0; i < 10; i++) {
printf("%d ", arr[i]);
printf("\n");
}
}

最佳答案

您的代码中存在以下问题。

您已声明 void insertionSort(int arr[]); and void printArray(int arr[]);需要 pointer 的函数至int array作为参数,但你正在传递 insertionSort(elements[MAXLEN].st); & printArray(elements[MAXLEN].st); 只是int作为参数

还有insertionSort将对 st 进行排序struct Element elements[MAXLEN];
的字段但不是elements本身和insertionSort将以升序对数组进行排序,而不是按照您想要的降序排列。

因此更改此

   while (j >= 0 && elements[j].st > key)

   while (j >= 0 && elements[j].st < key.st)

以下面的代码为例。

    #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100

struct Element
{
char one[100];
char two[100];
char three[100];
int st;
};

void insertionSort(struct Element elements[]);
void printArray(struct Element elements[]);


int main() {


int i;
struct Element elements[MAXLEN];


FILE * fpointer = fopen("clients.txt", "r");

char buffer[1024]; // Define a really big string to hold the line of text in
char *field;
int field_number;


while(fgets(buffer,1024,fpointer))
{
field_number=0;
field=strtok(buffer,";");
while(field)
{
switch(field_number)
{
case 0:
strcpy(elements[i].one,field);
break;
case 1:
strcpy(elements[i].two,field);
break;
case 2:
strcpy(elements[i].three,field);
break;
case 3:
elements[i].st=atoi(field);
break;
}
field=strtok(NULL,";"); // Get next field
field_number++;
}
i++; // Move the index for elements to the next one
}


insertionSort(elements);
printArray(elements);

fclose(fpointer);
return 0;

}


void insertionSort(struct Element elements[])
{
int i, j;
struct Element key;
for (i = 1; i < 10; i++)
{
key = elements[i];
j = i-1;


while (j >= 0 && elements[j].st < key.st)
{
elements[j+1] = elements[j];
j = j-1;
}
elements[j+1] = key;
}

}


void printArray(struct Element elements[])
{
int i;
for (i=0; i < 3; i++) {
printf("%s %s %s%d ", elements[i].one,elements[i].two,elements[i].three,elements[i].st);
printf("\n");
}
}

关于c - C 中数据结构的排序数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52118870/

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