IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C语言--结构体 -> 正文阅读

[C++知识库]C语言--结构体

引言

C语言的一些基本例题
全部例题来自于C语言王道训练营 链接如下

B站链接.
书籍:《跟“龙哥”学C语言编程》

1.结构体的scanf读取和输出

下面展示一些 可运行的代码

#include<stdio.h>
#include<stdlib.h>
struct  student
{
	int num;
	char name[20];
	char sex;
	int age;
	float score;
	char addr[30];
};//结构体类型声明
int main()
{
	struct student s = { 1001,"wbx",'M',20,88.5,"ShanDong" };
	struct student sarr[3];
	int i;
	printf("%d %s %c %d %5.2f %s\n", s.num, s.name, s.sex, s.age, s.score, s.addr);
	for (i=0;i<3;i++)
	{
		scanf("%d%s %c%d%f%s", &sarr[i].num, &sarr[i].name, &sarr[i].sex, &sarr[i].age, &sarr[i].score, &sarr[i].addr);
	}
	for (i = 0; i < 3; i++)
	{
		printf("%d %s %c %d %5.2f %s\n",sarr[i].num, sarr[i].name, sarr[i].sex, sarr[i].age, sarr[i].score, sarr[i].addr);

	}
	system("pause");
	return 0;
}

运行结果

1001 wbx M 20 88.50 ShanDong
------------------------------------------------------
输入
1002 lxx F 19 99 Shanxi
1004 WKK M 18 60 BeiJing
1009 KKS F 25 68 XiAn
------------------------------------------------------
1002 lxx F 19 99.00 Shanxi
1004 WKK M 18 60.00 BeiJing
1009 KKS F 25 68.00 XiAn
请按任意键继续. . .

2.结构体指针的使用

下面展示一些 可运行的代码

#include<stdio.h>
#include<stdlib.h>
struct student {
	int num;
	char name[20];
	char sex;
};
int main()
{
	struct student s = { 1001,"wangle",'M' };
	struct student sarr[3] = { 1001,"lilei",'M',1005,"zhangsan",'M',1007,"lili",'F' };
	struct student* p;//定义结构体指针
	int num;
	p = &s;
	printf("%d %s %c\n", p->num, p->name, p->sex);
	p = sarr;
	printf("%d %s %c\n", p->num, p->name, p->sex);//方式一获取成员
	printf("%d %s %c\n", (*p).num, (*p).name, (*p).sex);//方式二获取成员
	printf("-------------------------------\n");
	num = p->num++;//num=p->num;p->num++;
	printf("num=%d,p->name=%d\n", num, p->num);
	num = p++->num;//num=p->num;p++;
	printf("num=%d,p->name=%d\n", num, p->num);
	system("pause");
	return 0;
}

运行结果

1001 wangle M
1001 lilei M
1001 lilei M
-------------------------------
num=1001,p->name=1002
num=1002,p->name=1005
请按任意键继续. . .

3.typedef的使用

下面展示一些 可运行的代码

#include<stdio.h>
#include<stdlib.h>
typedef struct student {
	int num;
	char name[20];
	char sex;
}stu,*pstu;

typedef int INTEGER;//不要忘记;号

int main()
{
	stu s = { 1001,"wangle",'M' };
	pstu p;
	INTEGER i = 10;
	p = &s;
	printf("i=%d p->num=%d\n", i, p->num);
	system("pause");
}

运行结果

i=10 p->num=1001
请按任意键继续. . .

4.链表的增删查改

下面展示一些 可运行的代码
func.h

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student {
	int num;
	float score;
	struct student* pNext;
}stu,*pstu;
//尾插法
void list_tail_insert(pstu*, stu**, int);
//头插法
void list_head_insert(pstu*, pstu*, int);
//有序插入
void list_sort_insert(pstu*, pstu*, int);
//删除
void list_delete(pstu*, stu**, int);
//修改
void list_modify(pstu, int,float);
//链表打印
void list_print(pstu);
void list_print_score(pstu);

func.c

