書籍
- C言語プログラミング ハーベイ M.ダイテル (著), ポール J.ダイテル (著), 小嶋 隆一 (翻訳)
- development environment
- Visual Stdio Code
- gcc 8.1.0
10.7: Using the program in Listing 10.9 (P.388), run a high-performance card shuffling and distribution simulation (shown in Listing 10.2 (P.374)).
source
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
typedef struct bitCard{
unsigned face : 4;
unsigned suit : 2;
unsigned color : 1;
}Card;
void fillDeck(Card *);
void shuffle(Card *);
void deal(Card *, char *[], char *[], char *[]);
int main()
{
Card deck[52];
char *face[] = {"Ace", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
"Jack", "Queen", "King"};
char *suit[] = {"Heart", "Diamond", "Club", "Spade"};
char *color[] = {"red", "black"};
// srand(time(NULL));
fillDeck(deck);
shuffle(deck);
deal(deck, face, suit, color);
getch();
return 0;
}
void fillDeck(Card *wDeck)
{
int i;
for (i = 0; i <=51; i++)
{
wDeck[i].face = i % 13;
wDeck[i].suit = i / 13;
wDeck[i].color = i / 26;
}
}
void shuffle(Card *wDeck)
{
int i, j;
Card tmp;
for (i = 0; i <= 51; i++)
{
j = rand() % 52;
tmp = wDeck[i];
wDeck[i] = wDeck[j];
wDeck[j] = tmp;
}
}
void deal(Card *wDeck, char *wFace[], char *wSuit[], char *wColor[])
{
int k1;
//int k2;
/*
for (k1 = 0, k2 = k1 + 26; k1 <= 25; k1++, k2++)
{
printf("%10s : %8s of %-8s\t",
wColor[wDeck[k1].color], wSuit[wDeck[k1].suit], wFace[wDeck[k1].face]);
printf("%10s : %8s of %-8s\n",
wColor[wDeck[k2].color], wSuit[wDeck[k2].suit], wFace[wDeck[k2].face]);
}
*/
for (k1 = 0; k1 <= 51; k1++)
{
printf("%6s : %8s of %-8s%c",
wColor[wDeck[k1].color], wSuit[wDeck[k1].suit], wFace[wDeck[k1].face], (k1 + 1) % 2 ? '\t' : '\n');
}
}
10.8: char c, short s, int i, long lをメンバとする共用体integerを作り値を格納、かつ、中身を表示
C言語
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
typedef union integer{
char c;
short s;
int i;
long l;
}Integer;
int main()
{
Integer a;
Integer b;
Integer c;
Integer d;
a.c = 'a';
printf("1\n");
printf("%c\n", a.c);
printf("%d\n", a.i);
printf("%ld\n", a.l);
printf("%hd\n", a.s);
b.s = (short)10;
printf("2\n");
printf("%c\n", b.c);
printf("%d\n", b.i);
printf("%ld\n", b.l);
printf("%hd\n", b.s);
c.i = 19;
printf("3\n");
printf("%c\n", c.c);
printf("%d\n", c.i);
printf("%ld\n", c.l);
printf("%hd\n", c.s);
d.l = (long)1000;
printf("4\n");
printf("%c\n", d.c);
printf("%d\n", d.i);
printf("%ld\n", d.l);
printf("%hd\n", d.s);
getch();
return 0;
}
10.9: float f, double d, long double lをメンバとする共用体floatingPointを作り値を格納、かつ、中身を表示
C言語
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
typedef union floatingPoint{
float f;
double d;
long double l;
}FloatingPoint;
int main()
{
FloatingPoint a;
FloatingPoint b;
FloatingPoint c;
a.f = 4.0f;
b.d = 5.0;
c.l = 5.1L;
printf("a\n");
printf("%f\n", a.f);
printf("%lf\n", a.d);
printf("%Lf\n", a.l);
printf("\nb\n");
printf("%f\n", b.f);
printf("%lf\n", b.d);
printf("%Lf\n", b.l);
printf("\nc\n");
printf("%f\n", c.f);
printf("%lf\n", c.d);
printf("%Lf\n", c.l);
getch();
return 0;
}
10.10: 整数型変数を4ビット右にシフト
C言語
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>
// ビット列を表示する関数
void printBitInt(int value)
{
for (int i = 31; i >= 0; i--) {
int bit = (value >> i) & 1;
printf("%d", bit);
if (i % 8 == 0 && i != 0) {
printf(" "); // 8ビットごとにスペース
}
}
}
// 右シフトする関数
int shiftRight(int value, int n)
{
return value >> n;
}
int main()
{
int data = 64;
printBitInt(data);
printf("\n");
data = shiftRight(data, 4);
printBitInt(data);
printf("\n");
getch();
return 0;
}
10.11: 4バイト整数マシンを使っている場合にリスト10.5(P.381)のプログラムを4バイト整数でも動くように改造
C言語
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>
int main()
{
unsigned x;
//printf("%d\n", sizeof(unsigned));
void displayBits(unsigned);
printf("符号なし整数を入力してください\n");
scanf("%u", &x);
displayBits(x);
getch();
return 0;
}
void displayBits(unsigned value)
{
unsigned c, displayMask = 1 << (sizeof(unsigned) * 8) - 1;
printf("%8u = ", value);
for (c = 1; c <= 32; c++)
{
putchar(value & displayMask ? '1' : '0');
value <<= 1;
if (c % 8 == 0)
{
putchar(' ');
}
}
putchar('\n');
}
10.12: 2つの整数型引数numberとpowを受け取って、number * 2^(pow)を計算する関数power2を実装(^はべき乗)
C言語
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int power2(int, int);
void printBitInt(int number)
{
int i;
int size = sizeof(int) * 8; // 通常32ビット
for (i = size - 1; i >= 0; i--) {
printf("%d", (number >> i) & 1);
if (i % 8 == 0) printf(" "); // 8ビットごとにスペース
}
}
int main()
{
int a = 4;
printf("%d\n", a);
printBitInt(a);
printf("\n\n");
a = power2(a, 1);
printf("%d\n", a);
printBitInt(a);
getch();
return 0;
}
int power2(int number, int power)
{
return number << power;
}
10.13: キーボードから2つの文字を入力し、それらを2バイトの整数型変数にパック
C言語
#include <stdio.h>
#include <conio.h>
void packCharacters(char, char, unsigned short *);
void printBitsChar(char);
void printBitsShort(unsigned short);
int main()
{
char first, second;
unsigned short pack;
printf("文字を2つ入力してください\n");
scanf(" %c %c", &first, &second); // 先頭に空白を入れて改行をスキップ
printBitsChar(first);
printf("\n");
printBitsChar(second);
printf("\n");
packCharacters(first, second, &pack);
return 0;
}
void packCharacters(char a, char b, unsigned short *p)
{
*p = a;
printBitsShort(*p);
printf("\n");
*p <<= 8;
printBitsShort(*p);
printf("\n");
*p |= b;
printBitsShort(*p);
printf("\n");
}
void printBitsChar(char c)
{
for (int i = 7; i >= 0; i--)
printf("%d", (c >> i) & 1);
}
void printBitsShort(unsigned short s)
{
for (int i = 15; i >= 0; i--)
printf("%d", (s >> i) & 1);
}
10.14: 10.13のunsigned整数を受け取って、そこから2つの文字をアンパック
C言語
#include <stdio.h>
void packCharacters(char, char, unsigned short *);
void unpackCharacters(unsigned short, char *, char *);
void printBitsShort(unsigned short);
void printBitsChar(char); // 追加
int main()
{
char first, second;
char a, b;
unsigned short pack;
printf("文字を2つ入力してください\n");
scanf(" %c %c", &first, &second); // 空白を追加してバッファ問題を回避
printBitsChar(first);
printf("\n");
printBitsChar(second);
printf("\n");
printf("pack\n");
packCharacters(first, second, &pack);
printf("unpack\n");
unpackCharacters(pack, &a, &b);
printf("%c %c\n", a, b);
getchar(); // getch() の代わり
return 0;
}
void packCharacters(char a, char b, unsigned short *p)
{
*p = a;
*p <<= 8;
*p |= b;
printBitsShort(*p);
printf("\n\n");
}
void unpackCharacters(unsigned short p, char *a, char *b)
{
*a = (p >> 8) & 0xFF;
*b = p & 0xFF;
}
void printBitsShort(unsigned short s)
{
for (int i = 15; i >= 0; i--)
printf("%d", (s >> i) & 1);
}
void printBitsChar(char c)
{
for (int i = 7; i >= 0; i--)
printf("%d", (c >> i) & 1);
}
10.15: 10.13のプログラムを4文字パックするように改造
C言語
#include <stdio.h>
void packCharacters(char, char, char, char, unsigned *);
void printBitsChar(char);
void printBitsInt(int);
int main()
{
char first, second, third, forth;
unsigned pack;
printf("文字を4つ入力してください\n");
scanf("%c %c %c %c", &first, &second, &third, &forth);
printf("1つ目\n");
printBitsChar(first);
printf("\n");
printf("2つ目\n");
printBitsChar(second);
printf("\n");
printf("3つ目\n");
printBitsChar(third);
printf("\n");
printf("4つ目\n");
printBitsChar(forth);
printf("\n\n");
packCharacters(first, second, third, forth, &pack);
getchar(); // getch() の代わり
return 0;
}
void packCharacters(char a, char b, char c, char d, unsigned *p)
{
printBitsInt(*p);
printf("\n");
*p = a;
printBitsInt(*p);
printf("\n");
*p <<= 8;
printBitsInt(*p);
printf("\n");
*p |= b;
printBitsInt(*p);
printf("\n");
*p <<= 8;
printBitsInt(*p);
printf("\n");
*p |= c;
printBitsInt(*p);
printf("\n");
*p <<= 8;
printBitsInt(*p);
printf("\n");
*p |= d;
printBitsInt(*p);
printf("\n");
}
void printBitsChar(char c)
{
for (int i = 7; i >= 0; i--)
printf("%d", (c >> i) & 1);
}
void printBitsInt(int number)
{
int i;
int size = sizeof(int) * 8; // 通常32ビット
for (i = size - 1; i >= 0; i--) {
printf("%d", (number >> i) & 1);
if (i % 8 == 0) printf(" "); // 8ビットごとにスペース
}
}
10.16: Modify the program in Exercise 10.14 to unpack 4 characters(The mask required to unpack each character is created by shifting the mask variable, which has a value of 255, left by 8 bits as many times as necessary)
source
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void packCharacters(char, char, char, char, unsigned *);
void unpackCharacters(unsigned, char *, char *, char *, char *);
void printBitsChar(char);
void printBitsInt(int);
int main()
{
char first, second, third, forth;
unsigned pack;
printf("please input four characters\n");
scanf("%c %c %c %c", &first, &second, &third, &forth);
printf("first\n");
printBitsChar(first);
printf("\n");
printf("second\n");
printBitsChar(second);
printf("\n");
printf("third\n");
printBitsChar(third);
printf("\n");
printf("fourth\n");
printBitsChar(forth);
printf("\n\n");
packCharacters(first, second, third, forth, &pack);
printf("Pack is complete\n");
printf("Unpack it\n");
unpackCharacters(pack, &first, &second, &third, &forth);
printf("%c %c %c %c\n", first, second, third, forth);
getch();
return 0;
}
void packCharacters(char a, char b, char c, char d, unsigned *p)
{
printBitsInt(*p);
printf("\n");
*p = a;
printBitsInt(*p);
printf("\n");
*p <<= 8;
printBitsInt(*p);
printf("\n");
*p |= b;
printBitsInt(*p);
printf("\n");
*p <<= 8;
printBitsInt(*p);
printf("\n");
*p |= c;
printBitsInt(*p);
printf("\n");
*p <<= 8;
printBitsInt(*p);
printf("\n");
*p |= d;
printBitsInt(*p);
printf("\n");
}
void unpackCharacters(unsigned pack, char *a, char *b, char *c, char *d)
{
unsigned mask = 255;
unsigned tmp;
printBitsInt(pack);
printf("\n");
tmp = pack & (mask << (3 * 8));
*a = tmp >> (3 * 8);
tmp = pack & (mask << (2 * 8));
*b = tmp >> (2 * 8);
tmp = pack & (mask << (1 * 8));
*c = tmp >> (1 * 8);
*d = pack & mask;
}
void printBitsChar(char c)
{
for (int i = 7; i >= 0; i--)
printf("%d", (c >> i) & 1);
}
void printBitsInt(int number)
{
int i;
int size = sizeof(int) * 8;
for (i = size - 1; i >= 0; i--) {
printf("%d", (number >> i) & 1);
if (i % 8 == 0) printf(" ");
}
}
reference
- a
- b
10.:
source
