已阅读5页,还剩25页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C语言程序设计 实验报告专业 信息安全 班级 1103 日期 成绩 实验组别 第 1次实验 指导教师 学生姓名 严志颖 学号 U201114113 同组人姓名 实验名称: 结构与联合实验1、 实验目的1熟悉和掌握结构的说明和引用、结构的指针、结构数组、以及函数中使用结构的方法。2掌握动态储存分配函数的用法,掌握自引用结构,单向链表的创建、遍历、结点的增删、查找等操作。3了解字段结构和联合的用法。2、 实验内容及要求1表达式求值的程序验证题设有说明:char u=UVWXYZ;char v=xyz;struct Tint x;char c;char *t;a=11,A,u,100, B,v,*p=a;请先自己计算下面表达式的值,然后通过编程计算来加以验证。(各表达式相互无关)序号表达式计算值验证值1(+p)-x2p+,p-c3*p+-t,*p-t4*(+p)-t5*+p-t6+*p-t2源程序修改、替换下面所给源程序的功能是:给定一批整数,以0作为结束标志且不作为结点,将其建成一个先进先出的链表,先进先出链表的指头指针始终指向最先创建的结点(链头),先建结点指向后建结点,后建结点始终是尾结点。请完成以下工作:(1) 源程序中存在什么样的错误(先观察执行结果)?对程序进行修改、调试,使之能够正确完成指定任务。(2) 修改替换create_list函数,将其建成一个后进先出的链表。后进先出链表的头指针始终指向最后创建的结点(链头),后建结点指向先建结点,先建结点始终是尾结点。源程序:#include stdio.h#include stdlib.hstruct s_list int data; /* 数据域 */struct s_list *next; /* 指针域 */ ;void create_list (struct s_list *headp,int *p);void main(void)struct s_list *head=NULL,*p;int s=1,2,3,4,5,6,7,8,0; /* 0为结束标记 */create_list(head,s);/* 创建新链表 */p=head; /*遍历指针p指向链头 */while(p)printf(%dt,p-data); /* 输出数据域的值 */p=p-next; /*遍历指针p指向下一结点 */printf(n);void create_list(struct s_list *headp,int *p)struct s_list * loc_head=NULL,*tail;if(p0=0) /* 相当于*p=0 */;else /* loc_head指向动态分配的第一个结点 */loc_head=(struct s_list *)malloc(sizeof(struct s_list);loc_head-data=*p+; /* 对数据域赋值 */tail=loc_head; /* tail指向第一个结点 */while(*p) /* tail所指结点的指针域指向动态创建的结点 */tail-next=(struct s_list *)malloc(sizeof(struct s_list);tail=tail-next; /* tail指向新创建的结点 */tail-data=*p+; /* 向新创建的结点的数据域赋值 */tail-next=NULL; /* 对指针域赋NULL值 */headp=loc_head; /* 使头指针headp指向新创建的链表 */3. 程序设计编写并上机调试运行能实现以下功能的程序或函数:(1)编写一个程序,实现以下功能:设计一个字段结构struct bits,它将一个8位无符号字节从最低位向最高位声明为8个字段,各字段依次为bit0, bit1, , bit7,且bit0的优先级最高。同时设计8个函数,第i个函数以biti(i=0,1,2,7)为参数,并且在函数体内输出biti的值。将8个函数的名字存入一个函数指针数组p_fun。如果bit0为1,调用p_fun0指向的函数。如果struct bits中有多位为1,则根据优先级从高到低依次调用函数指针数组p_fun中相应元素指向的函数。8个函数中的第0个函数可以设计为:void f0(struct bits b)Printf(“the function %d is called!n”,b); (2)假设用单向链表建立一张班级成绩单,包括每个学生的学号、姓名、英语、高等数学、普通物理、C语言程序设计四门课程的成绩。用函数编程实现下列功能:(1) 输入每个学生的各项信息。 (2) 输出每个学生的各项信息。(3) 修改指定学生的指定数据项的内容。(4) 统计每个同学的平均成绩(保留2位小数)。(5) 输出各位同学的学号、姓名、四门课程的总成绩和平均成绩。4.选做题(1)对上述程序设计题中第(2)题的程序,增加按照平均成绩进行升序排序的函数,试写出用交换节点数据域的方法升序排序的函数,排序可选择用选择法或冒泡法。(2)对选做题第(1)题,进一步写出用交换1节点指针域的方法升序排序的函数。(3)采用双向链表重做编程设计题中的第(2)题。三、实验步骤及结果1.表达式求值的程序验证序号表达式计算值验证值1(+p)-x1001002p+,p-cBB3*p+-t,*p-txx4*(+p)-txx5*+p-tVV6+*p-tVV验证程序:#includeint main(void) char *m;char *n;char *e;char *f; char u=UVWXYZ; char v=xyz; m=u,n=v; struct T int x; char c; char *t; a=11,A,u,100,B,v,*p=a; e=a0.t;f=a1.t; printf(1.(+p)-xt%dn,(+p)-x); p=a;*u=*m;*v=*n;a0.t=e;a1.t=f; printf(2.p+,p-ct%cn,(p+,p-c); p=a;*u=*m;*v=*n;a0.t=e;a1.t=f; printf(3.*p+-t,*p-tt%cn,(*p+-t,*p-t); p=a;*u=*m;*v=*n;a0.t=e;a1.t=f; printf(4.*(+p)-tt%cn,(*(+p)-t); p=a;*u=*m;*v=*n;a0.t=e;a1.t=f; printf(5.*+p-tt%cn,(*+p-t); p=a;*u=*m;*v=*n;a0.t=e;a1.t=f; printf(6.+*p-tt%cn,(+*p-t); return 0;2.源程序修改、替换(1)#include #include struct s_list int data; struct s_list *next;void creat_list(struct s_list *headp,int *p);int main() 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(%dt,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(p0=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 #include struct s_list int data; struct s_list *next;void creat_list(struct s_list *headp,int *p);int main() 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(%dt,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(p0=0) ; else loc_head=(struct s_list *)malloc(sizeof(struct s_list); tail=loc_head; loc_head-data=*p+; while(*p) loc_head=(struct s_list *)malloc(sizeof(struct s_list); loc_head-data=*p+; loc_head-next=tail; tail=loc_head; *headp=loc_head;3. 程序设计(1)#includestruct bits unsigned char bit0:1; unsigned char bit1:1; unsigned char bit2:1; unsigned char bit3:1; unsigned char bit4:1; unsigned char bit5:1; unsigned char bit6:1; unsigned char bit7:1;t;void f0(struct bits b) printf(the 1 function %d is called!n,b.bit0);void f1(struct bits b) printf(the 2 function %d is called!n,b.bit1);void f2(struct bits b) printf(the 3 function %d is called!n,b.bit2);void f3(struct bits b) printf(the 4 function %d is called!n,b.bit3);void f4(struct bits b) printf(the 5 function %d is called!n,b.bit4);void f5(struct bits b) printf(the 6 function %d is called!n,b.bit5);void f6(struct bits b) printf(the 7 function %d is called!n,b.bit6);void f7(struct bits b) printf(the 8 function %d is called!n,b.bit7);int main(void) printf(please input an unsigned bit:n); union w unsigned char h; struct bits t; a; a.h=getchar(); void (*p_fun8)(struct bits); p_fun0=f0; p_fun1=f1; p_fun2=f2; p_fun3=f3; p_fun4=f4; p_fun5=f5; p_fun6=f6; p_fun7=f7; if(a.t.bit0)p_fun0(a.t); if(a.t.bit1)p_fun1(a.t); if(a.t.bit2)p_fun2(a.t); if(a.t.bit3)p_fun3(a.t); if(a.t.bit4)p_fun4(a.t); if(a.t.bit5)p_fun5(a.t); if(a.t.bit6)p_fun6(a.t); if(a.t.bit7)p_fun7(a.t); return 0;(2)#include #include #include struct stu long num; char s10; int c1; int c2; int c3; int c4; struct stu *next;void creatlist(struct stu *headp);void outputlist(struct stu *headp);void correctlist(struct stu *headp);void avstu(struct stu *headp);void sumstu(struct stu *headp);int main() struct stu *head=NULL; creatlist(&head); outputlist(&head); correctlist(&head); avstu(&head); sumstu(&head); return 0;void creatlist(struct stu *headp) *headp=(struct stu *)malloc(sizeof(struct stu); struct stu *tail=*headp,*p=*headp; printf(please input the number of student the name Grade of English Grade of math Grade of physics Grade of C language.n); printf(To end the input ,please intput 0 as the number of student.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=(struct stu *)malloc(sizeof(struct stu); tail-next=p; tail=p; tail-next=NULL;void outputlist(struct stu *headp) struct stu *p=*headp; while(p-next!=NULL) printf(ID %ld NAME %s ENG %d MATH %d PHY %d C %dn,p-num,p-s,p-c1,p-c2,p-c3,p-c4); p=p-next; void correctlist(struct stu *headp) char c; struct stu *p; p=*headp; printf(1:look up by namen); printf(2:look up by numbern); c=getchar(); c=getchar(); switch(c) case 1: printf(please input the namen); char t10; scanf(%s,t); for(;p-next!=NULL&strcmp(p-s,t);p=p-next) ; if(!strcmp(p-s,t) printf(FIND ID %ld NAME %s ENG %d MATH %d PHY %d C %dn,p-num,p-s,p-c1,p-c2,p-c3,p-c4); else if(p-next=NULL) printf(NOT FOUNDn); return ; break; case 2: printf(please input the numbern); long tmp; scanf(%ld,&tmp); for(;p-next!=NULL&tmp!=p-num;p=p-next) ; if(tmp=p-num) printf(FIND ID %ld NAME %s ENG %d MATH %d PHY %d C %dn,p-num,p-s,p-c1,p-c2,p-c3,p-c4); else if(p-next=NULL) printf(NOT FOUNDn); return ; break; printf(which one to change?n); printf(1:numbern); printf(2:namen); printf(3:Grade of Englishn); printf(4:Grade of Mathn); printf(5:Grade of Physicsn); printf(6:Grade of C Languagen); printf(7:I dont want to chage anythingn); c=getchar(); c=getchar(); printf(please reinputn); 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 ; void avstu(struct stu *headp) struct stu *p=*headp; for(;p-next!=NULL;p=p-next) double tp; tp=(p-c1+p-c2+p-c3+p-c4)/4.0; printf(ID %ld Name %s average %.2lfn,p-num,p-s,tp); void sumstu(struct stu *headp) struct stu *p; p=*headp; for(;p-next!=NULL;p=p-next) double tp; int sum; sum=p-c1+p-c2+p-c3+p-c4; tp=sum/4.0; printf(ID %ld Name %s sum is %d average is %.2lfn,p-num,p-s,sum,tp); 4. 选做题(1)#include #include #include struct stu long num; char s10; int c1; int c2; int c3; int c4; double c5; struct stu *next;void creatlist(struct stu *headp);void outputlist(struct stu *headp);void correctlist(struct stu *headp);void avstu(struct stu *headp);void sumstu(struct stu *headp);void sort_lists(struct stu *head);void outputlist2(struct stu *headp);int main() struct stu *head=NULL; creatlist(&head); outputlist(&head); correctlist(&head); avstu(&head); sumstu(&head); sort_lists(head); outputlist2(&head); return 0;void creatlist(struct stu *headp) *headp=(struct stu *)malloc(sizeof(struct stu); struct stu *tail=*headp,*p=*headp; printf(please input the number of student the name Grade of English Grade of math Grade of physics Grade of C language.n); printf(To end the input ,please intput 0 as the number of student.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=(struct stu *)malloc(sizeof(struct stu); tail-next=p; tail=p; tail-next=NULL;void outputlist(struct stu *headp) struct stu *p=*headp; while(p-next!=NULL) printf(ID %ld NAME %s ENG %d MATH %d PHY %d C %dn,p-num,p-s,p-c1,p-c2,p-c3,p-c4); p=p-next; void correctlist(struct stu *headp) char c; struct stu *p; p=*headp; printf(1:look up by namen); printf(2:look up by numbern); c=getchar(); c=getchar(); switch(c) case 1: printf(please input the namen); char t10; scanf(%s,t); for(;p-next!=NULL&strcmp(p-s,t);p=p-next) ; if(!strcmp(p-s,t) printf(FIND ID %ld NAME %s ENG %d MATH %d PHY %d C %dn,p-num,p-s,p-c1,p-c2,p-c3,p-c4); else if(p-next=NULL) printf(NOT FOUNDn); return ; break; case 2: printf(please input the numbern); long tmp; scanf(%ld,&tmp); for(;p-next!=NULL&tmp!=p-num;p=p-next) ; if(tmp=p-num) printf(FIND ID %ld NAME %s ENG %d MATH %d PHY %d C %dn,p-num,p-s,p-c1,p-c2,p-c3,p-c4); else if(p-next=NULL) printf(NOT FOUNDn); return ; break; printf(which one to change?n); printf(1:numbern); printf(2:namen); printf(3:Grade of Englishn); printf(4:Grade of Mathn); printf(5:Grade of Physicsn); printf(6:Grade of C Languagen); printf(7:I dont want to chage anythingn); c=getchar(); c=getchar(); printf(please reinputn); 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 ; void avstu(struct stu *headp) struct stu *p=*headp; for(;p-next!=NULL;p=p-next) double tp; tp=(p-c1+p-c2+p-c3+p-c4)/4.0; p-c5=tp; printf(ID %ld Name %s average %.2lfn,p-num,p-s,tp); void sumstu(struct stu *headp) struct stu *p; p=*headp; for(;p-next!=NULL;p=p-next) double tp; int sum; sum=p-c1+p-c2+p-c3+p-c4; tp=sum/4.0; printf(ID %ld Name %s sum is %d average is %.2lfn,p-num,p-s,sum,tp); void sort_lists(struct stu *head) struct stu *p1=head,*p2; int len=0,i,j; double t; long int l; char c10; while(p1) len+; p1=p1-next; for(i=0,p1=head;inext) for(j=i+1,p2=p1-next;jnext) if(p1-c5p2-c5) t=p1-c5;l=p1-num;strcpy(c,p1-s); p1-c5=p2-c5;p1-num=p2-num;strcpy(p1-s,p2-s); p2-c5=t;p2-num=l;strcpy(p2-s,c); void outputlist2(struct stu *headp) struct stu *p=*headp; while(p-next!=NULL) printf(average point:nID %ld NAME %s average point %lfn,p-num,p-s,p-c5); p=p-next; (2)#include #include #include struct stu long num; char s10; int c1; int c2; int c3; int c4; double c5; struct stu *next;void creatlist(struct stu *headp);void outputlist(struct stu *headp);void correctlist(struct stu *headp);void avstu(struct stu *headp);void sumstu(struct stu *headp);void sort_lists_lists_by_pointer_v1(struct stu *headp);void outputlist2(struct stu *headp);int main() struct stu *head=NULL; creatlist(&head); outputlist(&head); correctlist(&head); avstu(&head); sumstu(&head); sort_lists_lists_by_pointer_v1(&head); outputlist2(&head); return 0;void creatlist(struct stu *headp) *headp=(struct stu *)malloc(sizeof(struct stu); struct stu *tail=*headp,*p=*headp; printf(please input the number of student the name Grade of English Grade of math Grade of physics Grade of C language.n); printf(To end the input ,please intput 0 as the number of student.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=(struct stu *)malloc(sizeof(struct stu); tail-next=p; tail=p; tail-next=NULL;void outputlist(struct stu *headp) struct stu *p=*headp; while(p-next!=NULL) printf(ID %ld NAME %s ENG %d MATH %d PHY %d C %dn,p-num,p-s,p-c1,p-c2,p-c3,p-c4); p=p-next; void correctlist(struct stu *headp) char c; struct stu *p; p=*headp; printf(1:look up by namen); printf(2:look up by numbern); c=getchar(); c=getchar
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025学年泰安市一中高一语文上学期期中考试卷附答案解析
- 插花花艺师设备安全技术规程
- 钟表及计时仪器制造工诚信品质水平考核试卷含答案
- 小朋友校园嬉闹摔伤协议书
- 函数的零点与方程的解(2大考点+12大题型)-2026年新高考数学一轮复习(讲义+专练)原卷版
- 海啸应急避险知识竞赛考试题库100题(含答案)
- 四年级学期回顾与展望
- 硕士教育的全景解析
- 《边做边学-After Effects 2021影视后期合成案例教》课件 第9章 添加声音效果
- 甘肃省陇南市康县2025-2026学年七年级上学期周期学业能力评鉴道德与法治试题(含答案)
- 博物馆安全生产工作总结
- 三级安全教育考试题及答案电气
- 2024年公务员多省联考《申论》题(天津行政执法卷)试题及答案解析
- 2025年动漫设计师创作能力检测试卷及答案
- 行政单位管理会计案例
- 检验检测机构授权签字人考核试题(+答案)
- 2025年中考英语单词词汇检测默写(背诵版)
- 《电机控制与维修》校本教材
- 2025至2030全球及中国LIMS软件和实验室信息系统行业项目调研及市场前景预测评估报告
- 2025至2030中国精细化工行业产业运行态势及投资规划深度研究报告
- DB43∕T 3191-2025 水利水电工程脉动灌浆技术规范
评论
0/150
提交评论