我是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
{ ...}
}
你能详细解释一下到底发生了什么吗?
答案 0 :(得分:1)
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
{ ...}
}