/设计一位字段结构存储下面信息。
字体ID:0~255之间的一个数
字体大小:0~127之间的一个数
对齐:0~2之间的一个数表示左对齐,居中,右对齐
加粗:开(1)或闭(0)
斜体:开(1)或闭(0)
在程序中使用该结构来打印字体参数,并使用循环菜单来让用户改变参数。例如,该程序的一个运行示例如下:
ID SIZE ALIGNMENT B I U
1 12 left off off off
f)change font s)change size a)change alignment
b)toggle bold i)toggle italic u)toggle underline
q)quit/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
unsigned char id; // 字体ID: 0~255
unsigned char size; // 字体大小: 0~127
unsigned char alignment; // 对齐: 0~2(左对齐、居中、右对齐)
unsigned char bold; // 加粗: 1 或 0
unsigned char italic; // 斜体: 1 或 0
unsigned char underline; // 下划线: 1 或 0
} FontInfo;
void printFontInfo(const FontInfo *font)
{
const char *alignments[] = {"left", "center", "right"};
printf("ID SIZE ALIGNMENT B I U\n");
printf("%d %d %s %s %s %s\n",
font->id, font->size, alignments[font->alignment],
font->bold ? "on" : "off",
font->italic ? "on" : "off",
font->underline ? "on" : "off");
}
void menu(FontInfo *font)
{
printFontInfo(font);
printf("f)change font s)change size a)change alignment\n");
printf("b)toggle bold i)toggle italic u)toggle underline\n");
printf("q)quit\n");
printf("Choose an option: ");
char choice = getchar();
while (getchar() != '\n');
switch (choice)
{
case 'f':
printf("Enter a new font ID (0-255): ");
scanf("%hhu", &font->id);
while (getchar() != '\n');
break;
case 's':
printf("Enter a new size (0-127): ");
scanf("%hhu", &font->size);
while (getchar() != '\n');
break;
case 'a':
printf("Enter alignment (l=left, c=center, r=right): ");
char align;
scanf(" %c", &align);
switch (align)
{
case 'l':
font->alignment = 0;
break;
case 'c':
font->alignment = 1;
break;
case 'r':
font->alignment = 2;
break;
default:
printf("Invalid alignment choice. It should be 'l', 'c' or 'r'.\n");
break;
}
while (getchar() != '\n');
break;
case 'b':
font->bold = !font->bold;
break;
case 'i':
font->italic = !font->italic;
break;
case 'u':
font->underline = !font->underline;
break;
case 'q':
exit(0);
break;
default:
printf("Invalid option. Please try again.\n");
break;
}
}
int main()
{
FontInfo font = {1, 12, 0, 0, 0, 0};
while (1)
{
menu(&font);
}
return 0;
}
标签:case,char,字体大小,或闭,break,printf,对齐,font
From: https://www.cnblogs.com/yesiming/p/18346162