day12
数据类型:
基本数据类型
构造数据类型
struct union enum
结构体:
1.结构体类型定义:
struct 结构体名
{
数据类型1 成员变量1;
数据类型2 成员变量2;
数据类型3 成员变量3;
};
2.结构体变量的定义:
存储类型 数据类型 变量名;
3.结构体变量的初始化:
初始化 != 赋值
1.全部初始化:
struct student s = {"zhangsan", 'm', 19, 100};
2.局部初始化:
struct student s = {
.name = "lisi",
.score = 100,
};
没有给定初值的成员变量为0值
3.结构体赋值:
struct student e;
e = {"zhangsan", 'm', 19, 100}; //错
e = a; //对
4.结构体访问:
结构体访问成员变量的类型,最终类型是由成员变量
类型决定的
两种方式:
1.结构体变量访问成员变量用.
2.结构体指针访问成员变量用->
练习:
从终端输入学生的姓名、性别、年龄和成绩放入结构体变量
中,并完成打印
eg1
#include <stdio.h>
struct date
{
int year;
int mon;
int day;
};
struct time
{
int hour;
int min;
int sec;
};
struct date_time
{
struct date today;
struct time now;
};
struct student
{
char name[32];
char sex;
int age;
int score;
};
int main(int argc, const char *argv[])
{
struct student a = {
"zhangsan", 'm', 18, 90};
struct student e;
struct student *pstu = NULL;
struct date_time b = {
{
2019, 12, 11}, {
10, 42, 45}};
struct student c = {
.name = "lisi",
.score = 90,
};
struct date_time *p = NULL;
struct date_time d = {
.today = {
.year = 2019,
.day = 11,
},
.now = {
.hour = 10,
.sec = 15,
},
};
e = a;
pstu = &a;
p = &d;
printf("===================\n");
printf("姓名:%s\n", a.name);
printf("性别:%c\n", a.sex);
printf("年龄:%d\n", a.age);
printf("成绩:%d\n", a.score);
printf("===================\n");
printf("姓名:%s\n", e.name);
printf("性别:%c\n", e.sex);
printf("年龄:%d\n", e.age);
printf("成绩:%d\n", e.score);
printf("===================\n");
printf("姓名:%s\n", c.name);
printf("性别:%c\n", c.sex);
printf("年龄:%d\n", c.age);
printf("成绩:%d\n", c.score);
printf("===================\n");
printf("姓名:%s\n", (*pstu).name);
printf("性别:%c\n", (*pstu).sex);
printf("年龄:%d\n", (*pstu).age);
printf("成绩:%d\n", (*pstu).score);
printf("===================\n");
printf("姓名:%s\n", pstu->name);
printf("性别:%c\n", pstu->sex);
printf("年龄:%d\n", pstu->age);
printf("成绩:%d\n", pstu->score);
printf("===================\n");
printf("姓名:%s\n", (&a)->name);
printf("性别:%c\n", (&a)->sex);
printf("年龄:%d\n", (&a)->age);
printf("成绩:%d\n", (&a)->score);
printf("====================\n");
printf("%04d年%02d月%02d日 %02d:%02d:%02d\n", b.today.year, b.today.mon, b.today.day,
b.now.hour, b.now.min, b.now.sec);
printf("%04d年%02d月%02d日 %02d:%02d:%02d\n", d.today.year, d.today.mon, d.today.day,
d.now.hour, d.now.min, d.now.sec);
printf("====================\n");
printf("%04d年%02d月%02d日 %02d:%02d:%02d\n", p->today.year, p->today.mon, p->today.day,
p->now.hour, p->now.min, p->now.sec);
return 0;
}
eg2
#include <stdio.h>
#include <string.h>
struct student
{
char name[32];
char sex;
int age;
int score;
};
int main(int argc, const char *argv[])
{
struct student s;
char str[32] = {
0};
memset(&s, 0, sizeof(s));
strcpy(s.name, "zhangsan");
s.sex = 'm';
s.age = 18;
s.score = 100;
printf("姓名:%s\n", s.name);
printf("性别:%c\n", s.sex);
printf("年龄:%d\n", s.age);
printf("成绩:%d\n", s.score);
return 0;
}
eg3
#include <stdio.h>
#include <string.h>
struct student
{
char name[32];
char sex;
int age;
int score;
};
int main(int argc, const char *argv[])
{
struct student s;
memset(&s, 0, sizeof(s));
printf("请输入姓名:\n");
gets(s.name);
printf("请输入性别:\n");
s.sex = getchar();
printf("请输入年龄:\n");
scanf("%d", &s.age);
printf("请输入成绩:\n");
scanf("%d", &s.score);
printf("姓名:%s\n", s.name);
printf("性别:%c\n", s.sex);
printf("年龄:%d\n", s.age);
printf("成绩:%d\n", s.score);
return 0;
}
eg4
#include <stdio.h>
struct student
{
char name[32];
char sex;
int age;
int score;
};
struct student GetStuInfo(void)
{
struct student s;
printf("请输入姓名:\n");
gets(s.name);
printf("请输入性别:\n");
s.sex = getchar();
printf("请输入年龄:\n");
scanf("%d", &s.age);
printf("请输入成绩:\n");
scanf("%d", &s.score);
return s;
}
void GetStuInfoByPoint(struct student *pstu)
{
printf("请输入姓名:\n");
gets(pstu->name);
printf("请输入性别:\n");
pstu->sex = getchar();
printf("请输入年龄:\n");
scanf("%d", &pstu->age);
printf("请输入成绩:\n");
scanf("%d", &pstu->score);
return;
}
int PutStuInfo(struct student s)
{
printf("姓名:%s\n", s.name);
printf("性别:%c\n", s.sex);
printf("年龄:%d\n", s.age);
printf("成绩:%d\n", s.score);
return 0;
}
int PutStuInfoByPoint(struct student *pstu)
{
printf("姓名:%s\n", pstu->name);
printf("性别:%c\n", pstu->sex);
printf("年龄:%d\n", pstu->age);
printf("成绩:%d\n", pstu->score);
return 0;
}
int main(int argc, const char *argv[])
{
struct student tmp;
#if 0
// tmp = GetStuInfo();
GetStuInfoByPoint(&tmp);
PutStuInfo(tmp);
PutStuInfoByPoint(&tmp);
#endif
printf("%d\n", sizeof(tmp));
return 0;
}
eg5
#include <stdio.h>
struct student
{
char name[32];
char sex;
int age;
int score;
};
int ShowAllStuInfo(struct student *pstu, int Len)
{
int i = 0;
for (i = 0; i < Len; i++)
{
printf("姓名:%s\n", pstu[i].name);
printf("性别:%c\n", pstu[i].sex);
printf("年龄:%d\n", pstu[i].age);
printf("成绩:%d\n", pstu[i].score);
}
#if 0
for (i = 0; i < Len; i++)
{
printf("姓名:%s\n", (*(pstu+i)).name);
printf("性别:%c\n", (*(pstu+i)).sex);
printf("年龄:%d\n", (*(pstu+i)).age);
printf("成绩:%d\n", (*(pstu+i)).score);
}
for (i = 0; i < Len; i++)
{
printf("姓名:%s\n", pstu->name);
printf("性别:%c\n", pstu->sex);
printf("年龄:%d\n", pstu->age);
printf("成绩:%d\n", pstu->score);
pstu++;
}
for (i = 0; i < Len; i++)
{
printf("姓名:%s\n", (pstu+i)->name);
printf("性别:%c\n", (pstu+i)->sex);
printf("年龄:%d\n", (pstu+i)->age);
printf("成绩:%d\n", (pstu+i)->score);
}
#endif
return 0;
}
void GetAllStuInfo(struct student *pstu, int Len)
{
int i = 0;
for (i = 0; i < Len; i++)
{
printf("请输入姓名:\n");
gets(pstu[i].name);
printf("请输入性别:\n");
pstu[i].sex = getchar();
printf("请输入年龄:\n");
scanf("%d", &pstu[i].age);
printf("请输入成绩:\n");
scanf("%d", &pstu[i].score);
getchar();
}
#if 0
for (i = 0; i < Len; i++)
{
printf("请输入姓名:\n");
gets(pstu->name);
printf("请输入性别:\n");
pstu->sex = getchar();
printf("请输入年龄:\n");
scanf("%d", &pstu->age);
printf("请输入成绩:\n");
scanf("%d", &pstu->score);
getchar();
pstu++;
}
for (i = 0; i < Len; i++)
{
printf("请输入姓名:\n");
gets((pstu+i)->name);
printf("请输入性别:\n");
(pstu+i)->sex = getchar();
printf("请输入年龄:\n");
scanf("%d", &(pstu+i)->age);
printf("请输入成绩:\n");
scanf("%d", &(pstu+i)->score);
getchar();
}
#endif
return;
}
int main(int argc, const char *argv[])
{
int i = 0;
int Len = 0;
#if 0
struct student s[5] = {
{
"zhangsan", 'm', 19, 100},
{
"lisi", 'f', 17, 90},
{
"wanger", 'm', 18, 70},
{
"zhaowu", 'm', 20, 80},
{
"maliu", 'm', 21, 90},
};
#endif
struct student s[3];
GetAllStuInfo(s, 3);
ShowAllStuInfo(s, 3);
return 0;
}
4.结构体的存储:
内存对齐:
字节对齐
半字对齐
字对齐
1.结构体成员变量只能存放在内存地址为自身基本类型长度
整数倍的内存单元中
2.结构体空间大小必须为最大类型长度整数倍
5.结构体数组:
共用体:
联合体
union
union 共用体类型
{
数据类型1 成员变量1;
数据类型2 成员变量2;
数据类型3 成员变量3;
};
#include <stdio.h>
union s
{
char a;
short b;
int c;
};
int main(int argc, const char *argv[])
{
union s s1;
s1.a = 'a';
printf("s1.a = %c\n", s1.a);
s1.b = 100;
printf("s1.b = %d\n", s1.b);
printf("s1.a = %c\n", s1.a);
s1.c = 200;
printf("s1.c = %d\n", s1.c);
printf("s1.b = %d\n", s1.b);
return 0;
}
测试当前平台的内存大小端:
内存小端:
内存低地址存放低数据位
内存高地址存放高数据位
内存大端:
内存低地址存放高数据位
内存高地址存放低数据位
判断大小端方法1
#include <stdio.h>
int main(int argc, const char *argv[])
{
int Num = 0x11223344;
char *p = (char *)&Num;
if (0x44 == *p)
{
printf("小端!\n");
}
else
{
printf("大端!\n");
}
return 0;
}
方法2
#include <stdio.h>
union s
{
char a;
int b;
};
int main(int argc, const char *argv[])
{
union s s1;
s1.b = 1;
if (s1.a)
{
printf("小端!\n");
}
else
{
printf("大端!\n");
}
return 0;
}
struct student
{
char name[32];
int id;
int lesson[5];
};
2.有一个班的4个学生,有5门课程。
1、求第一门课的平均分;
2、找出有两门以上课程不及格的学生,输出他们的学号和
全部课程成绩及平均分
3、找出平均分在90分以上或全部课程成绩在85分以上的学生。
分别编写三个函数来实现以上三个要求
#include <stdio.h>
struct student{
char name[32];
int id;
int lesson[5];
};
double GetAllStuAve(struct student *pStu,int n,int num)
{
int i = 0;
int sum = 0;
for (i = 0; i < n; i++)
{
sum += pStu[i].lesson[num];
}
return sum / (double)n;
}
int Is2FailStu(struct student stu)
{
int i = 0;
int cnt = 0;
for (i = 0; i < 5;i++)
{
if (stu.lesson[i] < 60)
{
cnt++;
}
}
if (cnt > 2)
{
return 1;
}
return 0;
}
int Get2FailStu(struct student *pStu,int n)
{
int i = 0;
int j = 0;
int sum = 0;
for (i = 0; i < n; i++)
{
if (Is2FailStu(pStu[i]) == 1)
{
printf("%d: ",pStu[i].id);
for (j = 0; j < 5;j++)
{
sum += pStu[i].lesson[j];
printf("%d ",pStu[i].lesson[j]);
}
printf("ave = %lf\n",sum / 5.0);
}
}
}
double GetAveOfOne(struct student stu)
{
int i = 0;
int sum = 0;
for (i = 0; i < 5; i++)
{
sum += stu.lesson[i];
}
return sum / 5.0;
}
int IsAllLessOver85(struct student stu)
{
int i = 0;
for (i = 0; i < 5; i++)
{
if (stu.lesson[i] <= 85)
{
return 0;
}
}
return 1;
}
void GetAveOver90OrAllLessOver85(struct student *pStu,int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
if (GetAveOfOne(pStu[i]) > 90 || IsAllLessOver85(pStu[i]))
{
printf("%s\n",pStu[i].name);
}
}
}
int main(int argc, const char *argv[])
{
double ave = 0;
struct student stu[4] = {
{
"zhangsan",1,{
89,57,88,49,99}},
{
"lisi",2,{
67,45,43,56,78}},
{
"wanger",3,{
78,90,79,46,90}},
{
"zhaosi",4,{
100,99,89,88,96}}
};
ave = GetAllStuAve(stu,4,0);
printf("所有学生第一门课的平均成绩:%lf\n",ave);
Get2FailStu(stu,4);
GetAveOver90OrAllLessOver85(stu,4);
return 0;
}
=====================================================================================
day13
共用体:
复习大小端
#include <stdio.h>
union s
{
char a;
int b;
};
int main(int argc, const char *argv[])
{
union s s1;
s1.b = 1;
if (s1.a)
{
printf("小端!\n");
}
else
{
printf("大端!\n");
}
return 0;
}
结果为:小端
1.有一个16进制数据0x11223344
让第一个字节和最后一个字节数字相加
让第二个字节和第三个字节数字相加得到新结果
例:0x11223344
struct s
{
char a;
char b;
char c;
char d;
};
union u
{
struct s s1;
int e;
};
#include <stdio.h>
struct s
{
char a;
char b;
char c;
char d;
};
union u
{
struct s s1;
int e;
};
int main(int argc, const char *argv[])
{
union u u1;
u1.e = 0x11223344;
printf("%#x\n", u1.s1.a + u1.s1