浙江大学:《计算机辅助设计与图形学》课程教学资源(PPT讲稿)程序设计专题——结构

程序设计专题 第2讲-结构 刘新国
刘新国 1

第9章结构 ◇什么是结构? ◇定义和使用 ◇结构+数组 ◇结构+指针 ◇结构+函数
什么是结构? 定义和使用 结构+数组 结构+指针 结构+函数

9.1结枸定义和使用 ◇数据类型 C语言提供的基本数据类型 ◇整数:int, uns i gned, short,long, ◇浮点数: float, double ◇字符:char ◇字符串不是基本的数据类型 ◇空/无类型:void ◇指针:各种数据类型都有对应的指针类型 ◇数组:各种数据类型都有对应的数组类型 ◇不同长度、不同维度的数组是不同的类型
数据类型 C语言提供的基本数据类型 整数:int,unsigned,short,long,…… 浮点数:float,double 字符:char 字符串不是基本的数据类型 空/无类型:void 指针:各种数据类型都有对应的指针类型 数组:各种数据类型都有对应的数组类型 不同长度、不同维度的数组是不同的类型

9.1结枸定义和使用 ◇结构类型 ◆用户自定义的数据类型 struct是C语言关键字 student是用户定义的标识 符,作为结构的名字,必须 struct student 和 struct联合使用。 int num 体*学号*/ char name 10] /*姓名*/ nt computer, eng l i sh,math;/*成绩*/ doub le average /*平均成绩* };/*不要遗漏分号*/
结构类型 用户自定义的数据类型 struct student { int num; /*学号*/ char name[10] /*姓名*/ int computer, english, math; /*成绩*/ double average; /*平均成绩*/ };/* 不要遗漏分号 */ struct是C语言关键字 student是用户定义的标识 符,作为结构的名字,必须 和struct联合使用

9.1结枸定义和使用 struct结构名 类型名结构成员名1 类型名结构成员名2 类型名结构成员名n ◇关键字 struct和结构名一起,构成一个数据类型 ◇结构的定义以分号结束,被看作一条语句(结构定义语 句) 一个结构体所占的字节数可以 s sizeof运算符确定
struct 结构名 { 类型名 结构成员名1; 类型名 结构成员名2; • • • 类型名 结构成员名n; }; 关键字struct和结构名一起,构成一个数据类型 结构的定义以分号结束,被看作一条语句(结构定义语 句) 一个结构体所占的字节数可以sizeof运算符确定

结构定义示例 定义平面坐标结构 定义一个图像 struct point struct image double x dth, he i ght double y; int format pixe Is; 或者 struct point 定义一个产品 struct product double x, y int id it type char name [100] nt price
定义平面坐标结构: struct point { double x; double y; }; 或者 struct point { double x, y; }; 定义一个图像 struct image { int width, height; int format; char * pixels; }; 定义一个产品 struct prooduct { int id; int type; char name[100]; int price; };

结构定义示例 定义一个复数: 定义一个朋友 struct complex struct friend double real, image char name [10] char phone [13] 定义一个地址 Int age struct address struct address addr; char memo [100] char city [20] char street[20] char code: int Zip;
定义一个复数: struct complex { double real, image; }; 定义一个地址 struct address { char city[20]; char street[20]; char code; int zip; }; 定义一个朋友 struct friend { char name[10]; char phone[13]; int age; struct address addr; char memo[100]; };

9.1结构定义和使用 [例9-1]建立一个学生信息库 struct student int num; *学号*/ char name [10] /*姓名*/ Int computer, engl ish,math;/*成绩*/ double average /*平均成绩*/
[例9-1] 建立一个学生信息库 struct student { int num; /*学号*/ char name[10] /*姓名*/ int computer, english, math; /*成绩*/ double average; /*平均成绩*/ };

