C Primer Plus(第六版)15.9 编程练习 第6题

//

// main.c

// 15.9-6

//

// Created by cjm on 2024/2/5.

//

#include <stdio.h>

#include <stdbool.h>

#include <string.h>

struct font_style{

unsigned int font_ID : 8;

unsigned int font_size : 7;

unsigned int alignment : 2;//如果这里是int类型,因位有一位是正负号,2位便表示不了2.

_Bool bold : 1;

_Bool italics : 1;

_Bool underline : 1;

};

void show_menu(struct font_style font_n);

int main()

{

char ch;

struct font_style font_n={1,12,1,0,0,0};

int id,size;

char alig;

show_menu(font_n);

while(scanf("%c",&ch)!=EOF&&ch!='q')

{

while(getchar()!='\n')

continue;

switch(ch)

{

case 'f':

puts("Enter font ID(0-255)");

scanf("%d", &id);

font_n.font_ID=id;

show_menu(font_n);

break;

case 's':puts("Enter font size(0-127)");

scanf("%d",&size);

font_n.font_size=size;

show_menu(font_n);

break;

case 'a':

puts("Select alignment:\nl) left\tc)center\tr)right\n");

scanf("%c",&alig);

while(getchar()!='\n')

continue;

if(alig=='l')

font_n.alignment=1;

else if(alig=='r')

font_n.alignment = 2;

else if(alig=='c')

font_n.alignment=0;

show_menu(font_n);

break;

case 'b':

font_n.bold=~(font_n.bold);

show_menu(font_n);

break;

case 'i':

font_n.italics=~(font_n.italics);

show_menu(font_n);

break;

case 'u':

font_n.underline=~(font_n.underline);

show_menu(font_n);

break;

default :;break;

}

while(getchar()!='\n')

continue;

}

puts("Bye!\n");

return 0;

}

void show_menu(struct font_style font_n)

{

char ali[7];

if(font_n.alignment==0)

strcpy(ali,"center");

else if (font_n.alignment==1)

strcpy(ali,"left");

else if(font_n.alignment==2)

strcpy(ali,"right");

printf("ID SIZE ALIGNMENT B I U\n");

printf("%u\t%u\t%s\t%s\t%s\t%s \n\n",font_n.font_ID,font_n.font_size,ali,font_n.bold == true ?"on":"off",font_n.italics == true ?"on":"off",font_n.underline == true?"on":"off");

printf("f)change font\ts)change size\ta)change alignment\nb)toggle bold\ti)toggle italic\tu)toggle underline\nq)quit\n");

}

相关推荐
科大饭桶20 分钟前
C++入门自学Day11-- String, Vector, List 复习
c语言·开发语言·数据结构·c++·容器
范范之交24 分钟前
JavaScript基础语法two
开发语言·前端·javascript
Felven24 分钟前
C. Game of Mathletes
c语言·开发语言
点云SLAM1 小时前
C++中内存池(Memory Pool)详解和完整示例
开发语言·c++·内存管理·内存池·new/delete·malloc/free
程高兴1 小时前
遗传算法求解冷链路径优化问题matlab代码
开发语言·人工智能·matlab
wow_DG1 小时前
【C++✨】多种 C++ 解法固定宽度右对齐输出(每个数占 8 列)
开发语言·c++·算法
CHEN5_022 小时前
【Java基础】反射,注解,异常,Java8新特性,object类-详细介绍
java·开发语言
Cx330❀2 小时前
【数据结构初阶】--排序(四):归并排序
c语言·开发语言·数据结构·算法·排序算法
云间月13142 小时前
飞算JavaAI智慧文旅场景实践:从景区管理到游客服务的全链路系统搭建
java·开发语言
杜子不疼.2 小时前
《Python学习之使用标准库:从入门到实战》
开发语言·python·学习