实验九实验报告卓越14彭佳伟Word下载.docx
- 文档编号:21958726
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:20
- 大小:76.53KB
实验九实验报告卓越14彭佳伟Word下载.docx
《实验九实验报告卓越14彭佳伟Word下载.docx》由会员分享,可在线阅读,更多相关《实验九实验报告卓越14彭佳伟Word下载.docx(20页珍藏版)》请在冰豆网上搜索。
验证值
1
(++p)->
x
2
p++,p->
c
3
*p++->
t,*p->
t
4
*(++p)->
5
*++p->
6
++*p->
2.源程序修改、替换
下面所给源程序的功能是:
给定一批整数,以0作为结束标志且不作为结点,将其建成一个先进先出的链表。
先进先出链表的头指针始终指向最先创建的结点(链头),先建结点指向后建结点,后建结点始终是尾结点。
请完成以下工作:
(1)源程序中存在什么样的错误(先观察执行结果)?
对程序进行修改、调试。
使之能够正确完成指定任务。
(2)修改替换creat_list函数,将其建成一个后进先出的链表。
后进先出的链表的头指针始终指向最后创建的结点(链头),后建结点指向先建结点,先建结点始终是尾结点。
源程序
#include<
stdio.h>
stdlib.h>
structs_list{
intdata;
structs_list*next;
};
voidcreat_list(structs_list*headp,int*p);
intmain(void)
{
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;
}
\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=NULL;
headp=loc_head;
3.程序设计
编写并上机调试运行能实现以下功能的程序或函数:
(1)编写一个程序,实现以下功能:
定义一个字段结构structbits,它将一个8位无符号字节从最低位向最高位声明为8个字段,各字段依次为bit0,bit1,……bit7,且bit0的优先级最高。
同时设计8个函数,第i个函数以biti(i=0,1,……7)为参数,并且在函数体内输出biti的值。
将8个函数的名字存入一个函数指针数组p_fun。
如果bit0为1,调用p_fun[0]指向的函数。
如果structbits中有多位为1,则根据优先级从高到低依次调用函数指针数组p_fun中相应元素指向的函数。
8个函数中的第0个函数可以设计为
Voidf0(structbitsb)
Printf(“thefunction%discalled!
\n”,b);
(3)设计用单词链表建立一张班级成绩单,包括每个学生的学号、姓名、英语、高等数学、普通物理、C语言程序设计四门课程的成绩,试用函数编程实现下列功能:
1输入每个学生的各项信息。
2输出每个学生的各项信息。
3修改指定学生的指定数据项的内容。
4统计每个同学的平均成绩(保留两位小数)。
5输出各位同学的学号、姓名、四门课程的总成绩和平均成绩。
4.选做题
(1)对上述程序设计题中第
(2)题的程序,增加按照平均成绩进行升序排序的函数,试写出用交换结点数据域的方法升序排序的函数,排序可用选择法或冒泡法。
(2)对选做题第
(1)题,进一步写出用交换结点指针域的方法升序排序的函数。
(3)采用双向链表重做编程设计题中的第
(2)题。
三、实验步骤及结果
1、
100
‘B’
‘X’
‘V’
2、
(1)错误:
create_list函数中传入的是一级指针,应该传入二级指针。
修改后代码:
voidcreat_list(structs_list**headp,int*p);
creat_list(&
head,s);
voidcreat_list(structs_list**headp,int*p)
*headp=loc_head;
运行结果:
(2)源代码:
structs_list*loc_head=NULL,*temp=NULL;
loc_head=(structs_list*)malloc(sizeof(structs_list));
next=temp;
temp=loc_head;
3、
(1)源代码:
#include<
structbits
unsignedcharbit0:
1;
unsignedcharbit1:
unsignedcharbit2:
unsignedcharbit3:
unsignedcharbit4:
unsignedcharbit5:
unsignedcharbit6:
unsignedcharbit7:
unionw
structbitsa;
unsignedchart;
}m;
voidf0(unsignedcharbit)
thefunction%discalled!
bit);
voidf1(unsignedcharbit)
voidf2(unsignedcharbit)
voidf3(unsignedcharbit)
voidf4(unsignedcharbit)
voidf5(unsignedcharbit)
voidf6(unsignedcharbit)
voidf7(unsignedcharbit)
intmain(intargc,charconst*argv[])
unsignedintn;
void(*p_fun[8])(unsignedcharb);
inputn:
"
scanf("
%d"
&
n);
m.t=n;
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.bit0);
if(m.a.bit1)
p_fun[1](m.a.bit1);
if(m.a.bit2)
p_fun[2](m.a.bit2);
if(m.a.bit3)
p_fun[3](m.a.bit3);
if(m.a.bit4)
p_fun[4](m.a.bit4);
if(m.a.bit5)
p_fun[5](m.a.bit5);
if(m.a.bit6)
p_fun[6](m.a.bit6);
if(m.a.bit7)
p_fun[7](m.a.bit7);
3.
(2)源代码:
string.h>
ctype.h>
typedefstructStudentStudent;
structStudent
intnumber;
charname[10];
floatenglish;
floatmath;
floatphysics;
floatc;
floataverage;
floatsum;
Student*next;
voidInput(Student**head);
voidOutput(Student*head);
voidChangeInfo(Student*head);
voidSort(Student*head);
Student*head=NULL;
intn;
intflag=1;
while(flag==1)
Pleaseinputthenumberofoptions:
1:
InputInformation\t2:
ChangeInformation\n"
3:
OutputInformation\t4:
SortInformation\n"
5:
Quit\n"
%d"
switch(n)
case1:
Input(&
head);
break;
case2:
ChangeInfo(head);
case3:
Output(head);
case4:
Sort(head);
case5:
flag=0;
default:
IllegalInput!
Pleaseinputagain."
\n\n"
voidInput(Student**head)
//采用的是后进先出的单向链表
charanswer='
y'
while(answer=='
)
Student*current=NULL;
current=(Student*)malloc(sizeof(Student));
current->
next=*head;
*head=current;
\nPleaseinputthestudent'
snumber:
current->
number);
Pleaseinputthestudent'
sname:
%s"
current->
name);
Pleaseinputthescoreofenglish:
%f"
english);
Pleaseinputthescoreofmath:
math);
Pleaseinputthescoreofphysics:
physics);
Pleaseinputthescoreofc:
c);
sum=current->
english+current->
math+current->
physics+current->
c;
average=current->
sum/4;
Doyouwanttoinputtheinfomationofanotherstudent?
(Y/N)"
%c"
answer);
answer=tolower(answer);
voidOutput(Student*head)
if(head==NULL)
Youhavenotinputanyinformation!
"
\n\n\t\tTheInfomationOfStudents\n"
do{
name:
%s\t\t"
head->
number:
%d\t\t"
english:
%.2f\t"
math:
physics:
%.2f\n\n"
c:
sum:
%.2f"
sum);
average:
average);
head=head->
next;
}while(head!
=NULL);
voidChangeInfo(Student*head)
Student*current=head;
intflag=0;
%s"
name);
while(current->
next!
=NULL)
if(strcmp(name,current->
name)!
=0)
current=current->
flag=1;
english\t2:
math\t3:
physics\t4:
c\t5:
name\t6:
number\n"
Pleaseinputthescoreofenglish:
case6:
if(n<
=3)
voidSort(Student*head)
//采用的是冒泡排序
//交换的是数据域,不是指针
Studenttemp;
while(head->
if(current->
average<
next->
average)
temp=*current;
temp.next=current->
n
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 报告 卓越 14 彭佳伟
![提示](https://static.bdocx.com/images/bang_tan.gif)