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 (コンパイラ)
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)
test_sample328.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sample328.h"
int scores[] = {543, 323, 32, 554, 11, 3, 112};
rectangle rects[] = {{9, 8}, {1, 2}, {7, 6}, {3, 4}, {5, 0}};
char* names[] = {"def", "abc", "Def", "Abc"};
void p_scores(int scores[], int len)
{
int i;
for (i = 0; i < len; ++i)
printf("%d ", scores[i]);
puts("");
}
void p_rectangles(rectangle rects[], int len)
{
int i;
for (i = 0; i < len; ++i)
printf("%d ", rects[i].width * rects[i].height);
puts("");
}
void p_names(char** names, int len)
{
int i;
for (i = 0; i < len; ++i)
printf("%s ", names[i]);
puts("");
}
int compare_scores_desc(const void* score_a, const void* score_b)
{
return compare_scores(score_b, score_a);
}
int compare_areas(const void* a, const void* b)
{
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)
{
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(b, a);
}
int compare_names_desc(const void* a, const void *b)
{
return compare_names(b, a);
}
int main(int argc, char *argv[])
{
puts("点数");
qsort(scores, 7, sizeof(int), compare_scores);
printf("昇順: ");
p_scores(scores, 7);
qsort(scores, 7, sizeof(int), compare_scores_desc);
printf("降順: ");
p_scores(scores, 7);
puts("長方形の面積");
qsort(rects, 5, sizeof(rectangle), compare_areas);
printf("昇順: ");
p_rectangles(rects, 5);
qsort(rects, 5, sizeof(rectangle), compare_areas_desc);
printf("降順: ");
p_rectangles(rects, 5);
puts("名前一覧");
qsort(names, 4, sizeof(char*), compare_names);
printf("昇順: ");
p_names(names, 4);
qsort(names, 4, sizeof(char*), compare_names_desc);
printf("降順: ");
p_names(names, 4);
return (0);
}
Makefile
CC=cc CFLAGS = -g -Wall SRC=sample321.c OBJ=test_sample328.o sample328.o all: test_sample328 test_sample328: $(OBJ) $(CC) $(CFLAGS) -o test_sample328 $(OBJ) test_sample328.o: sample328.h test_sample328.c $(CC) $(CFLAGS) -c test_sample328.c sample328.o: sample328.h sample328.c $(CC) $(CFLAGS) -c sample328.c clean: rm -rf test_sample328 test_sample328.o sample328.o
入出力結果(Terminal)
$ make && ./test_sample328 cc -g -Wall -c test_sample328.c cc -g -Wall -o test_sample328 test_sample328.o sample328.o 点数 昇順: 3 11 32 112 323 543 554 降順: 554 543 323 112 32 11 3 長方形の面積 昇順: 0 2 12 42 72 降順: 72 42 12 2 0 名前一覧 昇順: def abc Def Abc 降順: Abc Def abc def $
0 コメント:
コメントを投稿