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

}

相关推荐
Eiceblue39 分钟前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
m0_555762901 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
学不动CV了1 小时前
ARM单片机启动流程(二)(详细解析)
c语言·arm开发·stm32·单片机·51单片机
浪裡遊2 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
lzb_kkk3 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
好开心啊没烦恼3 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
猫猫的小茶馆3 小时前
【STM32】通用定时器基本原理
c语言·stm32·单片机·嵌入式硬件·mcu·51单片机
简佐义的博客4 小时前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang
程序员爱钓鱼4 小时前
【无标题】Go语言中的反射机制 — 元编程技巧与注意事项
开发语言·qt