/*広告*/
/*[C言語]サンプルプログラム集 基本選択法ソース3*/
#include <stdio.h> struct PLAYER{ const char* name; int num; }; struct PLAYER set_data(const char* , int); void my_sort2(struct PLAYER* , int); int main(){ struct PLAYER player[5]; int i; player[0] = set_data("田中", 5); player[1] = set_data("鈴木", 1); player[2] = set_data("佐藤", 3); player[3] = set_data("斎藤", 4); player[4] = set_data("渡辺", 2); for (i = 0; i < 5; i++){ printf("%s %d\n", player[i].name, player[i].num); } printf("\n"); my_sort2(player, sizeof(player) / sizeof(struct PLAYER)); for (i = 0; i < 5; i++){ printf("%s %d\n", player[i].name, player[i].num); } return 0; } struct PLAYER set_data(const char* name, int num){ struct PLAYER tmp; tmp.name = name; tmp.num = num; return tmp; } void my_sort2(struct PLAYER* ary, int size){ struct PLAYER tmp; int i, j; for (i = 0; i < size - 1; i++){ for (j = i + 1; j < size; j++){ if ((ary + i)->num >(ary + j)->num){ tmp = *(ary + i); *(ary + i) = *(ary + j); *(ary + j) = tmp; } } } }/*Copyright 2016 K.N/petitetech.com*/
/*広告*/