// 结构体
void fun1();
void fun1()
{
// 结构体声明
// 结构体是一种自定义类型
// struct 结构体名称 {
// 类型说明符 成员名;
// ......
// 类型说明符 成员名;
// };
// 示例代码
struct student {
char name[30]; // 姓名
int age; // 年龄
float height; // 身高
};
// 定义结构体类型变量
struct student stu1 = {"lidaze", 18, 180};
// 结构体成员的表示形式:
// 结构体变量名.成员变量名
// 示例代码
// stu1.name = "lixiaoze";
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stu1.age = 12;
stu1.height = 180.0;
// 匿名结构体
// 结构体的声明与变量的定义组合在一起
// 不常用,了解即可
struct {
int num;
char name[20];
char sex;
float score;
}
stu3 = {12, "xiaoming", 'f', 42.5},
stu4 = {18, "xiaoli", 'm', 89,4};
}
// 给结构体起新的名字
void fun2();
void fun2()
{
// 为原类型起一个新名字
// 给int类型起一个新名字
typedef int Integer;
struct stu {
int num;
char name[20];
char sex;
float score;
};
// 为结构体起一个新名字
typedef struct stu Student;
// 或者,直接写成这个样子
typedef struct {
int num;
char name[20];
char sex;
float score;
} Students;
// 结构体变量可以直接赋值
Students stu5 = {3, "haha", 'm', 90.0};
Students stu6 = stu5;
}
// 练习1
void practice1();
void practice1()
{
// 有三个学生,编程找出分数最高者以及年龄最小者
// 1. 定义结构体类型
typedef struct {
char name[20];
int age;
float score;
} Student;
// 2. 创建三名学生,装进数组中
Student stu1 = {"xiaoliya", 18, 83.0};
Student stu2 = {"xiaohongzi", 10, 80.0};
Student stu3 = {"xiaoqiqi", 12, 98.3};
// 3. 对比出结果
// 年龄最小的人
Student minAgeStu = {0};
minAgeStu = stu1.age > stu2.age ? stu2 : stu1;
minAgeStu = minAgeStu.age > stu3.age ? stu3 : minAgeStu;
printf("minAgeStudent: name = %s age = %d score = %.2f\n", minAgeStu.name, minAgeStu.age, minAgeStu.score);
// 分数最高的人
Student maxScoreStu = {0};
maxScoreStu = stu1.score > stu2.score ? stu1 : stu2;
maxScoreStu = maxScoreStu.score > stu3.score ? maxScoreStu : stu3;
printf("maxScoreStudent: name = %s age = %d score = %.2f\n", maxScoreStu.name, maxScoreStu.age, maxScoreStu.score);
}
// 结构体的嵌套
void fun3();
void fun3()
{
// 日期结构体
typedef struct {
int year;
int month;
int day;
} Data;
// 人结构体
typedef struct {
char name[20];
int age;
Data birthday; // 生日:结构体类型
} Person;
// 嵌套结构体的使用
Person person1 = {"lidaze", 18, {2014, 12, 19}};
printf("person: name = %s age = %d birthday = %d-%d-%d", person1.name, person1.age, person1.birthday.year, person1.birthday.month, person1.birthday.day);
}
// 结构体数组
void fun4();
void fun4()
{
// Student结构体
typedef struct {
char name[20];
int age;
} Student;
// 结构体数组
Student stus[] = {
{"heihei", 19},
{"haha", 20},
{"nimenliaxiaopi", 29}
};
// 打印结构体数组中所有成员的名称
for (int i = 0; i < sizeof(stus) / sizeof(Student); i++) {
printf("name = %-15s age = %d\n", stus[i].name, stus[i].age);
}
}
// 练习2
typedef struct {
float score;
char name[20];
} Student;
// 4.1 数组排序函数
void bubbleSort(Student stu[], int count);
void bubbleSort(Student stu[], int count)
{
// sizeof(stu) / sizeof(Student); // 数组作为参数的时候,传递是首地址(指针),所以,不能使用这种方式获取数组长度
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (stu[j].score < stu[j + 1].score) {
Student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
// 5.1 打印结果函数
void printStus(Student stu[], int count);
void printStus(Student stu[], int count)
{
for (int i = 0; i < count; i++) {
printf("score = %.2f name = %s\n", stu[i].score, stu[i].name);
}
}
void practice2();
void practice2()
{
// 1. 定义结构体类型
// 放在函数的外部,这样就成了全部变量,作用域更大,使用起来更方便
// 2. 创建出来5个学生变量
Student stu1 = {89.23, "laoda"};
Student stu2 = {32.21, "laoer"};
Student stu3 = {83.23, "zhangsan"};
Student stu4 = {59.43, "laosi"};
Student stu5 = {88.88, "laowu"};
// 3. 把5名学生装进数组中
Student stus[] = {stu1, stu2, stu3, stu4, stu5};
// 4. 调用函数排序
bubbleSort(stus, (sizeof(stus) / sizeof(Student)));
// 5. 打印结果
printStus(stus, (sizeof(stus) / sizeof(Student)));
// 找出成绩最高的人
printf("maxScore = %.2f name = %s\n", stus[0].score, stus[0].name);
}
int main(int argc, const char * argv[])
{
// 结构体内存的占用
// 以最大成员变量类型所占空间为分配单位
// 按结构体成员声明顺序自上而下分配。
// 注:分配空间不足以存储成员变量时,分配新的空间单位
return 0;
}