#include"func.h"
void list_tail_insert(pstu* pphead, stu** pptail, int i)
{
	pstu pnew;
	pnew = (pstu)malloc(sizeof(stu));//申请空间
	memset(pnew, 0, sizeof(stu));//初始化
	pnew->num = i;
	if (NULL == *pptail)//判断链表是否为空,为空时,pnew既为头也是尾
	{
		*pphead = pnew;
		*pptail = pnew;
	}
	else {
		(*pptail)->pNext = pnew;//将新节点的地址赋值为为节点的pNext
		*pptail = pnew;//新节点变为链表尾部
	}
}
void list_print(pstu phead)
{
	while (phead != NULL)
	{
		printf("%-3d", phead->num);
		phead = phead->pNext;
	}
	printf("\n");
}
void list_print_score(pstu phead)
{
	while (phead != NULL)
	{
		printf("%-3d %5.2f\n", phead->num,phead->score);
		phead = phead->pNext;
	}
	printf("\n");
}
//头插法
void list_head_insert(pstu* pphead, pstu* pptail, int i)
{
	pstu pnew;
	pnew = (pstu)malloc(sizeof(stu));
	memset(pnew, 0, sizeof(stu));
	pnew->num = i;
	if (*pptail == NULL)//判断链表是否为空,为空时,pnew既为头也是尾
	{
		*pphead = pnew;
		*pptail = pnew;
	}
	else {
		pnew->pNext = *pphead;
		*pphead = pnew;
	}
}
//有序插入
void list_sort_insert(pstu* pphead, pstu* pptail, int i)
{
	pstu pnew;
	pstu pcur;
	pstu ppre;
	pnew = (pstu)malloc(sizeof(stu));
	memset(pnew, 0, sizeof(stu));
	pnew->num = i;
	pcur = *pphead;//让两个指针都指向链表的头
	ppre = *pphead;
	if (NULL == pcur)//在头部插入
	{
		*pphead = pnew;
		*pptail = pnew;
	}
	else if (i < pcur->num) 
	{
		pnew->pNext = pcur;
		*pphead = pnew;
	}
	else 
	{
		while (pcur != NULL)//在中间插入
		{
			if (pcur->num > i)
			{
				ppre->pNext = pnew;
				pnew->pNext = pcur;
				break;
			}
			ppre = pcur;
			pcur = pcur->pNext;
		}
		if (pcur == NULL)//未插入中间 插入到尾部
		{
			(*pptail)->pNext = pnew;
			*pptail = pnew;
		}
	}
}
//删除节点
void list_delete(pstu* pphead, stu** pptail, int i)
{
	pstu pcur, ppre;
	pcur = *pphead;
	ppre = *pphead;
	if (pcur!= NULL)
	{
		if (pcur->num == i)//如果删除的num与头节点的值相等
		{
			*pptail = NULL;
			free(pcur);
		}
		else 
		{
			while (pcur != NULL)//删除的num中后面时
			{
				if (pcur->num == i)
				{
					ppre->pNext = pcur->pNext;
					free(pcur);
					break;
				}
				ppre = pcur;
				pcur = pcur->pNext;
				if (NULL == ppre->pNext)
				{
					*pptail = ppre;
				}
				if (pcur == NULL)
				{
					printf("no this node\n");
				}
			}
		}
	}
	else 
	{
		printf("list is NULL\n");
	}
}
//修改
void list_modify(pstu phead, int i, float f)
{
	while (phead != NULL)
	{
		if (phead->num == i)
		{
			phead->score = f;
			break;
		}
		phead = phead->pNext;
	}
	if (NULL == phead)
	{
		printf("no this node\n");
	}
}

main.c

#include"func.h"
int main()
{
	pstu p;
	pstu phead = NULL, ptail = NULL;
	int i;
	float f;
	while (scanf("%d", &i) != EOF)
	{
		//list_tail_insert(&phead, &ptail, i);
		//list_head_insert(&phead, &ptail, i);
		list_sort_insert(&phead, &ptail, i);
	}
	list_print(phead);
	while (printf("please input delete num:"), scanf("%d", &i) != EOF)
	{
		list_delete(&phead, &ptail, i);
		list_print(phead);
	}
	while (printf("please input modify num:"), scanf("%d%f", &i,&f) != EOF)
	{
		list_modify(phead, i, f);
		list_print_score(phead);
	}
	system("pause");
	return 0;
}

运行结果

5 55 10 6 99
^Z
^Z
^Z
5  6  10 55 99
please input delete num:55
5  6  10 99
please input delete num:^Z
^Z
^Z
please input modify num:99
99.5
5    0.00
6    0.00
10   0.00
99  99.50

please input modify num:^Z
^Z
^Z
请按任意键继续. . .

总结

第一次发表一篇完整的博客,对于一些遗漏,读者不要太过深究~
比较适合新手打基础大佬请绕路~
希望这些可以帮助你更好的理解C语言
马上就考研了 我居然还在纠结一些基础 真是闲的闲的闲的闲*10000!
过几天在更新后几章的内容
欢迎大家评论收藏点赞 !!!

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-11 11:59:07  更:2021-08-11 12:00:07 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 7:02:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码