2012年10月11日木曜日

開発環境

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

実習11-1.

コード(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 loc;
  void print_graphics(void);
  int x, y;
  char line[100];
  
  for(loc = 0; loc < X_SIZE; ++loc){
    SET_BIT(loc, loc);
  }
  printf("5 × 5: ");
  if(TEST_BIT(5,5)){
    printf("true\n");
  } else {
    printf("false\n");
  }
  printf("5 × 6: ");
  if(TEST_BIT(5,6)){
    printf("true\n");
  } else {
    printf("false\n");
  }
  print_graphics();
  printf("5 × 5, 5 × 6をクリア\n");
  CLEAR_BIT(5,5);
  CLEAR_BIT(5,6);
  print_graphics();
  return (0);
}

void print_graphics(void){
  int x, 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
5 × 5: true
5 × 6: false
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
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
5 × 5, 5 × 6をクリア
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
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
$

0 コメント:

コメントを投稿