C語言課程設(shè)計報告_第1頁
C語言課程設(shè)計報告_第2頁
C語言課程設(shè)計報告_第3頁
C語言課程設(shè)計報告_第4頁
C語言課程設(shè)計報告_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、C語言課程設(shè)計報告(學(xué)生成績簡單管理程序二)071010202 胡玉琢一、 程序的主要功能用單向鏈表結(jié)構(gòu)實現(xiàn)簡單的學(xué)生成績管理功能,具有鏈表建立、鏈表輸出、結(jié)點有序插入、結(jié)點刪除、數(shù)據(jù)查詢等功能。程序執(zhí)行過程為:循環(huán)顯示主菜單,用戶在Give your Choice:處輸入選項,即按照相應(yīng)功能列表輸入09中的任意一個數(shù)字,按回車后,執(zhí)行相應(yīng)的功能。各菜單項功能如下:(1)Create List(建立有序單向鏈表)從鍵盤上一次輸入一個學(xué)生的姓名和成績。以姓名為序建立有序鏈表。插入一條記錄后,顯示提示信息:確認(rèn)是否輸入下一條記錄,如確認(rèn),繼續(xù)輸入,否則,退出輸入功能。(2)Display All

2、Record(顯示所有結(jié)點記錄)按順序顯示鏈表中所有記錄,每屏顯示10條記錄。每顯示10條,按Enter鍵繼續(xù)顯示下一屏。(3)Insert a Record(插入一條結(jié)點記錄)在姓名為序排列的鏈表中插入一條記錄,插入后,鏈表仍有序。輸出插入成功的信息。(4)Delete A Record(按姓名查找,刪除一條結(jié)點記錄)輸入待刪除記錄的姓名,顯示提示信息,讓用戶再次確認(rèn)是否要刪除。確認(rèn)后,將該姓名的記錄刪除。(5)Query(查找并顯示一個結(jié)點記錄)輸入姓名,查找該記錄,并顯示該同學(xué)的成績。(6)Add Records from a Text File(從正文文件中添加數(shù)據(jù)到鏈表中)用戶可事前

3、建立一個正文文件data.txt,存放多個待加入的記錄。提示輸入正文文件的文件名,然后從該文件中一次性加入多條學(xué)生記錄。文件data.txt格式如下:2WangXiao 95LiuLin 87注意:該文件第一行的數(shù)字表示待添加的記錄數(shù),下面每行為待添加的姓名和成績。(7)Write to a Text File將鏈表中的全部記錄寫入文件records.txt,要求文件格式和文件data.txt相同。(8)Reverse List將鏈表中的所有結(jié)點逆序存放。(9)Delete the Sane Record刪除相同姓名的記錄。(0)Quit(退出學(xué)生成績管理程序)釋放鏈表存儲空間。二、(1)題目

4、的分析:本題是要求用單向鏈表結(jié)構(gòu)實現(xiàn)簡單的學(xué)生成績管理功能,因此主要考察了鏈表及其算法這一章的知識,此外,還會用到指針、數(shù)據(jù)文件的使用等多方面的知識,在設(shè)計的過程中,函數(shù)的嵌套、調(diào)用也會經(jīng)常出現(xiàn)。 (2)設(shè)計中遇到的主要問題和解決方法:a、在執(zhí)行Insert a Record模塊時,插入后Score一欄顯示的都是0.000000,原來是在scanf("%s%d",P->Name,&P->Score);這句中把成績定義為int型,而之前全部定義的是float型,所以出現(xiàn)錯誤。b、在執(zhí)行Add Records from a Text File模塊時,在機(jī)器上

5、創(chuàng)建了一個data.txt的文件,第一行數(shù)字表示待添加的記錄數(shù),下面每行為姓名和成績。當(dāng)調(diào)用Insert()函數(shù)作有序插入時,程序會將第一行的數(shù)字也當(dāng)做一組數(shù)據(jù),并在viod Display()中顯示出來,還有亂碼。原程序為Student*AddfromText(Student*head) FILE *fp; Student*p; char name20; printf("Please input the filename:n"); scanf("%s",name); fp=fopen(name,"r"); if (fp=NULL)

