2012年7月11日水曜日

開発環境

『実践プログラミング 第3版』 (Steve Oualline (著)、 望月 康司 (監修) (翻訳)、 谷口 功 (翻訳)、 オライリー・ジャパン、1998年、ISBN978-4900900646) II部(単純なプログラミング)の11章(ビット演算)11.10(プログラミング実習)実習11-2を解いてみる。

実習11-2.

コード(TextWrangler)

#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)))

int main(){
  int x;
  int y;
  int size = 10;
  void print_graphics(void);
  
  for (x = 0; x < size; ++x){
    for(y = 0; y < size; ++y){
      SET_BIT(x,y);
    }
  }
  
  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");
  }
}

入出力結果(Terminal)

$ cc -g -o sample sample.c
$ ./sample
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
XXXXXXXXXX..............................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
$

0 コメント:

コメントを投稿