1. 程式人生 > >【c基礎練習】結構體

【c基礎練習】結構體

1.結構體成員的使用

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct Person
{
	char name[64];
	int age;
};
int main()
{
	//棧上分配空間
	struct Person p1;
	strcpy(p1.name, "john");
	p1.age = 30;
	printf("Name:%s Age:%d\n", p1.name, p1.age);

	//在堆區分配空間
	struct Person* p2 = (struct Person*)malloc(sizeof(struct Person));
	strcpy(p2->name, "tom");
	p2->age = 22;
	printf("Name:%s Age:%d\n", p2->name, p2->age);
	system("pause");
	return EXIT_SUCCESS;
}


2.結構體賦值

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct Person
{
	char name[20];
	int age;
};
int main()
{
	struct Person p1 = { "John", 30 };
	struct Person p2 = { "tom", 22 };
	printf("Name:%s Age:%d\n", p1.name, p1.age);
	printf("Name:%s Age:%d\n", p2.name, p2.age);
	p1 = p2;
	printf("Name:%s Age:%d\n", p1.name, p1.age);
	printf("Name:%s Age:%d\n", p2.name, p2.age);
	system("pause");
	return EXIT_SUCCESS;
}


3.結構體陣列

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Person
{
	char name[20];
	int age;
};
int main()
{
	//在棧上分配空間
	struct Person p1[3] = 
	{
		{ "john", 30 },
		{ "tom", 22 },
		{ "jerry",23 }
	};
	struct Person p2[3] = { "john", 30, "tom", 22, "jerry", 23 };
	for (int i = 0; i < 3; i++)
	{
		printf("Name:%s,Age:%d\n", p1[i].name, p1[i].age);
	}
	printf("________________________________\n");
	for (int i = 0; i < 3; i++)
	{
		printf("Name:%s,Age:%d\n", p2[i].name, p2[i].age);
	}
	printf("________________________________\n");

	//在堆上分配空間
	struct Person* p3 = (struct Person*)malloc(sizeof(struct Person) * 3);
	for (int i = 0; i < 3; i++)
	{
		sprintf((p3+i)->name, "Name:%d", i + 1);
		p3[i].age = 20 + i;
	}
	for (int i = 0; i < 3; i++)
	{
		printf("Name:%s,Age:%d\n", p3[i].name, p3[i].age);
	}
	system("pause");
	return EXIT_SUCCESS;
}


4.結構體巢狀一級指標

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct Person
{
	char* name;
	int age;
};
void allocate_mem(struct Person** p)
{
	if (NULL == p)
	{
		return;
	}
	struct Person* temp =(struct Person*)malloc(sizeof(struct Person));
	if (NULL == temp)
	{
		return;
	}
	temp->age = 100;
	//給name指標分配記憶體
	temp->name = (char*)malloc(sizeof(char)* 64);
	strcpy(temp->name, "tom");	
	*p= temp;
}

void printf_Person(struct Person* p)
{
	printf("Name:%s,Age:%d\n", p->name, p->age);
}

void free_mem(struct Person** p)
{
	if (NULL == p)
	{
		return;
	}
	struct Person* temp = *p;
	if (NULL == temp->name)
	{
		free(temp->name);
		temp->name = NULL;
	}
	free(temp);
}

int main()
{	
	struct Person* p=NULL;
	allocate_mem05(&p);
	printf_Person05(p);
	free_mem05(&p);
	system("pause");
	return EXIT_SUCCESS;
}


5.結構體巢狀二級指標

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

//一個老師有n個學生
typedef struct _TEACHER
{
	char name[64];
	char** students;
}Teacher;

void create_teacher(const Teacher** teacher, int n, int m)
{
	if (teacher == NULL)
	{
		return;
	}
	//建立老師陣列
	Teacher* temp_teacher = (Teacher*)malloc(sizeof(Teacher)*n);
	if (NULL==temp_teacher)
	{
		return;
	}
	//給每個老師分配學生
	int num = 0;
	for (int i = 0; i < n; i++)
	{
		sprintf(temp_teacher[i].name, "老師_%d", i + 1);
		temp_teacher[i].students = (char**)malloc(sizeof(char*)*m);
		for (int j = 0; j < m; j++)
		{
			temp_teacher[i].students[j] = (char*)malloc(64);
			sprintf(temp_teacher[i].students[j], "學生_%d", num + 1);
			num++;
		}
	}
	*teacher = temp_teacher;
}

void print_teacher(const Teacher* teacher, int n, int m)
{
	for (int i = 0; i < n; i++)
	{
		printf("%s\n", teacher[i].name);
		for (int j = 0; j < m; j++)
		{
			printf("   %s", teacher[i].students[j]);
		}
		printf("\n");
	}
}

void free_mem(const Teacher** teacher, int n, int m)
{
	if (NULL==teacher)
	{
		return;
	}
	Teacher* temp_teacher = *teacher;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			free(temp_teacher[i].students[j]);
			temp_teacher[i].students[j]=NULL;
		}
		free(temp_teacher[i].students);
		temp_teacher[i].students = NULL;
	}
	free(temp_teacher);
	temp_teacher = NULL;
}
int main()
{
	Teacher* p=NULL;
	create_teacher06(&p, 2, 3);
	print_teacher06(p, 2, 3);
	free_mem06(&p, 2, 3);
	system("pause");
	return EXIT_SUCCESS;
}

習題:

//01 寫一個程式滿足一下要求:
//a.外部定義一個name結構模板,它含有2個成員:一個字串用於存放名字,另一個字串用於存放姓氏。
//b.外部定義一個student結構模板,它含有3個成員:一個name結構,一個存放3個浮點數分數的grade陣列,以及
//一個存放這3個分數的平均分的變數。
//c.在main函式中宣告一個具有CSIZE(CSIZE = 4)個student結構的陣列,並隨意初始化這些結構體的名字部分。使用
//函式來執行d、e、f以及g部分所描述的任務。
//d.請求使用者輸入學生的姓名和分數,以互動地獲得每個學生的成績。將分數存放到相應結構的grade陣列成員中。
//您可以自主選擇在main或者一個函式中實現這個迴圈。
//e.為這個結構計算平均分,並把這個值賦給適合的成員。
//f.輸出每個結構中的資訊。
//g.輸出結構的每個數值成員的班級平均分

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define CSIZE 4
struct Name
{
	char FristName[10];
	char EndName[20];
};
typedef struct Student
{
	struct Name n;
	float Grade[3];
	float	Average;
}stu;
//d函式 : 錄入學生資訊,求算平均分
void	fun_in(struct Student *p, int num)
{
	for (int i = 0; i < num; i++)
	{
		printf("請輸入學生姓名\n");
		scanf("%s%s", &p[i].n.FristName, &p[i].n.EndName);
		printf("請輸入成績\n");
		scanf("%f%f%f", &p[i].Grade[0], &p[i].Grade[1], &p[i].Grade[2]);
		p[i].Average = (p[i].Grade[0] + p[i].Grade[1] + p[i].Grade[2]) / 3;
	}
}
void	fun_out(struct Student *p, int num)
{
	for (int i = 0; i <= num; i++)
	{
		printf("學生姓名:%s%s\n", p[i].n.FristName, p[i].n.EndName);
		printf("成績%f\n", p[i].Average);
	}
}

int main()
{
	stu p[CSIZE];
	fun_in(p,CSIZE);
	fun_out(p, CSIZE);
	system("pause");
	return EXIT_SUCCESS;
}