سؤال

#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 لذلك يعمل. ما زلت أحاول معرفة من أجل العمل الكفوليات العامل لكنني لا أعرف حقا.

الطريقة التي راغتها فيها يجب أن تعمل هي: إذا كانت الحجج 5، 8، 10، ثم علامة تبويب داخل أول 4 أحرف تؤدي إلى وضع 5، في 5 - 7th char يؤدي إلى نقاط البيع 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
}

إليك بعض pseudocode لتوسيع علامات التبويب:

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

جربه في Codepad..

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top