修改 detab 以接受制表位列表

发布于 2024-08-17 07:20:35 字数 1717 浏览 5 评论 0原文

这是我的 detab 版本,来自这个 K&R 练习:

修改 detab 以接受制表位列表作为参数。如果没有参数,则使用默认选项卡设置。

#include <stdio.h>
#include <stdlib.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;
}
int main(int argc, char *argv[])
{
     int valid;

     while((valid=Detab())!=0);

     printf("Press any key to continue.\n");
     getchar();
     return 0;
}

我的问题是,如果有多个参数(例如 5、8、10),则下一个制表位何时开始激活?程序应该在什么时候开始使用 TAB_STOP 8 而不是开始的 5?换行之后或者我应该怎么做?

我也不确定是否应该将所有这些都放入 main 中,还是应该坚持使用单独的函数?

编辑:好的,这就是我尝试过的。

#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];
           }

           nums[x++] = val;
           val = 0;

     }

     Detab(nums);       


     printf("Press any key to continue.\n");
     getchar();
     return 0;
}

我走在正确的轨道上吗?这可以吗?我还没有修改detab。

This is my version of detab, from this K&R exercise:

Modify detab to accept a list of tab stops as arguments. Use the default tab setting if there are no arguments.

#include <stdio.h>
#include <stdlib.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;
}
int main(int argc, char *argv[])
{
     int valid;

     while((valid=Detab())!=0);

     printf("Press any key to continue.\n");
     getchar();
     return 0;
}

My question is if there are more then one argument—for example 5, 8, 10—when is the next tab stop suppose to start being active? At which point should program start using TAB_STOP 8 instead of the starting 5? After a newline or how should I do this?

I'm also not really sure if I should put all of this into main, or should I stick with a separate function?

Edit: ok this is what i tried.

#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];
           }

           nums[x++] = val;
           val = 0;

     }

     Detab(nums);       


     printf("Press any key to continue.\n");
     getchar();
     return 0;
}

Am i on the right track? Can this work? I still havent modified detab.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不乱于心 2024-08-24 07:20:35

制表位列表指定特定的列,而不是停止位之间的距离。也就是说,如果列表是 5、8、10,则位置 1-4 中的选项卡应将光标置于 5,位置 5-7 中的选项卡应将光标置于 8,位置 8-9 中应将光标置于 10。每个换行符列表都应该从第一个制表位重新开始。最后定义的制表位之后的行的行为取决于您,通常您会返回到某个默认制表位间隔。

A list of tab stops specifies particular columns, not distances between stops. That is, if the list is 5,8,10 then a tab in positions 1-4 should place the cursor at 5, in positions 5-7 should place the cursor at 8, and 8-9 place the cursor at 10. After each newline the argument list should start over from the first tab stop again. The behavior on a line after the last defined tab stop is up to you, typically you would go back to some default tab stop interval.

妄断弥空 2024-08-24 07:20:35

我将 TABSTOP 5 8 10 解释为在第 5th、第 8 和 10th 列(以及之后)有制表位每 8 列,或者您使用的默认值,第 10 列之后的下一个制表符是否应该位于第 18 列(后面有 8 个空格)还是第 16 列(默认 8 列的下一个倍数),这是一个值得商榷的问题。

I'd interpret TABSTOP 5 8 10 to mean there are tab stops at the 5th, 8th, and 10th columns (and after that, every 8 columns, or whatever you're using as the default. It's open to question whether the next tab stop after column 10 should be at column 18 (8 spaces later) or 16 (the next multiple of the default 8).

旧梦荧光笔 2024-08-24 07:20:35

当你设计它时,你在这里有一些回旋余地;然而,最流行的方法是在最后提供的宽度之后继续使用默认宽度。

例如,如果提供 [5, 8, 10] 并且默认值为 8,则它将继续为 [5, 8, 10, 18, 26, 34, ...] 或 [5, 8, 10, 16, 24, 32, ...],取决于偏好。

请注意,我使用这些数字作为制表位,而不是宽度。因此 [5, 8] 表示第一个停靠点位于 5 处,宽度为 5,第二个停靠点位于 8 处,宽度为 3

You have some leeway here, as you're designing it; however, the most popular method is to continue with the default width after the last supplied width.

For example, if [5, 8, 10] is supplied and the default is 8, it would continue as [5, 8, 10, 18, 26, 34, ...] or [5, 8, 10, 16, 24, 32, ...], depending on preference.

Note that I'm using these numbers as tab stops, instead of widths. So [5, 8] means the first stop is at 5 with width 5, and the second is at 8 with width 3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文