当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

长江大学:《C语言程序设计》PPT课件_第十一章 结构体与共用体(1/2)

资源类别:文库,文档格式:PPT,文档页数:17,文件大小:127KB,团购合买
点击下载完整版文档(PPT)

第十一章结构体与共用体 §11.1概述 一什么是结构体 整型 实型 基本数据类型 只能定义单一的数据种类 字符型 枚举型在实际应用中希望组成一个整体结构体类型 结构体(类型)由基本数据类型,组合而成的一种数据类型。 学学号num10010(int) 生姓名 name Liming(char) 的性别sex (char 10010 Liming F1890.0 情年龄age 18 (int) 况分数 score90.0(foat) 二。结构体类型的定义 struct结构体名 注: 类型符成员名1; 1)结构体、成员的命名规格 类型符成员名2; 与变量相同; 类型符成员名:“域 2)“成员”又名“分量”或

第11章第1节 §11.1 概 述 一. 什么是结构体 整型 实型 字符型 枚举型 基本数据类型 只能定义单一的数据种类 在实际应用中,希望组成一个整体—— 结构体类型 结构体(类型)——由基本数据类型,组合而成的一种数据类型。 学 生 的 情 况 学号 num 10010 (int) 姓名 name Liming (char) 性别 sex M (char) 年龄 age 18 (int) 分数 score 90.0 (float) 10010 Liming F 18 90.0 二. 结构体类型的定义 struct 结构体名 { 类型符 成员名1; 类型符 成员名2; ...... 类型符 成员名n;} 注: 1)结构体、成员的命名规格 与变量相同; 2) “成员”又名“分量”或 “域

第11章第1节 例: struct student—结构体名(是类型名,而非变量名) int num char nam30成员名 char sex; ge, float score;; §112结构体类型变量的定义 三种方法 先定义结构体类型,再定义变量 struct student Int num char name 303; char sex; 定义结构体类型 int age; float score; 3 分号不能少 struct student stul stu2 变量名 类型名 关键字

第11章第1节 例: struct student { int num; char name[30]; char sex; int age; float score;}; 结构体名(是类型名,而非变量名) 成员名 §11.2 结构体类型变量的定义 三种方法 一. 先定义结构体类型, 再定义变量 struct student { int num; char name[30}; char sex; int age; float score;}; struct student stu1, stu2; 分号不能少 定义结构体类型 变量名 类型名 关键字

二。同时定义 第11章第2节 struct student 类型名 f int num; char name 30 char sex Int age; float score; stul, stu2; 变量名 直接定义 无分号 struct 无类型名 d int num; char name 305; char sex; int age; float score; stul, stu2; 四.说明 变量名 注意结构体类型与结构体变量的不同含义:比 struct student stul stu2 较 2.结构体的成员可以是另一个结构体类型; 即:结构体中又包含结构体

第11章第2节 二. 同时定义 struct student { int num; char name[30}; char sex; int age; float score;} stu1, stu2; 变量名 类型名 三. 直接定义 无分号 struct { int num; char name[30}; char sex; int age; float score;} stu1, stu2; 无类型名 变量名 四. 说明 1. 注意结构体类型与结构体变量的不同含义; struct student stu1,stu2; int a, b; 比 较 2. 结构体的成员可以是另一个结构体类型; 即: 结构体中又包含结构体

第11章第2节 exame 如 num name sex age EnglishMath struct score 或: float english 定义类型 float math; 3; struct student struct student fint num; fint num; char name 30; char name 30l; char sex: char sex; int age; int age; struct score exame;使用已定义 f at exame 2; stul stu2 3 stul, stu2; 的结构体 3.成员名与程序中其他变量同名时,互不干抚

第11章第2节 age exame English Math 如: num name sex struct score {float english; float math;}; struct student {int num; char name[30]; char sex; int age; struct score exame; } stu1,stu2; struct student {int num; char name[30]; char sex; int age; float exame[2]; } stu1,stu2; 或: 3. 成员名与程序中其他变量同名时, 互不干抚。 定义类型 使用已定义 的结构体

第11章第3节 s113结构体变量的引用 引用原则—只能引用成员,不能引用整个结构体变量 方法:结构体变量名成员名 成员运算符。优先级最高,左结合 例如:stu1.num 引用结构体变量stu中的成员num 二使用—结构体变量中成员的使用与一般的变量(或数组)相同。 1.输出 printf("%od, %S, %c, %d, %fn", stul num, stul name, stul sex, stul age, stul score); printi(“%d,%s,%oc,%d,%fn”,stul); 2.输入 scanf("%d, %S, %c, %d,%f\n",&stul num, stul name &stul sex, &stul age, stul score) scanf(“%d,%s,%c,%d,%fn”,&stu1); 3.运算 stu2 score=stul score strcpy(stu2 name stul name); sum=stul score+stu2score;

§11.3 结构体变量的引用 第11章第3节 一. 引用原则 ——只能引用成员,不能引用整个结构体变量 方法: 结构体变量名. 成员名 成员运算符。优先级最高,左结合 例如: stu1. num —— 引用结构体变量stu1中的成员num 二. 使用 ——结构体变量中成员的使用与一般的变量(或数组)相同。 1. 输出 printf(“%d,%s,%c,%d,%f\n”, stu1.num, stu1.name, stu1.sex, stu1.age, stu1.score); printf(“%d,%s,%c,%d,%f\n”, stu1); 2. 输入 scanf(“%d,%s,%c,%d,%f\n”, &stu1.num, stu1.name, &stu1.sex, &stu1.age, stu1.score); scanf(“%d,%s,%c,%d,%f\n”, &stu1); 3. 运算 stu2. score=stu1.score; strcpy (stu2.name, stu1.name); sum=stu1.score+stu2.score;

