c primer plus 第十五章课后编程7题

#include <stdio.h>
#include <stdbool.h>
struct set_up {
unsigned int id : 8; //24-31 储存ID信息
unsigned int size : 8; //16-23位 字体大小
unsigned int : 2; //14 15位 空
unsigned int alignment : 2; //12、13位 对齐
unsigned int : 4; //8-11位 空
bool b : 1; //7位 加粗 1/0
unsigned int : 2; //5、6位 空
bool i : 1; //4位。 斜体1/0
unsigned int : 3; //1-3位。 空
bool u : 1; //0位。 U 1/0
};

union views /把数据看作结构 unsigned long 类型的变量/
{
struct set_up st_view;
unsigned long us_view;
};
unsigned long font_id(void);
unsigned long size_size(void);
unsigned short a_alignment(void);

int main(void)
{
union views font={{0,0,0,0,0,0}};
char choice;
char l_c_r[3][10]={“left”,“center”,“right”};
unsigned long clear_space=12288; //用来清空位
unsigned long biu_b=64; //用于切换控制B的位
unsigned long biu_i=16; //用于切换控制I的位
unsigned long biu_u=1; //用于切换控制U的位

puts("f)chane font     s)change size    a)change alignment");
puts("b)toggle bold    i)toggle italic  u)toggle underline");
puts("q)quit");
while(scanf("%c",&choice)==1 && choice!='q')
{
    while(getchar()!='\n')
        continue;
    switch(choice)
    {
        case 'f': font.us_view=font_id();
            break;
        case 's': font.us_view|=size_size();
            break;
        case 'a': font.us_view=font.us_view & ~clear_space;
                  font.us_view|=a_alignment();
            break;
        case 'b': font.us_view^=biu_b;
            break;
        case 'i': font.us_view^=biu_i;
            break;
        case 'u': font.us_view^=biu_u;
            break;
    }
    printf(" ID    SIZE  ALIGNMENT     B       I       U\n");
    printf("%3ld%8ld%11s",(font.us_view>>24)&255,
           (font.us_view>>16)&127,
           l_c_r[(font.us_view>>12)&3]);
    printf("%6s",(0==(font.us_view & biu_b))?"off":"on");
    printf("%8s",(0==(font.us_view & biu_i))?"off":"on");
    printf("%8s",(0==(font.us_view & biu_u))?"off":"on");
    printf("\n\n");
    puts("f)chane font     s)change size    a)change alignment");
    puts("b)toggle bold    i)toggle italic  u)toggle underline");
    puts("q)quit");
}
return 0;

}

unsigned long font_id(void)
{
unsigned long num=0;
printf(“请输入字体ID号(0–255)\n”);
scanf("%ld",&num);
num&=255;
num=num<<24;
getchar();
return num;
}

unsigned long size_size(void)
{
unsigned long num;

printf("enter font size(0--127):\n");
scanf("%ld",&num);
num&=127;
while(getchar()!='\n')
    continue;
num=num<<16;
return num;

}

unsigned short a_alignment(void)
{
unsigned short num=0;
char c;
printf(“Select alignment:\n”);
printf(“l)left c)center r)right\n”);
while(scanf("%c",&c)==1)
{
if(c!=‘l’ && c!=‘c’ && c!=‘r’)
{
printf(“Select alignment:\n”);
printf(“l)left c)center r)right\n”);
getchar();
continue;
}
else
break;
}
if(‘l’==c)
num=0;
else if(‘c’==c)
{
num=1;
num<<=12;
}
else
{
num=2;
num<<=12;
}
getchar();
return num;
}

发布了85 篇原创文章 · 获赞 1 · 访问量 1889

猜你喜欢

转载自blog.csdn.net/Tekkenwxp/article/details/103176059