c语言程序基础数组结构,C语言程序设计基础第9章 结构体

《C语言程序设计基础第9章 结构体》由会员分享,可在线阅读,更多相关《C语言程序设计基础第9章 结构体(47页珍藏版)》请在人人文库网上搜索。

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

2、0; float score; ; struct student stu1, stu2;,2、定义结构类型的同时定义变量 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 结构变量的初始化,只有全局变量或静态变量才能初始化。 static struct student stu2=200012, “Li”, 94;,struct student long 。

3、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的首地址,2、结构变量中分量的引用 struct student long int num; char name20; float score; stu1, stu2;,(1) 结构变量.分量 stu1.num = 9901; p。

4、rintf(%s, stu2.name);,(2) 结构变量中的分量可以依据它的类型进行各种运算 x = stu1.score; strcpy(stu1.name, “Wang”); (3) 可以引用结构变量中的分量的地址 scanf(%ld, ,9.2 结构数组,一个结构变量只能存放一个学生的资料。 若班上有20个学生,需要用结构数组。 即,数组中的每个元素都是结构类型。 9.2.1 定义 struct student long int num; char name20; float score; stu20;,9.2.2 初始化 struct student long int num; c。

5、har name20; float score; stu20=200011,”Zhang”,85, 200012,”Li”,90;,9.2.3 引用 struct student long int num; char name20; float score; stu20; stu0.num stu0.name stu0.score,程序举例,例1、输入某班30位学生的姓名及数学、英语成绩,计算并输出每位学生的平均分。 struct student char name10; int math, eng; float aver; ;,void main( ) struct student s30;。

6、 int i; for(i=0; i30; i+) scanf(%s%d%d, si.name, ,输入某班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.name, ,输入30位学生的姓名及数学、英语成绩,输出平均分最高的学生的姓名及其数学和英语成绩。 struct student char name10; int math, eng; float aver; ;,例3,。

7、void main( ) struct student s30; int i, sub; for(i=0; i30; i+) scanf(%s%d%d, si.name, 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 st。

8、u1, *ptr; ptr = ,9.3.2 结构指针的使用,struct student stu1, *ptr = ,(2) - (*ptr).num = 200011; ptr-num = 200011; ptr-score = 85; strcpy(ptr-name,”Zhang”); 当ptr = int m; int d; ,struct student long int num; char name20; struct day birthday; float score; ,struct day int y; int m; int d; ; struct student long 。

9、int num; char name20; struct day birthday; float score; stu1, stu2;,或: struct student long int num; char name20; struct int y, m, d; birthday; float score; stu1, stu2;,struct student long int num; char name20; struct int y, m, d; birthday; float score; stu1=9901, “Zhao”, 1980, 10,30, 80, stu2; stu2.。

10、birthday.y=1979; stu2.birthday.y=1; stu2.birthday.y=20;,9.4.2 单向链表,struct student long int num; float score; struct student *next; 结构的递归定义,1、动态内存分配函数,(1) void *malloc(unsigned size) 功能:在内存的动态存贮区中分配一块长度为size的连续空间。 返回值:指针,存放被分配内存的起始地址。若未申请到空间,则返回 NULL( 0 )。 void *:指向任何类型的数据,在使用时,要进行强制类型转换。 例如:(int *) 。

11、malloc(sizeof(int) (struct student *) malloc(sizeof(struct student),int *p1,*p2,a; P1=,(2) void free(void *ptr) 功能:释放由malloc()申请的动态内存空间,ptr存放该空间的首地址。 返回值:无。 p=(struct student *) malloc(sizeof(struct student); free(p);,2、建立链表,编写一个函数,要求用单向链表建立学生档案,从键盘输入数据,如果学号为0,输入结束,并返回链表的头指针。 struct student long int。

12、 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(size);,p,head,tail,tail,head=tail=NULL; input num, score while num!=0 p= (struct student *) malloc(s。

13、izeof(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.h struct student int num; float score; struct student *next; ; struct student *creat( ); main( ) struct student *head; head = creat( ); ,struct student *creat() struct student *。

14、head, *tail, *p; float score; int num, size = sizeof(struct student); head = tail = NULL; scanf(%d %f, ,3、遍历链表,ptr-num ptr-score,for(ptr=head; ptr!=NULL; ptr=ptr-next) printf(“%ld, %f”, ptr-num, ptr-score);,ptr=ptr-next,# include stdio.h struct student int num; float score; struct student *next; ; s。

15、truct 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 %fn, ptr-num, ptr-。

16、score); ,4、对链表的删除操作,ptr2=ptr1-next ptr1-next=ptr2-next,free(ptr2) ptr2=ptr1-next,4、对链表的插入操作,s-next = ptr-next ptr-next = s 先连后断,9.5 位运算,9.5.1 位运算符 1、位逻辑运算符 按位取反(与!同级) 单目右结合 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 NUM10 typedef int NUM10 NUM a int a10。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值