繁体   English   中英

如何使用qsort在顶点高度之后对抛物线结构进行排序?

[英]How to sort an struct of parabolas after their vertex height, using qsort?

我遇到了一个问题,我必须首先创建一个 function 来计算给定抛物线struct的顶点高度,并为抛物线返回0 ,否则返回1 然后使用qsort对它们进行排序,使用顶点高度作为上升的参数。 此外,如果a == 0 (不是抛物线),它们将在抛物线之后排序。 然后我必须使用测试主来打印示例抛物线并查看它们是否正确排序。

我想我得到了顶点高度的计算,但我不知道我必须在qsort的 function 中写入什么。 我对编程有点陌生,还没有完全了解 c 中的指针概念。

给定的header.h

#ifndef header
#define header 1

struct parabola {
    double a;
    double b;
    double c;
};

int vertexheight(struct parabola *p, double *y);
void sort_parabola(struct parabola *p, int n);

#endif

我的程序

#include <stdio.h>
#include <stdlib.h>
#include "header.h"

int compare();

int vertexheight(struct parabola *p, double *y) {
    int rc = 0;
        
    if (p->a == 0) {
        rc = 1;
    } else {
        *y = p->c - ((p->b * p->b) / (4 * p->a));
    }
      
    return rc;
}

int compare(const void *a, const void *b) {

    //?

    return 0;
}

void sort_parabola(struct parabola *p, int n) {
    qsort(p, n, sizeof(struct parabola), compare);  
}

int main() {
    struct parabola p[] = {
        {1,2,3},
        {2,5,-19}, {0,-100,-56}, {-967,24,-24}, {36,2,70},
        {5,72,0}, {75,-4,55}, {20,41,7},
        {-1,0,0}
    };
    double y;
    int i, size = sizeof(p) / sizeof(struct parabola);
    
    sort_parabola(p, sizeof(p) / sizeof(struct parabola)); 
 
    for (i = 0; i < size; i++) {
        
        //output
        
    }
    return 0;
}

如果有人可以帮助我,非常感谢。

编辑:

int compare(const void *vp1, const void *vp2) {
    
    const struct parabola *p1 = (const struct parabola *)vp1;
    const struct parabola *p2 = (const struct parabola *)vp2;
    double h1, h2;
    int rc1, rc2;
    rc1 = vertexheight(p1, &h1);
    rc2 = vertexheight(p2, &h2);
    
    if (h1 < h2) return -1;
    if (h1 > h2) return 1;
    if (h1 == h2) return 0;
    if (vertexheight((struct parabola*)p1, &h1) == 1)
        return 1; //here I try to sort parabolas with a == 0 at the end of the list, because they have no vertex, doesn't work yet
    
    return 0;   
}

编辑2:

我认为我的 function 现在可以按预期工作

int compare(const void *vp1, const void *vp2) { 
    double h1, h2;
    
    struct parabola *p1 = (struct parabola *)vp1;
    struct parabola *p2 = (struct parabola *)vp2;
    
    vertexheight(p1, &h1);
    vertexheight(p2, &h2);
    
    if (vertexheight(p1, &h1) == 0 && vertexheight(p2, &h2) == 0){
    
        if (h1 < h2) return -1;
        if (h1 > h2) return 1;
        if (h1 == h2) return 0; 
    }
    
    else if (vertexheight(p1, &h1) != 0 && vertexheight(p2, &h2) == 0) {
        
        return 1;       
    }
    
    else if (vertexheight(p1, &h1) == 0 && vertexheight(p2, &h2) != 0) {
        
        return -1;
    }
    else {
        return 0;
    }
        return 0;
}

void sort_parabola(struct parabola *p, int n) {
    qsort(p, n, sizeof(struct parabola), compare);
}

arguments 与您的compare function 是指向您需要比较的两个对象(此处为struct parabola )的指针; 您只需要投射它们就可以使用它们。 创建一些适当类型的临时指针变量,然后使用它们访问对象通常很方便,因此只需编写一次强制转换。 我将 arguments 的名称从a,b更改为p1, p2以避免与structa,b,c成员混淆。

int compare(const void * vp1, const void * vp2) {
    const struct parabola * p1 = (const struct parabola *)vp1;
    const struct parabola * p2 = (const struct parabola *)vp2;
    double h1, h2;
    int rc1, rc2;
    rc1 = vertexheight(p1, &h1); // now h1 contains the vertexheight of the first parabola
    rc2 = vertexheight(p2, &h2);
    // deal with these as you wish
    // return -1, 0, 1 as appropriate
}

作为旁注,不要使用像int compare();这样的声明。 未指定参数类型。 始终使用完整原型: int compare(const void *, const void *); 同样,在定义main时,您应该编写int main(void) {而不是int main() {

暂无
暂无

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

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