我遇到了一个问题,我必须首先创建一个函数来计算给定抛物线的struct的顶点高度,如果不是,则返回抛物线和1的0。然后使用qsort对它们进行排序,使用顶点高度作为上升的参数。同样,如果a == 0 (不是抛物线),这些将按抛物线排序。然后,我必须使用测试主打印示例抛物线,看看它们是否正确排序。
我想我已经得到了顶点高度的计算,但是我不知道我要在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,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:
我想我的功能现在可以正常工作了。
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);
}发布于 2021-01-10 16:19:19
compare函数的参数是指向需要比较的两个对象(这里是struct parabola)的指针;您只需要对它们进行转换,就可以这样使用它们。通常可以方便地创建一些适当类型的临时指针变量,然后使用它们访问对象,从而只需要编写一次强制转换。我将参数的名称从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() {。
https://stackoverflow.com/questions/65647646
复制相似问题