设为首页 加入收藏

TOP

第 15 章 位操作(fields)
2018-10-21 14:13:45 】 浏览:33
Tags:操作 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 章 位操作(binbit) 下一篇开发方式-----C语言

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目