[例9-1建立一个学生信息厍 #define maxsize 50 struct student students [MaxSize] int count =0 MaxSize是一个宏,定 用结构 struct student 定义了一个数组 students 长度为50 宏定义的一般格式 # define宏名宏体 之后所有的宏名都会被编译器替换为宏体
#define MaxSize 50 struct student students[MaxSize]; int count = 0; MaxSize是一个宏,定义为50 宏定义的一般格式 #define 宏名 宏体 之后所有的宏名都会被编译器替换为宏体 用结构struct student 定义了一个数组students 长度为50

[例9-1建立一个学生信息厍 void new student(struct student students [) struct student s 结构数组/指针作为形式参数 等价于 struct student* students if( count==MaxSize)I printf ("the array is ful l\n") return scanf ("%d",&s num 用运算符 scanf("%s", s name 使用结构成员变量 scanf("%d",&s math scanf("%",&. computer) scanf("%d",&s engl ish) tun:=6;结构变量可以整体赋值
void new_student(struct student students[]) { struct student s; if( count==MaxSize ) { printf("The array is full\n"); return; } scanf("%d", &s.num); scanf("%s", s.name); scanf("%d", &s.math); scanf("%d", &s.computer); scanf("%d", &s.english); s.verage = ( s.math + s.computer + s.english ) / 3.0; students[count ++] = s; } 用运算符 . 使用结构成员变量 结构变量可以整体赋值 结构数组/指针作为形式参数 等价于struct student *students
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 浙江大学:《计算机辅助设计与图形学》课程教学资源(PPT讲稿)程序设计专题——结构化程序设计与递归函数.pptx
- 浙江大学:《计算机辅助设计与图形学》课程教学资源(PPT讲稿)SDL(Simple DirectMedia Layer)图形程序设计.pptx
- 《计算机辅助设计(CD)》课程教学大纲.pdf
- MATLAB简介.ppt
- 《数字图像处理技术 Digital Image Processing》课程教学资源(教学大纲).pdf
- linux系统知识培训(PPT讲稿).pptx
- 高性能计算机和曙光GHPC1000集群系统.ppt
- 曙光:机群应用开发(并行编程原理及程序设计)Parallel Programming - Fundamentals and Implementation(MPI并行程序设计 Parallel Programming with the Massage Passing Interface(MPI)).ppt
- 中科院昆明动物研究所培训:曙光5000A超级计算机.ppt
- 《网页设计教程》PPT课件:第9章 美化网页.ppt
- 《网页设计教程》PPT课件:第8章 网页表单的处理.ppt
- 《网页设计教程》PPT课件:第7章 在网页中使用超链接.ppt
- 《网页设计教程》PPT课件:第6章 网页图像处理.ppt
- 《网页设计教程》PPT课件:第5章 网页框架的处理.ppt
- 《网页设计教程》PPT课件:第4章 网页表格的处理.ppt
- 《网页设计教程》PPT课件:第3章 网页文本的处理.ppt
- 《网页设计教程》PPT课件:第2章 创建站点.ppt
- 《网页设计教程》PPT课件:第12章 利用ADO实现网页与数据库的链接.ppt
- 《网页设计教程》PPT课件:第11章 示例网页利用ASP实现交互功能.ppt
- 《网页设计教程》PPT课件:第10章 JavaScript和DHTML技术.ppt
- 分布式虚拟环境:虚拟现实的基础理论、算法及实现项目结题报告(分布并行图形绘制技术及系统).ppt
- 浙江大学:R语言基础(PPT讲稿).pptx
- 浙江大学信息与电子工程学系:《无线网络应用》课程教学资源(PPT讲稿)网线制作实验.ppt
- 浙江大学:《计算机控制装置》课程教学资源(PPT讲稿)计算机控制系统的抗干扰设计.ppt
- 浙江大学:《计算机辅助设计与图形学》课程教学资源(PPT讲稿)查找、排序.pptx
- 数据结构与控制算法分析(PPT专题讲稿)查找与排序.ppt
- 浙江大学:《计算机辅助设计与图形学》课程教学资源(PPT讲稿)简单图形库介绍.pptx
- 浙江大学:《计算机辅助设计与图形学》课程教学资源(PPT讲稿)数据可视化基础.ppt
- Python的基本应用(PYTHON的入门应用).pptx
- 浙江大学计算机科学与技术学院:C语言程序设计基础与试验(PPT讲稿).ppt
- 耶鲁大学:A Sparse Parametric Mixture Model for BTF Compression, Editing and Rendering.ppsx
- 浙江大学:程序设计专题(PPT讲稿)结构化程序设计与递归函数(刘新国).pptx
- 浙江大学:循环结构(PPT讲稿).pptx
- 浙江大学:《计算机辅助设计与图形学》课程教学资源(PPT讲稿)基于图像的绘制技术 Image Based Rendering, IBR.ppt
- 浙江大学计算机系:网络图形技术 Chinagraph‘2000 讨论组.ppt
- 结构(9.1 构建手机通讯录 9.2 结构变量 9.3 结构数组 9.4 结构指针).ppt
- 大型综合程序范例解析(PPT讲稿).ppt
- 生物信息数据分析技能培训:计算机基础技能培训(linux基础知识).pptx
- 浙江大学:虚拟现实中基于图像的建模和绘制(报告PPT).ppt
- 《E-commerce 2014》电子商务(PPT讲稿)Chapter 9 Online Retail and Services.ppt