c++--结构体及案例

结构体定义和使用

结构体属于用户自定义的数据类型,允许用户存储不同的数据类型。

语法:struct 结构体名 {结构体成员列表};

通过结构体创建变量的方式有三种:

struct  结构体名  变量名

struct 结构体名 变量名={成员1值,成员2值...}

定义结构体时顺便创建变量

#include<iostream>
#include<string>
using namespace std;
//1、创建学生数据类型:学生包括(姓名、年龄、分数)
struct Student
{
	//成员列表
	//自定义数据类型是内置数据类型的集合
	//姓名
	string name;
	//年龄
	int age;
	//分数
	int score;
}s3;//顺便创建结构体变量

int main() {
	//2、创建结构体变量,三种方式
//2.1 struct Student s1
	//创建结构体变量时,struct的关键字可以省略
	struct Student s1;
		//给s1属性赋值,通过.访问结构体变量中的属性
		s1.name = "张三";
	    s1.age = 30;
	    s1.score = 78;
		cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;
	//2.2 struct Student s2={...}
		struct Student s2 = { "李四",22,90 };
		cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;
	//2.3在定义结构体时顺便创建结构体变量
		s3.name = "王五";
		s3.age = 21;
		s3.score = 98;
		cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;

	system("pause");
	return 0;
}

1.结构体数组

作用:把自定义的结构体放入到数组中方便维护。

语法:struct 结构体名 数组名[元素个数] ={ {数据 }, { } ,... { } }

#include<iostream>
using namespace std;

//结构体数组
//1.定义结构体
struct Student
{
	//姓名
	string name;
	//年龄
	int age;
	//分数
	int score;
 };
int main() {
//2.创建结构体数组
	struct Student stuArray[3] = {
		{"张三",18,100},
		{"李四",28,99},
		{"王五",38,66}
	};
//3.给结构体数组中的元素赋值
	stuArray[2].name = "赵六";
	stuArray[2].age = 80;
	stuArray[2].score = 60;
//4.遍历结构体数组
	for (int i = 0; i < 3; i++)
	{
		cout << "姓名:" << stuArray[i].name
			 << "年龄:" << stuArray[i].age
			 << "分数:" << stuArray[i].score << endl;
	}
	system("pause");
	return 0;
}

2.结构体指针

作用:通过指针访问结构体中的成员

利用操作符->可以通过结构体指针访问结构体属性

#include<iostream>
#include<string>
using namespace std;
//1、创建学生数据类型:学生包括(姓名、年龄、分数)
struct Student
{
	//成员列表
	//自定义数据类型是内置数据类型的集合
	//姓名
	string name;
	//年龄
	int age;
	//分数
	int score;
};
int main() {
	//1.创建学生结构体变量
	struct Student s = { "张三",20,90 };
	//2.通过指针指向结构体变量
	Student* p = &s;
	//3.通过指针访问结构体变量中的数据
	//通过结构体指针,访问结构体中的属性,需要利用'->'.
	cout << "姓名:" << p->name << " 年龄:" << p->age<< " 分数:" << p->score << endl;
	system("pause");
	return 0;
}

3.结构体嵌套结构体

作用:结构体中的成员可以是另一个结构体。

#include<iostream>
#include<string>
using namespace std;
//创建学生结构体
struct student 
{
	string name;
	int age;
	int score;
};
//创建老师结构体
struct teacher
{
	//成员列表
	//自定义数据类型是内置数据类型的集合
	//姓名
	string name;
	//年龄
	int age;
	//分数
	int id;
	struct student stu;
};
int main() {
	//创建结构体变量
	teacher t;
	t.id = 1000;
	t.age = 50;
	t.name = "张三";
	t.stu.name = "李四";
	t.stu.age = 20;
	t.stu.score = 100;
	cout << "老师的姓名:" << t.name << " 老师的年龄:" << t.age<< " 老师的id:" << t.id 
	<< "辅导的学生的姓名:" << t.stu.name << " 学生的年龄:" << t.stu.age << " 学生的分数:" << t.stu.score<< endl;
	system("pause");
	return 0;
}

4.结构体做函数参数

作用:将结构体作为参数向函数中传递

传递方式有两种:1.值传递

                            2.地址传递

