- 从堆区申请能存5个结构体变量的数组的空间,完成数组中成员的输入,根据学生成绩,用选择排序的方式,对学生排序并输出。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char name[20];
float score;
}stu;void input(stu p[],int n);
void paixu(stu p[],int n);
void output(stu p[],int n);int main(int argc, const char *argv[])
{
stu *p=(stu*)malloc(sizeof(stu)*5);
input(p,5);
paixu(p,5);
output(p,5);
return 0;
}void input(stu p[],int n)
{
for(int i=0;i<n;i++)
{
printf("请输入第%d个学生姓名:",i+1);
scanf("%s",p[i].name);
printf("请输入第%d个学生成绩:",i+1);
scanf("%f",&p[i].score);
}}
void paixu(stu p[],int n)
{
for(int i=0;i<n-1;i++)
{
for (int j=i;j<n-1;j++ )
{
if(p[i].score>p[j+1].score)
{
int temp=p[j+1].score;
p[j+1].score=p[i].score;
p[i].score=temp;
}
}
}
}
void output(stu p[],int n)
{
for(int i=0;i<n;i++)
printf("姓名:%s 成绩:%f\n",p[i].name,p[i].score);
}
- 求以下结构体的大小
typedef struct {
int id; //4
char name[50]; //50
char grade[3]; //3 //3
} Student; //60
typedef struct {
int id; //4
char name[50]; //50 //2
Student student; //60 //4
} Teacher; //120
typedef struct {
int id;
char name[50];
Teacher teacher;
} Course;