開発環境
- OS: macOS High Sierra - Apple
- IDE(統合開発環境): Xcode - Apple
- プログラミング言語: C
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の7章(高度な関数 - 関数を最大限に活用する)、長いエクササイズ(p. 329)を取り組んでみる。
長いエクササイズ(p. 329)
// // main.c // sample1 // // Created by kamimura on 2018/02/14. // Copyright © 2018 kamimura. All rights reserved. // #include <stdio.h> #include <stdlib.h> #include <string.h> int scores[] = {543, 323, 32, 554, 11, 3, 112}; int scores_n = 7; void scores_display() { for (size_t i = 0; i < scores_n; i++) { printf("%i\n", scores[i]); } } typedef int (*fn_type)(const void *, const void*); int compare_scores(const void * score_a, const void * score_b) { puts(__func__); int a = *(int*)score_a; int b = *(int*)score_b; return a - b; } int compare_scores_desc(const void * score_a, const void * score_b) { puts(__func__); return compare_scores(score_b, score_a); } fn_type comps[] = {compare_scores, compare_scores_desc, NULL}; typedef struct { int width; int height; } Rectangle; int rects_n = 5; void rects_display(Rectangle rects[]) { for (size_t i = 0; i < rects_n; i++) { printf("%i x %i\n", rects[i].width, rects[i].height); } } int compare_areas(const void * a, const void * b) { puts(__func__); Rectangle *rect_a = (Rectangle*)a; Rectangle *rect_b = (Rectangle*)b; int area_a = rect_a->width * rect_a->height; int area_b = rect_b->width * rect_b->height; return area_a - area_b; } int compare_names(const void * a, const void * b) { puts(__func__); char **name_a = (char**)a; char **name_b = (char **)b; return strcmp(*name_a, *name_b); } int compare_area_desc(const void * a, const void * b) { puts(__func__); return compare_areas(b, a); } int compare_names_desc(const void *a, const void *b) { puts(__func__); return compare_names(b, a); } int names_n = 5; void names_display(char *names[]) { for (size_t i = 0; i < names_n; i++) { puts(names[i]); } } fn_type rect_comps[] = {compare_areas, compare_area_desc, NULL}; fn_type name_comps[] = {compare_names, compare_names_desc, NULL}; int main(int argc, const char * argv[]) { for (size_t i = 0; comps[i] != NULL; i++) { puts("before"); scores_display(); qsort(scores, scores_n, sizeof(int), comps[i]); puts("after"); scores_display(); } Rectangle rects[5]; int nums[5] = {5, 1, 4, 2, 3}; for (size_t i = 0; i < 5; i++) { rects[i] = (Rectangle){.width=1, .height=nums[i]}; } for (size_t i = 0; rect_comps[i] != NULL; i++) { puts("before"); rects_display(rects); qsort(rects, rects_n, sizeof(Rectangle), rect_comps[i]); puts("after"); rects_display(rects); } char *names[] = {"e", "a", "d", "b", "c"}; for (size_t i = 0; name_comps[i] != NULL; i++) { puts("before"); names_display(names); qsort(names, names_n, sizeof(char*), name_comps[i]); puts("after"); names_display(names); } return 0; }
入出力結果(Terminal)
before 543 323 32 554 11 3 112 compare_scores compare_scores compare_scores compare_scores compare_scores compare_scores compare_scores compare_scores compare_scores compare_scores compare_scores compare_scores compare_scores compare_scores compare_scores compare_scores compare_scores after 3 11 32 112 323 543 554 before 3 11 32 112 323 543 554 compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores compare_scores_desc compare_scores after 554 543 323 112 32 11 3 before 1 x 5 1 x 1 1 x 4 1 x 2 1 x 3 compare_areas compare_areas compare_areas compare_areas compare_areas compare_areas compare_areas compare_areas compare_areas after 1 x 1 1 x 2 1 x 3 1 x 4 1 x 5 before 1 x 1 1 x 2 1 x 3 1 x 4 1 x 5 compare_area_desc compare_areas compare_area_desc compare_areas compare_area_desc compare_areas compare_area_desc compare_areas compare_area_desc compare_areas compare_area_desc compare_areas compare_area_desc compare_areas compare_area_desc compare_areas compare_area_desc compare_areas compare_area_desc compare_areas after 1 x 5 1 x 4 1 x 3 1 x 2 1 x 1 before e a d b c compare_names compare_names compare_names compare_names compare_names compare_names compare_names compare_names compare_names after a b c d e before a b c d e compare_names_desc compare_names compare_names_desc compare_names compare_names_desc compare_names compare_names_desc compare_names compare_names_desc compare_names compare_names_desc compare_names compare_names_desc compare_names compare_names_desc compare_names compare_names_desc compare_names compare_names_desc compare_names after e d c b a Program ended with exit code: 0
0 コメント:
コメントを投稿