23.08.02数据结构作业

23.08.02数据结构作业_第1张图片

fun.h

 

#ifndef __FUN_H__
#define __FUN_H__
#include
#include
#include
#define MAX 40 //定义班级人数最大值

//枚举类型定义职业
typedef enum 
{
	student,
	teacher
}job;

//定义结构体一个成员的数据结构
typedef struct CY
{
	char name[10];
	char sex[10];
	job job;
	union PD
	{
		int score;
		char course[50];
	}add;	//定义共同体输出分数or课程
}Member;

//定义结构体一个班级的数据结构
typedef struct BJ
{
	//定义了Member类型的数组
	Member members[MAX];
	//计算班级人数
	int count;
}Classroom;


//定义申请堆区班级空间的函数并把人数初始化为0
Classroom* space();

//定义函数判断班级是否满员并向班级中添加成员
void addMember(Classroom* classroom,Member member);

//定义函数展示出当前班级的所有人员信息
void showMember(Classroom* classroom);

//定义函数查找姓名是否是该班成员
void searchMember(Classroom* classroom, const char* name);

//释放空间
void release(Classroom* classroom);
#endif

fun.c 

#include "fun.h"

//定义申请堆区班级空间的函数并把人数初始化为0
Classroom* space()
{
	Classroom* classroom=(Classroom*)malloc(sizeof(Classroom));
	classroom->count=0;
	return classroom;
}

//定义函数判断班级是否满员并向班级中添加成员
void addMember(Classroom* classroom,Member member)
{
	if(classroom->countmembers[classroom->count]=member;
		classroom->count++;
		printf("%s成功加入班级\n",member.name);
	}
	else
	{
		printf("班级人数已满无法添加\n");
	}
}

//定义函数展示出当前班级的所有人员信息
void showMember(Classroom* classroom)
{
	printf("班级成员信息:\n");
	for(int i=0;icount;i++)
	{
		Member member = classroom->members[i];
		if(member.job == student)
		{
			printf("姓名:%s,性别:%s,职业:学生,分数:%d\n",member.name,member.sex,member.add.score);
		}
		else if(member.job==teacher)
		{
			printf("姓名:%s,性别:%s,职业:老师,课程:%s\n",member.name,member.sex,member.add.course);
		}
	}
}
//定义查找函数给定一个姓名查找该名字是否未该班级成员
void searchMember(Classroom* classroom, const char* name) 
{
    for (int i = 0; i < classroom->count; i++)
	{
        Member member = classroom->members[i];
		int s=strcmp(member.name,name);
        if (s == 0) 
		{
            printf("%s是该班级成员。\n", name);
            break; 
        }
		else
		{
            printf("%s不是该班级成员。\n", name);
			break;

		}
    }
}

//释放空间

void release(Classroom* classroom)
{
	free(classroom);
}

main.c 

#include "fun.h"
int main(int argc, const char *argv[])
{
	Classroom* classroom = space();

	Member student1 = {"张三","男",student,.add.score = 80};
	Member student2 = {"李四","女",student,.add.score = 80};
	Member teacher1 = {"王五","男",teacher};
	strcpy(teacher1.add.course,"数学");

	addMember(classroom,student1);
	addMember(classroom,student2);
	addMember(classroom,teacher1);

	showMember(classroom);
	
	searchMember(classroom,"张三");
    searchMember(classroom,"王五");
	
	release(classroom);

	return 0;
}

你可能感兴趣的:(数据结构)