開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅡ部(単純なプログラミング)の11章(ビット演算)、11.8(ビットマップグラフィックス)、11.10(プログラミング実習)、実習11-2を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
実習11-2.
コード
sample.c
#include <stdio.h> #define X_SIZE 40 #define Y_SIZE 60 char graphics[X_SIZE / 8][Y_SIZE]; #define SET_BIT(x, y) graphics[(x)/8][y] |= (0x80 >> ((x) % 8)) #define CLEAR_BIT(x, y) graphics[(x)/8][y] &= ~(0x80 >> ((x) % 8)) #define TEST_BIT(x, y) (graphics[(x)/8][y] & (0x80 >> ((x) % 8))) > 0 int main() { int loc1, loc2; void print_graphics(void); for (loc1 = 0; loc1 < 10; loc1++){ for (loc2 = 0; loc2 < 10; loc2++){ SET_BIT(loc1, loc2); } } print_graphics(); return (0); } void print_graphics(void) { int x; int y; unsigned int bit; for (y = 0; y < Y_SIZE; ++y){ for (x = 0; x < X_SIZE / 8; ++x){ for (bit = 0x80; bit > 0; bit = (bit >> 1)){ if ((graphics[x][y] & bit) != 0){ printf("X"); } else { printf("."); } } } printf("\n"); } }
makefile
CC=cc CFLAGS=-g sample: sample.c $(CC) $(CFLAGS) -o sample sample.c clean: rm -f sample
入出力結果(Terminal)
$ make cc -g -o sample sample.c $ ./sample XXXXXXXXXX.............................. XXXXXXXXXX.............................. XXXXXXXXXX.............................. XXXXXXXXXX.............................. XXXXXXXXXX.............................. XXXXXXXXXX.............................. XXXXXXXXXX.............................. XXXXXXXXXX.............................. XXXXXXXXXX.............................. XXXXXXXXXX.............................. ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ $
0 コメント:
コメントを投稿