【发布时间】:2019-10-07 02:24:35
【问题描述】:
在搜索 K&R C 练习 7-2 语句的含义时,我在 https://clc-wiki.net/wiki/K%26R2_solutions:Chapter_7:Exercise_2 上找到了 K&R C 练习 7.2 的答案
练习要求
编写一个程序,以合理的方式打印任意输入。作为最低限度,它应该根据当地习惯以八进制或十六进制打印非图形字符,并打破长文本行。
我也无法理解练习的句子的含义,特别是“至少应根据当地习惯以八进制或十六进制打印非图形字符”部分。
这个练习 7-2 要求什么?请解释运动陈述的含义。
下面的代码使用格式说明符“%02x”和“%3o”在代码末尾用于打印不可打印的字符,但是这个格式说明符对 Non-Printables 究竟做了什么?
else
{
if(textrun > 0 || binaryrun + width >= split)
{
printf("\nBinary stream: ");
textrun = 0;
binaryrun = 15;
}
printf(format, ch);
binaryrun += width;
}
其余代码将长行拆分为较小的行并按原样打印所有可打印字符。
完整程序如下:
#include <stdio.h>
#define OCTAL 8
#define HEXADECIMAL 16
void ProcessArgs(int argc, char *argv[], int *output)
{
int i = 0;
while(argc > 1)
{
--argc;
if(argv[argc][0] == '-')
{
i = 1;
while(argv[argc][i] != '\0')
{
if(argv[argc][i] == 'o')
{
*output = OCTAL;
}
else if(argv[argc][i] == 'x')
{
*output = HEXADECIMAL;
}
else
{
/* Quietly ignore unknown switches, because we don't want to
* interfere with the program's output. Later on in the
* chapter, the delights of fprintf(stderr, "yadayadayada\n")
* are revealed, just too late for this exercise.
*/
}
++i;
}
}
}
}
int can_print(int ch)
{
char *printable = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 !\"#%&'()*+,-./:;<=>?[\\]^_{|}~\t\f\v\r\n";
char *s;
int found = 0;
for(s = printable; !found && *s; s++)
{
if(*s == ch)
{
found = 1;
}
}
return found;
}
int main(int argc, char *argv[])
{
int split = 80;
int output = HEXADECIMAL;
int ch;
int textrun = 0;
int binaryrun = 0;
char *format;
int width = 0;
ProcessArgs(argc, argv, &output);
if(output == HEXADECIMAL)
{
format = "%02X ";
width = 4;
}
else
{
format = "%3o ";
width = 4;
}
while((ch = getchar()) != EOF)
{
if(can_print(ch))
{
if(binaryrun > 0)
{
putchar('\n');
binaryrun = 0;
textrun = 0;
}
putchar(ch);
++textrun;
if(ch == '\n')
{
textrun = 0;
}
if(textrun == split)
{
putchar('\n');
textrun = 0;
}
}
else
{
if(textrun > 0 || binaryrun + width >= split)
{
printf("\nBinary stream: ");
textrun = 0;
binaryrun = 15;
}
printf(format, ch);
binaryrun += width;
}
}
putchar('\n');
return 0;
}
【问题讨论】:
-
是的.. 我读过关于 %o 和 %x 的文章(以及关于 %02x 的文章)。他们将 i/p 整数打印为八进制或 hexDec 格式(如果宽度小于 2,则填充 0)。我的主要障碍是理解将这些说明符用于打印不可见字符的目的。我可以 i/p 哪些非图形或不可打印字符以及如何输入?我知道从 0x00 到 0x20 是不可见的 ASCII 字符。我也无法准确理解练习的陈述,除了它要求以固定长度打破长文本行。
-
接下来,阅读
ctype.h中的字符分类功能。或者只看您发布的代码中的can_print()函数。
标签: c format-specifiers