C,
pasted
on May 25:
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct mycomplex {
double re;
double im;
};
int cmp(void const *a, void const *b) {
struct mycomplex const *x, *y;
x = a;
y = b;
if ((x->re * x->re + x->im * x->im) > (y->re * y->re + y->im * y->im))
return 1;
else
return -1;
}
int main() {
int n, i;
static struct mycomplex data[] = {
{ 9.7, 9.3 },
{ 3.1, 4.1 },
{ 3.3, 8.3 },
{ 2.3, 8.4 },
{ 6.2, 6.4 },
{ 5.9, 2.6 },
{ 5.3, 5.8 },
{ 2.7, 9.5 }
};
n = sizeof(data) / sizeof(data[0]);
qsort(data, n, sizeof(struct mycomplex), cmp);
for (i = 0; i < n; i++)
printf("(%.1f, %.1f) = %.3f\n", data[i].re, data[i].im, sqrt(data[i].re * data[i].re + data[i].im * data[i].im));
putchar('\n');
return 0;
}
/* end */
|
Output:
|
(3.1, 4.1) = 5.140
(5.9, 2.6) = 6.447
(5.3, 5.8) = 7.857
(2.3, 8.4) = 8.709
(6.2, 6.4) = 8.911
(3.3, 8.3) = 8.932
(2.7, 9.5) = 9.876
(9.7, 9.3) = 13.438
|
|