




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、C語言程序設(shè)計實驗報告班級卓越工程師班成績指導教師 李開學號 U201414716同組人姓名專業(yè)計算機科學與技術(shù)日期2014年1月13日第九次實驗結(jié)構(gòu)與聯(lián)合實驗學生姓名 彭佳偉實驗組別實驗名稱結(jié)構(gòu)與聯(lián)合實驗一、實驗?zāi)康模?)熟悉和掌握結(jié)構(gòu)的說明和引用、結(jié)構(gòu)的指針、結(jié)構(gòu)數(shù)組,以及函數(shù)中使用結(jié)構(gòu)的方 法。(2)掌握動態(tài)存儲分配函數(shù)的用法,掌握自引用結(jié)構(gòu)和單向鏈表的創(chuàng)建、遍歷、結(jié)點的 增刪、查找等操作。(3)了解字段結(jié)構(gòu)和聯(lián)合的用法。二、實驗任務(wù)1表達式求值的程序驗證設(shè)有說明:char u = "UVWXYZ"char v = "xyz"struct Tint
2、兀char c;char *t;a = 11, 'A', u, 100, 'B', v, *p = a;請先自己計算表2.1中表達式的值,然后編寫程序并運行來加以驗證。(各表達式相互無關(guān))表2.1表達式值的計算序號表達式計算值驗證值1(+p) -> x2p+, p -> c3* p+ -> t, * p -> t4* (+p) -> t5* +p -> t6+ * p -> t2源程序修改、替換下面所給源程序的功能是: 給定一批整數(shù),以0作為結(jié)束標志且不作為結(jié)點, 將其建成一個 先進先出的鏈表。先進先出鏈表的頭指針始終指
3、向最先創(chuàng)建的結(jié)點(鏈頭),先建結(jié)點指向后建結(jié)點,后建結(jié)點始終是尾結(jié)點。請完成以下工作:(1)源程序中存在什么樣的錯誤(先觀察執(zhí)行結(jié)果)?對程序進行修改、調(diào)試。使之能 夠正確完成指定任務(wù)。(2)修改替換creat_list函數(shù),將其建成一個后進先出的鏈表。后進先出的鏈表的頭指針始終指向最后創(chuàng)建的結(jié)點(鏈頭),后建結(jié)點指向先建結(jié)點,先建結(jié)點始終是尾結(jié)點。 源程序#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.程序設(shè)計 編寫并上機調(diào)試運行能實現(xiàn)以下功能的
6、程序或函數(shù):(1) 編寫一個程序,實現(xiàn)以下功能:定義一個字段結(jié)構(gòu)struct bits ,它將一個 8 位無符號字節(jié)從最低位向最高位聲明為8個字段,各字段依次為bitO, bitl,bit7,且bitO的優(yōu)先級最高。同時設(shè)計8個函數(shù),第i個函數(shù)以biti (i = 0, 1,7)為參數(shù),并且在函數(shù)體內(nèi)輸出 biti的值。將8個函數(shù)的名字存入一個函數(shù)指針數(shù)組p_fun。如果bit0為1,調(diào)用p_funO指向的函數(shù)。如果 struct bits中有多位為1,則根據(jù)優(yōu)先級從高到低依次調(diào)用函數(shù)指針數(shù)組 p_fun中相應(yīng)元素指向的函數(shù)。8個函數(shù)中的第0個函數(shù)可以設(shè)計為Void f0(struct bit
7、s b)Printf( fhe function %d is called!'n ”,b);(3)設(shè)計用單詞鏈表建立一張班級成績單,包括每個學生的學號、姓名、英語、高等數(shù)學、普通物理、C語言程序設(shè)計四門課程的成績,試用函數(shù)編程實現(xiàn)下列功能: 輸入每個學生的各項信息。 輸出每個學生的各項信息。 修改指定學生的指定數(shù)據(jù)項的內(nèi)容。 統(tǒng)計每個同學的平均成績(保留兩位小數(shù))。 輸出各位同學的學號、姓名、四門課程的總成績和平均成績。4選做題(1) 對上述程序設(shè)計題中第(2)題的程序,增加按照平均成績進行升序排序的函數(shù),試寫 出用交換結(jié)點數(shù)據(jù)域的方法升序排序的函數(shù),排序可用選擇法或冒泡法。(2)對選
8、做題第(1)題,進一步寫出用交換結(jié)點指針域的方法升序排序的函數(shù)。(3) 采用雙向鏈表重做編程設(shè)計題中的第(2)題。三、實驗步驟及結(jié)果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函數(shù)中傳入的是一級指針,應(yīng)該傳入二級指針。修改
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;運行結(jié)果: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;運行結(jié)果: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;運行結(jié)果: 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", ¤t->number);printf("Please input the student's name : "); scanf(" %s", current->name);printf("Please input the score of english: "); scanf(" %f&
24、quot;, ¤t->english);printf("Please input the score of math : "); scanf(" %f", ¤t->math);printf("Please input the score of physics : "); scanf(" %f", ¤t->physics);printf("Please input the score of c : "); scanf(&
25、quot; %f", ¤t->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", ¤t->english); break;case 2: printf("Please input the score of math : "); scanf(" %f", ¤t->math); break;case 3:printf("Please input the score of physics : "); scanf(" %f", ¤t->physics);bre
31、ak;case 4: printf("Please input the score of c : "); scanf(" %f", ¤t->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", ¤t->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)/ 采用的是冒泡排序/ 交換的是數(shù)據(jù)域,不是指針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;運行結(jié)果: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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 車號租賃協(xié)議書
- 解除訂婚協(xié)議書
- 采茶收茶協(xié)議書
- 贍養(yǎng)家人協(xié)議書
- 物流公司理貨員派遣及物流品牌推廣協(xié)議
- 2025至2030中國羊奶行業(yè)營銷態(tài)勢與消費趨勢研究報告
- 2025至2030中國鹽酸洛美沙星片行業(yè)競爭狀況及需求潛力研究報告
- 2025至2030中國焦寶石行業(yè)發(fā)展環(huán)境及競爭格局展望研究報告
- 2025至2030中國溫度補償晶體振蕩器行業(yè)發(fā)展方向與需求前景研究報告
- 2025至2030中國液態(tài)硅膠市場多元化發(fā)展趨勢及經(jīng)營形勢研究報告
- 海門村干部考試試題及答案
- (二模)2025年5月濟南市高三高考針對性訓練生物試卷(含答案)
- 2024年云南省會澤縣事業(yè)單位公開招聘醫(yī)療衛(wèi)生崗筆試題帶答案
- 全球及中國雙特異性抗體治療行業(yè)市場發(fā)展分析及前景趨勢與投資發(fā)展研究報告2025-2028版
- 2025年電工操作資格證考試復(fù)習考試題庫(共583題)(含答案)
- 初中地理澳大利亞 課件-2024-2025學年七年級地理下學期(人教版2024)
- 2025-2030中國射擊器材行業(yè)市場發(fā)展趨勢與前景展望戰(zhàn)略研究報告
- 2025-2030中國采耳行業(yè)市場深度調(diào)研及競爭格局與投資前景研究報告
- logo保密合同協(xié)議
- 網(wǎng)格員考試題及答案重慶
- 消費者心理與行為附微課第2版白玉苓課后參考答案
評論
0/150
提交評論