|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<malloc.h>
struct student
{
int number ;
char name[100];
float score;
};
void sortS(struct student *q , int len)
{
int i;
int j;
struct student t;
for( i = 0; i<len ; i++)
{
for( j =0 ; j < len -1 -i ; j++)
{
if( q[j].score > q[j+1].score )
{
t = q[j];
q[j] = q[j+1] ;
q[j+1] = t;
}
}
}
for(int o= 0 ; o<len ; o ++)
{
printf("第%d个学生信息为 :", o+1 );
printf("学号:%d\n",q[o].number);
printf("姓名:%s\n",q[o].name);
printf("分数:%f\n",q[o].score);
}
}
void sortN(struct student *r , int len)
{
int i;
int j;
struct student t;
for( i = 0; i<len ; i++)
{
for( j =0 ; j < len -1 -i ; j++)
{
if( r[j].number> r[j+1].number )
{
t = r[j];
r[j] = r[j+1] ;
r[j+1] = t;
}
}
}
for(int k= 0 ; k<len ; k ++)
{
printf("第%d个学生信息为 :", k+1 );
printf("学号:%d\n",r[k].number);
printf("姓名:%s\n",r[k].name);
printf("分数:%f\n",r[k].score);
}
}
int main()
{
printf("请输入学生个数 : ");
int len;
scanf("%d",&len);
struct student *p;
p = (struct student *) malloc (len* sizeof(struct student ));
for(int i = 0; i < len ; i++)
{
printf("请输入第%d个学生信息 :\n",i+1);
printf("学号 number : \n");
scanf("%d", &p[i].number);
printf("姓名 name : \n");
scanf("%s",p[i].name);
printf("分数 score : \n");
scanf("%f",&p[i].score);
}
printf("请选择你想要的输出结果\n");
printf("按分数排序请按 1 \n");
printf("按学号排序请按 2 \n");
int c;
scanf("%d",&c);
switch(c)
{
case1:sortS(p ,len);
break;
case2:sortN(p ,len);
break;
}
return 0;
} |
|