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

问题描述

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

我想我得到了顶点高度的计算,但我不知道我必须在 qsort函数中写什么。我对编程有点陌生,还不太了解 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,70},{5,72,0},{75,-4,55},{20,41,7},{-1,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:

我认为我的功能现在按预期工作

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,&h2) != 0) {
        
        return -1;
    }
    else {
        return 0;
    }
        return 0;
}

void sort_parabola(struct parabola *p,compare);
}

解决方法

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

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,1 as appropriate
}

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