第11章第3节 ANS新标准:具有相同结构的两个结构体变量可整体赋值 stu2=s1;当于 stu2.num← stulnum stu2.name←stul.name stu2 score stul score 4.成员又是结构体类型—只能引用最低级别的成员 例: struct score float english float math struct student Bint num: char name 301; char sex int age struct score exame: 3 stul, stu2; scanf(%d, %f, %f, &stul num, &stul exame. english, &stul exame. math); ave=(stul exame english+stu2 exame. math)/2.0

第11章第3节 ANS新标准:具有相同结构的两个结构体变量可整体赋值 stu2=stu1; stu2.num ←stu1.num stu2.name ←stu1.name ...... stu2.score ←stu1.score 相当于 4. 成员又是结构体类型——只能引用最低级别的成员 例: scanf(“%d,%f,%f”, &stu1.num, &stu1.exame.english, &stu1.exame.math); ... ave= (stu1.exame.english+stu2.exame.math)/2.0; struct score {float english; float math;}; struct student {int num; char name[30]; char sex; int age; struct score exame; } stu1,stu2;

§11.4结构体变量的初始化 第11章第4节 在定义结构体变量时赋初值 例1分开定义时赋初值 struct student 注意: f int num; char name 303; 初始值的顺序、类型与成员要一致 char sex; int as m1=(1001, "Liming", 'M, 18, 90.07 float se struct student stul 例2同时定义时赋初值 struct student f int num; char name 301; char sex int age: float score;}stul={1001,“ Liming”,M’,18,90.0}; 例3直接定义时赋初值 struct d int num; char name 3( char sex int age: float score; stul=(1001,Liming ", M,, 18, 900j;

§11.4 结构体变量的初始化 第11章第4节 在定义结构体变量时赋初值 struct student { int num; char name[30}; char sex; int age; float score;}; struct student stu1={1001,“Liming”,’M’,18, 90.0} 例1 例2 struct student { int num; char name[30}; char sex; int age; float score;} stu1={1001,“Liming”,’M’,18, 90.0}; 例3 struct { int num; char name[30}; char sex; int age; float score;} stu1={1001,“Liming”,’M’,18, 90.0}; 注意: 初始值的顺序、类型与成员要一致 分开定义时赋初值 同时定义时赋初值 直接定义时赋初值

第11章第5节 §11.5结构体数组 数组中的每一个元素都属于同一种类型的结构体 结构体数组的定义 第1种方法 第2种方法 第3种方法 struct student struct student struct f int num; int num f int num; char name 305 char name 301: char name 30) char sex; char sex char sex int age; int age; nt age; float score float score; ) stu[10 float score; stu 10J struct student stu 10: 二结构体数组的初始化定义结构体数组时,给各元素的成员赋初值 struct student f int num char name 305; char sex; int age; float score struct student stul10}={1001,“ Liming”,M’,18,90.0}, 1002,“ wangan”,F',18,87.5}

第11章第5节 §11.5 结构体数组 ——数组中的每一个元素都属于同一种类型的结构体 一. 结构体数组的定义 第1种方法 struct student { int num; char name[30}; char sex; int age; float score;}; struct student stu[10]; struct student { int num; char name[30}; char sex; int age; float score;} stu[10]; struct { int num; char name[30}; char sex; int age; float score;} stu[10]; 第2种方法 第3种方法 二. 结构体数组的初始化——定义结构体数组时,给各元素的成员赋初值 struct student { int num; char name[30}; char sex; int age; float score;}; struct student stu[10]={{1001,“Liming”,’M’,18, 90.0}, {1002,“wangfan”,’F’,18,87.5}, ...... };

第11章第5节 struct student f int num; char name 3 char sex int age; float score; stu[10=(1001,Liming", M, 18, 90.03; 1002,“ wangan”,F’,18,875}; 三.结构体数组的使用 )符合数组引用方法: 只能引用元素 2)合结构体变量的引用原则:只能引用成员 例P297习题115 英语 C程序 数学 学生平均分 stun 90 70 82 (90+70+82)3.0=80.7 stu1 (82+64+75)3.0=73.7 stu2 78 (70+78+853.0=77.7 科目平ave|0 e2 均分

struct student { int num; char name[30}; char sex; int age; float score;} stu[10]={{1001,“Liming”,’M’,18, 90.0}; {1002,“wangfan”,’F’,18,87.5}; ...... }; 第11章第5节 三 . 结构体数组的使用 1)符合数组引用方法: 只能引用元素 2)符合结构体变量的引用原则: 只能引用成员 例 P297 习题11.5 英语 C程序 数学 90 70 82 82 64 75 70 78 85 ...... ...... ...... ave[0] ave[1] ave[2] stu[0] stu[1] stu[2] ...... (90+70+82)/3.0=80.7 (82+64+75)/3.0=73.7 (70+78+85)/3.0=77.7 ...... 学生平均分 科目平 均分

maino 第11章第5节 srtuct student {ir nt num char name 30 float score 3 定义结构体数组 struct student stu 10; float ave 3,aves Int I for(i=0;iaes)生位置k faves=(stui]- score[0]+stui score[1+stuli score[2)3.0

main() {srtuct student {int num; char name[30]; float score[3]; }; struct student stu[10]; float ave[3], aves int i,j,k; for (i=0;iaves) {aves=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0; k=i;} 第11章第5节 定义结构体数组 输 入 数 据 求各科平均分 求最高分数的学 生位置k

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共17页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有