2014年3月4日火曜日

開発環境

C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅡ部(シンプルなプログラミング)の11章(ビット演算)、11.8(ビットマップグラフィックス)、実習 11-2を解いてみる。

その他参考書籍

実習 11-2

コード(BBEdit, Emacs)

sample.cpp

#include ≪iostream>
#include ≪assert.h>

const int X_SIZE = 40;
const int Y_SIZE = 60;

char graphics[X_SIZE / 8][Y_SIZE];

inline void set_bit(const int x, const int y)
{
  assert((x >= 0) && (x ≪ X_SIZE));
  assert((y >= 0) && (y ≪ Y_SIZE));
  graphics[(x)/8][y] |= static_cast≪char>(0x80 >> ((x)%8));
}

inline void clear_bit(const int x, const int y)
{
  assert((x >= 0) && (x ≪ X_SIZE));
  assert((y >= 0) && (y ≪ Y_SIZE));
  graphics[(x)/8][y] ^= static_cast≪char>(0x80 >> ((x)%8));
}

int main()
{
  int loc_x, loc_y;
  void print_graphics();

  for (loc_x = 0; loc_x ≪ 10; ++loc_x) {
    for (loc_y = 0; loc_y ≪ 10; ++loc_y) {
      set_bit(loc_x, loc_y);
    }
  }

  std::cout ≪≪ "10×10の正方形のビットマップ\n";
  print_graphics();

  return (0);
}

void print_graphics()
{
  int x;
  int y;
  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)) {
        assert((x >= 0) && (x ≪ (X_SIZE / 8)));
        assert((y >= 0) && (y ≪ Y_SIZE));

        if ((graphics[x][y] & bit) != 0) {
          std::cout ≪≪ "X";
        } else {
          std::cout ≪≪ ".";
        }
      }
    }
    std::cout ≪≪ '\n';
  }
}

Makefile

#
# FSFのg++コンパイラ用のMakefile
#
CC=g++
CFLAGS=-g -Wall
all: sample195_2

sample195_2: sample195_2.cpp
 ${CC} ${CFLAGS} -o sample195_2 sample195_2.cpp

clean:
 rm sample195_2

入出力結果(Terminal)

$ make && ./sample195_2
g++ -g -Wall -o sample195_2 sample195_2.cpp
10×10の正方形のビットマップ
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
$

0 コメント:

コメントを投稿