Last active
August 29, 2015 14:06
-
-
Save ochaochaocha3/2fb783d93d699e9e03bf to your computer and use it in GitHub Desktop.
構造体中のメンバでソート
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define C_SIZE 3 | |
typedef enum { | |
X, | |
Y, | |
Z | |
} coordinate; | |
const char *C_NAME[C_SIZE] = {"x", "y", "z"}; | |
typedef struct { | |
char *name; | |
int x[C_SIZE]; | |
} point; | |
void | |
print_points(point points[], int length) | |
{ | |
point *p; | |
int i; | |
puts( | |
"Name x y z\n" | |
"----------------" | |
); | |
for (i = 0; i < length; i++) { | |
p = &points[i]; | |
printf("%-4s%4d%4d%4d\n", p->name, p->x[X], p->x[Y], p->x[Z]); | |
} | |
} | |
void | |
sort_points_by(coordinate c, point points[], int length) | |
{ | |
point temp; | |
int last = length - 1; | |
int i, j; | |
for (i = 0; i < last; i++) { | |
for (j = last; j > i; j--) { | |
if (points[j - 1].x[c] > points[j].x[c]) { | |
temp = points[j - 1]; | |
points[j - 1] = points[j]; | |
points[j] = temp; | |
} | |
} | |
} | |
} | |
int | |
main(void) | |
{ | |
#define N 5 | |
int i; | |
point points[N] = { | |
{"A", {1, 2, 3}}, | |
{"B", {4, 3, 1}}, | |
{"C", {2, 8, 5}}, | |
{"D", {6, 4, 8}}, | |
{"O", {0, 0, 0}} | |
}; | |
print_points(points, N); | |
for (i = 0; i < C_SIZE; i++) { | |
printf("\n[%s でソート]\n", C_NAME[i]); | |
sort_points_by(i, points, N); | |
print_points(points, N); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment