【发布时间】:2010-01-08 00:07:04
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define TAB_STOP 8
/* replaces tabs from input with the proper amount of blank spots */
int Detab()
{
int c, x;
int column;
x = column = 0;
while((c=getchar())!=EOF)
{
if(c == '\n') /* reseting counter if newline */
{
putchar(c);
return 1;
}
else if(c!='\t') /* column counts places to tab spot */
{
putchar(c);
column++;
if(column == TAB_STOP)
column = 0;
}
else /* tab */
{
for(x=0; x<TAB_STOP - column; x++)
putchar('_');
column = 0;
}
}
return 0;
}
#define MAX_ARGUMENTS 100
int main(int argc, char *argv[])
{
int i, val = 0;
int nums[MAX_ARGUMENTS];
int x = 0;
for(i = 1; i < argc; i++) {
while(isdigit(*argv[i])) {
val = val * 10 + *argv[i] - '0';
*++argv[i];
}
if(x > MAX_ARGUMENTS - 1)
return 0;
nums[x++] = val;
nums[x] = '\0';
val = 0;
}
while(Detab(nums));
printf("Press any key to continue.\n");
getchar();
return 0;
}
在 main 中,我将所有参数(数字)放入 nums 数组中,然后将其传递给 detab。所以现在我对编辑 detab 的聪明方法很感兴趣,所以它可以工作。我仍在尝试找出一个有效的伪代码,但我真的不知道。
我认为它应该工作的方式是: 如果参数是 5、8、10,则前 4 个字符内的制表符导致位置 5,在 5 - 7th 字符中导致位置 8 等。 在换行的情况下,参数从头开始。
【问题讨论】:
-
我在这里没有看到具体的问题。一般来说,这里的人不会做一般的“修复代码”事情。他们回答具体的技术问题。
-
是参数解析的问题还是detab函数本身的问题?
-
工具:澄清问题会很有帮助,例如“我如何编写 Detab 以便它接受参数列表?它的原型是什么?”您从上一个问题中继承了很多不适用和分散注意力的信息。
-
是的......我正在寻找一个原型,或者至少某种伪代码能够正确编辑 detab。
标签: c pointers function entab-detab