Dans ce programme, je veux trier une liste, en fonction du prix, puis j'utilise un tri rapide pour trier la liste, dans le tri rapide, j'ai utilisé la comparaison de comp_on_price, puis appelez le tri rapide à l'interface. Cependant, quand je l'exécute, il arrive avec l'erreur de compilation de trop peu d'arguments. Y a-t-il quelque chose à voir avec la méthode d'appel?fonction d'appel avec erreur: trop peu d'arguments pour fonctionner 'quicksort'
#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);
}
Vous devez corriger le format de votre code pour le rendre lisible. –
Avez-vous entendu parler de 'qsort()'? Cela fait partie de la norme C depuis C89. – qrdl
@qrdl Je pense qu'il veut juste l'implémenter tout seul. –