Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- double a;
- double b;
- double div;
- } data_t;
- int data_compare(const void *p1, const void *p2) {
- data_t *q1 = (data_t *) p1;
- data_t *q2 = (data_t *) p2;
- if (q1->div < q2->div) return 1;
- else if (q1->div > q2->div) return -1;
- else if (q1->a < q2->a) return -1;
- else if (q1->a > q2->a) return 1;
- else if (q1->b < q2->b) return -1;
- else if (q1->b > q2->b) return 1;
- return 0;
- }
- int main(void) {
- double a[] = { 2., 8., 4., 5., 10. };
- double b[] = { 3., 5., 4., 7., 10. };
- int n = sizeof(a) / sizeof(double);
- data_t dat[n];
- for (int i = 0; i < n; i++) {
- dat[i].a = a[i];
- dat[i].b = b[i];
- dat[i].div = a[i] / b[i];
- }
- for (int i = 0; i < n; i++) {
- printf("%2d %3.1lf %3.1lf %5.2lf\n", i + 1, dat[i].a, dat[i].b, dat[i].div);
- }
- puts("");
- qsort(dat, n, sizeof(data_t), data_compare);
- for (int i = 0; i < n; i++) {
- printf("%2d %3.1lf %3.1lf %5.2lf\n", i + 1, dat[i].a, dat[i].b, dat[i].div);
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement