大家好,大家都需要一些 C 编程方面的帮助。 我需要编写没有数字的 ascii 表:0、7-10、13。每个字符之间将是“\t”,每 10 个字符之后它将跳过行。 我现在的代码:
void ascii()
{
int i;
for (i = 0; i <= 255; i++)
{
if (i == 0)
{
printf("\t");
}
if (0 == i % 10)
{
printf("\n");
}
printf("%d = %c\t", i, i);
}
return;
}
最佳答案
为什么这些代码被排除在外?排除所有不可打印的代码不是更好吗? isprint
将做到这一点。
#include <ctype.h>
if (isprint(i)) printf("%d = %c\t", i, i);
如果您想排除这些特定代码,您可以使用与 isxxx 函数相同的技术:
char is_print[256] = { 0,1,1,1,1,1,1,0,0,0,0,1,1,0,1,1, /* 0 in pos 0, 7-10 and 13 */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
}; /* Filter with 0 for the chars we want to exclude. */
int myIsprint(char c) {
return is_print((unsigned char)c); /* Use the filter */
}
在你的函数中:
if (myIsprint(i)) printf("%d = %c\t", i, i);
注意:
在简单的情况下,您不妨使用 if
测试特定代码 if (i != 0 && i != 13 && (i < 7 || i > 10)) print...
。
上述过滤技术的优点是速度快如闪电,并且即使案例数量很大,代码也易于阅读。
关于C编程(初学者帮助)Ascii表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29942397/