结构与联合实验实验报告.docx

上传人:b****5 文档编号:5227562 上传时间:2022-12-14 格式:DOCX 页数:41 大小:187.07KB
下载 相关 举报
结构与联合实验实验报告.docx_第1页
第1页 / 共41页
结构与联合实验实验报告.docx_第2页
第2页 / 共41页
结构与联合实验实验报告.docx_第3页
第3页 / 共41页
结构与联合实验实验报告.docx_第4页
第4页 / 共41页
结构与联合实验实验报告.docx_第5页
第5页 / 共41页
点击查看更多>>
下载资源
资源描述

结构与联合实验实验报告.docx

《结构与联合实验实验报告.docx》由会员分享,可在线阅读,更多相关《结构与联合实验实验报告.docx(41页珍藏版)》请在冰豆网上搜索。

结构与联合实验实验报告.docx

结构与联合实验实验报告

C语言程序设计实验报告

专业:

计算机科学与技术班级:

计算机科学与技术创新实验班日期:

2012-1-4成绩:

实验组别:

第次实验:

指导教师:

学生姓名:

学号:

同组人姓名:

实验名称:

结构与联合实验

1、实验目的

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

2.掌握动态储存分配函数的用法,掌握自引用结构,单向链表的创建、遍历、结点的增删、查找等操作。

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

2、实验内容及要求

1.表达式求值的程序验证题

设有说明:

charu[]="UVWXYZ";

charv[]="xyz";

