[英]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
以避免與struct
的a,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.