开发者

Modify detab to accept list of tab stops

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.


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.


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