1:从堆区申请能存5个结构体变量的数组的空间,完成数组中成员的输入,根据学生成绩,用选择排序的方式,对学生排序并输出。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Stu
{
char name[100];
int age;
double sorce;
double high;
};
void jiandan(struct Stu*p);
int main(int argc, const char *argv[])
{
struct Stu *p=(struct Stu*)malloc(5*sizeof(struct Stu));
for(int i=0;i<5;i++)
{
// putchar(10);
// getchar();
printf("请输入第%d个同学的名字:",i+1);
printf("%s",((p+i)->name));
// getchar();
gets((p+i)->name);
}
for(int i=0;i<5;i++)
{
printf("请输入第%d个同学的的成绩:",i+1);
scanf("%lf",&(p+i)->sorce);
// printf("%lf\n",(p+i)->sorce);
}
jiandan(p);
for(int i=0;i<5;i++)
{
printf("%s ",(p+i)->name);
printf("%.2lf",(p+i)->sorce);
putchar(10);
}
free(p);
return 0;
}
void jiandan(struct Stu *p)
{
int m;
for(int i=0;i<4;i++)
{
char s[100];
strcpy(s,p->name);
m=i;
for(int j=i+1;j<5;j++)
{
if(((p+m)->sorce)>((p+j)->sorce))
{
m=j;
}
}
if(m!=i)
{
int t=(p+i)->sorce;
(p+i)->sorce=(p+m)->sorce;
(p+m)->sorce=t;
strcpy(s,(p+i)->name);
strcpy((p+i)->name,(p+m)->name);
strcpy((p+m)->name,s);
}
}
}
2:
typedef struct {
int id; //4
char name[50]; //50
char grade[3]; //3
} Student; //60
typedef struct {
int id; //4
char name[50]; //50
Student student; //60
} Teacher; //116
typedef struct {
int id; //4
char name[50]; //50
Teacher teacher; //116
} Course; //172
3思维导图: