《C语言与程序设计》实验报告_2.9.doc_第1页
《C语言与程序设计》实验报告_2.9.doc_第2页
《C语言与程序设计》实验报告_2.9.doc_第3页
《C语言与程序设计》实验报告_2.9.doc_第4页
《C语言与程序设计》实验报告_2.9.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

.C语言程序设计 实验报告专业 计算机科学与技术学院 班级 日期 2013/12/19 成绩 实验组别 第7次实验 指导教师 李开 学生姓名 学号 同组人姓名 实验名称 结构与联合实验 1、 实验目的(1) 熟悉和掌握结构的说明和引用、结构的指针、结构数组,以及函数中使用结构的方法。(2) 掌握动态存储分配函数的用法,掌握自引用结构和单向列表的创建、遍历、结点的增删、查找等操作。(3) 了解字段结构和联合的用法。2、 实验内容及要求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.1 表达式值的计算序号表达式计算值验证值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);int main(void) struct s_list *head=NULL, *p; int s=1,2,3,4,5,6,7,8,9,0; /*0为结束标记*/ create_list(head,s); /*创建新链表*/ p=head; /*遍历指针p指向链头*/ while (p) printf(%dt, p-data); p=p-next; printf(n);return 0;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-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语言程序设计四门课程的成绩,试用函数编程实现下列功能:输入每个学生的各项信息。输出每个学生的各项信息。修改指定学生的指定数据项的内容。统计每个同学的平均成绩(保留2位小数)。输出各位同学的学号、姓名、四门课程的总成绩和平均成绩。3、 实验步骤及结果1.如下表表2.1 表达式值的计算序号表达式计算值验证值1(+p)-x1001002p+, p-cBB3*p+-t, *p-tUVWXYZx4*(+p)-txyzx5*+p-tVV6+*p-tVV2.(1) 运行结果:输出为空。调试后,发现,在退出create_list函数后,head的值仍为NULL。应该把函数声明中第一个参数struct s_list *headp改成struct s_list *headp,并且把函数调用时create_list(head,s)改为create_list(&head,s)。并且把 create_list函数内的headp=loc_head改为*headp=loc_head。最后的源程序:#include stdio.h#include stdlib.hstruct s_list int data; /*数据域*/ struct s_list *next; /*指针域*/;void create_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,9,0; /*0为结束标记*/ create_list(&head,s); /*创建新链表*/ p=head; /*遍历指针p指向链头*/ while (p) printf(%dt, p-data); p=p-next; printf(n); return 0;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-next=(struct s_list *)malloc(sizeof(struct s_list); tail=tail-next; /*tail指向新创建的结点*/ tail-data=*p+; /*向新建结点的数据域赋值*/ tail-next=NULL; /*对指针域赋NULL值*/ *headp=loc_head; /*使头指针headp指向新建的链表*/(2)最终的源程序:#include stdio.h#include stdlib.hstruct s_list int data; struct s_list *next;void create_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,9,0; create_list(&head,s); p=head; while (p) printf(%dt, p-data); p=p-next; printf(n); return 0;void create_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+; loc_head-next=NULL; while (*p) tail=(struct s_list *)malloc(sizeof(struct s_list); tail-next=loc_head; tail-data=*p+; loc_head=tail; *headp=loc_head;3. (1)#include struct bits unsigned char bit7:1, bit6:1, bit5:1, bit4:1, bit3:1, bit2:1, bit1:1, bit0:1;void f0(void) printf(The function 0 is called! n);void f1(void) printf(The function 1 is called! n);void f2(void) printf(The function 2 is called! n);void f3(void) printf(The function 3 is called! n);void f4(void) printf(The function 4 is called! n);void f5(void) printf(The function 5 is called! n);void f6(void) printf(The function 6 is called! n);void f7(void) printf(The function 7 is called! n);int main(void) struct bits b=1,1,1,0,0,0,0,1; void (*p_fun8)(void)=f0,f1,f2,f3,f4,f5,f6,f7; if (b.bit0) p_fun0(); if (b.bit1) p_fun1(); if (b.bit2) p_fun2(); if (b.bit3) p_fun3(); if (b.bit4) p_fun4(); if (b.bit5) p_fun5(); if (b.bit6) p_fun6(); if (b.bit7) p_fun7(); return 0;(2)#include #include #include struct stu char id10; char name10; float en,math,phy,c; float total,average; struct stu *next;void print_function(void);void create_list(struct stu *, int);void print_list(struct stu *);void correct_score(struct stu *, int, char *, int, float);void calculate(struct stu *);void print_total(struct stu *);int main(void) struct stu *head=NULL; int n,flag=0; char id_name20; int iorn,cou_id;float tar_data; printf(Input the number of students:n); scanf(%d, &n); print_function(); iNput: printf(Choice:); scanf(%d, &flag); switch (flag) case 1: create_list(&head,n); break; case 2: print_list(head); break; case 3: printf(correct: (1 or 0)(means id or name), (id or name), course_id (1.en 2.math 3.phy 4.C), target_data. (eg. 1 U201315088 2 90)n); scanf(%d%s%d%f, &iorn, id_name, &cou_id, &tar_data); correct_score(&head,iorn,id_name,cou_id,tar_data); break; case 4: calculate(&head); break; case 5: print_total(head); break; case 6: return 0; default: printf(Input wrong! Again!n); break; goto iNput; return 0;void print_function(void) printf(Now please choose the function you want:n); printf(1.input each students information.n); printf(2.print each students information.n); printf(3.modify a students information.n); printf(4.calculate each students average score.(with 2 decimal places)n); printf(5.print each students id,name,total score,average score.n); printf(6.exit.n);void create_list(struct stu *headp, int n) struct stu *loc_head=NULL, *tail; if (n=0) ; else loc_head=(struct stu *)malloc(sizeof(struct stu); printf(Input id,name,score of english,math,physics,c:n); scanf(%s%s%f%f%f%f, loc_head-id, loc_head-name, &loc_head-en, &loc_head-math, &loc_head-phy, &loc_head-c); tail=loc_head; n-; while (n0) tail-next=(struct stu *)malloc(sizeof(struct stu); tail=tail-next; scanf(%s%s%f%f%f%f, tail-id, tail-name, &tail-en, &tail-math, &tail-phy, &tail-c); n-; tail-next=NULL; *headp=loc_head;void print_list(struct stu *headp) struct stu *tail=headp; if (tail=NULL) ; else while (tail!=NULL) printf(%st%st%6.2ft%6.2ft%6.2ft%6.2fn,tail-id,tail-name,tail-en,tail-math,tail-phy,tail-c); tail=tail-next; void correct_score(struct stu *headp, int iorn, char *id_name, int cou_id, float tar_data) struct stu *tail=*headp; if (tail=NULL) printf(Not found!n); return; else if (iorn) while (tail!=NULL) if (strcmp(tail-id,id_name)=0) switch (cou_id) case 1: tail-en=tar_data; break; case 2: tail-math=tar_data; break; case 3: tail-phy=tar_data; break; case 4: tail-c=tar_data; break; default: exit(-1); break; if (str

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论