structT{

intx;

charc;

char*t;

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

请先自己计算下面表达式的值,然后通过编程计算来加以验证。

(各表达式相互无关)

序号

表达式

计算值

验证值

1

(++p)->x

2

p++,p->c

3

*p++->t,*p->t

4

*(++p)->t

5

*++p->t

6

++*p->t

2.源程序修改、替换

下面所给源程序的功能是:

给定一批整数,以0作为结束标志且不作为结点,将其建成一个先进先出的链表,先进先出链表的指头指针始终指向最先创建的结点(链头),先建结点指向后建结点,后建结点始终是尾结点。

请完成以下工作:

(1)源程序中存在什么样的错误(先观察执行结果)?

对程序进行修改、调试,使之能够正确完成指定任务。

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

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

源程序:

#include"stdio.h"

#include"stdlib.h"

structs_list{

intdata;/*数据域*/

structs_list*next;/*指针域*/

};

voidcreate_list(structs_list*headp,int*p);

voidmain(void)

{

structs_list*head=NULL,*p;

ints[]={1,2,3,4,5,6,7,8,0};/*0为结束标记*/

create_list(head,s);/*创建新链表*/

p=head;/*遍历指针p指向链头*/

while(p){

printf("%d\t",p->data);/*输出数据域的值*/

p=p->next;/*遍历指针p指向下一结点*/

}

printf("\n");

}

voidcreate_list(structs_list*headp,int*p)

{

structs_list*loc_head=NULL,*tail;

if(p[0]==0)/*相当于*p==0*/

;

else{/*loc_head指向动态分配的第一个结点*/

loc_head=(structs_list*)malloc(sizeof(structs_list));

loc_head->data=*p++;/*对数据域赋值*/

tail=loc_head;/*tail指向第一个结点*/

while(*p){/*tail所指结点的指针域指向动态创建的结点*/

tail->next=(structs_list*)malloc(sizeof(structs_list));

tail=tail->next;/*tail指向新创建的结点*/

tail->data=*p++;/*向新创建的结点的数据域赋值*/

}

tail->next=NULL;/*对指针域赋NULL值*/

}

headp=loc_head;/*使头指针headp指向新创建的链表*/

}

3.程序设计

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

(1)编写一个程序,实现以下功能:

设计一个字段结构structbits,它将一个8位无符号字节从最低位向最高位声明为8个字段,各字段依次为bit0,bit1,…,bit7,且bit0的优先级最高。

同时设计8个函数,第i个函数以biti(i=0,1,2,…,7)为参数,并且在函数体内输出biti的值。

将8个函数的名字存入一个函数指针数组p_fun。

如果bit0为1,调用p_fun[0]指向的函数。

如果structbits中有多位为1,则根据优先级从高到低依次调用函数指针数组p_fun中相应元素指向的函数。

8个函数中的第0个函数可以设计为:

voidf0(structbitsb)

{

Printf(“thefunction%discalled!

\n”,b);

}

(2)假设用单向链表建立一张班级成绩单,包括每个学生的学号、姓名、英语、高等数学、普通物理、C语言程序设计四门课程的成绩。

用函数编程实现下列功能:

(1)输入每个学生的各项信息。

(2)输出每个学生的各项信息。

(3)修改指定学生的指定数据项的内容。

(4)统计每个同学的平均成绩(保留2位小数)。

(5)输出各位同学的学号、姓名、四门课程的总成绩和平均成绩。

4.选做题

(1)对上述程序设计题中第

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

(2)对选做题第

(1)题,进一步写出用交换1节点指针域的方法升序排序的函数。

(3)采用双向链表重做编程设计题中的第

(2)题。

三、实验步骤及结果

1.表达式求值的程序验证

序号

表达式

计算值

验证值

1

(++p)->x

100

100

2

p++,p->c

B

B

3

*p++->t,*p->t

x

x

4

*(++p)->t

x

x

5

*++p->t

V

V

6

++*p->t

V

V

验证程序:

#include

intmain(void)

{

char*m;char*n;char*e;char*f;

charu[]="UVWXYZ";

charv[]="xyz";

m=u,n=v;

structT{

intx;

charc;

char*t;

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

e=a[0].t;f=a[1].t;

printf("1.(++p)->x\t%d\n",(++p)->x);

p=a;*u=*m;*v=*n;a[0].t=e;a[1].t=f;

printf("2.p++,p->c\t%c\n",(p++,p->c));

p=a;*u=*m;*v=*n;a[0].t=e;a[1].t=f;

printf("3.*p++->t,*p->t\t%c\n",(*p++->t,*p->t));

p=a;*u=*m;*v=*n;a[0].t=e;a[1].t=f;

printf("4.*(++p)->t\t%c\n",(*(++p)->t));

p=a;*u=*m;*v=*n;a[0].t=e;a[1].t=f;

printf("5.*++p->t\t%c\n",(*++p->t));

p=a;*u=*m;*v=*n;a[0].t=e;a[1].t=f;

printf("6.++*p->t\t%c\n",(++*p->t));

return0;

}

2.源程序修改、替换

(1)

#include

#include

structs_list{

intdata;

structs_list*next;

};

voidcreat_list(structs_list**headp,int*p);

intmain()

{

structs_list*head=NULL,*p;

ints[]={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");

return0;

}

voidcreat_list(structs_list**headp,int*p)

{

structs_list*loc_head=NULL,*tail;

if(p[0]==0)

;

else

{

loc_head=(structs_list*)malloc(sizeof(structs_list));

loc_head->data=*p++;

tail=loc_head;

while(*p)

{

tail->next=(structs_list*)malloc(sizeof(structs_list));

tail=tail->next;

tail->data=*p++;

}

tail->next=NULL;

}

*headp=loc_head;

}

(2)

#include

#include

structs_list{

intdata;

structs_list*next;

};

voidcreat_list(structs_list**headp,int*p);

intmain()

{

structs_list*head=NULL,*p;

ints[]={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");

return0;

}

voidcreat_list(structs_list**headp,int*p)

{

structs_list*loc_head=NULL,*tail;

if(p[0]==0)

;

else

{

loc_head=(structs_list*)malloc(sizeof(structs_list));

tail=loc_head;

loc_head->data=*p++;

while(*p)

{

loc_head=(structs_list*)malloc(sizeof(structs_list));

loc_head->data=*p++;

loc_head->next=tail;

tail=loc_head;

}

}

*headp=loc_head;

}

3.程序设计

(1)

#include

structbits{

unsignedcharbit0:

1;

unsignedcharbit1:

1;

unsignedcharbit2:

1;

unsignedcharbit3:

1;

unsignedcharbit4:

1;

unsignedcharbit5:

1;

unsignedcharbit6:

1;

unsignedcharbit7:

1;

}t;

voidf0(structbitsb)

{

printf("the1function%discalled!

\n",b.bit0);

}

voidf1(structbitsb)

{

printf("the2function%discalled!

\n",b.bit1);

}

voidf2(structbitsb)

{

printf("the3function%discalled!

\n",b.bit2);

}

voidf3(structbitsb)

{

printf("the4function%discalled!

\n",b.bit3);

}

voidf4(structbitsb)

{

printf("the5function%discalled!

\n",b.bit4);

}

voidf5(structbitsb)

{

printf("the6function%discalled!

\n",b.bit5);

}

voidf6(structbitsb)

{

printf("the7function%discalled!

\n",b.bit6);

}

voidf7(structbitsb)

{

printf("the8function%discalled!

\n",b.bit7);

}

intmain(void)

{

printf("pleaseinputanunsignedbit:

\n");

unionw{

unsignedcharh;

structbitst;

}a;

a.h=getchar();

void(*p_fun[8])(structbits);

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(a.t.bit0)p_fun[0](a.t);

if(a.t.bit1)p_fun[1](a.t);

if(a.t.bit2)p_fun[2](a.t);

if(a.t.bit3)p_fun[3](a.t);

if(a.t.bit4)p_fun[4](a.t);

if(a.t.bit5)p_fun[5](a.t);

if(a.t.bit6)p_fun[6](a.t);

if(a.t.bit7)p_fun[7](a.t);

return0;

}

(2)

#include

#include

#include

structstu

{

longnum;

chars[10];

intc1;

intc2;

intc3;

intc4;

structstu*next;

};

voidcreatlist(structstu**headp);

voidoutputlist(structstu**headp);

voidcorrectlist(structstu**headp);

voidavstu(structstu**headp);

voidsumstu(structstu**headp);

intmain()

{

structstu*head=NULL;

creatlist(&head);

outputlist(&head);

correctlist(&head);

avstu(&head);

sumstu(&head);

return0;

}

voidcreatlist(structstu**headp)

{

*headp=(structstu*)malloc(sizeof(structstu));

structstu*tail=*headp,*p=*headp;

printf("pleaseinputthenumberofstudentthenameGradeofEnglishGradeofmathGradeofphysicsGradeofClanguage.\n");

printf("Toendtheinput,pleaseintput0asthenumberofstudent.\n");

for(;;)

{

scanf("%ld",&p->num);

if(p->num==0)

break;

scanf("%s%d%d%d%d",p->s,&p->c1,&p->c2,&p->c3,&p->c4);

p=(structstu*)malloc(sizeof(structstu));

tail->next=p;

tail=p;

}

tail->next=NULL;

}

voidoutputlist(structstu**headp)

{

structstu*p=*headp;

while(p->next!

=NULL)

{

printf("ID%ldNAME%sENG%dMATH%dPHY%dC%d\n",p->num,p->s,p->c1,p->c2,p->c3,p->c4);

p=p->next;

}

}

voidcorrectlist(structstu**headp)

{

charc;

structstu*p;

p=*headp;

printf("1:

lookupbyname\n");

printf("2:

lookupbynumber\n");

c=getchar();

c=getchar();

switch(c)

{

case'1':

printf("pleaseinputthename\n");

chart[10];

scanf("%s",t);

for(;p->next!

=NULL&&strcmp(p->s,t);p=p->next)

;

if(!

strcmp(p->s,t))

printf("FINDID%ldNAME%sENG%dMATH%dPHY%dC%d\n",p->num,p->s,p->c1,p->c2,p->c3,p->c4);

elseif(p->next==NULL)

{

printf("NOTFOUND\n");

return;

}

break;

case'2':

printf("pleaseinputthenumber\n");

longtmp;

scanf("%ld",&tmp);

for(;p->next!

=NULL&&tmp!

=p->num;p=p->next)

;

if(tmp==p->num)

printf("FINDID%ldNAME%sENG%dMATH%dPHY%dC%d\n",p->num,p->s,p->c1,p->c2,p->c3,p->c4);

elseif(p->next==NULL)

{

printf("NOTFOUND\n");

return;

}

break;

}

printf("whichonetochange?

\n");

printf("1:

number\n");

printf("2:

name\n");

printf("3:

GradeofEnglish\n");

printf("4:

GradeofMath\n");

printf("5:

GradeofPhysics\n");

printf("6:

GradeofCLanguage\n");

printf("7:

Idon'twanttochageanything\n");

c=getchar();

c=getchar();

printf("pleasereinput\n");

switch(c)

{

case'1':

scanf("%ld",&p->num);

break;

case'2':

scanf("%s",p->s);

break;

case'3':

scanf("%d",&p->c1);

break;

case'4':

scanf("%d",&p->c2);

break;

case'5':

scanf("%d",&p->c3);

break;

case'6':

scanf("%d",&p->c4);

default:

return;

}

}

voidavstu(structstu**headp)

{

structstu*p=*headp;

for(;p->next!

=NULL;p=p->next)

{

doubletp;

tp=(p->c1+p->c2+p->c3+p->c4)/4.0;

printf("ID%ldName%saverage%.2lf\n",p->num,p->s,tp);

}

}

voidsumstu(structstu**headp)

{

structstu*p;

p=*headp;

for(;p->next!

=NULL;p=p->next)

{

doubletp;

intsum;

sum=p->c1+p->c2+p->c3+p->c4;

tp=sum/4.0;

printf("ID%ldName%ssumis%daverageis%.2lf\n",p->num,p->s,sum,tp);

}

}

4.选做题

(1)

#include

#include

#include

structstu

{

longnum;

chars[10];

intc1;

intc2;

intc3;

intc4;

doublec5;

structstu*next;

};

voidcreatlist(structstu**headp);

voidoutputlist(structstu**headp);

voidcorrectlist(structstu**headp);

voidavstu(structstu**headp);

voidsumstu(structstu**headp);

voidsort_lists(structstu*head);

voidoutputlist2(structstu**headp);

int

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 艺术

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1