Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c中结构的排序数组_C_Sorting_Struct - Fatal编程技术网

c中结构的排序数组

c中结构的排序数组,c,sorting,struct,C,Sorting,Struct,下面的函数按体积对结构进行排序 void selection_sort(struct cylinder my_cylinders[], int n) { int i, largest = 0; double temp; if (n == 1) return; for (i = 1; i < n; i++) if (my_cylinders[i].volume > my_cylinders[largest].volume) largest

下面的函数按体积对结构进行排序

void selection_sort(struct cylinder my_cylinders[], int n)
{
  int i, largest = 0;
  double temp;

  if (n == 1)
    return;

  for (i = 1; i < n; i++)
    if (my_cylinders[i].volume > my_cylinders[largest].volume)
      largest = i;

  if (largest < n - 1) {
    temp = my_cylinders[n - 1];   // ** Line 77
    my_cylinders[n - 1] = my_cylinders[largest];
    my_cylinders[largest] = temp; // ** Line 79
  }
这是我的结构

struct cylinder {
        double radius;
        double height;
        double weight;
        double volume;
};
和主要

int i, counter = 0; 

fprintf(cFileOut, "#    Radius           Height          Volume          Weight\n");

for (i = 0; i < 6; i++) 
{   
    while (fscanf(cFileIn, "%lf, %lf, %lf\n", &radius, &height,  &weight) != EOF)
{
   my_cylinders[counter].radius = radius;
   my_cylinders[counter].height = height;
   my_cylinders[counter].volume = volume;
   my_cylinders[counter].weight = weight;

   volume = PI * radius * radius * height;  
   fprintf(cFileOut, "%-d\t  %-12.6lf\t  %-12.6lf\t  %-12.6lf\t   %-12.6lf \n", 
    counter, radius, height, volume, weight);
    counter++;      
}
}

selection_sort(my_cylinders, counter);
inti,计数器=0;
fprintf(cFileOut,“#半径高度体积重量”);
对于(i=0;i<6;i++)
{   
而(fscanf(cFileIn、%lf、%lf、%lf\n、&半径、高度和重量)!=EOF)
{
my_圆柱体[计数器]。半径=半径;
my_圆柱体[计数器]。高度=高度;
my_圆柱体[计数器]。体积=体积;
my_气缸[计数器]。重量=重量;
体积=圆周率*半径*半径*高度;
fprintf(cFileOut,“%-d\t%-12.6lf\t%-12.6lf\t%-12.6lf\t%-12.6lf\n”,
计数器、半径、高度、体积、重量);
计数器++;
}
}
选择和排序(我的气缸、计数器);

我想我理解这些错误,但不知道如何修复它们。我尝试过改变类型,但在结构方面我缺少了一些东西

将温度类型从
double
更改为
struct-cyland

还请注意,您只是根据卷从结构中查找最大的项,并将其与最后一个索引交换。那不是分类

要排序,必须在循环中调用此函数,同时每次将计数器递减1

类似于“

并将函数内的if条件更改为:

if (n == 0)// since n=0 will mean that last item has been reached
    return;

第77行和第79行是什么?你怎么称呼它?你的函数显然不完整…@Carcigenicate,我对代码中的行号进行了注释。我刚刚用附加代码编辑了文章。@Deduplicator…谢谢。你想详细说明吗?
temp=my_圆柱体[n-1];
temp是一个数字,但您的圆柱体数组包含结构。您不能将结构分配给double。
while(counter>=0){
  selection_sort(my_cylinders, counter);
  counter--;
}
if (n == 0)// since n=0 will mean that last item has been reached
    return;