문제

#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 어레이 안에 넣은 다음 저하로 전달합니다. 그래서 이제 나는 Detaw를 편집하는 현명한 방법이 무엇인지 관심이 있으므로 작동합니다. 나는 여전히 가루 로코 코드를 알아 내려고 노력하고 있지만 나는 정말로 모른다.

내가 작동 해야하는 방식은 다음과 같습니다. 인수가 5, 8, 10이라면 처음 4 자 안에있는 탭은 5-7 번째 숯으로 이어지는 5-7 번째 숯으로 이어집니다. Newline의 경우, 인수는 모든 것을 시작합니다. 처음부터 다시.

도움이 되었습니까?

해결책

가장 일반적인 방법은 포인터 (배열의 요소를 가리키는)와 해당 배열의 길이를 수락하는 것입니다.

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, " ")

시도해보십시오 코드 패드.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top