user-0 提问: 结构体-不进入写好的排序算法。

回复:
user: user-0
content: 引用 7 楼 火花20180731 的回复:请参考以下排序算法代码：  /* 冒泡排序 */ void bubble_sort(int *data, int size) {     int i;     for (i = 0; i < size - 1; ++i)     {         int ordered = 1;         int j;         for (j = 0; j < size - 1 - i;++j)         {             if (data[j+1] < data[j])             {                 int swap = data[j];                 data[j] = data[j+1];                 data[j+1] = swap;                 ordered = 0;             }         }         if (ordered)         {             break;         }     } } /* 插入排序 */ void insert_sort(int* data, int size) {     int i;     for (i = 1; i < size; ++i)     {         int inserted = data[i];         int j;         for (j = i; (j > 0) && (inserted < data[j-1]); --j)         {             data[j] = data[j-1];         }         if (j != i)         {             data[j] = inserted;         }     } } /* 选择排序 */ void select_sort(int* data, int size) {     int i;     for (i = 0; i < size - 1; ++i)      {         int min = i, j;         for (j = i + 1; j < size; ++j)         {             if (data[j] < data[min])             {                 min = j;             }         }         if (min != i)         {             int swap = data[i];             data[i] = data[min];             data[min] = swap;         }     } } /* 快速排序 */ void quick_sort(int* data, int left, int right) {     int p = (left + right) / 2;     int pivot = data[p];     int i = left, j = right;     while (i < j)     {         for (; ! (i >= p || pivot <data[i]); ++i);         if (i < p)         {             data[p] = data[i];             p = i;         }         for (; ! (j <= p || data[j]< pivot); --j);         if (j > p)         {             data[p] = data[j];             p = j;         }     }     data[p] = pivot;     if (p - left > 1)     {         quick_sort (data, left, p - 1);     }     if (right - p > 1)     {         quick_sort (data, p + 1, right);     } }根据您所提供的算法，我作了如下修改，可还是不能 排序 double computerAvg(struct student stu, int n) {     int i;     double sum = 0.0;     for (i = 0; i < n; i++)     {         sum += stu.sc[i].score;              }     return sum / n; } double maxavg(struct student *stu)  /* 找最大的平均分 */ {     struct student temp;     int index;     int i=0,j;     stu[i].cavg = computerAvg(stu[i], 3);  /* !!!!!!!!! */     for (i = 0; i < 8; i++) //冒泡排序算法 	{         index=i; 		for (j = i+1; j < 9; j++) 		{              			if (stu[j].cavg>stu[index].cavg) 			{ 				index=j; 			} 		}         temp=stu[index];         stu[index]=stu[i];         stu[i]=temp; 	}     printf("The Avg max is ");     printinfos(stu[0]);     printf("cavg:%lf",stu[0].cavg); } void printinfos(struct student stu) {     printf("sno:%s", stu.sno);     printf("name:%s", stu.name); } 引用 7 楼 火花20180731 的回复:请参考以下排序算法代码：  /* 冒泡排序 */ void bubble_sort(int *data, int size) {     int i;     for (i = 0; i < size - 1; ++i)     {         int ordered = 1;         int j;         for (j = 0; j < size - 1 - i;++j)         {             if (data[j+1] < data[j])             {                 int swap = data[j];                 data[j] = data[j+1];                 data[j+1] = swap;                 ordered = 0;             }         }         if (ordered)         {             break;         }     } } /* 插入排序 */ void insert_sort(int* data, int size) {     int i;     for (i = 1; i < size; ++i)     {         int inserted = data[i];         int j;         for (j = i; (j > 0) && (inserted < data[j-1]); --j)         {             data[j] = data[j-1];         }         if (j != i)         {             data[j] = inserted;         }     } } /* 选择排序 */ void select_sort(int* data, int size) {     int i;     for (i = 0; i < size - 1; ++i)      {         int min = i, j;         for (j = i + 1; j < size; ++j)         {             if (data[j] < data[min])             {                 min = j;             }         }         if (min != i)         {             int swap = data[i];             data[i] = data[min];             data[min] = swap;         }     } } /* 快速排序 */ void quick_sort(int* data, int left, int right) {     int p = (left + right) / 2;     int pivot = data[p];     int i = left, j = right;     while (i < j)     {         for (; ! (i >= p || pivot <data[i]); ++i);         if (i < p)         {             data[p] = data[i];             p = i;         }         for (; ! (j <= p || data[j]< pivot); --j);         if (j > p)         {             data[p] = data[j];             p = j;         }     }     data[p] = pivot;     if (p - left > 1)     {         quick_sort (data, left, p - 1);     }     if (right - p > 1)     {         quick_sort (data, p + 1, right);     } } 引用 7 楼 火花20180731 的回复:请参考以下排序算法代码：  /* 冒泡排序 */ void bubble_sort(int *data, int size) {     int i;     for (i = 0; i < size - 1; ++i)     {         int ordered = 1;         int j;         for (j = 0; j < size - 1 - i;++j)         {             if (data[j+1] < data[j])             {                 int swap = data[j];                 data[j] = data[j+1];                 data[j+1] = swap;                 ordered = 0;             }         }         if (ordered)         {             break;         }     } } /* 插入排序 */ void insert_sort(int* data, int size) {     int i;     for (i = 1; i < size; ++i)     {         int inserted = data[i];         int j;         for (j = i; (j > 0) && (inserted < data[j-1]); --j)         {             data[j] = data[j-1];         }         if (j != i)         {             data[j] = inserted;         }     } } /* 选择排序 */ void select_sort(int* data, int size) {     int i;     for (i = 0; i < size - 1; ++i)      {         int min = i, j;         for (j = i + 1; j < size; ++j)         {             if (data[j] < data[min])             {                 min = j;             }         }         if (min != i)         {             int swap = data[i];             data[i] = data[min];             data[min] = swap;         }     } } /* 快速排序 */ void quick_sort(int* data, int left, int right) {     int p = (left + right) / 2;     int pivot = data[p];     int i = left, j = right;     while (i < j)     {         for (; ! (i >= p || pivot <data[i]); ++i);         if (i < p)         {             data[p] = data[i];             p = i;         }         for (; ! (j <= p || data[j]< pivot); --j);         if (j > p)         {             data[p] = data[j];             p = j;         }     }     data[p] = pivot;     if (p - left > 1)     {         quick_sort (data, left, p - 1);     }     if (right - p > 1)     {         quick_sort (data, p + 1, right);     } }根据您提供的算法，我做了如下修改，还是不能排序。 double computerAvg(struct student stu, int n) {     int i;     double sum = 0.0;     for (i = 0; i < n; i++)     {         sum += stu.sc[i].score;              }     return sum / n; } double maxavg(struct student *stu)  /* 找最大的平均分 */ {     struct student temp;     int index;     int i=0,j;     stu[i].cavg = computerAvg(stu[i], 3);  /* !!!!!!!!! */     for (i = 0; i < 8; i++) //冒泡排序算法 	{         index=i; 		for (j = i+1; j < 9; j++) 		{              			if (stu[j].cavg>stu[index].cavg) 			{ 				index=j; 			} 		}         temp=stu[index];         stu[index]=stu[i];         stu[i]=temp; 	}     printf("The Avg max is ");     printinfos(stu[0]);     printf("cavg:%lf",stu[0].cavg); } void printinfos(struct student stu) {     printf("sno:%s", stu.sno);     printf("name:%s", stu.name); }

user: user-1
content: 请参考以下排序算法代码：  /* 冒泡排序 */ void bubble_sort(int *data, int size) {     int i;     for (i = 0; i < size - 1; ++i)     {         int ordered = 1;         int j;         for (j = 0; j < size - 1 - i;++j)         {             if (data[j+1] < data[j])             {                 int swap = data[j];                 data[j] = data[j+1];                 data[j+1] = swap;                 ordered = 0;             }         }         if (ordered)         {             break;         }     } } /* 插入排序 */ void insert_sort(int* data, int size) {     int i;     for (i = 1; i < size; ++i)     {         int inserted = data[i];         int j;         for (j = i; (j > 0) && (inserted < data[j-1]); --j)         {             data[j] = data[j-1];         }         if (j != i)         {             data[j] = inserted;         }     } } /* 选择排序 */ void select_sort(int* data, int size) {     int i;     for (i = 0; i < size - 1; ++i)      {         int min = i, j;         for (j = i + 1; j < size; ++j)         {             if (data[j] < data[min])             {                 min = j;             }         }         if (min != i)         {             int swap = data[i];             data[i] = data[min];             data[min] = swap;         }     } } /* 快速排序 */ void quick_sort(int* data, int left, int right) {     int p = (left + right) / 2;     int pivot = data[p];     int i = left, j = right;     while (i < j)     {         for (; ! (i >= p || pivot <data[i]); ++i);         if (i < p)         {             data[p] = data[i];             p = i;         }         for (; ! (j <= p || data[j]< pivot); --j);         if (j > p)         {             data[p] = data[j];             p = j;         }     }     data[p] = pivot;     if (p - left > 1)     {         quick_sort (data, left, p - 1);     }     if (right - p > 1)     {         quick_sort (data, p + 1, right);     } }

user: user-0
content: 引用 10 楼 火花20180731 的回复:[quote=引用 9 楼 风吟xqy 的回复:] 还是折腾出来了，就是多加了一段循环，和之前只改变了平均分的位置，没改变学号和姓名 double computerAvg(struct student stu, int n) {     int i;     double sum = 0.0;     for (i = 0; i < n; i++)     {         sum += stu.sc[i].score;              }     return sum / n; } double maxavg(struct student *stu)  /* 找最大的平均分 */ {     struct student temp;     int index;     int i,j;     for (int k = 0; k < 8; k++)     {         stu[k].cavg = computerAvg(stu[k], 3);  /* !!!!!!!!! */     }     for (i = 0; i < 8; i++) //冒泡排序算法 	{         index=i; 		for (j = i+1; j < 9; j++) 		{              			if (stu[j].cavg>stu[index].cavg) 			{ 				index=j; 			} 		}         temp=stu[index];         stu[index]=stu[i];         stu[i]=temp; 	}     printf("The Avg max is ");     printinfos(stu[0]);     printf("cavg:%.2lf",stu[0].cavg); }  所以你的问题不在于排序算法，而在于平均值的复制不对，不是所有的结构体对象都被赋予了平均值[/quote]是的，只获取了一个平均值，无法进行比较。

user: user-1
content: 引用 9 楼 风吟xqy 的回复:还是折腾出来了，就是多加了一段循环，和之前只改变了平均分的位置，没改变学号和姓名 double computerAvg(struct student stu, int n) {     int i;     double sum = 0.0;     for (i = 0; i < n; i++)     {         sum += stu.sc[i].score;              }     return sum / n; } double maxavg(struct student *stu)  /* 找最大的平均分 */ {     struct student temp;     int index;     int i,j;     for (int k = 0; k < 8; k++)     {         stu[k].cavg = computerAvg(stu[k], 3);  /* !!!!!!!!! */     }     for (i = 0; i < 8; i++) //冒泡排序算法 	{         index=i; 		for (j = i+1; j < 9; j++) 		{              			if (stu[j].cavg>stu[index].cavg) 			{ 				index=j; 			} 		}         temp=stu[index];         stu[index]=stu[i];         stu[i]=temp; 	}     printf("The Avg max is ");     printinfos(stu[0]);     printf("cavg:%.2lf",stu[0].cavg); }  所以你的问题不在于排序算法，而在于平均值的复制不对，不是所有的结构体对象都被赋予了平均值

user: user-0
content: 还是折腾出来了，就是多加了一段循环，和之前只改变了平均分的位置，没改变学号和姓名 double computerAvg(struct student stu, int n) {     int i;     double sum = 0.0;     for (i = 0; i < n; i++)     {         sum += stu.sc[i].score;              }     return sum / n; } double maxavg(struct student *stu)  /* 找最大的平均分 */ {     struct student temp;     int index;     int i,j;     for (int k = 0; k < 8; k++)     {         stu[k].cavg = computerAvg(stu[k], 3);  /* !!!!!!!!! */     }     for (i = 0; i < 8; i++) //冒泡排序算法 	{         index=i; 		for (j = i+1; j < 9; j++) 		{              			if (stu[j].cavg>stu[index].cavg) 			{ 				index=j; 			} 		}         temp=stu[index];         stu[index]=stu[i];         stu[i]=temp; 	}     printf("The Avg max is ");     printinfos(stu[0]);     printf("cavg:%.2lf",stu[0].cavg); }

user: user-0
content: 引用 3 楼 xws245925587 的回复:double maxavg(struct student *stu)  /* 找最大的平均分 */ {     int temp;     int i,j;                                                        //========================》i  并没有赋值，vs居然不报错，神奇     stu[i].cavg = computerAvg(stu[i], 3);  /* !!!!!!!!! */  //===》就用stu[i] ？？？ 用这个不应该是 stu+i ？？要么就是 &(stu[i])     for (i = 0; i < 8; i++) //冒泡排序算法     {         for (j = i; j < 8; j++)         {             if (stu[i].cavg< stu[j].cavg)             {                 temp = stu[i].cavg;         /*交换数组元素*/                 stu[i].cavg = stu[j].cavg;                 stu[j].cavg = temp;             }         }     }     printf("The Avg max is ");     printinfos(stu[0]);     printf("cavg:%lf",stu[0].cavg); }   你这个写法有问题啊，所以大佬，我应该改成什么？

user: user-0
content: 引用 4 楼 风吟xqy 的回复:[quote=引用 1 楼 ?Victor.?? 的回复:] double maxavg(struct student *stu)  /* 找最大的平均分 */ {     student temp;     int i,j;     for(i = 0; i < 8; i++)     	stu[i].cavg = computerAvg(stu[i], 3);  /* !!!!!!!!! */     for (i = 0; i < 8; i++) //冒泡排序算法 	{ 		for (j = i; j < 8 - i - 1; j++) 		{ 			if (stu[i].cavg< stu[j].cavg) 			{ 				temp = stu[i];         /*交换数组元素*/ 				stu[i] = stu[j]; 				stu[j] = temp; 			} 		} 	}     printf("The Avg max is ");     printinfos(stu[0]);     printf("cavg:%lf",stu[0].cavg); }这样改了不会哎[/quote]  引用 1 楼 ?Victor.?? 的回复:double maxavg(struct student *stu)  /* 找最大的平均分 */ {     student temp;     int i,j;     for(i = 0; i < 8; i++)     	stu[i].cavg = computerAvg(stu[i], 3);  /* !!!!!!!!! */     for (i = 0; i < 8; i++) //冒泡排序算法 	{ 		for (j = i; j < 8 - i - 1; j++) 		{ 			if (stu[i].cavg< stu[j].cavg) 			{ 				temp = stu[i];         /*交换数组元素*/ 				stu[i] = stu[j]; 				stu[j] = temp; 			} 		} 	}     printf("The Avg max is ");     printinfos(stu[0]);     printf("cavg:%lf",stu[0].cavg); }我这样改了不对

user: user-0
content: 引用 1 楼 ?Victor.?? 的回复:double maxavg(struct student *stu)  /* 找最大的平均分 */ {     student temp;     int i,j;     for(i = 0; i < 8; i++)     	stu[i].cavg = computerAvg(stu[i], 3);  /* !!!!!!!!! */     for (i = 0; i < 8; i++) //冒泡排序算法 	{ 		for (j = i; j < 8 - i - 1; j++) 		{ 			if (stu[i].cavg< stu[j].cavg) 			{ 				temp = stu[i];         /*交换数组元素*/ 				stu[i] = stu[j]; 				stu[j] = temp; 			} 		} 	}     printf("The Avg max is ");     printinfos(stu[0]);     printf("cavg:%lf",stu[0].cavg); }这样改了不会哎

user: user-2
content: double maxavg(struct student *stu)  /* 找最大的平均分 */ {     int temp;     int i,j;                                                        //========================》i  并没有赋值，vs居然不报错，神奇     stu[i].cavg = computerAvg(stu[i], 3);  /* !!!!!!!!! */  //===》就用stu[i] ？？？ 用这个不应该是 stu+i ？？要么就是 &(stu[i])     for (i = 0; i < 8; i++) //冒泡排序算法     {         for (j = i; j < 8; j++)         {             if (stu[i].cavg< stu[j].cavg)             {                 temp = stu[i].cavg;         /*交换数组元素*/                 stu[i].cavg = stu[j].cavg;                 stu[j].cavg = temp;             }         }     }     printf("The Avg max is ");     printinfos(stu[0]);     printf("cavg:%lf",stu[0].cavg); }   你这个写法有问题啊，

user: user-3
content: double computerAvg(struct student stu, int n) ==》》 double computerAvg(struct student *stu, int n) 或 double computerAvg(struct student stu[], int n)

user: user-4
content: double maxavg(struct student *stu)  /* 找最大的平均分 */ {     student temp;     int i,j;     for(i = 0; i < 8; i++)     	stu[i].cavg = computerAvg(stu[i], 3);  /* !!!!!!!!! */     for (i = 0; i < 8; i++) //冒泡排序算法 	{ 		for (j = i; j < 8 - i - 1; j++) 		{ 			if (stu[i].cavg< stu[j].cavg) 			{ 				temp = stu[i];         /*交换数组元素*/ 				stu[i] = stu[j]; 				stu[j] = temp; 			} 		} 	}     printf("The Avg max is ");     printinfos(stu[0]);     printf("cavg:%lf",stu[0].cavg); }