C 指向函数的指针

标签 c pointers function

我正在开发一个程序,该程序根据传递给 main 的参数按字母/数字对输入行进行排序。这是后续练习:

添加字段处理功能,因此可以对行内的字段进行排序,每个字段根据一组独立的选项进行排序。 (本书的索引排序是用 -df 表示索引类别,-n 表示页码。)

我对字段的含义感到有点困惑。

field_of 函数到底做了什么?它是否增加了原始指针字段时间,字段是一串非空白字符?

另外,如果输入了 num_fields,那么比较函数会在到达第一个非零比较时返回,对吗?如果结果为零(相等的字符串),那么它不会返回任何东西,因为不需要替换字符串。否则,它返回一个数字。

    #include <stdio.h>
    #include <string.h>

    #define MAX_FIELDS 10

    #define FLAG_DIRECTORY (0x01 << 0)
    #define FLAG_FOLD  (0x01 << 1)
    #define FLAG_NUMERIC (0x01 << 2)
    #define FLAG_REVERSE (0x01 << 3)

    int fieldarray[MAX_FIELDS];
    unsigned char flagarray[MAX_FIELDS];
    int num_fields = 0;

    #define MAXLINES 5000  /* max #lines to be sorted */
    char *lineptr[MAXLINES]; /* pointers to text lines */

    int readlines(char *lineptr[], int nlines);
    void writelines(char *lineptr[], int nlines);

    void qsort(void *lineptr[], int left, int right,
               int (*comp)(void *, void *));

    static char *field_of(char *s, int n);
    static isdir(int c);
    int compare(char *, char *);
    int stringcmp(char *, char *);
    int stringcmpi(char *, char *);
    int dircmp(char *, char *);
    int numcmp(char *, char *);
int main()

     if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
      qsort((void **) lineptr, 0, nlines-1, (int (*)(void *, void *)) compare);
      writelines(lineptr, nlines);
      return 0;
     } else {
      printf("input too big to sort\n");
      return 1;
     }
    }

    #define MAXLEN 1000 /* max length of any input line */
    int getline(char *, int);
    char *alloc(int);



    /* qsort:  sort v[left]...v[right] into increasing order */
    void qsort(void *v[], int left, int right,
               int (*comp)(void *, void *))
    {
     int i, last;
     void swap(void *v[], int, int);

     if (left >= right) /* do nothing if array contains */
      return;   /* fewer than two elements */
     swap(v, left, (left + right)/2);
     last = left;
     for (i = left+1; i <= right; i++)
      if ((*comp)(v[i], v[left]) < 0)
       swap(v, ++last, i);
     swap(v, left, last);
     qsort(v, left, last-1, comp);
     qsort(v, last+1, right, comp);
    }

    void swap(void *v[], int i, int j)
    {
     void *temp;

     temp = v[i];
     v[i] = v[j];
     v[j] = temp;
    }

    static char *field_of(char *s, int n)
    {
     while (isspace(*s))
      s++;
     while (--n > 0) {
      while (!isspace(*s)) {
       if (*s == '\0')
        return NULL;
       s++;
      }
     }

     return s;
    }

    static isdir(int c)
    {
     return isalpha(c) || isdigit(c) || isspace(c);
    }

    int compare_field(char *s1, char *s2, unsigned int flags)
    {
     int d;

     if (flags & FLAG_NUMERIC) {
      d = numcmp(s1, s2);
     } else if (flags & FLAG_DIRECTORY) {
      do {
       while (!isdir(*s1) && !isspace(*s1) && *s1 != '\0')
        s1++;
       while (!isdir(*s2) && !isspace(*s2) && *s2 != '\0')
        s2++;
       if (flags & FLAG_FOLD)
        d = toupper(*s1) - toupper(*s2);
       else
        d = *s1 - *s2;
      } while (d == 0 && !isspace(*s1) && !isspace(*s2)
               && *s1++ != '\0' && *s2++ != '\0');
     } else {
      do {
       if (flags & FLAG_FOLD)
        d = toupper(*s1) - toupper(*s2);
       else
        d = *s1 - *s2;
      } while (d == 0 && !isspace(*s1) && !isspace(*s2)
               && *s1++ != '\0' && *s2++ != '\0');
     }

     if (flags & FLAG_REVERSE)
      return -d;
     else
      return d;
    }

    /* compare:  compare s1 and s2 according to the values of the
     external variables numeric, reverse, fold, and directory. */
    int compare(char *s1, char *s2)
    {
     int i, d;
     char *f1, *f2;

     for (i = 0; i < num_fields; i++) {
      f1 = field_of(s1, fieldarray[i]);
      f2 = field_of(s2, fieldarray[i]);
      d = compare_field(f1, f2, flagarray[i]);
      if (d != 0)
       return d;
     }
     if (numeric)
      d = numcmp(s1, s2);
     else if (directory)
      d = dircmp(s1, s2);
     else
      d = stringcmp(s1, s2);

     if (reverse)
      return -d;
     else
      return d;
    }

    /* stringcmp:  compare s1 and s2 as strings */
    int stringcmp(char *s1, char *s2)
    {
     if (fold)
      return stringcmpi(s1, s2);
     else
      return strcmp(s1, s2);
    }

    /* stringcmpi:  compare s1 and s2 case-insensitively */
    int stringcmpi(char *s1, char *s2)
    {
     while (toupper(*s1) == toupper(*s2)) {
      if (*s1 == '\0')
       return 0;
      s1++;
      s2++;
     }

     return toupper(*s1) - toupper(*s2);
    }

    /* dircmp:  compare s1 and s2 in "directory order" */
    int dircmp(char *s1, char *s2)
    {
     int d;

     do {
      while (!isdir(*s1) && *s1 != '\0')
       s1++;
      while (!isdir(*s2) && *s2 != '\0')
       s2++;
      if (fold)
       d = toupper(*s1) - toupper(*s2);
      else
       d = *s1 - *s2;
     } while (d == 0 && *s1++ != '\0' && *s2++ != '\0');

     return d;
    }

    /* numcmp:  compare s1 and s2 numerically */
    int numcmp(char *s1, char *s2)
    {
     extern double atof(const char *);
     double v1, v2;

     v1 = atof(s1);
     v2 = atof(s2);
     if (v1 < v2)
      return -1;
     else if (v1 > v2)
      return 1;
     else
      return 0;
    }

最佳答案

字段的概念是行的子部分。听起来你在问分隔字段,所以像这样一行:

a b 1 2 3

字段 1 是 'a',字段 2 是 'b',等等。

假设您所有的台词都像上面的例子。您可能希望根据字段 3 对行进行数字排序。

虽然我使用了空格分隔字段的示例,但还有其他方法可以定义字段。定义字段的另一种常见方法是使用绝对位置(可能字段 1 从偏移量 0 到 9,而字段 2 从偏移量 10 到 14)。

当您按字段排序时,您需要隔离每一行的适当部分并比较这些隔离部分。

最后,当你有多个字段时,你可以做多次比较。首先比较主要字段,但如果它们相等,则返回到第二个字段。在伪代码中:

# returns -1 if line1 < line2, 0 if line1 == line2, 1 if line1 > line2
int sort(line1, line2):
    foreach fielddefinition:
        line1_field = extract_field(line1)
        line2_field = extract_field(line2)

        if line1_field < line2_field:
            return -1
        elif line2_field < line1_field:
            return 1

        # this field is equal, continue checking with the next field

    # Since all fields are equal, the lines are equal
    return 0

关于C 指向函数的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2058613/

相关文章:

c - malloc 分配中的额外字节

创建用 C 中的链表实现的通用堆栈

C 函数语法,在参数列表之后声明的参数类型

c - 如何将结构初始化为指向结构

swift - 在 Swift 中重复字符串

C:关于通过使用超过当前 seek_set 的偏移量来使用 lseek 系统调用

c++ - 为什么链表的第一个节点声明为指针?

c++ - 指针如何引用硬盘中的一个段

function - Racket : expected: procedure?

javascript - 使用动态参数数调用动态函数