Day 27 结构体

1.结构体数组当作形参

和数组传参类似,将结构体数组的数组名当作形参传入函数

void sortStruct(STU student[], int length)

定义一个长度为5的结构体数组,做一个输入输出函数,再将5个人的成绩最大的和成绩最小的找出来,再通过ID来找到某个同学,并把信息输出,再做一个用名字查人的函数

#include<stdio.h>
#include<string.h>

//1001 张惠妹 22 97.5
//1005 蔡依林 23 78.5
//1003 蔡徐坤 22 88.9
//1004 蔡徐坤 20 74.3
//1002 袁咏琳 17 89.6

typedef struct stu
{
	int id;
	char name[20];
	int age;
	float score;
}STU;

void scanfStruct(STU student[], int length);
void printStruct(STU student[], int length);
void sortStruct(STU student[], int length);
STU maxStruct(STU *pstudent, int length);
STU minStruct(STU *pstudent, int length);

STU findId(STU *pstudent, int length, int id);
void findName(STU *pstudent, int length, char pname[]);

int main()
{
	STU student[5];
	scanfStruct(student, 5);
	printStruct(student, 5);
	sortStruct(student, 5);
	printStruct(student, 5);
	STU ret = maxStruct(student, 5);//最大值 
	printf("%d, %s, %d, %.1f\n", ret.id, ret.name, ret.age, ret.score);
	
	STU ret1 = minStruct(student, 5);//最小值 
	printf("%d, %s, %d, %.1f\n", ret1.id, ret1.name, ret1.age, ret1.score);

	STU ret2 = findId(student, 5, 1010);
	if(ret2.id != 0)
	{
		printf("%d, %s, %d, %.1f\n", ret2.id, ret2.name, ret2.age, ret2.score);
	}
	else
	{
		printf("没有找到该学号\n");
	}
	
	findName(student, 5, "蔡徐坤");

	
	
	
	return 0;
}

void scanfStruct(STU student[], int length)
{
	for(int i = 0; i < length; i++)
	{
		scanf("%d%s%d%f", &student[i].id, student[i].name, &student[i].age, &student[i].score);
	}
}

void printStruct(STU student[], int length)
{
	for(int i = 0; i < length; i++)
	{
		printf("%d, %s, %d, %1.f\n", student[i].id, student[i].name, student[i].age, student[i].score);
	}
	printf("\n");
}

void sortStruct(STU student[], int length)
{
	STU temp;
	for(int i = 0; i < length - 1; i++)
	{
		for(int j = 0; j < length - 1 - i; j++)
		{
			if(student[j].score > student[j+1].score)
			{
				temp = student[j];
				student[j] = student[j+1];
				student[j+1] = temp;
			}	
		}	
	}	
}

STU maxStruct(STU *pstudent, int length)
{
	STU temp;
	float maxScore = pstudent[0].score;
	for(int i = 0; i < length; i++)
	{
		if(maxScore < pstudent[i].score)
		{
			maxScore = pstudent[i].score;
			temp = pstudent[i];
		}
	}
	return temp;
}

STU minStruct(STU *stu, int length)
{
	STU temp;
	int i; 
	float minScore = stu[0].score;
	for(i = 0; i < length; i++)
	{
		if(minScore > stu[i].score)
		{
			minScore = stu[i].score;
			temp = stu[i];
		}
	}
	return temp;
}


STU findId(STU *pstudent, int length, int id)
{
	int flag = 0;
	STU temp = {0, '\0', 0, 0}; 
	for(int i = 0; i < length; i++)
	{
		if(id == pstudent[i].id)
		{
			flag = 1;
			return pstudent[i];
		}
	}
	if(flag == 0)
	{
		return temp;
	}	
}

void findName(STU *pstudent, int length, char pname[])
{
	int flag = 0;
	STU temp = {0, '\0', 0, 0}; ;
	for(int i = 0; i < length; i++)
	{
		if(strcmp(pstudent[i].name, pname) == 0)
		{
			flag = 1;
			printf("%d, %s, %d, %1.f\n", pstudent[i].id, pstudent[i].name, pstudent[i].age, pstudent[i].score);
		}

	}
	if(flag == 0)
	{
		printf("没有找到该名字\n");
	}
}

2.结构体嵌套指针

我们可以在定义一个结构体的时候,定义一个指针,这个指针指向另一个结构体

typedef struct home
{
	int id;
	char name[20];
	char addr[20];
}HOME;

typedef struct per
{
	char name[20];
	int age;
	HOME *home;	
} PER;

然后自己输入值,用冒泡排序根据年龄的大小,将输入的信息从小到大排序

#include<stdio.h>
#include<string.h>

typedef struct home
{
	int id;
	char name[20];
	char addr[20];
}HOME;

typedef struct per
{
	char name[20];
	int age;
	HOME *home;	
} PER;

void scanfStruct(PER *per);
void printStruct(PER *per); 
void sortStruct(PER *per);

int main()
{
	HOME home = {1001, "lilei", "西安"};
	PER person1 = {
	.name = "lilei",
	.age = 23,
	.home = &home}, person2;
//	printf("%s, %d, %d, %s, %s\n", person1.name, person1.age, person1.home->id, person1.home->name, person1.home->addr);
	PER per[6];
	scanfStruct(per);
	printStruct(per);
	sortStruct(per);
	printStruct(per);
	
	 
}

void scanfStruct(PER *per)
{
	HOME home = {0,NULL,NULL};
	printf("请输入姓名,年龄,学号,房产证名字,地址\n");
	for(int i = 0; i < 6; i++)
	{
		per[i].home = &home;
		scanf("%s%d%d%s%s", per[i].name, &per[i].age, &home.id, home.name, home.addr);
		per[i].home->id = home.id;
		strcpy(per[i].home->name, home.name);
		strcpy(per[i].home->addr, home.addr);
	}
	
}

void printStruct(PER *per)
{
	for(int i = 0; i < 6; i++)
	printf("%s, %d, %d, %s, %s\n", per[i].name, per[i].age, per[i].home->id, per[i].home->name, per[i].home->addr);	
	printf("\n");
}

void sortStruct(PER *per)
{
	PER temp;
	for(int i = 0; i < 6; i++)
	{
		for(int j = 0; j < 5 - i; i++)
		{
			if(per[j].age > per[j + 1].age)
			{
				temp = per[j];
				per[j] = per[j + 1];
				per[j + 1] = temp;	
			}	
		}	
	}	
}

这里要记得,给带指针的结构体为他们赋值要分别赋值

有三种方法:

void scanf_struct(PER person[],int length)
{
	int i;
	HOME home = {0,NULL,NULL};
	for(i=0;i<length;i++)
	{
		person[i].home = &home;	
//方法一: 
//		scanf("%s%d",person[i].name,&person[i].age);
//		scanf("%d%s%s",&home.id,home.name,home.addr);
//		person[i].home->id = home.id;
//		strcpy(person[i].home->name,home.name);
//		strcpy(person[i].home->addr,home.addr);	
//方法二: 
//		scanf("%s%d%d%s%s",person[i].name,&person[i].age,&home.id,home.name,home.addr);
//		person[i].home->id = home.id;
//		strcpy(person[i].home->name,home.name);
//		strcpy(person[i].home->addr,home.addr);		
		
//方法三: 	
		scanf("%s%d%d%s%s",person[i].name,&person[i].age,&person[i].home->id,person[i].home->name,person[i].home->addr);

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值