編程實(shí)習(xí)報(bào)告精選楊宇超_第1頁(yè)
編程實(shí)習(xí)報(bào)告精選楊宇超_第2頁(yè)
編程實(shí)習(xí)報(bào)告精選楊宇超_第3頁(yè)
編程實(shí)習(xí)報(bào)告精選楊宇超_第4頁(yè)
編程實(shí)習(xí)報(bào)告精選楊宇超_第5頁(yè)
已閱讀5頁(yè),還剩6頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)專心-專注-專業(yè)精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)編程實(shí)習(xí)報(bào)告(2009屆)題目:用字符串指針實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)姓名:楊宇超學(xué)號(hào):學(xué)院:自動(dòng)化學(xué)院班級(jí):指導(dǎo)教師:席旭剛高云園Timeyyyy年M月d日2011年7月5日2011年7月5日摘要:本文介紹運(yùn)用C語(yǔ)言中單用字符串指針實(shí)現(xiàn)一個(gè)學(xué)生信息管理工具軟件,主要功能有:add、show、sort、Delete。程序由七個(gè)子函數(shù)(creat、edit、extra、imdelete、print、save、)和一個(gè)主函數(shù)(main)組成。關(guān)鍵字:C語(yǔ)言、用字符串指針、學(xué)生信息

2、一、任務(wù)要求(用字符串指針實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),具體完成的功能見(jiàn)可執(zhí)行程序Student.exe。完成函數(shù)voidDeleteStudent(char*students,int*marks);voidSortClass(char*students,int*marks);voidShowClass(char*students,int*marks);voidEditMarks(char*students,int*marks);二、詳細(xì)設(shè)計(jì)(分析各函數(shù)的功能,設(shè)計(jì)各函數(shù)的處理過(guò)程及其流程圖)voidInsertStudent(char*students,int*marks);插入學(xué)生姓名基本想法:先

3、讀取學(xué)生姓名,判斷指針是否為空,為空則建立內(nèi)存。否則再開(kāi)拓新的內(nèi)存空間,然后將讀取的學(xué)生與名單一一比較,若相同,則顯示已存在該學(xué)生,若沒(méi)有,則開(kāi)拓內(nèi)存給新到的學(xué)生,并對(duì)名單進(jìn)行排序,最后釋放內(nèi)存。voidDeleteStudent(char*students,int*marks);刪除學(xué)生信息基本想法:先讀取要?jiǎng)h除的學(xué)生姓名,將讀入的學(xué)生姓名與已存在的學(xué)生進(jìn)行比較,如相同,記住該學(xué)生所在位置,判斷該學(xué)生下一個(gè)是否為空,若為空,就可以直接把該學(xué)生內(nèi)存釋放掉。若不是,則用一個(gè)while將后面的所有學(xué)生向前移一位直到NULL,再釋放最后的內(nèi)存,最后再釋放學(xué)生姓名的那個(gè)內(nèi)存voidSortClass(

4、char*students,int*marks);排序基本想法:采用冒泡法來(lái)進(jìn)行排序。N次排序先進(jìn)行n-1次比大小,找到最小的,與第一個(gè)交換,再進(jìn)行n-2次。voidShowClass(char*students,int*marks);顯示所有學(xué)生信息基本想法:直接用printf輸出,voidEditMarks(char*students,int*marks);編輯學(xué)生成績(jī)基本想法:先讀取要編輯的學(xué)生姓名,然后與所有的學(xué)生姓名進(jìn)行比較,判斷是否在名單內(nèi),若不在,就輸出不在,否則就再讀取該學(xué)生的5個(gè)成績(jī)到marks三、編碼實(shí)現(xiàn)#include#include#includevoidInsertS

5、tudent(char*students,int*marks);voidDeleteStudent(char*students,int*marks);voidSortClass(char*students,int*marks);voidShowClass(char*students,int*marks);voidEditMarks(char*students,int*marks);char*ReadLine();#definemerror(a)printf(memoryallocationerror%dn,a);exit(1);/*functionmain-*/intmain()char*st

6、udents=NULL;int*marks=NULL;charline100;intmenu;while(1)printf(Enter(1)toAddaStudenttoClassListn(2)toDeleteaStudentfromClassListn(3)toshowClassListn(4)toEditmarksofaStudentn(5)toQuitn);fflush(stdout);gets(line);if(sscanf(line,%d,&menu)!=1)printf(incorrectentry);continue;if(menu5)printf(incorrectselec

7、tion);continue;if(menu=1)InsertStudent(&students,&marks);elseif(menu=2)DeleteStudent(&students,&marks);elseif(menu=3)ShowClass(students,marks);elseif(menu=4)EditMarks(students,marks);elsebreak;/*endwhile*/return0;/readhere/*endmain*/*functionInsertStudent-*/*Thisfunctionpromptstheusertoenteranewstud

8、entname.ThepreciseformofthepromptisEnterStudentNametobeaddedtoClassList:Thenthisfunctionreadsthenamefromstandardinput.Butitmustbeabletoreadanynameofanysize!Thusyouhavetoallocatememory(usingmalloc()toreadtheinputinto,andifitisnotbigenough,mustuserealloc()toextendit.Youarenotallowedtoallocatemorethan1

9、0bytesinoneallocationorextendyoursegmentbymorethan10bytesinonecalltorealloc().Donotforgettodeallocatethememorybeforeexitingthefunction!Oncethefunctionhasthenametobeentered,ittraversesthearraystudenttofindoutifthenameisalreadythere.Ifso,thefunctiondisplaysawarningmessagestudentxxxalreadyintheclasslis

10、tandterminates(wherexxxisthenameofthestudent).Ifthenameisnotinthearray,thearraystudentsisextendedbyoneitem(usingrealloc()andacopyofthestringwiththenameisplacedthere.Alsothearraymarksisextendedbyonerow(usingrealloc()andallfivemarksinthatrowaresetto-1.ThenupdatedarraysstudentsandmarksarepassedtoSortCl

11、ass()sotheycanbesortedalphabetically,asyouarerequiredtokeeptheclasslistandthelistofmarksinalphabeticalorder.Notethatbothstudentsandmarksarepassedtothisfunctionbyreference,forthisfunctiononoccasionsmustbeabletomodifythevaluestoredinthepointerstudent(inmain()-whenputtingtheretheveryfirststudentorwhenr

12、ealloc()possiblymovesthearrayinthememorysomewhereelse.Thesameappliestomarks.*/voidInsertStudent(char*students,int*marks)intfound,i;char*students1;int*marks1;students1=*students;marks1=*marks;char*name;printf(EnterStudentNametobeaddedtoClassList:n);fflush(stdout);name=ReadLine();if(students1=NULL)if(

13、students1=(char*)malloc(2*sizeof(char*)=NULL)merror(2);if(students10=(char*)malloc(strlen(name)+1)=NULL)merror(3);strcpy(students10,name);students11=NULL;if(marks1=(int*)malloc(2*sizeof(int*)=NULL)merror(4);if(marks10=(int*)malloc(5*sizeof(int)=NULL)merror(5);marks100=marks101=marks102=marks103=mark

14、s104=0;marks11=NULL;*students=students1;*marks=marks1;return;/*dowehavethestudentyet*/for(found=i=0;students1i!=NULL;i+)if(strcmp(students1i,name)=0)found=1;break;if(found)printf(student%salreadyintheclasslistn,name);return;/*soitisnotintheclasslistyet*/students1=(char*)realloc(void*)students1,(i+2)

15、*sizeof(char*);if(students1=NULL)merror(6);if(students1i=(char*)malloc(strlen(name)+1)=NULL)merror(7);strcpy(students1i,name);students1i+1=NULL;free(void*)name);if(marks1=(int*)realloc(void*)marks1,sizeof(int*)*(i+2)=NULL)merror(8);if(marks1i=(int*)malloc(5*sizeof(int)=NULL)merror(9);marks1i0=marks1

16、i1=marks1i2=marks1i3=marks1i4=0;marks1i+1=NULL;SortClass(students1,marks1);*students=students1;*marks=marks1;/*functionDeleteStudent-*/*Thisfunctionpromptstheusertoenterastudentnametobedeleted.ThepreciseformofthepromptisEnterStudentNametobedeletedfromClassList:Thenthisfunctionreadsthenamefromstandar

17、dinput.Butitmustbeabletoreadanynameofanysize!Thusyouhavetoallocatememory(usingmalloc()toreadtheinputinto,andifitisnotbigenough,mustuserealloc()toextendit.Youarenotallowedtoallocatemorethan10bytesinoneallocationorextendyoursegmentbymorethan10bytesinonecalltorealloc().Donotforgettodeallocatethememoryb

18、eforeexitingthefunction!NotethatthispartofthecodeisthesameasinInsertStudent(),soyoucaneithercopyitorwriteafunctionthatwoulddoitforboth.Oncethefunctionhasthenametobedeleted,ittraversesthearraystudenttofindoutifitisthere.Ifnot,awarningmessageisdisplayedstudentxxxnotintheclasslistandthefunctionterminat

19、es.Ifitisfound(sayitsisstudentsd),adynamicone-dimensionalarrayoneitemshorterthanstudentsiscreatedandthestringsfromthearraystudentsareunhookedfromthearraysstudentsandhookedtothenewarraywiththeexceptionofthestringtobedeleted(dontforgetthatthedeletedstringmustbedeallocated).Nowdeallocatethearraystudent

20、sandmakethepointerstudentpointtothenewarray.Asyoucanseewehavethesamearrayofstringasbefore,butwithonestringdeleted.Youmustdothesamewiththearraymarks,foryoumustdeletecompletelyd-throw(soitdidcorrespondtostudentdwhichwedeleted).Notethatbothstudentsandmarksarepassedtothisfunctionbyreference,forthisfunct

21、iononmustbeabletomodifythevaluestoredinthepointerstudent(inmain()-whenastudentnameisdeleted.Thesameapliestomarks.*/voidDeleteStudent(char*students,int*marks)intfound,i,j,a,b;char*studentsx;int*marksx;char*students1;int*marks1;char*namex;char*name;printf(EnterStudentNametobedeletedfromClassList:n);ff

22、lush(stdout);name=ReadLine();namex=name;studentsx=*students;marksx=*marks;/*dowehavethestudentyet*/for(found=i=0;studentsxi!=NULL;i+)if(strcmp(studentsxi,name)=0)found=1;j=i;b=i;/*givegroopspace*/students1=(char*)malloc(b+1)*sizeof(char*);marks1=(int*)malloc(b+1)*sizeof(int*);if(found)for(i=0;ij;i+)

23、students1i=studentsxi;marks1i=marksxi;for(i=j;i=0)temp=studentsi;studentsi=studentsi+1;studentsi+1=temp;temp1=marksi;marksi=marksi+1;marksi+1=temp1;/*functionShowClass-*/*Inthisfunctionyoudisplayonthescreeneachstudentintheclassandhis/her5marksonaline.Ifthemarkis-1displayaspaceinstead.Afterthenameofs

24、tudentdisplay:andseparatethemarksby,Notethatbothstudentsandmarksarepassedtothisfunctionbyvalue,forthisfunctiondoesnotneedtomodifythevaluestoredinthepointerstudent.Thesameappliestomarks.*/voidShowClass(char*students,int*marks)inti;for(i=0;studentsi!=NULL;i+)printf(%s,%d,%d,%d,%d,%dn,studentsi,marksi0

25、,marksi1,marksi2,marksi3,marksi4);/*functionEditMarks-*/*Thisfunctionpromptstheusertoenterastudentnamewhosemarksaretobeedited.ThepreciseformofthepromptisEnterStudentNamewhosemarksaretobeedited:Thenthisfunctionreadsthenamefromstandardinput.Butitmustbeabletoreadanynameofanysize!Thusyouhavetoallocateme

26、mory(usingmalloc()toreadtheinputinto,andifitisnotbigenough,mustuserealloc()toextendit.Youarenotallowedtoallocatemorethan10bytesinoneallocationorextendyoursegmentbymorethan10bytesinonecalltorealloc().Donotforgettodeallocatethememorybeforeexitingthefunction!NotethatthispartofthecodeisthesameasinInsert

27、Student()andDeleteStudent(),soyoucaneithercopyitorwriteafunctionthatwoulddoitforallthree.Ifthestudentisnotfoundinthearraystudents,awarningmessagestudentxxxnotintheClassListisdisplayedonthescreenandthefunctionterminates.Ifitisfound,thestudentsnamefollowedby:andhis/herfivemarksaredisplayed(again,-1mus

28、tbeshownasaspace,asinShowClass().Letusassumethatthestudentinquestionhasmarks10,-1,20,15,5Thenthefunctiondisplaysaprompttotheuserforthefirstmarktoleavethe1stmark10unchangedpressotherwisetypethenewmarkandpressandreadstheusersreplyusinggets()intoacharacterarraysofsize80.Thenitusessscanf()onthatarraytoe

29、xtracttheresponse.Thenthefunctiondisplaysaprompttotheuserforthesecondmarktoleavethe2ndmarkunchangedpressotherwisetypethenewmarkandpressandreadstheusersreplyusinggets()intoacharacterarraysofsize80.Thenitusessscanf()onthatarraytoextracttheresponse.Etc.forthethird,fourth,andfifth.Notethatbothstudentsan

30、dmarksarepassedtothisfunctionbyvalue,forthisfunctiondoesnotneedtomodifythevaluestoredinthepointerstudent.Thesameappliestomarks.*/voidEditMarks(char*students,int*marks)intfound,i,j;char*name;printf(EnterStudentNametobeaddedmarksin:n);fflush(stdout);name=ReadLine();/*dowehavethestudentyet*/for(found=i

31、=0;studentsi!=NULL;i+)if(strcmp(studentsi,name)=0)found=1;j=i;break;if(found)printf(pleaseinput%ssmarksoffiveclassesdividedwithan,n,name);scanf(%d,%d,%d,%d,%d,&marksj0,&marksj1,&marksj2,&marksj3,&marksj4);return;elseprintf(wrongstudentnamen);return;/*fuctionreadline*/char*ReadLine()char*buf;inti,j,size;if(buf=(char*)malloc(10)=NULL)merror(0);size=10;j=-1;while(

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論