Presentation is loading. Please wait.

Presentation is loading. Please wait.

安排座位.

Similar presentations


Presentation on theme: "安排座位."— Presentation transcript:

1 安排座位

2 数据结构 授课教师:李超燕 计算机专业 东校区3号楼206办公室

3 本课程介绍 章 节 学时 共4*14=56学时,全程在机房上课 C语言复习 2 第二章 线性表 16 第三章 栈和队列 14 第六章 树 8
第七章 图 6 第八章 查找 4 第九章 排序

4 本课程要求 复习C语言的内容。 上课不无故缺席,不迟到、早退。 上课认真听讲,努力思考。 课后独立完成作业,上交到网络平台。
认真完成上机任务。 期末成绩中平时成绩占40%,期末成绩占60% 上课时带草稿本和笔,教材,U盘。

5 平时成绩的组成 出勤情况 学习态度 完成作业 提问测试

6 本次课主题:结构体与指针

7 思考? 实例1 结构体 num name sex age score addr Beijing 87.5 18 M Li Fun 1001
学生的基本信息 结构体将不同类型的数据组合成一个有机的整体。

8 … ….. 例 struct student { int num; char name[20]; char sex; int age;
一、结构体的定义 结构体是一种构造数据类型 用途:把不同类型的数据组合成一个整体--自定义数据类型 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; name num sex age score addr 2字节 20字节 1字节 4字节 30字节 …..

9 struct student stu1,stu2;
二、结构体变量的定义 struct 结构体名 { 类型标识符 成员名; ……………. }; struct 结构体名 变量名表列; 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; struct student stu1,stu2;

10 struct student stu1,stu2;
三、结构体变量的引用 引用规则 结构体变量不能整体引用,只能引用变量成员 引用方式: 结构体变量名.成员名 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; struct student stu1,stu2; stu1.num=10; stu1.score=85.5; stu1.score+=stu2.score; stu1.age++;

11 作用:声明新的类型名来代替已有的类型名。
四、typedef的使用 作用:声明新的类型名来代替已有的类型名。 例如:typedef int aa; typedef float bb; int x;  aa x; float y;  bb y; 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; struct student stu1,stu2; typedef struct { int num; char name[20]; char sex; int age; float score; char addr[30]; }student; student stu1,stu2;

12 练习1 结构体 1.利用typedef来定义1个结构体变量student,该结构体有三个成员构成 no:字符型数组,长度为8
name:字符型数组,长度为20 age:整型 2.给student变量赋上自己的学号后7位,姓名,年龄。 3.在devcpp中利用主函数写程序并输出student变量的值。

13 实例2 结构体和指针 一、指向结构体变量的指针 定义形式:struct 结构体名 *结构体指针名; 例 struct student *p;
num name sex age stu p struct student { int num; char name[20]; char sex; int age; }; struct student stu; struct student *p; p=&stu;

14 思考 利用typedef类型定义符来完成。 typedef struct struct student { int num;
char name[20]; char sex; int age; }; struct student stu; struct student *p; p=&stu; typedef struct { int num; char name[20]; char sex; int age; }student; student *p; student stu; p=&stu;

15 stu1.num=101;  (*p).num=101; p->num=101
使用结构体指针变量引用成员形式 typedef struct { int num; char name[20]; char sex; int age; }student; student stu1; student *p=&stu1; stu1.num=101;  (*p).num=101; p->num=101 (*结构体指针名).成员名 结构体指针名->成员名 结构体变量名.成员名

16 提问 定义一个变量stu2,指针p2,指针指向stu2,利用三种方式将stu2的age赋成20。 typedef struct
{ int num; char name[20]; char sex; int age; }student; student stu2,*p2; p2=&stu2; p2->age=20; (*p2).age=20; stu2.age=20;

17 练习2 结构体指针 此练习在练习1的基础上进行修改:
1.利用typedef来定义1个结构体变量student,1个结构体指针p,该结构体有三个成员构成 no:字符型数组,长度为8 name:字符型数组,长度为20 age:整型 2.利用指针指向的形式给student变量赋上自己的学号后7位,姓名,年龄。 3.在devcpp中利用主函数写程序并利用指针指向的形式输出student变量的值。


Download ppt "安排座位."

Similar presentations


Ads by Google