//
// main.c
// 15.9-7
//
// Created by cjm on 2024/2/5.
//猜测题意是用一个unsigned long 64个位去储存全部信息,一共需要20位即可
//00000000 0000000 00 0 0 0
// ID size alignment B I U
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
#define MASK_U 1 //001
#define MASK_I 2 //010
#define MASK_B 4 //100
#define MASK_A 0x18 //11000
#define MASK_S 0xfe0 //111111100000
#define MASK_ID 0xff000 //11111111000000000000
void show_menu(unsigned long font_n);
int main()
{
printf("The number of bits in an unsigned long: %ld\n", CHAR_BIT * sizeof (unsigned long));//确认unsigned long的位数
unsigned long font_n;
char ch;
font_n=0b00000001000110001000;//初始化为题目的初始状态
int id,size = 0;
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_n&(~MASK_ID);
font_n=font_n+(id<<12);
show_menu(font_n);
break;
case 's':puts("Enter font size(0-127)");
scanf("%d",&size);
font_n=font_n&(~MASK_S);
font_n=font_n+(size<<5);
show_menu(font_n);
break;
case 'a':
puts("Select alignment:\nl) left\tc)center\tr)right\n");
scanf("%c",&alig);
while(getchar()!='\n')
continue;
font_n=font_n&(~MASK_A);//清空位
if(alig=='l')
font_n=font_n+8;
else if(alig=='r')
font_n=font_n+16;
show_menu(font_n);
break;
case 'b':
font_n^=MASK_B;
show_menu(font_n);
break;
case 'i':
font_n^=MASK_I;
show_menu(font_n);
break;
case 'u':
font_n^=MASK_U;;
show_menu(font_n);
break;
default :;break;
}
while(getchar()!='\n')
continue;
}
puts("Bye!\n");
return 0;
}
void show_menu(unsigned long font_n)
{
char ali[7];
if(((font_n>>3)&(MASK_A>>3))==0)
strcpy(ali,"center");
else if (((font_n>>3)&(MASK_A>>3))==1)
strcpy(ali,"left");
else if(((font_n>>3)&(MASK_A>>3))==2)
strcpy(ali,"right");
printf("ID\tSIZE\tALIGNMENT\tB\tI\tU\n");
printf("%lu\t%lu\t\t%s\t\t%s\t%s\t%s \n\n",
(font_n>>12)&(MASK_ID>>12),
(font_n>>5)&(MASK_S>>5),ali,
((font_n>>2)&(MASK_B>>2)) == true?"on":"off",
((font_n>>1)&(MASK_I>>1)) == true?"on":"off",
(font_n&MASK_U) == 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");
}