堆排序 - 数组结构 - C



我正在尝试堆排序(按字母顺序排序(从 txt 文件中读取的字符数组结构。我的算法适用于整数,但是当我更改为"char"类型时,它不会显示任何结果或任何错误

因此,我真的不知道我的代码出了什么问题。请帮忙,我是新手。

我的 txt 文件

iPhone_XR             128              6.1               599
Galaxy_s20            256              5.8               599
oppo_find_x           128              4.7               429
iPhone_SE             128              4.0               349

我的代码

#include <stdio.h>
struct phone
{
char a[100];
char b[100];
char c[100];
char d[100];
};
struct phone array[100];
void swap(char *e, char *f) 
{
char temp = *e;
*e = *f;
*f = temp;
}
void heapify(char arr[], int n, int i) 
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;

if (largest != i) 
{
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}

void heapSort(char arr[], int size) 
{
for (int i = size / 2 - 1; i >= 0; i--)
heapify(arr, size, i);
for (int i = size - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
int main(void)
{
int size;
char ch;
int count = 0;
char A[1000];
FILE *myfile = fopen("phonedb.txt", "r");
if (myfile == NULL) {
printf("Cannot open file.n");
return 1;
}
else {
do //count lines
{
ch = fgetc(myfile);
if (ch == 'n') count++;
} while (ch != EOF);
rewind(myfile);
// scan all the line inside the text
int i;
for (i = 0; i < count; i++) {
fscanf(myfile, "%s %s %s %sn", array[i].a, array[i].b, array[i].c, array[i].d);
printf("%s %s %s %sn", array[i].a, array[i].b, array[i].c, array[i].d);
}
}
heapSort(A, count);
printf("nYour sorted listn");
for (int i=0; i<size; i++)
{
printf("%sn", array[i].a);
}
return 0;
}

你的程序有很多错误和一些不必要的字符数组使用。 但是,由于您使用字符串的电话名称对数据进行排序, 直接比较不起作用,因为它不是原始数据类型。您需要使用库中<string.h>库函数strcmp()。 这是我的工作代码。

#include<stdio.h>
#include<string.h>
struct phone
{
char phoneName[100];
int second,fourth;
float third;
};
struct phone array[100];
void swap(struct phone *e, struct phone *f)
{
struct phone temp = *e;
*e = *f;
*f = temp;
}

void heapify(struct phone  arr[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && strcmp(arr[left].phoneName, arr[largest].phoneName)>0)
largest = left;

if (right < n && strcmp(arr[right].phoneName, arr[largest].phoneName)>0)
largest = right;


if (largest != i)
{
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}


void heapSort(struct phone arr[], int size)
{
for (int i = size / 2 - 1; i >= 0; i--)
heapify(arr, size, i);
for (int i = size - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}

int main()
{
int size;
char ch;
int count = 0;
FILE *myfile = fopen("phonedb.txt", "r");
if (myfile == NULL) {
printf("Cannot open file.n");
return 1;
}
else {
do //count lines
{
ch = fgetc(myfile);
if (ch == 'n') count++;
} while (ch != EOF);
rewind(myfile);
// scan all the line inside the text
int i;
for (i = 0; i < count; i++) { // using floating point with 2 precision.
fscanf(myfile, "%s %d %f %dn", array[i].phoneName, &array[i].second, &array[i].third, &array[i].fourth);
printf("%s %d %0.2f %dn", array[i].phoneName, array[i].second, array[i].third, array[i].fourth);
}
}
heapSort(array, count);
printf("nYour sorted listn");
for (int i=0; i<count; i++)
{
printf("%s %d %0.3f %dn", array[i].phoneName,array[i].second,array[i].third,array[i].fourth);
}
return 0;
}

这是输出:

iPhone_XR 128 6.10 599
Galaxy_s20 256 5.80 599
oppo_find_x 128 4.70 429
iPhone_SE 128 4.00 349
Your sorted list
Galaxy_s20 256 5.80 599
iPhone_SE 128 4.00 349
iPhone_XR 128 6.10 599
oppo_find_x 128 4.70 429

相关内容

  • 没有找到相关文章

最新更新