//本博主所写的代码仅为阅读者提供参考;
//若有不足之处请提出,博主会尽其所能修改;
//附上课后编程练习题目;
//若是对您有用的话请点赞或分享提供给它人;
//-----------------------------------------------------------------------------
//15.9-1.c
#include <stdio.h>
#include <string.h>
int convert(const char *str);
int main(void)
{
const char *pbin = "01001001";
printf("binary %s is decimal %d\n", pbin, convert(pbin));
//书上有误, 01001001对应的不是25而是73
return 0;
}
int convert(const char *str)
{
int sum = 0, len = strlen(str);
for (int i = len - 1, exp = 1; i >= 0; --i, exp *= 2)
{
sum += (str[i] - '0') * exp;
}
return sum;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//15.9-2.c
#include <stdio.h>
#include <stdlib.h>
int bstr_to_dec(const char * str);
char *itobs(int n, char *str);
int main(int argc, char *argv[])
{
char bstr[8 * sizeof(int) + 1];
if (argc != 3)
{
fprintf(stderr, "Usage: %s binarynum1 binarynum2\n", argv[0]);
exit(EXIT_FAILURE);
}
int v1 = bstr_to_dec(argv[1]);
int v2 = bstr_to_dec(argv[2]);
printf("~%s = %s\n", argv[1], itobs(~v1, bstr));
printf("~%s = %s\n", argv[2], itobs(~v2, bstr));
printf("%s & %s = %s\n", argv[1], argv[2], itobs(v1 & v2, bstr));
printf("%s | %s = %s\n", argv[1], argv[2], itobs(v1 | v2, bstr));
printf("%s ^ %s = %s\n", argv[1], argv[2], itobs(v1 ^ v2, bstr));
return 0;
}
int bstr_to_dec(const char * str)
{
int val = 0;
while (*str)
{
val = val * 2 + (*str++) - '0';
}
return val;
}
char *itobs(int n, char *str)
{
int sz = 8 * sizeof(int);
for (int i = sz - 1; i >= 0; --i, n >>= 1)
{
str[i] = (n & 1) + '0';
}
str[sz] = '\0';
return str;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//15.9-3.c
#include <stdio.h>
#define TEST_NUM 13
char *itobs(int n, char *str);
int onbits(int x);
int main(int argc, char *argv[])
{
int x = TEST_NUM;
char bstr[8 * sizeof(int) + 1];
printf("%d(%s) has %d bit on.\n", x, itobs(x, bstr), onbits(x));
return 0;
}
char *itobs(int n, char *str)
{
int sz = 8 * sizeof(int);
for (int i = sz - 1; i >= 0; --i, n >>= 1)
{
str[i] = (n & 1) + '0';
}
str[sz] = '\0';
return str;
}
int onbits(int x)
{
int sum = 0;
for (int i = 8 * sizeof(int) - 1; i >= 0; --i)
{
sum += (x >> i) & 1;
}
return sum;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//15.9-4.c
#include <stdio.h>
#define TEST_NUM 13
int test_bit(int x, int pos);
char *itobs(int n, char *str);
int main(int argc, char *argv[])
{
int x = TEST_NUM;
int sz = 8 * sizeof(int);
char bstr[8 * sizeof(int) + 1];
printf("%d(%s):\n", x, itobs(x, bstr));
for (int i = sz - 1; i >= 0; --i)
{
printf("The position %d is %d\n", i + 1, test_bit(x, i));
}
return 0;
}
int test_bit(int x, int pos)
{
return (x >> pos) & 1;
}
char *itobs(int n, char *str)
{
int sz = 8 * sizeof(int);
for (int i = sz - 1; i >= 0; --i, n >>= 1)
{
str[i] = (n & 1) + '0';
}
str[sz] = '\0';
return str;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//15.9-5.c
#include <stdio.h>
#define TEST_NUM 13
#define ROTATE_BITS 4
char *itobs(int n, char *str);
unsigned int rotate_l(unsigned int n, unsigned int b);
int main(int argc, char *argv[])
{
unsigned int val = TEST_NUM;
unsigned int rot = rotate_l(val, ROTATE_BITS); //可更改宏定义改变反转的位;
char bstr1[8 * sizeof(int) + 1];
char bstr2[8 * sizeof(int) + 1];
printf("%u rotated is %u.\n", val, rot);
printf("%s rotated is %s.\n", itobs(val, bstr1), itobs(rot, bstr2));
return 0;
}
char *itobs(int n, char *str)
{
int sz = 8 * sizeof(int);
for (int i = sz - 1; i >= 0; --i, n >>= 1)
{
str[i] = (n & 1) + '0';
}
str[sz] = '\0';
return str;
}
unsigned int rotate_l(unsigned int n, unsigned int b)
{
static const int size = 8 * sizeof(int);
b %= size;
return (n << b) | (n >> (size - b));
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//15.9-6.c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
typedef unsigned int uint;
typedef struct
{
uint id : 8;
uint sz : 7;
uint at : 2;
uint b : 1;
uint i : 1;
uint u : 1;
} font;
static font ft = {1, 12, 0, 0, 0, 0};
const char *state[4] = {"off", "on"};
const char *alignment[7] = {"left", "center", "right"};
void eatline(void);
int get_first(void);
int get_choice(void);
void change_font(void);
void change_size(void);
void change_alignment(void);
void change_toggle(int ch);
int main(void)
{
int ch;
while ((ch = get_choice()) != 'q')
{
switch (ch)
{
case 'f':
{
change_font();
break;
}
case 's':
{
change_size();
break;
}
case 'a':
{
change_alignment();
break;
}
case 'b':
case 'i':
case 'u':
{
change_toggle(ch);
break;
}
}
putchar('\n');
}
printf("Bye!\n");
return 0;
}
void eatline(void)
{
while (getchar() != '\n')
continue;
}
int get_first(void)
{
int ch;
do
{
ch = getchar();
} while (isspace(ch));
eatline();
return ch;
}
int get_choice(void)
{
int ch;
printf("ID SIZE ALIGNMENT B I U\n");
printf("%-7u%-9u%-12s", ft.id, ft.sz, alignment[ft.at]);
printf("%-8s%-8s%-8s\n", state[ft.b], state[ft.i], state[ft.u]);
printf("f) change font s) change size a) change alignment\n");
printf("b) toggle bold i) toggle italic u) toggle underline\n");
printf("q) quit\n");
while (ch = get_first(), NULL == strchr("fsabiuq", ch))
{
printf("Please enter with f, s, a, b, i, u or q: ");
}
return ch;
}
void change_font(void)
{
int ch;
uint id;
printf("Enter font id (0-255): ");
while (scanf("%u", &id) != 1)
{
while ((ch = getchar()) != '\n')
{
putchar(ch);
}
printf(" is not a id.\n");
printf("Please enter a number such as 0, 5 or 255: ");
}
ft.id = id & 0XFF;
}
void change_size(void)
{
int ch;
uint sz;
printf("Enter font sz (0-127): ");
while (scanf("%u", &sz) != 1)
{
while ((ch = getchar()) != '\n')
{
putchar(ch);
}
printf(" is not a size.\n");
printf("Please enter a number such as 0, 5 or 127: ");
}
ft.sz = sz & 0x7F;
}
void change_alignment(void)
{
int ch;
printf("Select alignment:\n");
printf("l) left c) center r) right\n");
while (ch = get_first(), NULL == strchr("lcr", ch))
{
printf("Please enter with l, c or r: ");
}
ft.at = (ch == 'l' ? 0 : ch == 'c' ? 1 : 2);
}
void change_toggle(int ch)
{
if (ch == 'b')
{
ft.b ^= 1;
}
else if (ch == 'i')
{
ft.i ^= 1;
}
else
{
ft.u ^= 1;
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//15.9-7.c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
typedef unsigned long ulong;
static ulong ft = 0x00001180; //题目初始状态;
//从右往左数, 第1位表示U, 第2位表示I, 第3位表示B, 第4至5位表示ALIGNMENT, 第6至第12位表示SIZE, 第13至20位表示ID
const char *state[4] = {"off", "on"};
const char *alignment[7] = {"left", "center", "right"};
void eatline(void);
int get_first(void);
int get_choice(void);
void change_font(void);
void change_size(void);
void change_alignment(void);
void change_toggle(int ch);
int main(void)
{
int ch;
while ((ch = get_choice()) != 'q')
{
switch (ch)
{
case 'f':
{
change_font();
break;
}
case 's':
{
change_size();
break;
}
case 'a':
{
change_alignment();
break;
}
case 'b':
case 'i':
case 'u':
{
change_toggle(ch);
break;
}
}
putchar('\n');
}
printf("Bye!\n");
return 0;
}
void eatline(void)
{
while (getchar() != '\n')
continue;
}
int get_first(void)
{
int ch;
do
{
ch = getchar();
} while (isspace(ch));
eatline();
return ch;
}
int get_choice(void)
{
int ch;
printf("ID SIZE ALIGNMENT B I U\n");
printf("%-7u%-9u%-12s", (ft >> 12) & 0XFF, (ft >> 5) & 0x7F, alignment[(ft >> 3) & 0x03]);
printf("%-8s%-8s%-8s\n", state[(ft >> 2) & 1], state[(ft >> 1) & 1], state[ft & 1]);
printf("f) change font s) change size a) change alignment\n");
printf("b) toggle bold i) toggle italic u) toggle underline\n");
printf("q) quit\n");
while (ch = get_first(), NULL == strchr("fsabiuq", ch))
{
printf("Please enter with f, s, a, b, i, u or q: ");
}
return ch;
}
void change_font(void)
{
int ch;
ulong id;
printf("Enter font id (0-255): ");
while (scanf("%lu", &id) != 1)
{
while ((ch = getchar()) != '\n')
{
putchar(ch);
}
printf(" is not a id.\n");
printf("Please enter a number such as 0, 5 or 255: ");
}
id &= 0XFF, id <<= 12;
for (int i = 12; i < 20; ++i)
{
ft &= ~(ulong)(1 << i);
}
ft |= id;
}
void change_size(void)
{
int ch;
ulong sz;
printf("Enter font sz (0-127): ");
while (scanf("%lu", &sz) != 1)
{
while ((ch = getchar()) != '\n')
{
putchar(ch);
}
printf(" is not a size.\n");
printf("Please enter a number such as 0, 5 or 127: ");
}
sz &= 0X7F, sz <<= 5;
for (int i = 5; i < 12; ++i)
{
ft &= ~(ulong)(1 << i);
}
ft |= sz;
}
void change_alignment(void)
{
int ch;
printf("Select alignment:\n");
printf("l) left c) center r) right\n");
while (ch = get_first(), NULL == strchr("lcr", ch))
{
printf("Please enter with l, c or r: ");
}
ft &= ~(ulong)(1 << 3), ft &= ~(ulong)(1 << 4);
ft = ft | (ch == 'c' ? (ulong)(1 << 3) : ch == 'r' ? (ulong)(1 << 4) : 0);
}
void change_toggle(int ch)
{
if (ch == 'b')
{
if (ft & 0x04)
{
ft &= ~(ulong)(0x04);
}
else
{
ft |= (ulong)(0x04);
}
}
else if (ch == 'i')
{
if (ft & 0x02)
{
ft &= ~(ulong)(0x02);
}
else
{
ft |= (ulong)(0x02);
}
}
else
{
if (ft & 0x01)
{
ft &= ~(ulong)(0x01);
}
else
{
ft |= (ulong)(0x01);
}
}
}
//-----------------------------------------------------------------------------
//---------------------------2022年12月30日----------------------------