如何修改detab接受的参数列表
-
19-09-2019 - |
题
#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;
}
在主我把NUMS阵列内的所有参数(号码),然后将它传递到detab。所以,现在即时通讯有兴趣的会是什么编辑detab因此它可以聪明的办法。我仍然在试图找出了工作,但伪代码我真的不知道。
我因子评分它应该工作的方式是: 如果参数是5,8,10,则内侧第一4个字符的标签导致5位,在5 - 第七炭导致POS 8等 在换行的情况下,所述参数从一开始从头再来。
解决方案
最常见的方式是有Detab接受一个指针(它指向一个数组中的元素),并且阵列的长度:
int Detab(int* data, int len); // access data[0] through data[len - 1]
调用它像这样:
void example() {
int array[] = {5, 8, 10};
Detab(array, 3);
// or:
Detab(array, sizeof array / sizeof *array); // second parameter evaluates to 3
// without using a magic constant
}
下面是一些伪代码用于扩展标签:
def expandtabs_in_line(line, tabstops, default, space):
result = ""
for c in line:
if c != "\t":
result += c
else:
for stop in tabstops:
if stop > len(result):
result += space * (stop - len(result))
break
else:
result += space * (default - (len(result) % default))
return result
def expandtabs(lines, tabstops=[], default=8):
for line in lines:
yield expandtabs_in_line(line, tabstops, default, " ")
立即尝试在键盘。
不隶属于 StackOverflow