试验九试验报告_卓越14_彭佳伟_第1页
试验九试验报告_卓越14_彭佳伟_第2页
试验九试验报告_卓越14_彭佳伟_第3页
试验九试验报告_卓越14_彭佳伟_第4页
试验九试验报告_卓越14_彭佳伟_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

1、C语言程序设计实验报告班级卓越工程师班成绩指导教师 李开学号 U201414716同组人姓名专业计算机科学与技术日期2014年1月13日第九次实验结构与联合实验学生姓名 彭佳伟实验组别实验名称结构与联合实验一、实验目的(1)熟悉和掌握结构的说明和引用、结构的指针、结构数组,以及函数中使用结构的方 法。(2)掌握动态存储分配函数的用法,掌握自引用结构和单向链表的创建、遍历、结点的 增删、查找等操作。(3)了解字段结构和联合的用法。二、实验任务1表达式求值的程序验证设有说明:char u = "UVWXYZ"char v = "xyz"struct Tint

2、兀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作为结束标志且不作为结点, 将其建成一个 先进先出的链表。先进先出链表的头指针始终指

3、向最先创建的结点(链头),先建结点指向后建结点,后建结点始终是尾结点。请完成以下工作:(1)源程序中存在什么样的错误(先观察执行结果)?对程序进行修改、调试。使之能 够正确完成指定任务。(2)修改替换creat_list函数,将其建成一个后进先出的链表。后进先出的链表的头指针始终指向最后创建的结点(链头),后建结点指向先建结点,先建结点始终是尾结点。 源程序#in clude<stdio.h>#include<stdlib.h>struct s_list int data;struct s_list *next;void creat_list(struct s_list

