Browse Source

上传文件至 '结构体1'

Creamo 3 years ago
parent
commit
618620d463

+ 77 - 0
结构体1/1、结构体基本概念、定义和使用.cpp

@@ -0,0 +1,77 @@
+#include <iostream>
+
+//结构体基本概念
+//结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
+
+
+//结构体定义和使用
+//struct  结构体名  { 结构体成员列表 };
+//通过结构体创建变量的方式有三种:
+//⚪struct 结构体名 变量名
+//⚪struct 结构体名 变量名 = { 成员1值 , 成员2值 }
+//⚪定义结构体时顺便创建变量
+
+//1、创建学生数据类型 学生包括的属性:( 姓名、年龄、分数  )
+//一些类型的集合组成的自定义的数据类型
+
+#if(0)
+
+struct Student
+{
+	//成员列表
+
+	//姓名
+	std::string name;
+	
+	//年龄
+	int age;
+	
+	//分数
+	int score;
+
+
+
+}s3;  //创建一个结构体变量
+
+
+
+int main()
+{
+	
+	
+	//2、通过学生类型创建具体的学生
+
+	//① struct Student s1
+	//创建变量 struct 关键字可以省略  定义不可以省略
+	Student s1;
+	//给s1属性赋值,通过.访问结构体变量中的属性
+	s1.name = "张三";
+	s1.age = 18;
+	s1.score = 100;
+
+	std::cout << "姓名:" << s1.name << "年龄:" << s1.age << "分数:" << s1.score << std::endl;
+
+	//② struct Student s2 = { ... }
+	struct Student s2 = { "李四" , 19 , 80 };
+	std::cout << "姓名:" << s2.name << "年龄:" << s2.age << "分数:" << s2.score << std::endl;
+
+	//③ 在定义结构体时  顺便创建结构体变量(不建议用)
+	s3.name = "王五";
+	s3.age = 20;
+	s3.score = 60;
+	std::cout << "姓名:" << s3.name << "年龄:" << s3.age << "分数:" << s3.score << std::endl;
+
+
+	system("pause");
+
+	return 0;
+
+}
+
+#endif
+
+//总结1:定义结构体时关键字是struct ,  不可省略
+
+//总结2:创建结构体变量时,关键字struct可以省略
+
+//总结3:结构体变量利用操作符"." 访问成员

+ 62 - 0
结构体1/2、结构体数组.cpp

@@ -0,0 +1,62 @@
+#include <iostream>
+#include <string>
+
+//结构体数组
+
+//作用:将自定义的结构体放入到数组中方便维护
+//语法:struct  结构体名  数组名[元素个数] = { {} , {} , ...{} }
+
+//1、定义结构体
+
+#if(0)
+
+struct Student
+{
+	//姓名
+	std::string name;
+
+	//年龄
+	int age;
+
+	//分数
+	int score;
+
+};
+
+
+
+int main()
+{
+
+	//2、创建一个结构体数组
+	struct Student stuArry[8] =
+	{
+		{"张三" , 18 , 100},
+		{"李四" , 28 , 99},
+		{"王五" , 38 , 66}
+
+	};
+
+
+	//3、给结构体数组中的元素赋值
+	stuArry[2].name = "赵六";
+	stuArry[2].age = 80;
+	stuArry[2].score = 60;
+	
+	//4、遍历结构体数组
+	for (int i = 0; i < 3; i++)
+	{
+		std::cout << "姓名:" << stuArry[i].name
+				  << "年龄:" << stuArry[i].age
+				  << "分数:" << stuArry[i].score
+				  << std::endl;
+
+	}
+
+
+	system("pause");
+
+	return 0;
+}
+
+#endif

+ 36 - 0
结构体1/3、结构体指针.cpp

@@ -0,0 +1,36 @@
+#include <iostream>
+
+//结构体指针
+//作用:通过指针访问结构体中的成员
+
+//利用操作符 -> 可以通过结构体指针访问结构体属性
+
+#if(0)
+
+struct Student
+{
+	std::string name;   //姓名
+	int age;            //年龄
+	int score;          //分数
+};
+
+int main()
+{
+	//1、创建学生的结构体变量
+	struct Student s = { "张三" , 18 ,100 };
+
+	//2、通过指针指向结构体变量
+	struct Student* p = &s;
+
+	//3、通过指针访问结构体变量中的数据
+	std::cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << std::endl;
+
+
+	system("pause");
+
+	return 0;
+}
+
+//总结:想通过结构体的指针来访问结构体中的属性,需利用' -> '
+
+#endif

+ 47 - 0
结构体1/4、结构体嵌套结构体.cpp

@@ -0,0 +1,47 @@
+#include <iostream>
+
+//作用:结构体中的成员可以是另一个结构体
+//例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
+
+#if(0)
+
+//定义学生的结构体
+struct Student
+{
+	std::string name;        //姓名
+	int age;				 //年龄
+	int score;               //分数
+};
+
+
+//定义一个老师的结构体
+struct Teacher
+{
+	int id;             //教师编号
+	std::string name;   //教师姓名
+	int age;            //年龄
+	struct Student stu; //辅导的学生
+
+};
+
+int main()
+{
+
+	//结构体嵌套结构体
+	//创建老师
+	Teacher t;
+	t.id = 10000;
+	t.name = "老王";
+	t.age = 50;
+	t.stu.name = "小王";
+	t.stu.age = 20;
+	t.stu.score = 60;
+
+	std::cout << "老师姓名:" << t.name << "老师编号:" << t.id << "老师年龄:" << t.age << "老师辅导的学生姓名:" << t.stu.name << "学生的年龄:" << t.stu.age << "学生的成绩:" << t.stu.score << std::endl;
+
+	system("pause");
+
+	return 0;
+
+}
+#endif