c语言实训指导书 下载本文

实训八 结构体与共用体 【实训目的】

1.通过本次实训内容,使学生初步了解构造类型--C语言结构体类型 2.掌握结构体类型的定义、结构体数组的定义

3.结构体指针的定义及初始化、引用、应用,并在此基础上重点掌握链表的建立及输出。 【实训内容】

1. 阅读程序,并给输入及程序运行结果

(1)一个学生的基本信息有:学号、姓名、性别和年龄,现要存放5个同学的信息,编程实现把各项信息输出出来。

struct student /*定义一种结构体类型*/ { int num;

char name[20]; char sex; int age; } ;

#include “stdio.h” main() {

struct student stu[5]; int i;

for(i=0;i<5; i++)

scanf(\ for(i=0;i<5; i++)

printf(\ }

(2)建立一个静态链表,它由三个学生数据结点组成,输出各结点的数据

#include “stdio.h” #define NULL 0 struct student {

long num; float score;

struct student *next; };

main() {

struct student a,b,c,*head,*p;

16

a.num=99101;a.score=89.5; b.num=99103;b.score=90; c.num=99107;c.score=85;

head=&a; a.next=&b; b.next=&c; c.next=NULL; p=head; while(p!=NULL) {

printf(“%ld%5.1f\\n”,p->num,p->score); p=p->next; } }

2. 编程题

(1)定义一个结构体类型,包括职工的如下信息:职工号、姓名、年龄、工资和职称,输入3个人的信息,然后输出。

(2)、编程实现静态链表的建立和输出。

17