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

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

资源类别:文库,文档格式:PPTX,文档页数:27,文件大小:209.6KB,团购合买
 什么是结构?  定义和使用  结构+数组  结构+指针  结构+函数
点击下载完整版文档(PPTX)

程序设计专题 第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

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

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

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