c - 根据key进行归并排序

标签 c

在这个程序中,它就像一个书店,有价格、相关性、星级和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);
}

最佳答案

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 *)) ^^^^ 数组 int int 函数指针

您的来电:

 mergesort(N,0,,N-1, comp_on_price);
           ^ ^   ^         ^
          int int int     function name, 
                           a function point is an address of your function

关于c - 根据key进行归并排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13454139/

相关文章:

c - 工作树遍历程序似乎不起作用

c - 递归解决方案的任何其他解决方案

c - 解决超过 2^32 个子句的 SAT

c - C 中的结构体赋值

C语言: Function interactions

c - 为什么当我将一个字符串字面量赋值给一个char变量时打印出字符d?

c - 如何使用嵌套for循环计算多个平均值?

c - 如何使用 C 应用程序从 WMI 获取数据?

c - 二维数组和指针 - C

c - 如何为 %20s 的 scanf 删除额外输入