2014年3月3日月曜日

開発環境

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

その他参考書籍

実習 11-1

コード(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;
  void print_graphics();

  std::cout << "set bits-----------------------------------------\n";
  for (loc = 0; loc < X_SIZE; ++loc) {
    set_bit(loc, loc);
  }

  print_graphics();

  std::cout << "clear bits---------------------------------------\n";
  for (loc = 0; loc < X_SIZE; ++loc) {
    if (loc != 20) {
      clear_bit(loc, loc);
    }
  }

  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_1

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

clean:
 rm sample195_1

入出力結果(Terminal)

$ make && ./sample195_1
g++ -g -Wall -o sample195_1 sample195_1.cpp
set bits-----------------------------------------
X.......................................
.X......................................
..X.....................................
...X....................................
....X...................................
.....X..................................
......X.................................
.......X................................
........X...............................
.........X..............................
..........X.............................
...........X............................
............X...........................
.............X..........................
..............X.........................
...............X........................
................X.......................
.................X......................
..................X.....................
...................X....................
....................X...................
.....................X..................
......................X.................
.......................X................
........................X...............
.........................X..............
..........................X.............
...........................X............
............................X...........
.............................X..........
..............................X.........
...............................X........
................................X.......
.................................X......
..................................X.....
...................................X....
....................................X...
.....................................X..
......................................X.
.......................................X
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
clear bits---------------------------------------
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
....................X...................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
$

0 コメント:

コメントを投稿