一个学习C语言的大三猿之结构体(9)

上一周学习完了最后的结构体和链表这一章节

也是通过结构体了解到了更多关于C语言的一些东西,随着对于它的了解,也是逐渐的发现了这就像是一个聚宝盆一般,总有着自己所猜想不到的东西,给人眼前一亮的感觉,一个小小的结构体却包含了很多的知识模块,这对于之后我们所需要搭建C语言最为经典的学生教务系统也是有着非常大的帮助。

  1. 对于结构体,它更多的像是一种自定制的类型,将现有的类型进行一种整体的组合,而形成一种包含多种类型的新类型。
  2. 在结构体之中对于类如学号和成绩之类的数字内容可以直接进行指针地址数据的转换,而对于姓名和性别的这一类的话,我们却需要利用Strcpy这样的函数来进行转换,也就是我们所说道的关于字符串的拷贝,把一个字符串复制一份,复制到另外一个数组之中。
  3. 对于结构体的使用,我会在后面的代码中进行一个具体的说明,也是方便自己更好的结合起来看,也是希望能够帮助到那些和我一样初学者。
  4. 对于链表的话,我自己的理解,就是在给予一个初始地址,可以确定从哪里开始,之后对于每一个链在最后的位置给予一个可以指向下一个链的指针,或者说是说明,能够让系统合理的进行下一个链表的读取,以此类推的进行下去,这也是我所理解到的最为完整的一个链表结构。
  5. 或许对于这两方面的内容也是因为学习的时间周期已经过去两周了,但是最近忙于考试周的复习,也没有及时发博客,所以很多东西的思路也不是非常清晰,之后会尽可能地将其补充完整。

练习代码

1. **输入十个职工信息,并查找任意一个**
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Worker
{
	int num;
	char name[20];
}worker[10];


void Init(int n){
	int i;
	for (i = 0; i < n; i++){
		scanf("%d%s", &worker[i].num, worker[i].name);
	}
}

void Printf(int n){
	int i;
	for (i = 0; i < n; i++){
		printf("工号:%d 姓名:%s\n", worker[i].num, worker[i].name);
	}
}


void Find(int num, int n){
	int left = 0;
	int right = n - 1;
	while (left < right){
		int mid = (left + right) / 2;
		if (worker[mid].num>num){
			right = mid - 1;
		}
		if (worker[mid].num < num){
			left = mid + 1;
		}
		else{
			printf("找到了,它是第%d个.\n", mid);
		}

	}
}

int main(){
	printf("请输入十名工人的信息:\n");
	Init(10);
	printf("十名职工的信息如下:\n");
	Printf(10);
	Find(103, 10);
	system("pause");
	return 0;

}

2. **关于定义的概念**
#include <stdio.h>
#include <stdlib.h>

#define Z int*//define和typedef的区别
typedef int* ZX;

int main(){
	ZX i,j;//整体的替换
	Z a, b;//Z是进行查找替换的,只能够替换a前面的,而不能够改变b的类型
	return 0;
}

3.**结构体数组指针**
#include <stdio.h>
#include <stdlib.h>
struct Student
{
	long num;
	char name[20];
	char sex;
	float score;
};
int main()
{
	struct Student stu_1;//定义struct Student 类型的变量stu_1;
	struct Student *p;//定义指向struct Student类型数据的指针变量p;
	p = &stu_1;//p指向stu_1;
	stu_1.num = 10101;
	strcpy(stu_1.name, "ChangHao");//对结构体相关变量赋值
	stu_1.sex = 'M';
	stu_1.score = 89.5;
	    printf("No.:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);//输出结果;
		printf("No.:%ld\nname:%s\nsex:%c\nscore:%5.1f\n", (*p).num, (*p).name, (*p).sex, (*p).score);//按指针类型输出函数;
	system("pause");
	return 0;
}

4.**结构体变量和结构体变量指针**
#include <stdio.h>
#include <stdlib.h>
#define N 3//定义N初始值;
struct Student//定义结构体;
{
	int num;
	char name[20];
	float score[3];
	float aver;
};
int main()
{
	void input(struct Student stu[]);//定义input函数;
	struct Student Max(struct Student stu[]);//定义max函数;
	void print(struct Student stu);//定义print函数;
	struct Student stu[N], *p = stu;//定义结构体数组和指针;
	input(p);//调用input函数;
	print(Max(p));//调用print函数;
	system("pause");
	return 0;
}

void input(struct Student stu[])
{
	int i;
	printf("请输入各学生的信息:学号,姓名,三门课成绩\n");
	for (i = 0; i < N; i++)//依次输入三个学生的相关信息;
	{
		scanf("%d%s%f%f%f", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
		stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;//将输入的三门分数求平均数;
	}
}

struct Student Max(struct Student stu[])
{
	int i, m = 0;
	for (i = 0; i < N;i++)
	if (stu[i].aver>stu[m].aver)//将m和i进行交换;
		m = i;
	return stu[m];
}

void print(struct Student stud)
{
	printf("\n成绩最高的学生是:\n");
	printf("学号:%d\n姓名:%s\n三门课成绩:%5.1f,%5.1f,%5.1f\n平均成绩:%6.2f\n", stud.num, stud.name, stud.score[0], stud.score[1], stud.score[2], stud.aver);
}

简单链表

1.简单链表
#include <stdio.h>
#include <stdlib.h>

struct Student
{
	int num;
	float score;
	struct Student *next;
};

int main(){
	struct Student a, b, c, *head, *p;
	a.num = 10101; a.score = 89.5;
	b.num = 10103; a.score = 90;
	c.num = 10108; c.score = 85;
	head = &a;
	a.next = &b;
	b.next = &c;
	c.next = NULL;
	p = head;
	do
	{
		printf("%ld %5.1f\n", p->num, p->score);//p指向的结点数据
		p = p->next;
	} while (p != NULL);
	system("pause");
	return 0;
}

发布了18 篇原创文章 · 获赞 12 · 访问量 957

猜你喜欢

转载自blog.csdn.net/Luckily0818/article/details/103636504