第12章 自定义数据类型.pptxVIP

  • 0
  • 0
  • 约1.87千字
  • 约 46页
  • 2017-11-26 发布于湖北
  • 举报
第12章 自定义数据类型

第12章 自定义数据类型 ;主要内容;在程序里表示一个人(姓名、年龄、性别、成绩),怎么表示? 想表示多个人呢? 如何用计算机程序实现下述表格的管理?;I. 使用数组; ;struct student { int num; char name[20]; char sex; float score; };;声明结构体变量;struct date { int month; int day; int year; };;用typedef定义数据类型;;;如果要将“zhang”改为“zhong”,只要将结构变量student1中的数组成员name下标为2的元素‘a’改为‘o’即可。 可以使用下列语句: student1.name[2] = o; /* 为结构变量中的数组成员的一个元素赋值 */;对结构变量的整体操作 要对结构体进行整体操作有很多限制,C语言中能够对结构进行整体操作的运算不多,只有赋值“=”操作。 例如: struct date sunday, today; sunday = today; /* 结构变量整体赋值 */;不能将一个结构变量作为一个整体直接访问。 例如,不能这样引用: printf (%s,%c,%d,%d,%d\n, student1); 如果成员本身又是一个结构类型,则要用若干个成员运算符,一级一级地找到最低的一级的成员。 例如,对上面定义的结构变量student1,可以这样访问各个成员: student1.name student1.sex student1.birthday.month student1.birthday.day student1.birthday.year;问题描述: 根据学员的成绩,输出不及格学员的详细信息。 ;struct student stu,*pstu; *pstu=stu; (*pstu).num 或者: pstu-num;#includestdio.h struct student{ int num; char *name; char sex; float score;} stu={1,张宾,F,55},*pstu; int main() { pstu=stu; printf(学号:%d 姓名:%s\n,stu.num,stu.name); printf(性别:%c 成绩:%5.2f\n\n,stu.sex,stu.score); printf(学号:%d 姓名:%s\n,(*pstu).num,(*pstu).name); printf(性别:%c 成绩:%5.2f\n\n,(*pstu).sex,(*pstu).score); printf(学号:%d 姓名:%s\n,pstu-num,pstu-name); printf(“性别:%c 成绩:%5.2f\n\n,pstu-sex,pstu-score); return 0; };主要内容;struct student { int num; char name[20]; char sex; float score; }stu[30];;结构体数组的初始???;结构体数组示例;主要内容;可以将结构体作为参数传递给函数,也可以定义返回结构体值的函数。 结构体作为函数参数有两种不同方法: 将结构体变量作为参数值传递给函数。 将结构体指针作函数的参数。;结构体变量作参数;结构体变量作参数;结构体指针作参数;【例3】学生信息的排序 #include stdio.h #include string.h struct student { long sno; char name[10]; float score[3]; }; ;主要内容;29;30;2. 链表的操作;(1)创建链表;(1)创建链表;34;(2) 输出链表;36;(3)释放链表;(4)查询链表-查找某个结点;39;(5) 删除链表结点;41;42;(6)插入链表结点;44;45;3、链表与数组的比较

文档评论(0)

1亿VIP精品文档

相关文档