Head First C ―頭とからだで覚えるCの基本
(オライリージャパン)
David Griffiths (著) Dawn Griffiths (著)
中田 秀基(監訳)(翻訳) 木下 哲也 (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の7章(高度な関数)、長いエクササイズ(p.328)を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
- C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、 谷口 功 (翻訳)、 オライリージャパン)
長いエクササイズ(p.328)
コード(BBEdit, Emacs)
sample328.c
#include <stdio.h> #include <stdlib.h> #include <string.h> int compare_scores(const void* score_a, const void* score_b) { 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) { int a = *(int*)score_a; int b = *(int*)score_b; return -(a - b); } typedef struct { int width; int height; } rectangle; int compare_areas(const void* a, const void* b) { rectangle* rectangle_a = (rectangle*)a; rectangle* rectangle_b = (rectangle*)b; int area_a = rectangle_a->width * rectangle_a->height; int area_b = rectangle_b->width * rectangle_b->height; return area_a - area_b; } int compare_names(const void *a, const void *b) { char* name_a = *(char**)a; char* name_b = *(char**)b; return strcmp(name_a, name_b); } int compare_areas_desc(const void* a, const void* b) { return -compare_areas(a, b); } int compare_names_desc(const void *a, const void*b) { return -(compare_names(a, b)); } int main(int argc, char *argv[]) { int scores[] = {543, 323, 32, 554, 11, 3, 112}; int n1 = 7; rectangle rectangles[] = {{4, 5}, {1, 2}, {3, 4}, {2, 3}}; int n2 = 4; int width; int height; char *names[] = {"scheme", "c", "python", "haskell", "dart"}; int n3 = 5; int i; printf("数値配列\n"); for (i = 0; i < n1; ++i) { printf("%d ", scores[i]); } printf("\n"); printf("小さい順\n"); qsort(scores, n1, sizeof(int), compare_scores); for (i = 0; i < n1; ++i) { printf("%d ", scores[i]); } printf("\n大きい順\n"); qsort(scores, n1, sizeof(int), compare_scores_desc); for (i = 0; i < n1; ++i) { printf("%d ", scores[i]); } printf("\n長方形\n"); for (i = 0; i < n2; ++i) { width = rectangles[i].width; height = rectangles[i].height; printf("横:%d 縦:%d 面積:%d\n", width, height, width * height); } printf("面積の小さい順\n"); qsort(rectangles, n2, sizeof(rectangle), compare_areas); for (i = 0; i < n2; ++i) { width = rectangles[i].width; height = rectangles[i].height; printf("横:%d 縦:%d 面積:%d\n", width, height, width * height); } printf("面積の大きい順\n"); qsort(rectangles, n2, sizeof(rectangle), compare_areas_desc); for (i = 0; i < n2; ++i) { width = rectangles[i].width; height = rectangles[i].height; printf("横:%d 縦:%d 面積:%d\n", width, height, width * height); } printf("プログラミング言語名\n"); for (i = 0; i < n3; ++i) { printf("%s ", names[i]); } printf("\n大文字小文字を区別したアルファベット順(ASCIIコード順)\n"); qsort(names, n3, sizeof(char*), compare_names); for (i = 0; i < n3; ++i) { printf("%s ", names[i]); } printf("\n逆順\n"); qsort(names, n3, sizeof(char*), compare_names_desc); for (i = 0; i < n3; ++i) { printf("%s ", names[i]); } printf("\n"); return (0); }
Makefile
sample328: sample328.c cc -g -o sample328 sample328.c clean: rm sample328
入出力結果(Terminal)
$ make && ./sample328 cc -g -o sample328 sample328.c 数値配列 543 323 32 554 11 3 112 小さい順 3 11 32 112 323 543 554 大きい順 554 543 323 112 32 11 3 長方形 横:4 縦:5 面積:20 横:1 縦:2 面積:2 横:3 縦:4 面積:12 横:2 縦:3 面積:6 面積の小さい順 横:1 縦:2 面積:2 横:2 縦:3 面積:6 横:3 縦:4 面積:12 横:4 縦:5 面積:20 面積の大きい順 横:4 縦:5 面積:20 横:3 縦:4 面積:12 横:2 縦:3 面積:6 横:1 縦:2 面積:2 プログラミング言語名 scheme c python haskell dart 大文字小文字を区別したアルファベット順(ASCIIコード順) c dart haskell python scheme 逆順 scheme python haskell dart c $Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の7章(高度な関数)、長いエクササイズ(p.328)を解いてみる。
0 コメント:
コメントを投稿