gpt4 book ai didi

c - 代码解释 C 中的 QSORT 代码

转载 作者:行者123 更新时间:2023-11-30 21:00:37 25 4
gpt4 key购买 nike

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

我的教授轻轻地看了一眼包含以下代码的幻灯片,每个人都迷失了方向,包括我自己。

   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
{ ...}
}

关于c - 代码解释 C 中的 QSORT 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39861129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com