4、 *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("%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, *t

5、ail;if(p0 = 0)Jelseloc_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.程序设计 编写并上机调试运行能实现以下功能的

6、程序或函数:(1) 编写一个程序,实现以下功能:定义一个字段结构struct bits ,它将一个 8 位无符号字节从最低位向最高位声明为8个字段,各字段依次为bitO, bitl,bit7,且bitO的优先级最高。同时设计8个函数,第i个函数以biti (i = 0, 1,7)为参数,并且在函数体内输出 biti的值。将8个函数的名字存入一个函数指针数组p_fun。如果bit0为1,调用p_funO指向的函数。如果 struct bits中有多位为1,则根据优先级从高到低依次调用函数指针数组 p_fun中相应元素指向的函数。8个函数中的第0个函数可以设计为Void f0(struct bit

7、s b)Printf( fhe function %d is called!'n ”,b);(3)设计用单词链表建立一张班级成绩单,包括每个学生的学号、姓名、英语、高等数学、普通物理、C语言程序设计四门课程的成绩,试用函数编程实现下列功能: 输入每个学生的各项信息。 输出每个学生的各项信息。 修改指定学生的指定数据项的内容。 统计每个同学的平均成绩(保留两位小数)。 输出各位同学的学号、姓名、四门课程的总成绩和平均成绩。4选做题(1) 对上述程序设计题中第(2)题的程序,增加按照平均成绩进行升序排序的函数,试写 出用交换结点数据域的方法升序排序的函数,排序可用选择法或冒泡法。(2)对选

8、做题第(1)题,进一步写出用交换结点指针域的方法升序排序的函数。(3) 采用双向链表重做编程设计题中的第(2)题。三、实验步骤及结果1、序号表达式计算值验证值1(+p) -> x1001002p+, 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'2、(1)错误:create_list函数中传入的是一级指针,应该传入二级指针。修改

9、后代码:#in clude<stdio.h>#in clude<stdlib.h> struct s_listint data;struct s_list *n ext;;void creat_list(struct s_list *headp, int *p);int mai n(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("%dt", p -> dat

10、a);p = p -> n ext;prin tf("n ”);return 0;void creat_list(struct s_list *headp, int *p)struct s_list *loc_head = NULL, *tail;if(pO = 0)5elseloc_head = (struct s_list *)malloc(sizeof(struct s_list);loc_head -> data = *p+;tail = loc_head;while(*p)tail -> next = (struct s_list *)malloc(siz

11、eof(struct s_list);tail = tail -> n ext;tail -> data = *p+;tail -> next = NULL;*headp = loc_head;运行结果:12345678Program ended with exit code: 0(2 )源代码:#in clude<stdio.h>#in clude<stdlib.h> struct s_listint data;struct s_list *n ext;;void creat_list(struct s_list *headp, int *p);in

12、t 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("%dt", p -> data);p = p -> n ext;prin tf("n ”);return 0;void creat_list(struct s_list *headp, int *p)struct s_list *loc_head = NULL, *temp = NULL;if(pO =

13、 0)elsewhile(*p)loc_head = (struct s_list*)malloc(sizeof(struct s_list);loc_head -> data = *p+;loc_head -> n ext = temp;temp = loc_head;*headp = loc_head;运行结果:07654321Program Ended with exit cade: 03、( 1)源代码:#include <stdio.h> struct bitsunsigned char bit0 : 1;unsigned char bit1 : 1;unsi

14、gned char bit2 : 1;unsigned char bit3 : 1;unsigned char bit4 : 1;unsigned char bit5 : 1;unsigned char bit6 : 1;unsigned char bit7 : 1;union wstruct bits a;unsigned char t;m;void f0(unsigned char bit)printf("the function %d is called!n", bit);void f1(unsigned char bit)printf("the funct

15、ion %d is called!n", bit);void f2(unsigned char bit)printf("the function %d is called!n", bit);void f3(unsigned char bit)printf("the function %d is called!n", bit);void f4(unsigned char bit)printf("the function %d is called!n", bit); void f5(unsigned char bit)print

16、f("the function %d is called!n", bit);void f6(unsigned char bit)printf("the function %d is called!n", bit);void f7(unsigned char bit)printf("the function %d is called!n", bit);int main(int argc, char const *argv)unsigned int n;void (*p_fun8)(unsigned char b);printf(&quo

17、t;input n: ");scanf("%d", &n);m.t = n;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(m.a.bit0)p_fun0(m.a.bit0);if(m.a.bit1)p_fun1(m.a.bit1);if(m.a.bit2)p_fun2(m.a.bit2);if(m.a.bit3)p_fun3(m.a.bit3);if(m.a.bit4)p_fun4(m.a.bit4);if

18、(m.a.bit5)p_fun5(m.a.bit5);if(m.a.bit6)p_fun 6(m.a.bit6);if(m.a.bit7)P_fun 7(m.a.bit7);return 0;运行结果: int main(void)input n: 255 the function the function the "furKtion the function the function the function the function the function Prog1 is called!1 is called!1 is called!1 is called!1 is call

19、ed!1 is called!1 is called!1 is called!ram ended with exit code: 03. ( 2)源代码:#i nclude <stdio.h>#i nclude <stdlib.h>#in clude <stri ng.h>#in clude <ctype.h>typedef struct Stude nt Stude nt;struct Stude ntint nu mber;char n ame10;float en glish;float math;float physics;float c

20、;float average;float sum;Stude nt *n ext;void In put(Stude nt *head);void Output(Stude nt *head);void Cha ngel nfo(Stude nt *head);void Sort(Stude nt *head);Student *head = NULL;int n;int flag = 1;while (flag = 1)printf("Please input the number of options :n"); printf("1 : Input Infor

21、mationt 2 : Change Informationn"); printf("3 : Output Informationt 4 : Sort Informationn"); printf("5 : Quitn");scanf(" %d", &n);switch (n)case 1:Input(&head);break;case 2:ChangeInfo(head);break;case 3:Output(head);break;case 4:Sort(head);break;case 5:flag

22、= 0;break;default:printf("Illegal Input! Please input again."); break;printf("nn");return 0;void Input(Student *head)/ 采用的是后进先出的单向链表 char answer = 'y'while (answer = 'y')Student *current = NULL;current = (Student*)malloc(sizeof(Student);current->next = *head;*h

23、ead = current; printf("nPlease input the student's number : "); scanf(" %d", &current->number);printf("Please input the student's name : "); scanf(" %s", current->name);printf("Please input the score of english: "); scanf(" %f&

24、quot;, &current->english);printf("Please input the score of math : "); scanf(" %f", &current->math);printf("Please input the score of physics : "); scanf(" %f", &current->physics);printf("Please input the score of c : "); scanf(&

25、quot; %f", &current->c);current->sum = current->english + current->math + current->physics + current->c;current->average = current->sum / 4; printf("Do you want to input the infomation of another student? (Y/N) "); scanf(" %c", &answer);answer

26、 = tolower(answer);void Output(Student *head)if (head = NULL)printf("You have not input any information!");elseprintf("nnttThe Infomation Of Studentsn");doprintf("name:%stt", head->name); printf("number:%dtt", head->number); printf("english:%.2ft&qu

27、ot;, head->english); printf("math:%.2ft", head->math); printf("physics:%.2fnn", head->physics); printf("c:%.2ft", head->c);printf("sum:%.2f", head->sum);printf("average:%.2f", head->average);head = head->next; while (head != NULL

28、);void ChangeInfo(Student *head)if (head = NULL)printf("You have not input any information!");elseStudent *current = head;char name10;int flag = 0;int n;printf("Please input the student's name : "); scanf("%s", name);while (current->next != NULL)if (strcmp(name,

29、current->name) != 0)current = current->next;elseflag = 1;printf("Please input the number of options :n");printf("1 : englisht2 : matht 3 : physicst 4 : ct 5 : namet 6 : numbern");scanf("%d", &n);switch (n)case 1: printf("Please input the score of english

30、: "); scanf(" %f", &current->english); break;case 2: printf("Please input the score of math : "); scanf(" %f", &current->math); break;case 3:printf("Please input the score of physics : "); scanf(" %f", &current->physics);bre

31、ak;case 4: printf("Please input the score of c : "); scanf(" %f", &current->c); break;case 5: printf("Please input the student's name : "); scanf(" %s", current->name); break;case 6:printf("Please input the student's number : "); sc

32、anf(" %d", &current->number);default: break;if (n <= 3)+ current->math +current->sum = current->english current->physics + current->c;current->average = current->sum / 4;break;void Sort(Student *head)/ 采用的是冒泡排序/ 交换的是数据域,不是指针if (head = NULL)printf("You ha

33、ve not input any information!");elseStudent temp;while (head->next != NULL)Student *current = head; while (current->next != NULL)if (current->average < current->next->average)temp = *current;temp.next = current->next->next; current->next->next = current->next; *

34、current = *current->next;*current->next = temp;current = current->next;head = head->next;运行结果:Please input the number of options :1 : Input Information2 : Change Information3 : Output Information4 : Sort Information5 : Quit1Please input the student's number : 124Please input the stud

35、ent's name : pjwPlease input the score of english: 54Please input the score of math : 75Please input the score of physics :65Please input the score of c : 45Do you want to input the infomation of another student? (Y/N)yPlease input the student's number :7896Please input the student's nam

36、e :tsrPlease input the score of english:64Please input the score of math :23Please input the score of physics :75Please input the score of c : 34Do you want to input the infomation of another student? (Y/N)yPlease input the student's number : 0706Please input the student's name :lgyPlease in

37、put the score of english: 79Please input the score of math :64Please input the score of physics : 76Please input the score of c : 34Do you want to input the infomation of another student? (Y/N)nPlease input the number of options :1 : Input Information2 : Change Information3 : Output Information4 : Sort Information5 : Quit4Please input the number of options :1 : Input Information2 : Change Information3 : Output Information4 : Sort Information5 : Quit3math:64.00 physics:76.00number:124 english:54.00The

温馨提示

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

评论

0/150

提交评论