欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

第 15 章 位操作(fields)

程序员文章站 2023-12-22 21:31:46
1 /* 2 fields.c -- 定义并使用字段 3 */ 4 5 #include 6 7 //线的样式 8 #define SOLID 0 9 #define DOTTED 1 10 #define DASHED 2 11 12 //三原色 13 #define BLUE ......
第 15 章 位操作(fields)
 1 /*-----------------------------------
 2     fields.c -- 定义并使用字段
 3 -----------------------------------*/
 4 
 5 #include <stdio.h>
 6 
 7 //线的样式
 8 #define SOLID    0
 9 #define DOTTED    1
10 #define DASHED    2
11 
12 //三原色
13 #define BLUE    4
14 #define GREEN    2
15 #define RED        1
16 
17 //混合色
18 #define BLACK    (BLUE & GREEN & RED)
19 #define YELLOW    (RED | GREEN)
20 #define MAGENTA    (RED | BLUE)
21 #define CYAN    (GREEN | BLUE)
22 #define WHITE    (RED | GREEN | BLUE)
23 
24 const char *colors[] =
25 {
26     "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"
27 };
28 
29 struct box_props
30 {
31     unsigned int opaque : 1;
32     unsigned int fill_color : 3;
33     unsigned int : 4;
34     unsigned int show_border : 1;
35     unsigned int border_color : 3;
36     unsigned int border_style : 2;
37     unsigned int : 2;
38 };
39 
40 void show_settings(const struct box_props *pb);
41 
42 int main()
43 {
44     //创建并初始化 box_props 结构
45     struct box_props box = {true, YELLOW, true, GREEN, DASHED};
46 
47     printf("Original box settings:\n");
48     show_settings(&box);
49 
50     box.opaque = false;
51     box.fill_color = WHITE;
52     box.border_color = MAGENTA;
53     box.border_style = SOLID;
54 
55     printf("\nModified box settings:\n");
56     show_settings(&box);
57 
58     return 0;
59 }
60 
61 void show_settings(const struct box_props *pb)
62 {
63     printf("Box is %s.\n", pb->opaque ? "opaque" : "transparent");
64 
65     printf("The fill color is %s.\n", colors[pb->fill_color]);
66 
67     printf("Border %s.\n", pb->show_border ? "shown" : "not shown");
68 
69     printf("The border color is %s.\n", colors[pb->border_color]);
70 
71     printf("The border style is ");
72     switch (pb->border_style)
73     {
74     case SOLID:
75         printf("solid.\n");
76         break;
77 
78     case DOTTED:
79         printf("dotted.\n");
80         break;
81 
82     case DASHED:
83         printf("dashed.\n");
84         break;
85 
86     default:
87         printf("unknown type.\n");
88         break;
89     }
90 }
fields.c

第 15 章 位操作(fields)

上一篇:

下一篇: