Frage

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

In Haupt Ich habe alle Argumente (Zahlen) innerhalb nums Array und übergeben es dann an detab. So, jetzt interessiert im, was die intelligente Art und Weise wäre, zu bearbeiten detab so funktioniert es. Ich bin immer noch herauszufinden, für einen Arbeits Pseudo-Code versucht, aber ich nicht wirklich kennen.

Die Art und Weise i tought sollte es funktionieren wird: wenn Argumente sind 5, 8, 10 dann ein Register innerhalb der ersten 4 Zeichen führen 5 zu positionieren, in 5 - 7. char führt zu po 8 usw. Im Fall eines Newline beginnen die Argumente noch einmal von Anfang an.

War es hilfreich?

Lösung

Die gängigste Methode ist es, einen Zeiger (die zu einem Element in einem Array verweist) haben Detab akzeptieren und die Länge des Arrays:

int Detab(int* data, int len); // access data[0] through data[len - 1]

Nennen Sie es wie folgt:

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
}

Hier einige Pseudo-Code für die Erweiterung Registerkarten:

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

Versuchen Sie es unter Codepad .

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top