#include<iostream>
using namespace std;
//定义学生结构体
struct student {
	string name;
	int age;
	int score;
};
//打印学生信息函数
//1、值传递
void printStudent1(struct student s)
{
	s.age = 100;
	cout << "姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;
}
//2、地址传递
void printStudent2(struct student* p)
{
	p->age = 200;
	cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
}
int main() {
	//结构体做函数参数
	//将学生传入到一个参数中,打印学生身上所有信息

	//创建结构体变量
	struct student s = { "张三",20,90 };
	printStudent1(s);
	cout << "姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;

	printStudent2(&s);
	cout << "姓名:" << s.name << " 年龄:" << s.age << " 分数:" << s.score << endl;

	return 0;
}

 

5.结构体案例1

案例描述:设计老师和学生的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员,学生的成员有姓名、考试分数,创建数组存放三位老师;通过函数给每个老师所带的学生赋值,最终打印出老师数据及老师所带学生的数据。

#include<iostream>
using namespace std;
#include<string>
#include<ctime>

//定义学生结构体
struct Student {
	string name;
	int score;
};

struct Teacher {
   string name;
	//创建结构体数组 语法:数据类型+数组名+[元素个数]={具体信息};
	//struct Student 是数据类型 sArray是数组名;
	struct Student sArray[5];
};

//给老师和学生赋值的函数
void allocateSpace(struct Teacher tArray[],int len)
{
	string nameSeed = "ABCDE";
	//给老师开始赋值
	for (int i = 0; i < len; i++)
	{
		tArray[i].name = "Teacher_";
		tArray[i].name += nameSeed[i];
		for (int j = 0; j < 5; j++) {
          tArray[i].sArray[j].name = "Student_";
		tArray[i].sArray[j].name+= nameSeed[j];


		int random = rand() % 61+40;//40~100的随机数;rand()%61是0~60的随机数;
		tArray[i].sArray[j].score = random;
		}
		

	}
}

//打印所有信息
void printInfo(struct Teacher tArray[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "老师姓名:" << tArray[i].name << endl;
		for (int j = 0; j < 5; j++)
		{
			cout << "\t学生姓名:" << tArray[i].sArray[j].name <<
				"考试分数:" << tArray[i].sArray[j].score << endl;
		}
	}
}


int main() 
{
	//随机数种子
	srand((unsigned int)time(NULL));

	//1、创建3名老师的数组
	struct Teacher  tArray[3];
	int len = sizeof(tArray) / sizeof(tArray[0]);
	//2、通过函数给3名老师的信息赋值,并给老师带的学生信息赋值
	allocateSpace(tArray, len);
	//3、打印所有老师及所带学生信息
	printInfo(tArray, len);
	system("pause");
	return 0;
}

6.结构体案例2

案例描述:设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放

5名英雄。通过冒泡排序的方法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。

#include<iostream>
#include<string>
using namespace std;
struct hero {
	string name;
	int age;
	string gender;
};
void bubblesort(hero heroarray[], int len) {
	for (int i = 0; i < len - 1; i++) {
		for (int j = 0; j < len - i - 1; j++)
		{
			if (heroarray[j].age > heroarray[j + 1].age)
			{
				struct hero temp = heroarray[j];
				heroarray[j] = heroarray[j + 1];
				heroarray[j + 1] = temp;

			}
		}
	}
 }
void printhero(hero heroarray[], int len) {
	for (int i = 0; i < len; i++) {
		cout << "姓名:" << heroarray[i].name << "年龄:" << heroarray[i].age << "性别:" << heroarray[i].gender << endl;

	}
}
int main() {
	struct hero heroarray[5] = { {"刘备",23,"男"},{"关羽",22,"男",},{"张飞",20,"男"},{"赵云",21,"男"},{"貂蝉",19,"女"} };
	int len = sizeof(heroarray) / sizeof(heroarray[0]);
	for(int i=0;i<len;i++)
	{
	 cout << "姓名:" << heroarray[i].name << "年龄:" << heroarray[i].age << "性别:" << heroarray[i].gender << endl;
	}
	bubblesort(heroarray, len);
	printhero(heroarray, len);
	system("pause");
	return 0;
}

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值