#include
#include
//定义新的数据类型
struct student{
int age;
float score;
char name[100];
};
//动态构造一维数组
void f(struct student **q, int changdu){
*q = (struct student *)malloc(sizeof(struct student) * changdu);
}
//输入学生信息
void Input(struct student *r, int cd){
int m;
for (m=0; m printf("请输入第%d个学生的信息:\n", m+1);
printf("age = ");
scanf("%d", &(r+m)->age); //(*r)[m].age 等价于r[m]->age,以下类推
printf("score = ");
scanf("%f", &(r+m)->score);
printf("name = ");
scanf("%s", (r+m)->name);
}
}
//冒泡排序
void g(struct student *s, int chdu){
int i, j;
struct student t;
for (i=0; i for (j=i+1; j if ((s+i)->score > (s+j)->score){
struct student temp;
temp=*(s+i);*(s+i)=*(s+j);*(s+j)=temp;
}
}
}
printf("\n\n\n");
}
//输出学生信息
void Output(struct student * w, int chd){
int u;
for (u=0; u printf("第%d个学生信息是:\n", u+1);
printf("age = %d\n", (w+u)->age);
printf("score = %f\n", (w+u)->score);
printf("name = %s\n", (w+u)->name);
}
}
int main(void){
int len; //定义变量p:数组元素个数
struct student *p; //定义struct student类型指针变量 p
printf("请输入学生个数:\n");
printf("len = ");
scanf("%d", &len);
f(&p, len); //动态构造一维数组
Input(p, len); //输入学生信息
g(p, len); //冒泡排序
Output(p, len); //输出学生信息
return 0;
}