实验8复杂数据类型教程.docVIP

  • 19
  • 0
  • 约4.13千字
  • 约 9页
  • 2017-05-04 发布于湖北
  • 举报
实验8复杂数据类型教程

实验8 复杂数据类型 (一)、实验目的 1. 掌握结构体、结构体类型的数组、指向结构体数据的指针的定义及使用方法。 2. 了解共用体数据类型、枚举数据类型的定义及使用方法。 (二)、知识提示 1. 结构体变量成员的引用方法是:变量名.成员名,结构体变量成员的使用方法和单个变量一样。不能将结构体变量作为一个整体进行输入或输出,一般都是对其成员变量逐个操作。 2. 指向结构体变量的指针定义的一般形式为: struct 结构体类型名 *指针变量名; 3. 通过指向结构体变量的指针引用结构体成员的一般形式为: (*结构体指针变量).成员名 或 结构体指针变量-成员名 4. 不能利用初始化给共用体变量赋值。在给共用体变量赋值时,最新的成员有效,其它成员的值将被覆盖。 5. 枚举元素作为常量是有值的,C语言编译按定义时的顺序使它们的值为0,1,2…。 (三)、实验内容 【题目1】 改正下列程序中的错误,使之具有如下功能:输出结构体变量的学号、姓名及年龄。 #include stdio.h int main( ) { struct { char num[7]; char name[20]; int age; }student; student={200901,zhang san,20}; printf(%s,%s,%d\n,student); } 改:#include stdio.h int main( ) { struct { char num[7]; char name[20]; int age; }student={200901,zhang san,20}; printf(%s,%s,%d\n,student.num,student.name,student.age); } 【题目2】阅读下列程序,写出预习结果。然后上机验证。 #include stdio.h int main( ) { union stu /* 定义共用体类型 */ { int num; float score; char *name; }student; student.num=1010; student.score=80; student.name=zhangsan; printf(%f\n, student.score); return 0; } 预测结果:80 【题目3】阅读下列程序,写出预习结果。然后上机验证。 #include stdio.h int main() { enum weekday{sun,mon,tue,wed,thu,fri,sat}d1,d2,d3; d1=sun; d2=mon; d3=tue; printf(%d,%d,%d,d1,d2,d3); return 0; } 预测结果:0,1,2 【题目4】将以下程序补充完整,实现:输出数组中三个学生的姓名。 #include stdio.h struct student /* 定义结构体类型 */ { int num; char name[20]; int age; }; int main( ) { struct student ?p; struct student stud[3]= {{200801,zhang,20}, {200802,li,21}, {200803,wang, 19}}; for ( ___ p=stud _;pstud+3;p++) printf(%s\n, ___ p-name ______); return 0; } 【题目5】编程实现:有10个学生,每个学生的数据包括学号、姓名和三门课的成绩。从键盘输入10个学生的相关信息,计算并且输出每个学生三门课的平均成绩,以及平均分最高的学生的所有信息(包括学号、姓名、三门课的成绩和平均分数)。 提示: 用循环语句求出每个学生的平均分

文档评论(0)

1亿VIP精品文档

相关文档