既然这是我的第一篇文章,让我们看看这是如何进行的。让我先介绍一下问题:
- 创建一个4 "columns"或";cells"宽。
- 表应该包含一个给定范围的ASCII解码值,不需要扫描
- 范围可为:40-56、95-107、20-27
- 每一列之间都应该有制表符,但不是最后一个,所以FULL4宽的行不能以制表符结束,在其他情况下它应该
- 每个"cell"应该包含给定的ASCII解码值,该解码数的十六进制值和相应的ASCII字符本身,如果它是可打印的,在其他情况下,一个"?"
这个想法是使用if, less和循环来完成任务,例如,使用for或while循环。
这是我的代码,适用于第1,2,3和5部分,部分也适用于第4部分。我将解释输出的问题。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
int min=28; //give the starting point of the ASCII dec range
int max=40; //give the ending point of the ASCII dec range
int i, j; //these are used to produce rows and columns
for(i=min; i<=max; i=i+4) //create a four "cells" wide row
{
for(j=i; j<=i+2 && j<max; j++) //print the first three values for the row
{
if(isprint(j)){ //print the ASCII character if it's printable, if not, print question mark
printf("%3d 0x%02x %ct", j, j ,j);
}else {
printf("%3d 0x%02x ?t",j, j);
}
}
if(isprint(j+3)) //print the last value for each row
{
printf("%3d 0x%02x %c", j, j ,j);
}else {
printf("%3d 0x%02x ?",j, j);
}
printf("n"); //once a row has the max 4 "cells", start a new row and repeat the process
}
return 0;
}
然后给出"input "28-40:
28 0x1c ? 29 0x1d ? 30 0x1e ? 31 0x1f
32 0x20 33 0x21 ! 34 0x22 " 35 0x23 #
36 0x24 $ 37 0x25 % 38 0x26 & 39 0x27 '
40 0x28 (
现在是ISSUE本身。这个格式98%是我想要的,除了符号"("对于值40,应该有2个空格(=tab),就像列之间的其他间隙一样,但没有。就像我写的,一个完整的行不能以制表符结束,但是像最后一行这样的部分行应该是制表符。只是为了澄清,只有行可以小于4列宽,是最后一行。
因此,问题是:如果最后一行的最后一个ascii符号小于满(=4列宽),我如何在最后一行的最后一个ascii符号之后获得一个选项卡。
我已经被这个小问题困扰了好几天了(当然,我并没有把我所有的时间都花在编程上,但每天至少有几个小时),所以如果有人能给我介绍一个解决这个问题的方法,我将非常非常感激!问题是循环的i < max
条件阻止了最后一个值与制表符一起打印。
改为i <= max
。然后在打印第四列的周围添加一个条件,以检查是否已经达到最大值。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
int min=28; //give the starting point of the ASCII dec range
int max=40; //give the ending point of the ASCII dec range
int i, j; //these are used to produce rows and columns
for(i=min; i<=max; i=i+4) //create a four "cells" wide row
{
for(j=i; j<=i+2 && j<=max; j++) //print the first three values for the row
{
if(isprint(j)){ //print the ASCII character if it's printable, if not, print question mark
printf("%3d 0x%02x %ct", j, j ,j);
}else {
printf("%3d 0x%02x ?t",j, j);
}
}
if (j <= max) { //are we done?
if(isprint(j+3)) //print the last value for each row
{
printf("%3d 0x%02x %c", j, j ,j);
}else {
printf("%3d 0x%02x ?",j, j);
}
}
printf("n"); //once a row has the max 4 "cells", start a new row and repeat the process
}
return 0;
}