-2

在这个程序中,它就像一个书店,有价格、相关性、星级和ID等信息,程序需要根据价格(从最低到最高)对列表进行排序。程序只需要实现一个归并排序(排序会带一个compare_on_price参数,所以会从低到高列出价格),然后Interface调用排序在Interface函数中,最后在main函数中调用界面。

物品清单是这样的

Stars   Price   Relv    ID
4.5     12.49   7.9      1
5        7.99   7.6      2
2       16.99   6.2      3
2.5     15.49   9.1      4
3.5     20.99   6        5
1        9.99   8        6
1.5     13.99   1        7
5        8.49   8.3      8
3.5     10.49   5.2      9

排序后,价格应从低到高列出。到目前为止的错误:
part1.c:117:20: error: expected expression before ',' token
part1.c:117:20: warning: passing argument 1 of 'mergesort' makes pointer from integer without a cast
part1.c:74:6: note: expected 'int *' but argument is of type 'int'
part1.c:117:20: warning: passing argument 4 of 'mergesort' makes pointer from integer without a cast

#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_rating(const void *a, const void *b)
{
  if ((*(B *)a).rating < (*(B *)b).rating)
    return -1;
  else if ((*(B *)a).rating > (*(B *)b).rating)
    return 1;
  else
    return 0;  
}

int comp_on_relev(const void *a, const void *b)
{

  if ((*(B *)a).relevance < (*(B *)b).relevance)
    return -1;
  else if ((*(B *)a).relevance > (*(B *)b).relevance)
    return 1;
  else
    return 0;  
}

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;  
}


/* Stable sorting method: if it keeps elements with equal keys in he smae
relative order as they were in te input. */
void mergesort(int a[],int low, int high, int(*compar)(const void *, const void *))  
{  

 int i = 0;
 int length = high - low + 1;
 int pivot  = 0;
 int merge1 = 0;
 int merge2 = 0;
 int working[length];

 if(low == high)
  return;

 pivot  = (low + high) / 2;

 mergesort(a, low, pivot,compar );
 mergesort(a, pivot + 1, high,compar);

 for(i = 0; i < length; i++)
  working[i] = a[low + i];

 merge1 = 0;
 merge2 = pivot - low + 1;

 for(i = 0; i < length; i++) {
  if(merge2 <= high - low)
   if(merge1 <= pivot - low)
    if(working[merge1] > working[merge2])
     a[i + low] = working[merge2++];
    else
     a[i + low] = working[merge1++];
   else
    a[i + low] = working[merge2++];
  else
   a[i + low] = working[merge1++];
 }

} //end function.  

void user_interface(int N)
{

  // For Part 1 this function calls the sort function to sort on Price only
 mergesort(N,0,,N-1, comp_on_price);


}

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);
}

  /* top 20 for example */
  N=atoi(argv[1]);

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

  /*read the file into the n variable*/
  N=read_file(argv[2], N);

  user_interface(N);

  print_results(N);

  return(0);
}
4

1 回答 1

1

1.

mergesort(N,0,,N-1, comp_on_price);
            ^
          Extra , in there...
  1. void mergesort(int a[],int low, int high, int(*compar)(const void *, const void *)) ^ ^ ^ ^ array int int 函数指针

你的来电:

 mergesort(N,0,,N-1, comp_on_price);
           ^ ^   ^         ^
          int int int     function name, 
                           a function point is an address of your function
于 2012-11-19T13:02:55.950 回答