面向过程编程自学笔记整理
六、结构体
#include"iostream"
using namespace std;
#include"string"//字符串头文件
#include"ctime"//随机数种子头文件
//结构体属于用户自定的数据类型,允许用户存储不同的数据类型
/********************1.结构体的定义与使用********************************************/
//结构体语法:struct 结构体名{ 结构体成员列表 };
//三种用结构体创建变量的方式
//1.struct 结构体名 变量名
//2.struct 结构体名 变量名={成员1值,成员2值...};
//3.定义结构体时顺便创造变量
//1.创造学生数据类型:学生包括(姓名,年龄,班级,分数)
//自定义数据类型 ,一些类型的集合来组成的一个类型
//语法:struct 类型名称{成员列表};
//struct student//结构体定义
//{
// //成员列表
// //姓名
// string name;
// //年龄
// int age;
// //班级
// int clas;
// //分数
// double score;
//}s3;//顺便创造一个结构体变量
//
//
//int main()
//{
2. 通过学生类型创造具体学生
2.1 struct student s1
// //struct关键字可以省略
// struct student s1;
给s1属性赋值,通过一个点访问结构体变量的属性
// s1.name = "张三";
// s1.age = 15;
// s1.clas = 2;
// s1.score = 100;
// cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 班级:" << s1.clas << " 分数:" << s1.score << endl;
//
//
2.2 struct student s2={......}
// struct student s2 = {"李四",16,3,88};
// cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 班级:" << s2.clas << " 分数:" << s2.score << endl;
//
2.3定义结构体时顺便创造结构体变量
// s3.name = "王五";
// s3.age = 17;
// s3.clas = 1;
// s3.score = 99;
// cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 班级:" << s3.clas << " 分数:" << s3.score << endl;
//
// system("pause");
// return 0;
//}
/********************2.结构体数组********************************************/
//作用:将自定义结构体放入数组中方便维护
//语法:struct 结构体名 数组名[元素个数]={{},{},{},{}}
//1.定义一个结构体
//struct student
//{
// string name;//姓名
//
// int age;//年龄
//
// int score;//分数
//
//};
//
//int main()
//{
// //2.创建结构体数组
// struct student stuarry[4] =
// {
// {"马一",15,150},
// {"李二",17,33},
// {"张三",14,57},
// {"王四",16,88}
// };
// //3.给结构体数组中的元素赋值
// stuarry[2].name = "王五";
// stuarry[2].age = 65;
// stuarry[2].score = 99;
// //4.遍历结构体数组
// for (int i = 0; i < sizeof(stuarry) / sizeof(stuarry[0]); i++)
// {
// cout << "姓名:" << stuarry[i].name
// << " 年龄:"<< stuarry[i].age
// << " 分数:"<< stuarry[i].score
// << endl;
// }
//
// system("pause");
// return 0;
//}
/********************3.结构体指针********************************************/
//作用:通过指针访问结构体中的成员
//利用操作符->可以通过结构体指针访问结构体属性
//struct student
//{
// string name;//姓名
// string gender;//性别
// int age;//年龄
// int score;//分数
//};
//
//int main()
//{
1.创建学生结构体变量
// struct student s1 = {"张三","男",17,65};
// struct student s2 = {"李四","女",18,77};
// struct student s3 = {"王五","女",15,67};
//
2.通过指针指向结构体变量
// struct student* p1 = &s1;
// struct student* p2 = &s2;
// struct student* p3 = &s3;
//
3.通过指针访问结构体变量中的数据
// //通过结构体指针访问结构体变量,需要利用'->'
// cout << "姓名:" << p1->name
// << " 性别:" << p1->gender
// << " 年龄:" << p1->age
// << " 分数:" << p1->score
// << endl;
// cout << "姓名:" << p2->name
// << " 性别:" << p2->gender
// << " 年龄:" << p2->age
// << " 分数:" << p2->score
// << endl;
// cout << "姓名:" << p3->name
// << " 性别:" << p3->gender
// << " 年龄:" << p3->age
// << " 分数:" << p3->score
// << endl;
//
// system("pause");
// return 0;
//}
/********************4.结构体嵌套结构体********************************************/
//作用:结构体中的成员可以是另一个结构体
//案例:每一个老师辅导一个学员,一个老师的结构体中记录一个学生的结构体
//定义学生架构体
//struct student
//{
// string name;//姓名
// int age;//年龄
// int score;//分数
//};
//
定义老师结构体
//struct teacher
//{
// int id;//教师编号
// string name;//教师姓名
// int age;//教师年龄
// struct student stu;//老师辅导的学生
//};
//
//
//int main()
//{
// //结构体嵌套结构体
// //创建老师
// struct teacher t;
// t.id = 11111;
// t.name = "安娜";
// t.age = 38;
// t.stu.name = "李明";
// t.stu.age = 19;
// t.stu.score = 100;
//
// cout << "教师编号:" << t.id
// << " 教师姓名:" << t.name
// << " 教师年龄:" << t.age
// << endl;
// cout << "老师辅导学生姓名:" << t.stu.name
// << " 老师辅导学生年龄:" << t.stu.age
// << " 老师辅导学生分数:" << t.stu.score
// << endl;
// system("pause");
// return 0;
//}
/********************5.结构体做函数的参数********************************************/
//作用:将结构体作为参数向函数中传递
//两种传递方式:
//1.值传递
//2.地址传递
//定义学生结构体
//struct student
//{
// string name;
// int age;
// int score;
//};
//
打印学生信息的函数
1.值传递
形参发生改变,不会修改实参
//void printstudent1(struct student s)
//{
// //s.age = 100;
// cout << "子函数1中,姓名:" << s.name
// << " 年龄:" << s.age
// << " 分数:" << s.score
// << endl;
//}
//
2.地址传递
修改形参,会改变main函数中的参数
//void printstudent2(struct student * p)
//{
// p->name ="李明";
// cout << "子函数2中,姓名:" <<p->name
// << " 年龄:" << p->age
// << " 分数:" << p->score
// << endl;
//}
//
//
//int main()
//{
结构体做函数参数
将学生传入一个参数中,打印学生身上的所有信息
// struct student s;
// s.name = "张十三";
// s.age = 19;
// s.score = 100;
//
// //main函数中打印信息
// cout << "main函数中,姓名:" << s.name
// << " 年龄:" << s.age
// << " 成绩:" << s.score
// << endl;
//
// //子函数1中值传递打印信息
// //printstudent1(s);
//
// //子函数1中地址传递打印信息
// printstudent2(&s);
//
// system("pause");
// return 0;
//}
/********************6.结构体中const使用场景********************************************/
//作用:用const防止误操作
//创建学生结构体
//struct student
//{
// string name;
// int age;
// int score;
//};
值传递方式打印函数
//void printstudent1(struct student s)
//{
// cout << "姓名:" << s.name
// << " 年龄:" << s.age
// << " 分数:" << s.score
// << endl;
//}
//
地址传递方式打印函数(const使用的场景)
将函数中的形参改为指针,可以节省内存空间,而且不会复制一个新的副本出来
//void printstudent2(const struct student *s)//该函数只能读数据,不能改数据
//{
// //s->age = 300;//加入conat 则这个函数只能进行读的操作,无法修改main函数中的值,防止我们误操作
// cout << "姓名:" << s->name
// << " 年龄:" << s->age
// << " 分数:" << s->score
// << endl;
//}
//int main()
//{
// //创建结构体变量
// struct student s = {"张三",19,86};
// cout << "main函数中,姓名:" << s.name
// << " 年龄:" << s.age
// << " 分数:" << s.score
// << endl;
// //通过函数打印结构体变量信息
// printstudent1(s);
// printstudent2(&s);
//
// system("pause");
// return 0;
//}
/********************7.结构体案例1-毕设老师和学生(自己尝试写)********************************************/
//案例描述:学校在做毕设项目,每名老师带5名学生,共3名老师
/*设计学生与老师的结构体:老师结构体中,有老师姓名和存放5名学生的数组作为成员
学生成员有姓名,考试分数,创建数组存放3名老师,通过函数给每个老师以及所带的学
生赋值,最终打印出老师数据以及老师所带的学生数据
*/
学生的结构体
//struct student
//{
// string name;
// int score;
//};
老师的结构体
//struct teacher
//{
// string name;
// struct student stu1;
// struct student stu2;
// struct student stu3;
// struct student stu4;
// struct student stu5;
//};
//
//int main()
//{
// struct student stu1 = { "马一",100};
// struct student stu2 = { "郑二",88 };
// struct student stu3 = { "张三",79 };
// struct student stu4 = { "李四",77 };
// struct student stu5 = { "王五",66 };
//
// struct teacher te1 = {"爱丽丝",stu1,stu2,stu3,stu4,stu5};
// struct teacher te2 = {"萨利",stu1,stu2,stu3,stu4,stu5 };
// struct teacher te3 = {"怀特",stu1,stu2,stu3,stu4,stu5 };
//
// cout << "指导老师1:" << te1.name
// << endl;
// cout << " 指导学生1姓名:" << te1.stu1.name
// << " 指导学生1分数:" << te1.stu1.score
// << endl;
// cout << " 指导学生2姓名:" << te1.stu2.name
// << " 指导学生2分数:" << te1.stu2.score
// << endl;
// cout << " 指导学生3姓名:" << te1.stu3.name
// << " 指导学生3分数:" << te1.stu3.score
// << endl;
// cout << " 指导学生4姓名:" << te1.stu4.name
// << " 指导学生4分数:" << te1.stu4.score
// << endl;
// cout << " 指导学生5姓名:" << te1.stu5.name
// << " 指导学生5分数:" << te1.stu5.score
// << endl;
//
// cout << "指导老师2:" << te2.name
// << endl;
// cout << " 指导学生1姓名:" << te1.stu1.name
// << " 指导学生1分数:" << te1.stu1.score
// << endl;
// cout << " 指导学生2姓名:" << te1.stu2.name
// << " 指导学生2分数:" << te1.stu2.score
// << endl;
// cout << " 指导学生3姓名:" << te1.stu3.name
// << " 指导学生3分数:" << te1.stu3.score
// << endl;
// cout << " 指导学生4姓名:" << te1.stu4.name
// << " 指导学生4分数:" << te1.stu4.score
// << endl;
// cout << " 指导学生5姓名:" << te1.stu5.name
// << " 指导学生5分数:" << te1.stu5.score
// << endl;
//
// cout << "指导老师3:" << te3.name
// << endl;
// cout << " 指导学生1姓名:" << te1.stu1.name
// << " 指导学生1分数:" << te1.stu1.score
// << endl;
// cout << " 指导学生2姓名:" << te1.stu2.name
// << " 指导学生2分数:" << te1.stu2.score
// << endl;
// cout << " 指导学生3姓名:" << te1.stu3.name
// << " 指导学生3分数:" << te1.stu3.score
// << endl;
// cout << " 指导学生4姓名:" << te1.stu4.name
// << " 指导学生4分数:" << te1.stu4.score
// << endl;
// cout << " 指导学生5姓名:" << te1.stu5.name
// << " 指导学生5分数:" << te1.stu5.score
// << endl;
//
// system("pause");
// return 0;
//
//}
/********************7.结构体案例1-毕设老师和学生(老师讲解)********************************************/
//案例描述:学校在做毕设项目,每名老师带5名学生,共3名老师
/*设计学生与老师的结构体:老师结构体中,有老师姓名和存放5名学生的数组作为成员
学生成员有姓名,考试分数,创建数组存放3名老师,通过函数给每个老师以及所带的学
生赋值,最终打印出老师数据以及老师所带的学生数据
*/
学生的结构体定义
//struct student
//{
// //学生姓名
// string sname;
// //学生分数
// int score;
//};
老师的结构体定义
//struct teacher
//{
// //老师姓名
// string tname;
// //学生数组
// struct student sarry[5];
//};
//
给老师和学生赋值的函数
//void allocatespace(struct teacher tarry[],int len1, struct student sarry[], int len2)
//{
// string nameseed = "ABCDE";
// //给老师进行属性赋值
// for (int i = 0; i < len1; i++)
// {
// tarry[i].tname = "teacher_";
// tarry[i].tname += nameseed[i];
// //通过循环给每名老师所带学生赋值
// for (int j = 0; j < len2; j++)
// {
// //学生姓名
// tarry[i].sarry[j].sname = "student_";
// tarry[i].sarry[j].sname += nameseed[j];
// //学生分数(定值)
// //tarry[i].sarry[j].score = 60;
// //学生分数(随机值)
// int random = rand() % 61+40;//40~100分之间随机数
// tarry[i].sarry[j].score = random;
// }
//
// }
//
//}
//
打印所有的信息的函数
//void printinfo(struct teacher tarry[], int len1, struct student sarry[], int len2)
//{
// //遍历老师信息
// for (int i = 0; i < len1; i++)
// {
// cout << "指导老师姓名:" << tarry[i].tname << endl;
// for (int j = 0; j < len2; j++)
// {
// cout << "\t学生姓名:" << tarry[i].sarry[j].sname
// << " 学生成绩:" << tarry[i].sarry[j].score
// << endl;
// }
// }
//
//}
//
//int main()
//{
// //随机数种子
// srand((unsigned int)time(NULL));
//
1.创建三名老师的数组
// struct teacher tarry[3];
// //老师信息数组的长度
// int len1 = sizeof(tarry) / sizeof(tarry[0]);
//
// //创建五名学生的数组
// struct student sarry[5];
// //学生信息数组的长度
// int len2 = sizeof(sarry) / sizeof(sarry[0]);
//
2.通过函数给三名老师的信息赋值,并给老师所带的学生信息赋值
// allocatespace(tarry, len1,sarry,len2);
3.打印所有老师及所带学生的信息
// printinfo(tarry, len1, sarry,len2);
//
// system("pause");
// return 0;
//}
/********************8.结构体案例2-创建英雄结构体(自己尝试)********************************************/
/*说明:
设计一个英雄的结构体,包括成员姓名,年龄,性别,创建结构体数组
数组中存放五名英雄,通过冒泡排序算法,将数组中的英雄按照年龄进
行升序排列,打印排序后的结果
*/
英雄的结构体定义
//struct hero
//{
// string name;
// int age;
// string gender;
//};
//
//
冒泡排序的函数
//void bobble
//
//
//
//
//int main()
//{
// //定义英雄数组
// struct hero harry[5] =
// {
// {"刘备",23,"男"},
// {"关羽",22,"男"},
// {"张飞",20,"男"},
// {"赵云",21,"男"},
// {"貂蝉",19,"女"}
// };
// //英雄的数组长度
// int len = sizeof(harry) / sizeof(harry[0]);
//
// system("pause");
// return 0;
//
//}
/********************8.结构体案例2-创建英雄结构体(老师讲解)********************************************/
/*说明:
设计一个英雄的结构体,包括成员姓名,年龄,性别,创建结构体数组
数组中存放五名英雄,通过冒泡排序算法,将数组中的英雄按照年龄进
行升序排列,打印排序后的结果
*/
//1.设计英雄结构体数组
struct hero
{
string name;//姓名
int age;//年龄
string gender;//性别
};
//对年龄冒泡排序的函数
void bublesort(struct hero harray[], int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
//如果j下标的年龄>j+1下标的年龄,则这两个年龄互换
if (harray[j].age > harray[j + 1].age)
{
struct hero temp = harray[j];
harray[j] = harray[j + 1];
harray[j + 1] = temp;
}
}
}
}
//打印大银幕排序后的数组中的所有信息
void printhero(struct hero harray[], int len)
{
cout << "年龄升序排序后的结果" << endl;
for (int i = 0; i < len; i++)
{
cout << "姓名:" << harray[i].name
<< " 年龄:" << harray[i].age
<< " 性别:" << harray[i].gender
<< endl;
}
}
int main()
{
//2.创造数组存放五名英雄
struct hero harray[5] =
{
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"},
};
//英雄的数组长度
int len = sizeof(harray) / sizeof(harray[0]);
cout << "年龄升序排序前的结果" << endl;
for(int i=0;i<len;i++)
{
cout << "姓名:" << harray[i].name
<< " 年龄:" << harray[i].age
<< " 性别:" << harray[i].gender
<< endl;
}
//3.对数组进行排序,按照年龄进行升序
bublesort(harray, len);
//4.将排序后的结果打印
printhero(harray,len);
system("pause");
return 0;
}