C语言程序设计实验报告9

2019-05-24 17:55

C语言程序设计实验报告(九)

专业 计算机科学与技术 班级 卓越工程师班 日期 2011年12月23日 实验组别 第一组 成绩 第九次实验 结构与联合实验 指导教师 李开 学生姓名 学号

实验名称 结构与联合实验

(一) 实验目的

(1) 熟悉和掌握结构的说明和引用、结构的指针、结构数组,以及函数中使用结构的方

法。

(2) 掌握动态存储分配函数的用法,掌握自引用结构和单向链表的创建、遍历、结点的

增删、查找等操作。

(3) 了解字段结构和联合的用法。

(二) 实验内容及要求

1.表达式求值的程序验证 设有说明:

char u[] = \char v[] = \struct T{ int x; char c; char *t;

}a[] = {{11, 'A', u}, {100, 'B', v}}, *p = a;

请先自己计算表2.1中表达式的值,然后编写程序并运行来加以验证。(各表达式相互无关)

表2.1 表达式值的计算 序号 1 2 3 4 5 6 表达式 (++p) -> x p++, p -> c * p++ -> t, * p -> t * (++p) -> t * ++p -> t ++ * p -> t 计算值 验证值 2.源程序修改、替换

下面所给源程序的功能是:给定一批整数,以0作为结束标志且不作为结点,将其建成一个先进先出的链表。先进先出链表的头指针始终指向最先创建的结点(链头),先建结点指向后建结点,后建结点始终是尾结点。请完成以下工作:

(1) 源程序中存在什么样的错误(先观察执行结果)?对程序进行修改、调试。使之能

够正确完成指定任务。

(2) 修改替换creat_list函数,将其建成一个后进先出的链表。后进先出的链表的头指针

始终指向最后创建的结点(链头),后建结点指向先建结点,先建结点始终是尾结点。

源程序

#include

- 1 -

#include struct s_list{ int data;

struct s_list *next; };

void creat_list(struct s_list *headp, int *p); int main(void) {

struct s_list *head = NULL, *p; int s[] = {1, 2, 3, 4, 5, 6, 7, 8, 0}; creat_list(head, s); p = head; while(p) {

printf(\ p = p -> next; }

printf(\ return 0; }

void creat_list(struct s_list *headp, int *p) {

struct s_list *loc_head = NULL, *tail; if(p[0] == 0) ; else {

loc_head = (struct s_list *)malloc(sizeof(struct s_list)); loc_head -> data = *p++; tail = loc_head; while(*p) {

tail -> next = (struct s_list *)malloc(sizeof(struct s_list)); tail = tail -> next; tail -> data = *p++; }

tail -> next = NULL; }

headp = loc_head; }

3.程序设计

编写并上机调试运行能实现以下功能的程序或函数:

(1)编写一个程序,实现以下功能:定义一个字段结构struct bits,它将一个8位无符号字节从最低位向最高位声明为8个字段,各字段依次为bit0, bit1, …… bit7,且bit0的优先级

- 2 -

最高。同时设计8个函数,第i个函数以biti(i = 0, 1,……7)为参数,并且在函数体内输出biti的值。将8个函数的名字存入一个函数指针数组p_fun。如果bit0为1,调用p_fun[0]指向的函数。如果struct bits中有多位为1,则根据优先级从高到低依次调用函数指针数组p_fun中相应元素指向的函数。8个函数中的第0个函数可以设计为 Void f0(struct bits b) {

Printf(“the function %d is called!\\n”, b); }

(3) 设计用单词链表建立一张班级成绩单,包括每个学生的学号、姓名、英语、高等数

学、普通物理、C语言程序设计四门课程的成绩,试用函数编程实现下列功能:

① 输入每个学生的各项信息。 ② 输出每个学生的各项信息。 ③ 修改指定学生的指定数据项的内容。 ④ 统计每个同学的平均成绩(保留两位小数)。 ⑤ 输出各位同学的学号、姓名、四门课程的总成绩和平均成绩。 4.选做题

(1)对上述程序设计题中第(2)题的程序,增加按照平均成绩进行升序排序的函数,试写出用交换结点数据域的方法升序排序的函数,排序可用选择法或冒泡法。

(2)对选做题第(1)题,进一步写出用交换结点指针域的方法升序排序的函数。 (3)采用双向链表重做编程设计题中的第(2)题。

(三) 实验步骤及结果

1.表达式求值的程序验证的实验步骤及结果

表2.1 表达式值的计算 序号 1 2 3 4 5 6 表达式 (++p) -> x p++, p -> c * p++ -> t, * p -> t * (++p) -> t * ++p -> t ++ * p -> t 计算值 100 B U, x x V V 验证值 100 B U, x x V V 结果正确!

2.源程序修改、替换的实验步骤及结果

(1)改错:headp类型应为双重指针,即:void creat_list(struct s_list **headp, int *p); 同时第40行应该改为*headp = loc_head; 第12行改为creat_list(&head, s); 修改后的程序如下: #include #include struct s_list{ int data;

struct s_list *next; };

void creat_list(struct s_list **headp, int *p); int main(void)

- 3 -

{

struct s_list *head = NULL, *p; int s[] = {1, 2, 3, 4, 5, 6, 7, 8, 0}; creat_list(&head, s); p = head; while(p) {

printf(\ p = p -> next; }

printf(\ return 0; }

void creat_list(struct s_list **headp, int *p) {

struct s_list *loc_head = NULL, *tail; if(p[0] == 0) ; else {

loc_head = (struct s_list *)malloc(sizeof(struct s_list)); loc_head -> data = *p++; tail = loc_head; while(*p) {

tail -> next = (struct s_list *)malloc(sizeof(struct s_list)); tail = tail -> next; tail -> data = *p++; }

tail -> next = NULL; }

*headp = loc_head; }

程序运行结果如图所示:

结果正确!

(2)建立一个后进先出的链表如下:

- 4 -

修改后程序如下: #include #include struct s_list{ int data;

struct s_list *next; };

void creat_list(struct s_list **headp, int *p); int main(void) {

struct s_list *head = NULL, *p; int s[] = {1, 2, 3, 4, 5, 6, 7, 8, 0}; creat_list(&head, s); p = head; while(p) {

printf(\ p = p -> next; }

printf(\ return 0; }

void creat_list(struct s_list **headp, int *p) {

struct s_list * loc_head=NULL,*tail; struct s_list * temp; if(p[0]==0) ;

- 5 -


C语言程序设计实验报告9.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:浙江大学羽毛球理论考试试题整理

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: