I am creating a C code to sort using qsort(). I need to get an array from the user and after that I need to get a from the user to print this array in ascending order and d if I want to print it in descending order. The problem is that I need to use function pointers to do this. I tried to use an array of function pointers but the problem is that the user needs to enter two characters.
我正在创建一个使用qsort()排序的C代码。我需要从用户那里得到一个数组,之后我需要从用户那里得到一个按升序打印这个数组的d,如果我想按降序打印它。问题是我需要使用函数指针来执行此操作。我试图使用函数指针数组,但问题是用户需要输入两个字符。
#include <stdio.h>
#include <stdlib.h>
int a( const void *va , const void * vb )
{
const int * a = ( const int *) va ;
const int * b = ( const int *) vb ;
if (* a < *b ) return -1;
else if (* a > * b) return 1;
else return 0;
}
int d( const void *va , const void * vb )
{
const int * a = ( const int *) va ;
const int * b = ( const int *) vb ;
if (* a < *b ) return 1;
else if (* a > * b) return -1;
else return 0;
}
int main()
{
int *arr;
int n, i;
char c;
scanf("%d", &n);
arr=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
while(1)
{
scanf("%c", &c);
getchar();
if(c=='e')
break;
qsort ( arr , n , sizeof(arr[0]) , d);
}
for(i=0;i<n;i++)
{
printf("%d", arr[i]);
}
return 0;
}
1 个解决方案
#1
3
So, declare a pointer-to-function variable comparison_func
, and set it in an if
statement:
因此,声明一个指向函数的指针变量comparison_func,并在if语句中设置它:
int (*comparison_func)(const void *, const void *);
if (c == 'a') {
comparison_func = a;
}
else if (c == 'c') {
comparison_func = d;
}
then use this comparison_func
in call to qsort:
然后在调用qsort时使用this comparison_func:
qsort(arr, n, sizeof(arr[0]), comparison_func);
or you can declare a struct
that has option character and the corresponding pointer:
或者您可以声明一个具有选项字符和相应指针的结构:
typedef struct sortfunc {
char option;
int (*comparison_func)(const void *, const void *);
} SORTFUNC;
SORTFUNC sort_funcs[] = {
{'a', a},
{'d', d},
{0, 0}
};
then go through this array finding the matching option character:
然后通过这个数组找到匹配的选项字符:
SORTFUNC *i;
for (i = sort_funcs; i->option && i->option != c; i++);
if (! i->comparison_func) {
printf("choice %c is invalid\n", c);
}
else {
qsort(..., i->comparison_func);
}
#1
3
So, declare a pointer-to-function variable comparison_func
, and set it in an if
statement:
因此,声明一个指向函数的指针变量comparison_func,并在if语句中设置它:
int (*comparison_func)(const void *, const void *);
if (c == 'a') {
comparison_func = a;
}
else if (c == 'c') {
comparison_func = d;
}
then use this comparison_func
in call to qsort:
然后在调用qsort时使用this comparison_func:
qsort(arr, n, sizeof(arr[0]), comparison_func);
or you can declare a struct
that has option character and the corresponding pointer:
或者您可以声明一个具有选项字符和相应指针的结构:
typedef struct sortfunc {
char option;
int (*comparison_func)(const void *, const void *);
} SORTFUNC;
SORTFUNC sort_funcs[] = {
{'a', a},
{'d', d},
{0, 0}
};
then go through this array finding the matching option character:
然后通过这个数组找到匹配的选项字符:
SORTFUNC *i;
for (i = sort_funcs; i->option && i->option != c; i++);
if (! i->comparison_func) {
printf("choice %c is invalid\n", c);
}
else {
qsort(..., i->comparison_func);
}