c语言结构体数组怎么交换?

来源:百度知道 编辑:UC知道 时间:2024/05/17 23:35:59
我有一个结构体数组
struct cj
{
int No;
char Name[20];
int cj1;
int cj2;
int cj3;
int pingjun;
int Number;
};
我申明了struct cj student[50];的数组;
比如我数组中有3个元素
学号1 名字qq 平均分33;
学号2 名字ww 平均分44;
学号3 名字cc 平均分38;
我想用冒泡排序法把这3个元素按平分高低来排,怎么才可以实现啊?急求
哪个朋友告诉一下~

定义一个struct cj temp;
然后就直接交换啊。

我把那块做成函数,楼主直接调用吧,主函数就没写完整了。

#include <stdio.h>
struct cj
{
int No;
char Name[20];
int cj1;
int cj2;
int cj3;
int pingjun;
int Number;
};
int main(int argc, char *argv[])
{
struct cj student[50];
void sort(struct cj *student,int n);
sort(student,50);
return 0;
}

void sort(struct cj *student,int n)/*从大到小的*/
{
int flag;
int i,j;
struct cj temp;
for(i=0;i<n-1;i++)
{
flag=0;
for(j=0;j<n-i-1;j++)
if(student[j].pingjun<student[j+1].pingjun)
{
flag=1;
temp=student[j+1];
student[j+1]=student[j];
student[j]=temp;
}
if(flag==0)
break;
}
}

void swap(struct cj *a , struct cj *b)
{
struct cj temp ;
temp = *a ;
*a = *b ;
*b = temp ;