簡體   English   中英

C 程序未正確按升序對數組進行排序

[英]C program not sorting array in ascending order correctly

我有一個 C 程序,它讀取一個 20 xy 坐標的 CSV 文件。 元素首先放入一個鏈表,然后放入一個數組,其中每行的 x 和 y 坐標相乘。 乘法完成后,數組將按升序排序。 但是由於某種原因,在打印出數組時,這些值沒有正確排序並且似乎是隨機順序的。 我為此使用了 qsort。 我不確定為什么程序不能正確地對值進行排序。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <string.h>

#define MAX 200

typedef struct wake {
    double x, y;
    struct wake *next;
} data;

//typedef struct wake data;

void read_csv(data *head, data **tail);
void gridSearch();
void maxNode();
int num_elements(data *head);
int create_array(data *head, int num, double *array);
int compare_array(const void* a, const void* b);

int main(int argc, char *argv[]) {
    int num = 0;
    struct wake* head = NULL;
    struct wake** tail = &head;
    read_csv(head, tail);
    num = num_elements(head);
    //printf("%d\n", num);
    
    double *array = malloc(num * sizeof(double));
    create_array(head,num,array);
    
}

void read_csv(data *head, data **tail) {
    // Opens the CSV datafile
    FILE *fp = fopen("data4.csv", "r");
    
    fscanf(fp, "%*[^\n]\n");
    
    char buffer[MAX];
    while (fgets(buffer, MAX, fp)) {
        data *node = malloc(sizeof(data));
        node->x = atof(strtok(buffer, ","));
        node->y = atof(strtok(NULL, ","));
        node->next = NULL;
        *tail = node;
        tail = &node->next;
        
    }
}

int num_elements(data *head) {
    struct wake *temp;
    temp = head;
    int num = 0;
    while(temp!=NULL) {
        //printf("%lf,%lf\n", temp->x,temp->y);
        num++;
        
        temp = temp->next;
    }
    return num;
}

int compare_array(const void* a, const void* b) {
    int p1 = *((double*)a);
    int p2 = *((double*)b);
    
    if (p1 < p2) { 
        return -1;
    }
    else if (p1 > p2) { 
        return 1;
    }
    else {
        return 0;
    }
}

int create_array(data *head, int num, double *array) {
    struct wake *temp;
    temp = head;
    int i = 0;
    while(temp!=NULL) {
        //printf("%d: %lf %lf\n", i, temp->x, temp->y);
        array[i] = (temp->x)*(temp->y);
        i++;
        temp = temp->next;
    }
    qsort(array, num, sizeof(double), compare_array);
    for (i=0; i<num; i++) {
        printf("%d: %lf\n", i, array[i]);
    }
    
    return num;
}

這是輸入的 CSV 文件:

x,y
0,0.95351
0.000413,0.953579
0.001741,0.953692
0.002695,0.953709
0.002806,0.95362
0.002277,0.953444
0.001662,0.953219
0.001404,0.953006
0.001418,0.952866
0.001181,0.952824
0.000302,0.952881
-0.001057,0.953021
-0.002364,0.953184
-0.003202,0.95329
-0.003514,0.953341
-0.003451,0.95343
-0.003154,0.953606
-0.002697,0.953808
-0.002145,0.953952
-0.001569,0.95404

嘗試這個

int compare (const void * a, const void * b) {
   return ( *(double*)a - *(double*)b );
}

另外:這個話題在這里已經有了答案: qsort 不適用於雙數組

你在比較int

int compare_array(const void* a, const void* b) {
    double p1 = *((double*)a);
    double p2 = *((double*)b);
    
    if (p1 < p2) { 
        return -1;
    }
    else if (p1 > p2) { 
        return 1;
    }
    else {
        return 0;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM