C语言中QSORT代码的解释

C语言中QSORT代码的解释,c,C,我是新来的。事实上,我创建它只是为了问这个问题 我的教授在一张包含以下代码的幻灯片上若无其事,所有人都迷路了,包括我自己 main(int argc, char *argv[]){ int nlines; /* number of input lines read */ int numeric = 0; /* 1 if numeric sort */ if (argc > 1 && strcmp(argv[1], "-n") == 0)

我是新来的。事实上,我创建它只是为了问这个问题

我的教授在一张包含以下代码的幻灯片上若无其事,所有人都迷路了,包括我自己

   main(int argc, char *argv[]){

    int nlines; /* number of input lines read */

    int numeric = 0; /* 1 if numeric sort */

    if (argc > 1 && strcmp(argv[1], "-n") == 0)
      numeric = 1;
    if ((nlines = readlines(lineptr, MAXLINES)) >= 0) 
    {
      qsort((void**) lineptr, 0, nlines-1,
      (int (*)(void*,void*))(numeric ? numcmp : strcmp));
      writelines(lineptr, nlines);
      return 0;
    } 
    else 
    { ...}
} 
你能详细解释一下到底发生了什么吗?

nlines跟踪从输入中读取的行数。 numeric跟踪数字是否与字符相对排序

我的其余解释在评论中:

int main(int argc, char *argv[]){

    int nlines; /* number of input lines read */

    int numeric = 0; /* 1 if numeric sort */

    /* evaluates whether or not numeric sorting is to be applied */
    if (argc > 1 && strcmp(argv[1], "-n") == 0)
      numeric = 1;

    /* this reads lines if there are any.*/
    /* it looks like `lineptr` must've been declared elsewhere */
    if ((nlines = readlines(lineptr, MAXLINES)) >= 0) 
    {
      qsort((void**) lineptr, 0, nlines-1,
      (int (*)(void*,void*))(numeric ? numcmp : strcmp));
      /* sort the elements accordingly... e.g., either as strings or numerically. */


      writelines(lineptr, nlines);
      return 0;
    }

    /* else gets run if there is no input to take */
    else 
    { ...}
} 

我想说这是sort命令的源代码,只是在幻灯片上进行了模糊处理,避免了有趣的排序部分:请格式化您的代码。是否最好询问prod。他是为了帮助您而付费的请选择一种语言,校对你写的东西,希望不需要解释,并清晰地格式化你的代码。我能得到他的工资的百分比吗