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");

}

相关推荐
Yvonne爱编码9 分钟前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚10 分钟前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂17 分钟前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
pas13625 分钟前
41-parse的实现原理&有限状态机
开发语言·前端·javascript
琹箐36 分钟前
最大堆和最小堆 实现思路
java·开发语言·算法
Monly211 小时前
Java:修改打包配置文件
java·开发语言
我命由我123452 小时前
Android 广播 - 静态注册与动态注册对广播接收器实例创建的影响
android·java·开发语言·java-ee·android studio·android-studio·android runtime
island13142 小时前
CANN ops-nn 算子库深度解析:核心算子(如激活函数、归一化)的数值精度控制与内存高效实现
开发语言·人工智能·神经网络
xcLeigh2 小时前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh2 小时前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics