//全部使用结构体指针数组
#include<stdio.h>
struct student
{
int num;
char name[8];
char sex;
int age;
double grade;
};
int i,j;
int main()
{
struct student stu[10],*p[10],temp;
for(i=0;i<10;i++)
{
p[i]=&stu[i];
scanf("%d %s %c %d %lf",&p[i]->num,p[i]->name,&p[i]->sex,&p[i]->age,&p[i]->grade);
}
void sort();
sort(p);
printf("------output------\n学号\t姓名\t性别\t年龄\t成绩\n");
for(i=0;i<10;i++)
{
p[i]=stu+i;
printf("%d\t%s\t%c\t%d\t%0.1lf\n",p[i]->num,p[i]->name,p[i]->sex,p[i]->age,p[i]->grade);
}
return 0;
}
void sort(struct student *x[] )
{
int i=0,j=0;
struct student temp;
for (;i<9;i++)
{
for(j=0;j<9-i;j++)
{
if (x[j]->grade > x[j+1]->grade)
{
{temp=*x[j];
*x[j]=*x[j+1];
*x[j+1]=temp;
}
}
}
}
}
/*101 Zhang M 19 95.6
102 Wang F 18 92.2
103 Zhao M 19 85.7
104 Li M 20 96.3
105 Gao M 19 90.2
106 Lin M 18 91.2
107 Ma F 18 98.7
108 Zhen M 21 88.7
109 Xu M 19 90.1
110 Mao F 22 94.7*/
?//这个是排序同时给指针数组赋值
#include<stdio.h>
struct student
{
int num;
char name[8];
char sex;
int age;
double grade;
};
int i,j;
int main()
{
struct student *stu[10],input[10],temp;
void inputdata();
inputdata(stu,input,temp);
void outputdata();
outputdata(stu);
return 0;
}
void inputdata(struct student *stu[],struct student input[10],struct student temp)
{
printf("\n------input------\n");
for(i=0;i<10;i++)//输入
{
scanf("%d%s %c%d%lf",&input[i].num,input[i].name,&input[i].sex,&input[i].age,&input[i].grade);
}
for(i=0;i<9;i++)
{
for(j=0;j<9-i;j++)
{
if(input[j].grade>input[j+1].grade)
{
temp=input[j];
input[j]=input[j+1];
input[j+1]=temp;
}
}stu[j]=&input[j];//指针数组赋值
}
stu[0]=&input[0];//第一项没有参与循环
}
void outputdata(struct student *stu[])
{
printf("\n------output------\n");
printf("\n学号\t姓名\t性别\t年龄\t成绩\n");
for(i=0;i<10;i++)
{
printf("%d\t%s\t%c\t%d\t%0.1lf\n",(*stu[i]).num,(*stu[i]).name,(*stu[i]).sex,(*stu[i]).age,(*stu[i]).grade);
}
}
/*101 Zhang M 19 95.6
102 Wang F 18 92.2
103 Zhao M 19 85.7
104 Li M 20 96.3
105 Gao M 19 90.2
106 Lin M 18 91.2
107 Ma F 18 98.7
108 Zhen M 21 88.7
109 Xu M 19 90.1
110 Mao F 22 94.7*/
?
|