C 根据关键字合并排序
在这个程序中,它就像一个书店,有价格、相关性、明星和ID等信息, 程序需要根据价格(从最低到最高)对列表进行排序。该程序只需要实现一个合并排序(排序将采用一个参数compare_on_price,因此它将列出从最低到最高的价格),然后接口在接口处调用排序函数,最后在主函数中调用接口 项目列表如下所示C 根据关键字合并排序,c,C,在这个程序中,它就像一个书店,有价格、相关性、明星和ID等信息, 程序需要根据价格(从最低到最高)对列表进行排序。该程序只需要实现一个合并排序(排序将采用一个参数compare_on_price,因此它将列出从最低到最高的价格),然后接口在接口处调用排序函数,最后在主函数中调用接口 项目列表如下所示 Stars Price Relv ID 4.5 12.49 7.9 1 5 7.99 7.6 2 2 16.99 6.
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:错误:在“,”标记之前应有表达式
part1.c:117:20:警告:传递'mergesort'的参数1将从整数生成指针,而不进行强制转换
part1.c:74:6:注意:应为“int*”,但参数的类型为“int”
part1.c:117:20:警告:传递'mergesort'的参数4将从整数生成指针,而不进行强制转换
#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);
}
#包括
#包括
#包括
文件*fp;
typedef结构书{
双重评级;
双倍价格;
双重关联;
int-ID;
}B;
B*名单;
int read_文件(char*infle,int N)
{
INTC;
如果((fp=fopen(填充,“rb”))
{
fscanf(fp,“%*s\t%*s\t%*s\t%*s\n”);
c=0;
而((!feof(fp))&(c(*(B*)B.评级)
返回1;
其他的
返回0;
}
内部补偿释放(常数无效*a,常数无效*b)
{
如果((*(B*)a.相关性<(*(B*)B.相关性)
返回-1;
否则如果(*(B*)a.相关性>(*(B*)B.相关性)
返回1;
其他的
返回0;
}
内部补偿价格(常数无效*a,常数无效*b)
{
如果((*(B*)a.价格<(*(B*)B.价格)
返回1;
如果(*(B*)a.价格>(*(B*)B.价格)
返回-1;
其他的
返回0;
}
/*稳定排序方法:如果它在smae中保持元素的键相等
它们在te输入中的相对顺序*/
void合并排序(int a[],int low,int high,int(*compar)(常数void*,常数void*))
{
int i=0;
整数长度=高-低+1;
int-pivot=0;
int merge1=0;
int-merge2=0;
整数工作[长度];
如果(低==高)
返回;
枢轴=(低+高)/2;
合并排序(a、低位、枢轴、比较);
合并排序(a、pivot+1、high、compar);
对于(i=0;i
1
void合并排序(int a[],int low,int high,int(*compar)(常数void*,常数void*))
^ ^ ^ ^
数组int函数指针
您的电话:
mergesort(N,0,,N-1, comp_on_price);
^ ^ ^ ^
int int int function name,
a function point is an address of your function
您是否试图理解并修复错误?特别是,如果您实际查看了第117行,您可能会注意到对mergesort的调用不正确-您似乎缺少第一个参数,并且有一个额外的逗号。(包含part1.c:117:20的错误消息表示错误发生在文件part1.c第117行第20列。)@Editors我删除了家庭作业标记。请参阅。
mergesort(N,0,,N-1, comp_on_price);
^ ^ ^ ^
int int int function name,
a function point is an address of your function