运用“函数”来表达:带有“动态分配”的“结构体数组”及“指针”的 学生信息管理系统,附加对结构体成员其一的“成绩”进行“冒泡排序”

前言

初学C语言,请大佬多提宝贵意见,感激不尽。欢迎各位初学者前来交流。

运用“函数”来表达:带有“动态分配”的“结构体数组”及“指针”的 学生信息管理系统,附加对结构体成员其一的“成绩”进行“冒泡排序”

//运用“函数”来表达:带有“动态分配”的“结构体数组”及“指针”的 学生信息管理系统,附加对结构体成员其一的“成绩”进行“冒泡排序”
# include<stdio.h>
# include<malloc.h>

//写在函数顶上,便于所有函数直接调用
struct Student
{
	char name[20];
	char id[5];
	int score[5];
};
typedef struct Student STU;		//简写

void InputStudent(STU *st1,int n);		//输入函数
void sort_score_4(STU *st2,int n);		//排序函数
void OutputStudent(STU *st3,int n);		//输出函数

int main (void)
{
	int len;		//学生人数
	STU *pArr;		//结构体指针,在后文也相当于数组。

	printf("Please enter the number of students\nlen = ");
	scanf("%d", &len);
	printf("Please enter the information for students\n");

	pArr = (STU *)malloc(sizeof(STU)*len);		//对len个学生进行分配动态内存,组成数组,并将首地址赋给指针变量。“指针变量名”相当于“数组名”。

	InputStudent(pArr, len);					//使用地址传送的好处是:没有额外的内存占用,并且读取和处理速度快。
	sort_score_4(pArr,len);
	OutputStudent(pArr,len);

	return 0;
}

//输入函数
void InputStudent(STU *st1,int n)		//使用地址传送的好处是:没有额外的内存占用,并且读取和处理速度快。
{
	int i, j;

	for(i=0; i<n; i++)
	{
		printf("Please enter the information for the No.%d students\n", i+1);
		printf("enter the name ");
		scanf("%s",st1[i].name);		//此处对于字符串变量,不需要添加“&”,原因是name已经是一个“数组地址”.
		printf("enter the ID ");
		scanf("%s",st1[i].id);
		printf("Please enter the students' scores in orders\n");
		for(j=0; j<5; j++)
			scanf("%d",&st1[i].score[j]);		//需要加“&”

	}
	return ;
}

//冒泡排序函数
void sort_score_4(STU *st2,int n)
{
	int i, j;
	STU temp;

	for(i=0; i<n-1; i++)
	{
		for(j=0; j<n-i-1; j++)
		{
			if(st2[j].score[4] > st2[j+1].score[4])		//对结构体中的成员“score”进行排序
			{
				temp = st2[j];				//对结构体进行整体赋值
				st2[j] = st2[j+1];
				st2[j+1] = temp;
			}
		
		}
	}
	return ;
}

//输出函数
void OutputStudent(STU *st3,int n)
{
	int i;
	int j;

	for(i=0; i<n; i++)
	{
		printf("%-15s", st3[i].name);	//注意,与输入scanf的比较,一样。
		printf("%-10s", st3[i].id);
		for(j=0; j<5; j++)				//对成员中的数组进行输出。
		{
			printf("%-4d",st3[i].score[j]);		
		}
		printf("\n");
	
	}
	return ;
}


运行结果:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值