Head First C ―頭とからだで覚えるCの基本
(オライリージャパン)
David Griffiths (著) Dawn Griffiths (著)
中田 秀基(監訳)(翻訳) 木下 哲也 (翻訳)
開発環境
- OS X Mavericks - Apple、たまにFreeBSD 10(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang/LLVM (コンパイラ)
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)
sample329.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 b - a;
}
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* s_a = *((char**)a);
char* s_b = *((char**)b);
return strcmp(s_a, s_b);
}
int compare_areas_desc(const void*a, const void* b)
{
return compare_areas(b, a);
}
int compare_names_desc(const void* a, const void* b)
{
return compare_names(b, a);
}
int main(int argc, char *argv[])
{
int nums[] = {5, 1, 4, 2, 3};
rectangle r1 = {1, 5};
rectangle r2 = {1, 1};
rectangle r3 = {1, 4};
rectangle r4 = {1, 2};
rectangle r5 = {1, 3};
rectangle rs[] = {r1, r2, r3, r4, r5};
char* strs[] = {"Abc", "Def"};
int i;
qsort(nums, 5, sizeof(int), compare_scores);
for (i = 0; i < 5; i++)
printf("%i ", nums[i]);
puts("");
qsort(nums, 5, sizeof(int), compare_scores_desc);
for (i = 0; i < 5; i++)
printf("%i ", nums[i]);
puts("");
qsort(rs, 5, sizeof(rectangle), compare_areas);
for (i = 0; i < 5; i++)
printf("%ix%i ", rs[i].width, rs[i].height);
puts("");
qsort(strs, 2, sizeof(char*), compare_names);
for (i = 0; i < 2; i++)
printf("%s ", strs[i]);
puts("");
qsort(rs, 5, sizeof(rectangle), compare_areas_desc);
for (i = 0; i < 5; i++)
printf("%ix%i ", rs[i].width, rs[i].height);
puts("");
qsort(strs, 2, sizeof(char*), compare_names_desc);
for (i = 0; i < 2; i++)
printf("%s ", strs[i]);
puts("");
return (0);
}
Makefile
CC=cc CFLAGS=-g -Wall SRC=sample329.c OBJ=sample329.o all: sample329 sample329: sample329.o $(CC) $(CFLAGS) $(OBJ) -o sample329 sample329.o: sample329.c $(CC) $(CFLAGS) -c sample329.c -o sample329.o clear: rm -rf sample329 $(OBJ)
入出力結果(Terminal)
$ make && ./sample329 cc -g -Wall -c sample329.c -o sample329.o cc -g -Wall sample329.o -o sample329 1 2 3 4 5 5 4 3 2 1 1x1 1x2 1x3 1x4 1x5 Abc Def 1x5 1x4 1x3 1x2 1x1 Def Abc $
0 コメント:
コメントを投稿