调用函数时出现错误 : too few arguments to function ‘quicksort’

标签 c

在这个程序中,我想对一个列表按照价格进行排序,然后使用快速排序对列表进行排序,在快速排序中,我使用了comp_on_price的比较,然后在接口(interface)中调用快速排序。但是,当我运行它时,它会出现参数太少的编译错误。 和调用方式有关系吗?

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

FILE *fp;

typedef struct book{
    double rating;
    double price;
    double relevance;
    int ID;
}B;

B *list;

int read_file(char* infile, int N)
{
    int c;
    if((fp=fopen(infile, "rb")))
    {
        fscanf(fp, "%*s\t%*s\t%*s\t%*s\n");
        c=0;
        while((!feof(fp))&&(c<N))
        {
            fscanf(fp, "%lf\t%lf\t%lf\t%d\n", &list[c].rating, &list[c].price, &list[c].relevance, &list[c].ID);
            c++;
        }
        fclose(fp);
    }
    else
    {
        fprintf(stderr,"%s did not open. Exiting.\n",infile);
        exit(-1);
    }
    return(c);
}

int comp_on_price(const void *a, const void *b)
{
    if ((*(B *)a).price < (*(B *)b).price)
        return 1;
    else if ((*(B *)a).price > (*(B *)b).price)
        return -1;
    else
        return 0;
}

void quicksort(int x[10],int first, int last, int(*comp_on_price)(const void *, const void *))
{
    int pivot,j,temp,i;
    if(first<last)
    {
        pivot=first;
        i=first;
        j=last;

        while(i<j)
        {
            while(x[i]<=x[pivot]&&i<last)
            i++;
            while(x[j]>x[pivot])
            j--;
            if(i<j){
                temp=x[i];
                x[i]=x[j];
                x[j]=temp;
            }
        }/* while*/

        temp=x[pivot];
        x[pivot]=x[j];
        x[j]=temp;
        quicksort(x,first,j-1);
        quicksort(x,j+1,last);
    }
}

void user_interface(int N)
{
    // For Part 1 this function calls the sort function to sort on Price only
    comp_on_price(N);

    // For Part 2 this function
    // (1) asks the user if they would like to sort their search results
    // (2) asks for the most important field (or key), the next most etc
    // (3) calls your sort function
}

void print_results(int N)
{
    int i;
    if((fp=fopen("top20.txt","w")))
    {
        for(i=N-1;i>=N-20;i--)
        {
            printf("%g %g %g %d\n", list[i].rating, list[i].price, list[i].relevance, list[i].ID);
            fprintf(fp, "%g %g %g %d\n", list[i].rating, list[i].price, list[i].relevance, list[i].ID);
        }
        fclose(fp);
    }
    else
    {
        fprintf(stderr,"Trouble opening output file top20.txt\n");
        exit(-1);
    }
}

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

    if(argc!=3)
    {
        fprintf(stderr, "./exec <input_size> <filename>\n");
        exit(-1);
    }

    N=atoi(argv[1]);

    list = (B *)malloc(N*sizeof(B));

    N=read_file(argv[2], N);

    user_interface(N);

    print_results(N);

    return(0);
}

最佳答案

quicksort(x,first,j-1);

如果您查看 quicksort 函数声明,您会发现它有 4 个而不是 3 个参数。最后一个参数是一个指向比较函数的指针。

你应该这样调用它:

quicksort(x, first, j-1, comp_on_price);

关于调用函数时出现错误 : too few arguments to function ‘quicksort’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13431798/

相关文章:

C++ 打印函数的结果?

c - 使用 scanf 函数为变量赋值

python - 如何为 C 代码创建 python 接口(interface)?

iphone - 如何在 mac 或 iphone 上接收和播放 SHOUTcast 音频流?

c - 在 arm64 linux 中混合 64/32 位用户空间(使用 CONFIG_COMPAT)

用C编写的计算器程序

c - 将 execvp 的输出放入字符串中

Collat​​z链递归函数C

C 顶点数组错误值

c - 对 xxx include 的 undefined reference 不起作用