C语言程序设计实验报告(九)
专业 计算机科学与技术 班级 卓越工程师班 日期 2011年12月23日 实验组别 第一组 成绩 第九次实验 结构与联合实验 指导教师 李开 学生姓名 学号
实验名称 结构与联合实验
(一) 实验目的
(1) 熟悉和掌握结构的说明和引用、结构的指针、结构数组,以及函数中使用结构的方
法。
(2) 掌握动态存储分配函数的用法,掌握自引用结构和单向链表的创建、遍历、结点的
增删、查找等操作。
(3) 了解字段结构和联合的用法。
(二) 实验内容及要求
1.表达式求值的程序验证 设有说明:
char u[] = "UVWXYZ"; char v[] = "xyz"; struct T{ int x; char c; char *t;
}a[] = {{11, 'A', u}, {100, 'B', v}}, *p = a;
请先自己计算表2.1中表达式的值,然后编写程序并运行来加以验证。(各表达式相互无关)
2.源程序修改、替换
下面所给源程序的功能是:给定一批整数,以0作为结束标志且不作为结点,将其建成一个先进先出的链表。先进先出链表的头指针始终指向最先创建的结点(链头),先建结点指向后建结点,后建结点始终是尾结点。请完成以下工作:
(1) 源程序中存在什么样的错误(先观察执行结果)?对程序进行修改、调试。使之能
够正确完成指定任务。
(2) 修改替换creat_list函数,将其建成一个后进先出的链表。后进先出的链表的头指针
始终指向最后创建的结点(链头),后建结点指向先建结点,先建结点始终是尾结点。
源程序
#include<stdio.h>
#include<stdlib.h> 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("%d\t", p -> data); p = p -> next; }
printf("\n"); 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的优先级
最高。同时设计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 表达式值的计算
结果正确!
2.源程序修改、替换的实验步骤及结果
(1)改错:headp类型应为双重指针,即:void creat_list(struct s_list **headp, int *p); 同时第40行应该改为*headp = loc_head; 第12行改为creat_list(&head, s); 修改后的程序如下: #include<stdio.h> #include<stdlib.h> 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("%d\t", p -> data); p = p -> next; }
printf("\n"); 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)建立一个后进先出的链表如下:
修改后程序如下: #include<stdio.h> #include<stdlib.h> 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("%d\t", p -> data); p = p -> next; }
printf("\n"); 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) ;
else {
loc_head = (struct s_list *)malloc(sizeof(struct s_list)); loc_head -> data = *p++; tail = loc_head; while(*p){
temp = (struct s_list *)malloc(sizeof(struct s_list)); temp -> next = loc_head; loc_head = temp;
loc_head -> data = *p++; }
tail -> next = NULL; }
*headp = loc_head; }
程序运行结果如下:
3.程序设计的实验步骤及结果 (1)
1)创建一个工程
建立名为prj2的工程。 2)编辑源文件和保存 程序如下
#include<stdio.h> struct bits{
unsigned char bit0:1, bit1:1, bit2:1, bit3:1, bit4:1, bit5:1, bit6:1, bit7:1; }a;
union w{
unsigned char t; struct bits a; }m;
void f0(struct bits b) {
printf("the function %d is called!\n", b); }
void f1(struct bits b) {
printf("the function %d is called!\n", b); }
void f2(struct bits b) {
printf("the function %d is called!\n", b); }
void f3(struct bits b) {
printf("the function %d is called!\n", b); }
void f4(struct bits b) {
printf("the function %d is called!\n", b); }
void f5(struct bits b) {
printf("the function %d is called!\n", b); }
void f6(struct bits b) {
printf("the function %d is called!\n", b); }
void f7(struct bits b) {
printf("the function %d is called!\n", b); }
void main() {
unsigned int n;
void (*p_fun[8])(struct bits b); printf("input n: "); scanf("%d", &n); m.t = n;
m.a.bit0 = 1; p_fun[0] = f0; p_fun[1] = f1; p_fun[2] = f2; p_fun[3] = f3; p_fun[4] = f4; p_fun[5] = f5; p_fun[6] = f6; p_fun[7] = f7; if(m.a.bit0)
p_fun[0](m.a); if(m.a.bit1)
p_fun[1](m.a); if(m.a.bit2)
p_fun[2](m.a); if(m.a.bit3)
p_fun[3](m.a); if(m.a.bit4)
p_fun[4](m.a); if(m.a.bit5)
p_fun[5](m.a); if(m.a.bit6)
p_fun[6](m.a); if(m.a.bit7)
p_fun[7](m.a); }
运行结果如下所示:
结果正确! (2)
1)创建一个工程
建立名为prj3工程。 2)编辑源文件和保存 程序如下
#include<stdio.h> #include<stdlib.h> #include<string.h> struct list{
char num[8]; char name[9]; int english; int math; int phy; int c;
struct list *next; }*p, *q;
struct list *creat_list(int n) {
int i;
struct list *head = NULL;
for(i = 0; i < n; i++) {
p = (struct list *)malloc(sizeof(struct list)); if(i == 0)
head = p; else
q -> next = p; printf("num: ");
scanf("%s", p -> num); printf("name: ");
scanf("%s", p -> name);
printf("english math phy c: : ");
scanf("%d %d %d %d", &p -> english, &p -> math, &p -> phy, &p -> c); printf("\n"); q = p;
p -> next = NULL; }
return head; }
void output(struct list *head, int n) {
printf("num name english math phy c\n"); for (p = head; n > 0; n--, p = p -> next) {
printf("%-5s %3s %8d %9d %6d %6d\n", p -> num, p -> name, p -> english, p -> math, p -> phy, p -> c); } }
void xiugai(struct list *head) {
char a[8]; int s;
int k, i, m;
printf("您想修改第几个学生的信息?\n"); scanf("%d", &k);
for(q = head, i = 0; i < k - 1; i++) q = q -> next;
printf("您想修改哪项信息?\n");
printf("1:num 2:name 3:english 4:math 5:phy 6:c\n"); scanf("%d", &m); switch(m) {
case 1:
printf("输入新学号:"); scanf("%s", a);
strcpy(q -> num, a);
printf("确认新学号:%s", q -> num); break; case 2:
printf("输入新姓名:"); scanf("%s", a);
strcpy(q -> name, a);
printf("确认新姓名:%s", q -> name); break; case 3:
printf("输入新英语成绩:"); scanf("%d", &s); q -> english = s;
printf("确认新英语成绩:%d", q -> english); break; case 4:
printf("输入新数学成绩:"); scanf("%d", &s); q -> math = s;
printf("确认新数学成绩:%d", q -> math); break; case 5:
printf("输入新物理成绩:"); scanf("%d", &s); q -> phy = s;
printf("确认新物理成绩:%d", q -> phy); break; case 6:
printf("输入新c成绩:"); scanf("%d", &s); q -> c = s;
printf("确认新c成绩:%d", q -> c); break; default:
printf("error!\n"); }
printf("\n"); }
void aver(struct list *head, int n) {
float s; int i; q = head;
for(i = 0; i < n; i++) {
s = (q -> english + q -> math + q -> phy + q -> c); printf("%s的总成绩是:%.1f ", q -> name, s); s = s / 4;
printf("%s的平均成绩是:%.2f ", q -> name, s); q = q -> next; printf("\n"); }
printf("\n"); }
void main() {
int n;
struct list *head;
printf("输入学生数量: "); scanf("%d", &n); head = creat_list(n); output(head, n); xiugai(head); printf("\n"); aver(head, n); output(head, n); }
输入五个学生的信息,程序运行结果如下:
结果完全正确!
(四) 实验体会
通过对结构与联合这一章的学习,我掌握了结构的说明和引用、结构的指针、结构数组,以及函数中使用结构的方法。这一章的学习也要求我们能够掌握动态存储分配函数的用法,掌握自引用结构和单向链表的创建、遍历,结点的增删、查找等操作。结构、联合、字段结构字计算机程序设计中都有着广泛的应用,它们是构造其他描述能力更强的构造类型的基础。所以关于这一章的内容我们要熟练掌握!
要学会用动态存储解决一些问题,能够熟练创建单向链表。但是我在试验过程中,创建链表时总是会出错,还有些生疏,今后还要多加练习,求得改进。 熟练掌握结构与联合的内容,会为我们编程提供大大的帮助!

