C语言程序设计基础9.结构

上传人:ni****g 文档编号:575181760 上传时间:2024-08-17 格式:PPT 页数:46 大小:183KB
返回 下载 相关 举报
C语言程序设计基础9.结构_第1页
第1页 / 共46页
C语言程序设计基础9.结构_第2页
第2页 / 共46页
C语言程序设计基础9.结构_第3页
第3页 / 共46页
C语言程序设计基础9.结构_第4页
第4页 / 共46页
C语言程序设计基础9.结构_第5页
第5页 / 共46页
点击查看更多>>
资源描述

《C语言程序设计基础9.结构》由会员分享,可在线阅读,更多相关《C语言程序设计基础9.结构(46页珍藏版)》请在金锄头文库上搜索。

1、第九章 结构v结构v结构数组v结构指针v链表v位运算v自定义类型学号姓名性别出生地出生年出生月出生日数学物理 程序设计学号姓名性别出生地年 月 日 数学 物理 程序设计出生日期国家 省 市/县学习成绩结构:同一个数据项的若干成分构成的一个整体。例如:学生档案,每个学生有学号、姓名、性别、出生地、出生年月、学业成绩等。9.1 结构9.1.1 结构的定义struct student long int num; char name20; float score; ;定义一个结构类型: struct student9.1.2 结构变量的定义1、先定义结构类型,再定义变量struct student l

2、ong int num; char name20; float score; ;struct student stu1, stu2; num namescorestu1200011Zhang85stu2200012Li942、定义结构类型的同时定义变量struct student long int num; char name20; float score; stu1, stu2;3、不指定类型名,只定义变量struct long int num; char name20; float score; stu1, stu2;9.1.3 结构变量的初始化只有全局变量或静态变量才能初始化。stati

3、c struct student stu2=200012, “Li”, 94; num namescorestu2200012Li94 num namescorestu1200011Zhang85struct student long num; char name20; float score; stu1=200011, Zhang, 85;9.1.4 结构变量的使用1、结构类型变量的整体引用(1) 不能整体输入输出,但相同类型的变量可以互相赋值 printf(%ld%s%f, stu1); 非法 stu2=stu1; 合法(2) 可以引用结构体变量的地址 printf(%x, &stu1);

4、 输出stu1的首地址2、结构变量中分量的引用struct student long int num; char name20; float score; stu1, stu2;(1) 结构变量.分量 stu1.num = 9901; printf(%s, stu2.name); num namescorestu1200011Zhang85stu2200012Li94(2) 结构变量中的分量可以依据它的类型进行各种运算x = stu1.score;strcpy(stu1.name, “Wang”);(3) 可以引用结构变量中的分量的地址 scanf(%ld, &stu1.num);9.2 结构

5、数组一个结构变量只能存放一个学生的资料。若班上有20个学生,需要用结构数组。即,数组中的每个元素都是结构类型。9.2.1 定义 struct student long int num; char name20; float score; stu20;stu0200011Zhang85stu19200012Li90200029Zhao70stu19.2.2 初始化 struct student long int num; char name20; float score; stu20=200011,”Zhang”,85, 200012,”Li”,90;stu0200011Zhang85stu19

6、200012Li90200029Zhao70stu19.2.3 引用 struct student long int num; char name20; float score; stu20;stu0.numstu0.namestu0.scorestu0200011Zhang85stu19200012Li90200029Zhao70stu1程序举例例1、输入某班30位学生的姓名及数学、英语成绩,计算并输出每位学生的平均分。struct student char name10; int math, eng; float aver; ;void main( ) struct student s30

7、; int i; for(i=0; i30; i+) scanf(%s%d%d, si.name, &si.math, &si.eng); si.aver = (si.math+si.eng)/2.0 printf(%s%f, si.name, si.aver); s0s29s1Zhang8085Li7790wang6078输入某班30位学生的姓名及数学、英语成绩,计算并输出每门课程的平均分。void main( ) struct student s30; int i; float aver_m=0, aver_e=0;例2 for(i=0; i30; i+) scanf(%s%d%d, si

8、.name, &si.math, &si.eng); aver_m += si.math; aver_e += si.eng; printf(%f%f, aver_m/30, aver_e/30); 输入30位学生的姓名及数学、英语成绩,输出平均分最高的学生的姓名及其数学和英语成绩。struct student char name10; int math, eng; float aver; ;例3void main( ) struct student s30; int i, sub; for(i=0; i30; i+) scanf(%s%d%d, si.name, &si.math, &si.

9、eng); si.aver = (si.math+si.eng)/2.0 sub=0; for( i=1; i ssub.aver ) sub = k; printf(%s%d%dn, ssub.name, ssub.math, ssub.eng);9.3 结构指针9.3.1 定义struct student long int num; char name20; float score; ;struct student stu1, *ptr;ptr = &stu1; num namescorestu1200011Zhang85ptr9.3.2 结构指针的使用struct student stu

10、1, *ptr = &stu1;stu1.num=200011; stu1.score=85; strcpy(stu1.name,”Zhang”);通过指针 ptr 访问结构分量(1) *ptr (*ptr).num = 200011; (*ptr).score = 85; strcpy(*ptr).name,”Zhang”); num namescorestu1200011Zhang85ptr(2) - (*ptr).num = 200011; ptr-num = 200011; ptr-score = 85; strcpy(ptr-name,”Zhang”);当ptr = &stu1时st

11、u1.num(*ptr).numptr-num 三者等价 num namescorestu1200011Zhang85ptrptr-num + 等价于 (ptr-num) + ptr-num 等价于 + (ptr-num)(+ ptr)-num num namescorestu1200011Zhang85ptr9.4 链表9.4.1结构的嵌套定义struct day int y; int m; int d;年 月 日成绩出生日期姓名学号struct student long int num; char name20; struct day birthday; float score; ymds

12、corebirthdaynamenumstruct day int y; int m; int d;struct student long int num; char name20; struct day birthday; float score; stu1, stu2;ymdscorebirthdaynamenum或:struct student long int num; char name20; struct int y, m, d; birthday; float score; stu1, stu2;struct student long int num; char name20;

13、struct int y, m, d; birthday; float score; stu1=9901, “Zhao”, 1980, 10,30, 80, stu2;stu2.birthday.y=1979;stu2.birthday.y=1;stu2.birthday.y=20;ymdscorebirthdaynamenum9.4.2 单向链表struct student long int num; float score; struct student *next;结构的递归定义head9901 809902 909903 75 NULL1、动态内存分配函数(1) void *mallo

14、c(unsigned size)功能:在内存的动态存贮区中分配一块长度为size的连续空间。返回值:指针,存放被分配内存的起始地址。若未申请到空间,则返回 NULL( 0 )。 void *:指向任何类型的数据,在使用时,要进行强制类型转换。例如:(int *) malloc(sizeof(int)(struct student *) malloc(sizeof(struct student)(2) void free(void *ptr)功能:释放由malloc()申请的动态内存空间,ptr存放该空间的首地址。返回值:无。p=(struct student *) malloc(sizeof(

15、struct student);free(p);2、建立链表编写一个函数,要求用单向链表建立学生档案,从键盘输入数据,如果学号为0,输入结束,并返回链表的头指针。struct student long int num; float score; struct student *next;;struct student *head, *tail, *p;head=tail=NULL;struct student *head, *tail, *p;head=tail=NULL;size=sizeof(struct student);p = (struct student *) malloc(siz

16、e);pnum scorenextheadtailptailhead=tail=NULL;input num, scorewhile num!=0 p= (struct student *) malloc(sizeof(size) p-num=num, p-score=score, p-next=NULL y head=NULL n head=p tail-next=p tail=p input num, score # include stdio.hstruct student int num;float score;struct student *next;struct student *

17、creat( );main( )struct student *head;head = creat( );struct student *creat() struct student *head, *tail, *p; float score; int num, size = sizeof(struct student); head = tail = NULL; scanf(%d %f, &num, &score); while (num) p = (struct student *)malloc(size); p-num = num; p-score = score; p-next = NU

18、LL; if(head = NULL) head = p; else tail-next = p; tail = p; scanf(%d %f, &num, &score); return head; 3、遍历链表ptr-numptr-scorehead9901 809902 909903 75 NULLptrptrfor(ptr=head; ptr!=NULL; ptr=ptr-next) printf(“%ld, %f”, ptr-num, ptr-score);ptr=ptr-next# include stdio.hstruct student int num;float score;

19、struct student *next;struct student *creat( );void print(struct student *head)main( ) struct student *head;head = creat();print(head);void print(struct student *head) struct student *ptr; if(head = NULL) printf(n No listn); return; printf(n listn); for (ptr = head; ptr; ptr = ptr-next) printf( %d %f

20、n, ptr-num, ptr-score);4、对链表的删除操作ptr2=ptr1-nextptr1-next=ptr2-nextheadptr1ptr2headptr1free(ptr2)ptr2=ptr1-nextptr24、对链表的插入操作s-next = ptr-nextptr-next = s先连后断headptrs9.5 位运算9.5.1 位运算符1、位逻辑运算符 按位取反(与!同级) 单目右结合& 按位与 按位异或 相同取0,不同取1| 按位或1、位逻辑运算符 按位取反& 按位与 按位异或 相同取0,不同取1| 按位或x=0 00000000 00000000 y=3 0000

21、0000 00000011x&y 00000000 00000000x|y 00000000 00000011xy 00000000 000000111010 0101 =1111 注意区分:&和|&和|x & y得 0x | y得 12、位移位运算符 对操作数右移给出的位数x3 将x向左移3位,空出的位用零填补 00111010 3 将x向右移3位 00111010 3 000001113、复合位赋值运算符&=|=例:a&=b 相当于 a=a&b a=2 相当于 a=a29.6 自定义类型用 typedef 定义新的类型名代替已有的类型 旧 新例1、typedef int INTEGER; int i, j; INTEGER i, j; 例2、 typedef int* POINT; int* p1; POINT p1; 定义方法(1) 定义变量 int i(2)变量名新类型名 int INTEGER(3)加上typedef typedef int INTEGER(4) 用新类型名定义变量 INTEGER i;例 int num10 int NUM10typedef int NUM10NUM a int a10

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号