c基础 day11

思维导图

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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值