6、printf("The file does not exist!n"); else while(!feof(fp) p=(Student *)malloc(sizeof(Student); fscanf(fp,"%s %d",p->Name,&p->Score); head=Insert(head,p); printf("Job completedn"); fclose(fp); return(head);int WritetoText(Student*head) FILE *fp;Student*p;char n

7、ame20;p=head; printf("Please input the filename:n"); scanf("%s",name); fp=fopen(name,"w"); fprintf(fp,"tnametscoren"); while(p!=NULL) fprintf(fp,"t%st%dn",p->Name,p->Score); p=p->next; fclose(fp);這段程序中沒有對插入數(shù)據(jù)的個數(shù)進(jìn)行定義,同時循環(huán)的條件也需要做一些改變。所以我就定義了一個

8、i用于記錄循環(huán)的次數(shù),n表示待插入數(shù)據(jù)的個數(shù),并把while循環(huán)的條件改為i<n。(3)設(shè)計的感想和心得第一次編寫如此復(fù)雜的程序,對我而言是一個巨大的挑戰(zhàn),在解決這個難題的過程中,我采用了各個擊破的方法,把各個功能分開編寫,最后再匯總。在解決每部分之前,我都會將此部分會涉及的知識看一遍,加深對它的了解。通過此次課設(shè),我知道了編寫程序時,要盡量讓每個函數(shù)功能清晰,主函數(shù)簡單明了,這樣的結(jié)構(gòu)易于調(diào)試,同時以結(jié)構(gòu)體為鏈表環(huán)節(jié)并通過創(chuàng)立動態(tài)鏈表可以更極大地節(jié)省了系統(tǒng)資源。另外,這次對綜合知識的考察也讓我鞏固了前面有關(guān)函數(shù)、結(jié)構(gòu)體、指針的知識,在這一過程中,我遇到了許許多多的困難,但我經(jīng)常與同學(xué)討

9、論,向老師請教,彌補了之前一些知識的漏洞,掃清了盲區(qū),收獲了很多經(jīng)驗,也漸漸發(fā)現(xiàn)C語言這門課程并不是那么的枯燥和乏味,真真深入其中的時候也是樂趣無窮的。三、源程序#include<stdio.h> /*調(diào)用庫函數(shù)*/#include<stdlib.h>#include<string.h>#include<ctype.h>struct stud /*定義結(jié)構(gòu)體*/char Name20;float Score; struct stud*next;typedef struct stud Student; /*定義Student為struct stud

10、類型*/int menu_select();Student*Create(); /*函數(shù)說明*/void Display();Student*Insert();Student*insert_a_record();Student*Delete();Student*Delete_a_record();Student*Query();Student*Query_a_record(Student*head);Student*AddfromText();int Quit();Student*head=NULL; /*定義頭指針為全局變量*/void main() /*主函數(shù)*/for(;) switch

11、(menu_select() case 1:printf("Create Listn"); system("pause"); /*暫停程序執(zhí)行,按任意鍵后繼續(xù)執(zhí)行*/ head=Create(); break; case 2:printf("Display All Recoardn"); Display(head); system("pause"); break; case 3:printf(" Insert a Recordn"); system("pause"); hea

12、d=insert_a_record(head); break; case 4:printf(" Delete a Recordn"); system("pause"); Delete_a_record(); break; case 5:printf(" Queryn"); system("pause"); Query_a_record(head); break; case 6:printf(" Add Records from a Text Filen"); head=AddfromText(h

13、ead); system("pause"); break; case 7:printf(" Write to a Textn"); system("pause"); WritetoText(head); break; case 0:printf(" Quitn"); Quit(head); exit(0); /*函數(shù)終止程序執(zhí)行,正常終止返回0*/ int menu_select() /*菜單實現(xiàn)程序清單*/char c;dosystem("cls"); /*清屏*/ printf("

14、1. Create Listn"); printf("2. Display All Recordn"); printf("3. Insert a Recordn"); printf("4. Delete a Recordn"); printf("5. Queryn"); printf("6. Add Records from a Text Filen"); printf("7. Write to a Text Filen"); printf("0. Qui

15、tnn"); printf("Give your choice(0-7):");c=getchar(); while(c<'0'|c>'7'); /*控制輸入數(shù)字在0-7之間*/return(c-'0'); /*返回選擇值*/Student*Create() /*建立有序鏈表*/ Student*p;char c='0' head=NULL; while(c!='n') /*循環(huán)輸入,建立鏈表*/ p=(Student*)malloc(sizeof(Student); /*

16、為p申請內(nèi)存空間*/ printf("Please insert one record:n"); scanf("%s%f",p->Name,&p->Score); /*輸入姓名成績*/ p->next=NULL; /*尾結(jié)點的處理*/ head=Insert(head,p); /*調(diào)用Insert函數(shù)*/ printf("Input next record?<y/n>"); /*詢問是否輸入下一條記錄*/ c=getchar(); c=getchar(); return(head); /*返回頭指

17、針變量*/void Display(Student*head) /*顯示所有姓名成績,每十個暫停一次*/ Student*p; /*通過指針p進(jìn)行訪問*/ int i=0; p=head; /*令p指向頭指針*/ if(p=NULL) printf("the data is emptyn"); while(p!=NULL) /*循環(huán)輸出*/ if(i%10=0) printf("tNametScoren"); /*輸出表頭*/ printf("t%st%fn",p->Name,p->Score); /*輸出姓名成績*/ p

18、=p->next; i+; if(i%10=0) /*控制行數(shù)*/ system("pause");system("cls"); Student*Insert(Student*head,Student*s) /*按名字序插入記錄s,返回鏈表頭指針*/ Student *p,*p1,*p2; p1=head; p=s; if(head=NULL) head=p; p->next=NULL; /*頭指針為空則把p傳給頭指針*/ return(head); p2=p1=head; while (strcmp(p->Name,p1->Na

19、me)>0 && p1->next!=NULL) p2=p1; p1=p1->next; if(strcmp(p->Name,p1->Name)<=0) p->next=p1; if(head=p1) p->next=p1;head=p; /*插在鏈表首部*/ else p->next=p1;p2->next=p; /*插在鏈表中間*/ else p1->next=p; p->next=NULL; /*插在鏈表尾結(jié)點之后*/ return(head); /*返回頭指針變量*/ Student*insert_

20、a_record() /*輸入姓名成績,調(diào)用insert做姓名有序插入*/ Student *p; p=(Student*)malloc(sizeof(Student); /*為p申請內(nèi)存空間*/ printf("Please input the inserted record:"); scanf("%s%f",p->Name,&p->Score); /*輸入姓名成績*/ head=Insert(head,p); /*調(diào)用Insert函數(shù)作有序插入*/ printf("The record has been successf

21、ully inserted.n"); /*輸出插入成功信息*/ system("pause"); return(head); /*返回頭指針變量*/Student*Delete(Student*head,char name) /*刪除姓名為name的記錄* / Student *p1,*p2;char c; if(head=NULL) /*原鏈表為空鏈表*/ printf("The Data is empty.n"); /*提示鏈表為空*/ return(head); p1=head; while(strcmp(name,p1->Name

22、)!=0&&p1->next!=NULL) /*循環(huán)查找待刪除結(jié)點*/ p2=p1;p1=p1->next; /*p1指向后一個結(jié)點,p2在p1之前*/ if(strcmp(name,p1->Name)=0) /*找到待刪除結(jié)點*/ printf("Really Delete?(y/n)"); c=getchar(); c=getchar(); if(c!='y') return(head); if(p1=head) head=p1->next; else p2->next=p1->next; printf

23、("The record has been successfully deleted.n"); /*輸出刪除成功信息*/ else printf("The record has been unsuccessfully deleted.n"); return(head);Student*Delete_a_record() /*按姓名刪除記錄*/ char name20; printf("Please input the deleted record:"); scanf("%s",name); head=Delete(

24、head,name); /*調(diào)用Delete函數(shù)進(jìn)行刪除*/ system("pause"); return(head);Student*Query(Student*head,char name) /*查找姓名為name的記錄*/Student *p1,*p2; if(head=NULL) printf("The Data is empty.n"); return(head); p1=head; while(strcmp(name,p1->Name)!=0&&p1->next!=NULL) /*按姓名進(jìn)行循環(huán)查找*/ p1=p

25、1->next; if(strcmp(name,p1->Name)=0) /*若找到姓名相同記錄*/ printf("The Data has been found.n"); return(p1); else printf("The Data has not been found.n");return(NULL);Student*Query_a_record(Student *head) /*輸入姓名調(diào)用Query查找記錄*/ Student*p;char name20; printf("Please input a record:

26、"); scanf("%s",name); p=Query(head,name); /*調(diào)用函數(shù)Query進(jìn)行查找*/ if (p!=NULL)printf("Name: %snScore:%fn",p->Name, p->Score);system("pause");Student*AddfromText(Student*head) /*從文件添加數(shù)據(jù)*/ FILE *fp; /*存放正文文件文件的地址*/ Student*p; /*通過指針p訪問文件中所有結(jié)點記錄*/ char name20; printf(

27、"Please input the filename:n"); scanf("%s",name); fp=fopen(name,"r"); /*打開正文文件*/ if (fp=NULL) printf("The file does not exist!n"); else int i=0,n; fscanf(fp,"%d",&n); while(i<n) p=(Student *)malloc(sizeof(Student); /*為p分配內(nèi)存空間*/ fscanf(fp,"

28、;%s%f",p->Name,&p->Score); /*從文件中添加記錄*/ head=Insert(head,p); /*調(diào)用Insert進(jìn)行有序插入*/ i+; printf("Job completedn"); fclose(fp); /*關(guān)閉正文文件*/ return(head);int WritetoText(Student*head) /*將鏈表中的結(jié)點記錄寫入文件*/ FILE *fp;Student*p; /*通過指針變量p訪問所有鏈表結(jié)點*/char name20;p=head; printf("Please in

29、put the filename:n"); scanf("%s",name); fp=fopen(name,"w"); fprintf(fp,"tnametscoren"); while(p!=NULL) /*循環(huán)輸出姓名成績*/ fprintf(fp,"t%st%fn",p->Name,p->Score); p=p->next; fclose(fp); /*關(guān)閉正文文件*/int Quit(Student*head) Student *p; while(head!=NULL) p=he

30、ad; head=head->next; free(p); 四、函數(shù)的調(diào)用關(guān)系圖mainCreate ListDisplayInsert_a_recordAddfromTextInsertWritetotextDeleteSameDelete_a_recordDeleteMemu_selectReverseQuery_a_recordQueryQuit五、測試流程及典型測試數(shù)據(jù)1Create List2Display All Record3Insert a Record4Delete a Record5Query6Add Records from a Text File7Write to Text Fil

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論