繁体   English   中英

堆排序 - 数组结构 - C

[英]Heapsort - Struct of Array - C

我正在尝试对从 txt 文件中读取的 char 数组结构进行 Heapsort(按电话名称的字母顺序排序)。 我的算法适用于 integer 但是当我更改为 '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 %s\n", array[i].a, array[i].b, array[i].c, array[i].d);
            printf("%s %s %s %s\n", array[i].a, array[i].b, array[i].c, array[i].d);
        }
    }
    heapSort(A, count);
    printf("\nYour sorted list\n");
    for (int i=0; i<size; i++)
    {
        printf("%s\n", array[i].a);
    }
    return 0;
}

你的程序有很多错误和一些不必要的使用 char arrays。 但是,由于您使用的是字符串的电话名称对数据进行排序,因此直接比较将不起作用,因为它不是原始数据类型。 您需要使用<string.h>库中的库 function 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 %d\n", array[i].phoneName, &array[i].second, &array[i].third, &array[i].fourth);
            printf("%s %d %0.2f %d\n", array[i].phoneName, array[i].second, array[i].third, array[i].fourth);
        }
    }
    heapSort(array, count);
    printf("\nYour sorted list\n");
    for (int i=0; i<count; i++)
    {
        printf("%s %d %0.3f %d\n", array[i].phoneName,array[i].second,array[i].third,array[i].fourth);
    }
    return 0;
}

这是 output:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM