THE SEVENTH DAY LEARNING (结构体 C语言)
结构体
//定义手机的数据类型(品牌, 价格, 颜色)
struct mobilePhone{
char brand[40];
float price;
char color[10];
};
//数据类型
//1. 容器大小(所占字节)
//2. 容器内存放什么样的数据
//BOOL 1 YES或NO %d
//char 1 'a' %c, %d
//short 2 18 %hd
//int 4 188 %d
//long 4或8 1888 %ld
//float 4 3.1415 %f
//double 8 3.1415926 %lf
//结构体是一种可以自定义的数据类型, 把系统的数据类型组合在一起, 形成新的数据类型
//结构体的声明: 创建一种新的数据类型
//格式
/*
struct 结构体的名字{
成员变量数据类型1 成员变量名1;
成员变量数据类型2 成员变量名2;
成员变量数据类型3 成员变量名3;
...
};
*/
/*
//定义一个学生的数据类型
struct student{
int number;//学号
char name[20];//姓名
char gender;//性别
int age;//年龄
float score;
};
//注:
//1.结构体名用小驼峰法
//2.每一个成员变量后加分号
//3.成员变量的数据类型可以不同
//4.结构体可以声明在函数外部, 也可以声明在函数内部
//结构体的使用
//定义一个整型的变量
// int a = 10;
//定义一个结构体的变量
struct student stu = {9527, "王麻子", 'f', 18, 99.9};
//注:
//1.struct student: 数据类型
//2.stu: 变量名
//{}: 为每个成员变量赋值, 按照成员变量的顺序和数据类型, 为其赋值
//定义一个手机的结构体变量
struct mobilePhone phone = {"iPhone 6s", 99.8, "土豪金"};
//如何使用结构体变量
//访问结构体的成员变量: 点语法
//格式: 结构体变量名.成员变量名
printf("number = %d\n", stu.number);
printf("name = %s\n", stu.name);
printf("gender = %c\n", stu.gender);
printf("age = %d\n", stu.age);
printf("score = %.2f\n", stu.score);
//注: 不能够直接打印结构体变量, 可以打印结构体变量的成员变量
printf("brand = %s\nprice = %.2f\ncolor = %s\n", phone.brand, phone.price, phone.color);
struct student stu1 = {0};//每一个成员变量都为0
//为每一个成员变量赋值
stu1.number = 989;
stu1.gender = 'm';
stu1.age = 19;
stu1.score = 98.5;
strcpy(stu1.name , "李四");
printf("number = %d\nname = %s\ngender = %c\nage = %d\nscore = %.2f\n", stu1.number, stu1.name, stu1.gender, stu1.age, stu1.score);
struct mobilePhone phone1 = {0};
strcpy(phone1.brand, "iphone 6s plus");
phone1.price = 199.9;
strcpy(phone1.color, "深空灰");
printf("手机:\nbrand = %s\nprice = %.2f\ncolor = %s\n", phone1.brand, phone1.price, phone1.color);
struct student stu2 = {0};
stu2 = stu1;//结构体变量可以直接赋值
printf("\nnumber = %d\nname = %s\ngender = %c\nage = %d\nscore = %.2f\n", stu2.number, stu2.name, stu2.gender, stu2.age, stu2.score);
*/
/*
//有三个学生, 编程找出分数最高者以及年龄最小者
struct student{
int number;//学号
char name[20];//姓名
char gender;//性别
int age;//年龄
float score;
};
struct student stu1 = {9527, "王麻子", 'f', 18, 99.9};
struct student stu2 = {9522, "李四", 'f', 22, 88};
struct student stu3 = {9520, "张三", 'f', 17, 90};
struct student stu4 = {0};
struct student stu5 = {0};
// stu4 = stu1.score > stu2.score ? stu1 : stu2;
// stu4 = stu4.score > stu3.score ? stu4 : stu3;
// stu5 = stu1.age < stu2.age ? stu1 : stu2;
// stu5 = stu5.age < stu3.age ? stu5 : stu3;
stu4 = stu1.score > stu2.score ? (stu1.score > stu3.score ? stu1 : stu3) : (stu2.score > stu3.score ? stu2 : stu3);
stu5 = stu1.age < stu2.age ? (stu1.age < stu3.age ? stu1 : stu3) : (stu2.age < stu3.age ? stu2 : stu3);
printf("分数最高者为: %d %s\t %c %d %.2f\n", stu4.number, stu4.name, stu4.gender, stu4.age, stu4.score);
printf("年龄最小者为: %d %s\t %c %d %.2f\n", stu5.number, stu5.name, stu5.gender, stu5.age, stu5.score);
//2.使用typedef为数组结构体类型量命名
//格式:
//typedef 元数据类型名新数据类型名
//注: 新的数据类型名字以大驼峰法命名
int b = 10;
typedef int AAA;
AAA c = 10;
int d = 10;
struct student s4 = {0};
typedef struct student Student;
Student s5 = {0};
*/
//声明一个结构体
//1
/*
struct computer {
char brand[20];//品牌
float price;//价格
};
typedef struct computer Computer;
*/
//2
/*
typedef struct computer {
char brand[20];//品牌
float price;//价格
}Computer;
*/
//3.匿名结构体重命名, 一定要配合着typedef
/*
typedef struct {
char brand[20];//品牌
float price;//价格
}Computer;
*/
/*
//3.结构体所占的字节数
//a.是最大成员变量数据类型的倍数
//b.有效调整成员变量的位置, 能够降低内存碎片
struct abc{
char a;
char b;
short c;
int d;
};
typedef struct abc ABC;
printf("ABC所占的字节数:%lu\n", sizeof(ABC));
//4.结构体变量和数组
int e = 0, f = 20, g = 30, h = 40;
int array[4] = {10, 20, 30, 40};
//结构体数组
//结构体声明
struct apple {
char color[20];
float price;
float weight;
};
//结构体重命名
typedef struct apple Apple;
//结构体数组
//1
Apple a1 = {"red", 3.5, 150};
Apple a2 = {"green", 5.5, 100};
Apple a3 = {"yellow", 4.5, 250};
Apple a4 = {"black", 0.1, 200};
Apple arr[4] = {a1, a2, a3, a4};
//2
/*
Apple arr1[4] = {
{"red", 3.5, 150},
{"green", 5.5, 100},
{"yellow", 4.5, 250},
{"black", 0.1, 200}
};
*/
/*
// arr[0].color
// arr[0].price
// arr[0].weight
for (int i = 0; i < 4; i++) {
printf("%s \t%.2f\t%.2f\n", arr[i].color, arr[i].price, arr[i].weight);
}
*/
/*
//对结构体数组进行冒泡排序
//按价格从高到低排序
struct apple {
char color[20];
float price;
float weight;
};
//结构体重命名
typedef struct apple Apple;
Apple arr1[4] = {
{"red", 3.5, 150},
{"green", 5.5, 100},
{"yellow", 4.5, 250},
{"black", 0.1, 200}
};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3 - i; j++) {
//比较的是某个成员变量的值, 交换的是结构体变量
if (arr1[j].price < arr1[j + 1].price) {
Apple temp = arr1[j];
arr1[j] = arr1[j + 1];
arr1[j + 1] = temp;
}
}
}
for (int i = 0; i < 4; i++) {
printf("%s %.2f %.2f\n", arr1[i].color, arr1[i].price, arr1[i].weight);
}
//按照重量从低到高排序
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3 - i; j++) {
if (arr1[j].weight > arr1[j + 1].weight) {
Apple temp = arr1[j];
arr1[j] = arr1[j + 1];
arr1[j + 1] = temp;
}
}
}
printf("\n");
for (int i = 0; i < 4; i++) {
printf("%s %.2f %.2f\n", arr1[i].color, arr1[i].price, arr1[i].weight);
}
//按照颜色从a~z
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3 - i; j++) {
if (strcmp(arr1[j].color, arr1[j + 1].color) > 0) {
Apple temp = arr1[j];
arr1[j] = arr1[j + 1];
arr1[j + 1] = temp;
}
}
}
printf("\n");
for (int i = 0; i < 4; i++) {
printf("%s %.2f %.2f\n", arr1[i].color, arr1[i].price, arr1[i].weight);
}
*/
//有5名学⽣生保存在结构体数组中,编程查找成绩最高者,输出该学⽣生全部信息
struct student{
int num;
char name[20];
char gender[5];
int age;
float score;
};
typedefstruct student Student;
Student stu = {};
Student array[5] ={{101, "张三", "男", 22, 87},{302, "李四", "男", 20, 88},{153, "王灵", "女", 19, 98},{255, "李明", "男", 25, 78},{353, "王丽", "女", 23, 89}};
//求成绩最高者
for (int i = 0 ; i < 5; i++) {
if (array[i].score > array[i + 1].score) {
stu = array[i];
}
}
printf("成绩最高:\n");
printf("%d %s %s %d %.2f\n",stu.num, stu.name, stu.gender, stu.age, stu.score);
//成绩从高到低排序
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4 - i; j++) {
if (array[j].score < array[j + 1].score) {
Student temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("\n");
printf("成绩从高到低排序结果:\n");
for (int i = 0; i < 5; i++) {
printf("%d %s %s %d %.2f\n", array[i].num, array[i].name, array[i].gender, array[i].age, array[i].score);
}