思维导图
2.从堆区申请能存5个结构体变量的数组的空间,完成数组中成员的输入,根据学生成绩,用选择排序的方式,对学生排序并输出。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Stu
{
char name[20];
int score;
}s;
void Input(struct Stu *p,int n);
void Select(struct Stu *p,int n);
void Output(struct Stu *p,int n);
int main(int argc, const char *argv[])
{
struct Stu *p=(struct Stu*)malloc(sizeof(struct Stu)*5);
printf("how many students:");
int n;
scanf("%d",&n);
getchar();
putchar(10);
Input(p,n);
Select(p,n);
Output(p,n);
return 0;
}
void Input(struct Stu *p,int n)
{
for(int i=0;i<n;i++)
{
printf("student%d's name:",i+1);
gets((p+i)->name);
printf("student%d's score:",i+1);
scanf("%d",&(p+i)->score);
getchar();
putchar(10);
}
}
void Select(struct Stu *p,int n)
{
struct Stu t;
for(int i=0;i<n-1;i++)
{
int max=i;
for(int j=i+1;j<n;j++)
{
if((p+j)->score>(p+max)->score)
{
max=j;
}
}
if(max!=i)
{
t=*(p+max);
*(p+max)=*(p+i);
*(p+i)=t;
}
}
}
void Output(struct Stu* p,int n)
{
for(int i=0;i<n;i++)
{
printf("number%d is:",i+1);
puts((p+i)->name);
printf("score:%d",(p+i)->score);
putchar(10);
}
}
3.求结构体大小
typedef struct {
int id;
char name[50];
char grade[3];
} Student;
typedef struct {
int id;
char name[50];
Student student;
} Teacher;
typedef struct {
int id;
char name[50];
Teacher teacher;
} Course;
Student:60
Teacher:116
Course:172