




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
/*這是個(gè)學(xué)生通訊錄管理程序,可以進(jìn)行添加、刪除、查找(按學(xué)號(hào)或姓名!、排序和列表輸出等操作*//*TYPEIN代替getch()輸入,使輸入同時(shí)在屏幕上顯示信息*/#defineTYPEINputch(getch())#include"stdio.h"#include"conio.h"#include"ctype.h"#include"malloc.h"#include"stdlib.h"#include"string.h"/*學(xué)生資料*/structstudent/*定義學(xué)生資料結(jié)構(gòu)體*/{charcnm[20];/*班級(jí)序號(hào)classnumber*/charnam[20];/*姓名name*/chardom[20];/*寢室dorm*/charnat[20];/*籍貫nativeplace*/charadd[40];/*家庭地址address*/chartel[20];/*電話telephone*/charqq[20];/*qq*/}*stu[1000],*tmp;/*定義一千個(gè)學(xué)生的全局?jǐn)?shù)組,以及一個(gè)用于交換數(shù)據(jù)的臨時(shí)指針和一個(gè)用于展示的指針*/FILE*fp=0;/*全局文件指針*//*數(shù)據(jù)狀態(tài)*/intcou,ord,syc;/*最后一個(gè)有效數(shù)據(jù)對(duì)應(yīng)的數(shù)組位置,及數(shù)組是否有序1為有序,文檔是否同步1為同步*//*函數(shù)聲明*/voidadd();voiddel();voidfin();voidfso();intfind(char*tar);voidlis();/*對(duì)函數(shù)進(jìn)行聲明*/char*inp(int);voidsort(int);voidshow(intn);voiditf();voidini();voidminitf();voiddatacopy(structstudent*in,structstudent*out);voiduniversal_search();voiduniversal_sort();voidimport();/*函數(shù)指針,實(shí)現(xiàn)被調(diào)函數(shù)對(duì)調(diào)用函數(shù)的處理*/char*(*loop)(int);/*導(dǎo)入默認(rèn)其他數(shù)據(jù)文件*/voidimport(){intn=5;char*imp,*a="\nerroronopen";imp=(char*)malloc(sizeof(char)*80);puts("\npleaseinputdata'saddresslikethis'd:\\data.txt'\n\n");scanf("%s",imp);/*打開(kāi)*/if((fp=fopen(imp,"r"))==NULL){/*打開(kāi)文件若文件不存在則直接返回*/gotoxy(30,12);printf("Thedatafileisnotexist\n");TYPEIN;return;}while(EOF!=n){stu[++cou]=(structstudent*)malloc(sizeof(structstudent));n=fscanf(fp,"%s\t%s\t%s\t%s\t%s\t%s\t%s\n",stu[cou]->cnm,stu[cou]->nam,/*把學(xué)生資料存入數(shù)組*/stu[cou]->dom,stu[cou]->nat,stu[cou]->add,stu[cou]->tel,stu[cou]->qq);}cou--;/*最后一行為空,刪除*/gotoxy(20,12);printf("Thedatafilehasbeenreadsuccessfully");TYPEIN;(*loop)(0);}/*通用搜索,可以搜索符合任意條件的所有信息。但效率低下,不適用于大型數(shù)據(jù)*/voiduniversal_search(){while(1){char*cmp[7],tar[40];intn,i=0,sta;for(n=0;n<7;cmp[n++]=(char*)malloc(sizeof(char)*40));/*界面及功能說(shuō)明*/minitf();gotoxy(55,24);printf("press'Esc'togoback");gotoxy(30,4);puts("UniversalSearch\n");printf("pleaseinputanypieceofthestudents'information\n\n");/*anditwillshowthecompleteinformationofallthestudentswhomatchyourcondition*//*BUTitwilltakeyoualongtimetowaitifyourdatasfileisbiggerthan100MB.ANDthisprogramhasnotmeantojumpoutofit.*/strcpy(tar,(*loop)(2));/*輸入*/fso('s');/*同步文件*//*我自己都沒(méi)想到這么漂亮~~~*/minitf();printf("Number\tName\tDorm\tNative\tAddress\tTelephone\tQQ\n\n");while(EOF!=fscanf(fp,"%s\t%s\t%s\t%s\t%s\t%s\t%s\n",cmp[0],cmp[1],cmp[2],cmp[3],cmp[4],cmp[5],cmp[6]))/*讀文件*/for(n=0;n<7;n++)/*對(duì)比*/if(0==strcmp(cmp[n],tar)){printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",cmp[0],cmp[1],cmp[2],cmp[3],cmp[4],cmp[5],cmp[6]);i++;if(i>15){i=0;(*loop)(1);minitf();printf("Number\tName\tDorm\tNative\tAddress\tTelephone\tQQ\n\n");}}puts("\nThat'sall");(*loop)(1);}}/*通用排序,實(shí)現(xiàn)按任意項(xiàng)目排序*/voiduniversal_sort(){charcmd;minitf();gotoxy(30,4);puts("UniversalSort\n");printf("Sortby\n");puts("1forNumber\n2forName\n3forDorm\n4forNative\n5forAddress\n6forTelephone\n7forQQ");cmd=*(*loop)(1);puts("\nSorting...\n");sort(cmd-'0');puts("thedatahasbeensortedasyoumean\n");*(*loop)(1);}/*據(jù)說(shuō)主函數(shù)的函數(shù)指針不能亂用,所以讓位給inp函數(shù)*/voidmain(){inp(-1);}goodbye(){/*退出程序前保存、GoodBye*/fso('s');fclose(fp);clrscr();gotoxy(30,12);puts("THANKYOUFORTESTING");TYPEIN;exit(1);}/*輸入,及對(duì)不同輸入的不同處理。goback為-1時(shí)進(jìn)行初始化操作,,0時(shí)不返回調(diào)用函數(shù),1時(shí)為命令控制,2為需要返回輸入字符串*/staticchar*inp(intgoback){chartar[40],ch;/*用于儲(chǔ)存輸入字符串*/intn=0;if(-1==goback){/*進(jìn)行初始化*/cou=-1;syc=1;ord=1;goback=0;/*變量初始化*/loop=inp;fso('o');/*打開(kāi)文件*/}while(1){if(0==goback){/*刷新界面*/itf();printf("\npleaseinputyourcommand..\n");}ch=getch();/*返回被調(diào)函數(shù)*/if(goback){/*無(wú)法修改首字符*/n=-1;/*n為數(shù)組中末尾有效字符的位置*/do{putch(ch);switch(tar[++n]=ch){/*若輸入首字符為下列字母則進(jìn)入相應(yīng)函數(shù),否則把字符地址返回調(diào)用函數(shù)*/case27:goback=0;(*inp)(0);break;/*返回主菜單,27為Esc的ASCII碼*//*case'\t':return"\n";break;若按Tab,返回空輸入*/case13:break;/*若按回車,返回空輸入。13為getch()獲得的回車的值,與'\n'不同,我也沒(méi)辦法。。。*/case'':ch='\n';break;/*空格當(dāng)回車處理*/case8:putch('');putch(8);n--;ch=getch();break;/*退格*/default:/*把輸入存如字符串*/if(1==goback)returntar;ch=getch();break;/*把輸入字符串賦值給tar[40],返回*/}}while(13!=ch);tar[n+1]='\0';returntar;}/*返回主菜單*/else{switch(ch){/*若輸入首字符為下列字母則進(jìn)入相應(yīng)函數(shù),否則把字符地址返回調(diào)用函數(shù)*/case'a':add();continue;break;case'd':del();continue;break;case'f':fin();continue;break;case's':fso('s');puts("\nthefilehasbeensaved\n");getch();continue;break;case'l':lis();continue;break;case'i':import();break;case'u':universal_search();continue;break;case'o':universal_sort();continue;break;case13:continue;break;/*對(duì)回車忽略*/case'e':goodbye();break;default:puts("\nUnlegalCommand\n");printf("pleaseinputyourcommand..\n");break;}}}}/*精簡(jiǎn)界面*/voidminitf(){clrscr();/*清屏*/gotoxy(1,1);/*光標(biāo)位置*/printf("|ContactsManagementSystem|");gotoxy(60,2);printf("developedbykdjf");/*版權(quán)*/gotoxy(1,4);}/*繪制簡(jiǎn)單界面*/voiditf(){/*若不聲明字符串直接用put()輸出,會(huì)造成界面混亂。因其輸出內(nèi)容儲(chǔ)存在緩沖區(qū),程序運(yùn)行過(guò)程會(huì)被改寫*/char*a="|'a'toadddata",*d="|'d'todeletedata",*f="|'f'tofinddata",*l="|'l'toshowthelist",*u="|'u'universalsearch",*e="|'e'tosaveandexit",*o="|'o'universalsort",*s="|'s'tosave",*i="|'i'toimportdata";minitf();/*簡(jiǎn)單框架和提示信息*/gotoxy(55,16);printf("%s",i);gotoxy(55,17);printf("%s",a);gotoxy(55,18);printf("%s",d);gotoxy(55,19);printf("%s",f);gotoxy(55,20);printf("%s",l);gotoxy(55,21);printf("%s",u);gotoxy(55,22);printf("%s",o);gotoxy(55,23);printf("%s",s);gotoxy(55,24);printf("%s",e);gotoxy(1,4);}/*同步txt文件,ch為's'時(shí)為保存,'o'為打開(kāi)*/voidfso(charch){intn=5;/*在下文中用于循環(huán)計(jì)數(shù)和儲(chǔ)存數(shù)據(jù)*//*保存*/if('s'==ch)/*若不同步且要求保存則寫入文件(寫文件并關(guān)閉)*/{fclose(fp);if(1!=ord)/*若數(shù)組無(wú)序則進(jìn)行排序*/sort(1);/*打開(kāi)文件及對(duì)錯(cuò)誤處理,為減少儲(chǔ)存過(guò)程中的意外造成的影響,使用讀寫方式而非只寫方式打開(kāi)*/if((fp=fopen("stu_list.txt","wt++"))==NULL){printf("Cannotopenfilestrikeanykeyexit!");(*loop)(1);exit(1);}for(n=0;n<=cou;n++)fprintf(fp,"%s\t%s\t%s\t%s\t%s\t%s\t%s\n",stu[n]->cnm,stu[n]->nam,stu[n]->dom,stu[n]->nat,stu[n]->add,stu[n]->tel,stu[n]->qq);/*寫入*/fclose(fp);syc=1;if((fp=fopen("stu_list.txt","r"))==NULL){/*打開(kāi)文件*/printf("Thedatafileisnotsaved");TYPEIN;clrscr();gotoxy(30,12);printf("Thedatafileissaved");TYPEIN;return;}return;}/*打開(kāi)*/if((fp=fopen("stu_list.txt","r"))==NULL){/*打開(kāi)文件若文件不存在則直接返回*/gotoxy(30,12);printf("Thedatafileisnotexist\n");TYPEIN;return;}while(EOF!=n){stu[++cou]=(structstudent*)malloc(sizeof(structstudent));n=fscanf(fp,"%s\t%s\t%s\t%s\t%s\t%s\t%s\n",stu[cou]->cnm,stu[cou]->nam,/*把資料存數(shù)組*/stu[cou]->dom,stu[cou]->nat,stu[cou]->add,stu[cou]->tel,stu[cou]->qq);}cou--;/*最后一行為空,刪除*/gotoxy(20,12);printf("Thedatafilehasbeenreadsuccessfully");TYPEIN;(*loop)(0);}/*刪除數(shù)據(jù)。使用find函數(shù)找出待刪除數(shù)據(jù)對(duì)應(yīng)的指針位置,將其與最后一個(gè)指針交換,并將數(shù)據(jù)個(gè)數(shù)減一*/voiddel(){while(1){intn;/*儲(chǔ)存將刪除的數(shù)據(jù)位置*/chartar[40],cd;/*tar儲(chǔ)存輸入的學(xué)生資料字符串,cd儲(chǔ)存命令*/minitf();gotoxy(55,24);printf("press'Esc'togoback");gotoxy(35,4);/*界面*/printf("DELETE!!!\n\nschoolnumber\n");strcpy(tar,(*loop)(2));n=find(tar);/*查找并顯示待刪除學(xué)生完整信息*/printf("\nNumber\tName\tDorm\tNative\tAddress\tTelephone\tQQ\n");show(n);puts("\n");printf("press'y'todelete\n\n");/*確認(rèn)刪除*/if('y'==getch()){/*刪除*/free(stu[n]);/*將要?jiǎng)h除的內(nèi)存空間釋放*/stu[n]=stu[cou--];/*將最后一個(gè)有效指針賦值給待刪除數(shù)據(jù)對(duì)應(yīng)的指針,并將有效指針個(gè)數(shù)減一*/printf("Thedataisdeleted\n");(*loop)(1);ord=0;/*數(shù)組變?yōu)闊o(wú)序*/syc=0;/*文檔有不同步*/return;}else{/*不刪除的情況*/printf("Thedataisnotdeleted\n");(*loop)(1);return;}}}/*對(duì)數(shù)組進(jìn)行希爾排序排序。/voidsort(intn){intgap,i,j,sortby=0;if(1!=n){switch(n){case2:sortby=stu[0]->nam-stu[0]->cnm;break;case3:sortby=stu[0]->dom-stu[0]->cnm;break;case4:sortby=stu[0]->nat-stu[0]->cnm;break;case5:sortby=stu[0]->add-stu[0]->cnm;break;case6:sortby=stu[0]->tel-stu[0]->cnm;break;case7:sortby=stu[0]->qq-stu[0]->cnm;break;default:puts("\nUnlegalCommand\n");break;}}for(gap=cou/2;gap>0;gap/=2)/*每次間隔折半至1*/for(i=gap;i<=cou;i++)/*第一進(jìn)步從gap遞增*/for(j=i-gap;j>=0&&strcmp(stu[j]->cnm+sortby,stu[j+gap]->cnm+sortby)>0;j-=gap){/*第二進(jìn)步從第一進(jìn)步-gap,對(duì)以gap為間隔的數(shù)據(jù)倒序?qū)Ρ冉粨Q*/tmp=stu[j];stu[j]=stu[j+gap];stu[j+gap]=tmp;}if(n=1)ord=1;elseord=0;}/*按姓名查找學(xué)生*/voidfin(){while(1){chartar[40];minitf();gotoxy(55,24);printf("press'Esc'togoback");gotoxy(38,4);/*界面*/printf("SEARCH\n\nschoolnumber\n");strcpy(tar,(*loop)(2));;/*輸入*/printf("\nNumber\tName\tDorm\tNative\tAddress\tTelephone\tQQ\n");show(find(tar));/*顯示*/(*loop)(1));}}/*輸出當(dāng)前用于展示的學(xué)生指針?biāo)鶎?duì)應(yīng)的數(shù)據(jù),處理數(shù)據(jù)不存在的情況*/voidshow(intn){if(n>cou||n<0){/*判斷輸入變量是否正常*/printf("thedatacannotbefound\n");(*loop)(1);return;}printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",stu[n]->cnm,stu[n]->nam,stu[n]->dom,stu[n]->nat,stu[n]->add,stu[n]->tel,stu[n]->qq);}intfind(char*tar)/*對(duì)輸入信息查找相應(yīng)學(xué)生數(shù)據(jù)。返回當(dāng)前學(xué)生對(duì)于數(shù)組的位置,-1為未找到*/{intlow=0,hig=cou,mid;/*midhif為二分法查找中的上下限,mid為當(dāng)前查找位置*/if(1!=ord)/*查找前進(jìn)行選擇排序*/sort(1);/*輸入為學(xué)號(hào)的查找辦法*/if(isdigit(tar[0])){while(1){/*用于二分法查找*/mid=(low+hig)/2;/*查找上下限的中間值*//*用字符串也可比較數(shù)字大小,但對(duì)兩數(shù)字位數(shù)要求相同*/if(0==strcmp(tar,stu[mid]->cnm))/*找中*/returnmid;elseif(strcmp(tar,stu[mid]->cnm)>0)/*待查值比當(dāng)前大,把下限改為當(dāng)前值加一*/low=mid+1;elseif(strcmp(tar,stu[mid]->cnm)<0)/*待查值比當(dāng)前小,把上限改為當(dāng)前值減一*/hig=mid-1;if(low>hig)break;/*找不到時(shí)跳出無(wú)限循環(huán)*/}}return-1;}/*列表輸出*/voidlis(){intn,i=0;if(1!=ord)/*輸出前進(jìn)行選擇排序*/sort(1);minitf();printf("Number\tName\tDorm\tNative\tAddress\tTelephone\tQQ\n\n");for(n=0;n<=cou;n++)/*一次顯示十五個(gè)*/if(i++<15)show(n);else{puts("\npressanybut'Esc'toshownextfifteenstudent'sdata");(*loop)(1);i=0;minitf();printf("Number\tName\tDorm\tNative\tAddress\tTelephone\tQQ\n\n");}(*loop)(1);return;}/*增加檔案*/voidadd(){while(1){minitf();gotoxy(55,24);printf("press'Esc'togoback");gotoxy(35,4);puts("ADDING...\n");stu[++cou]=(structstudent*)malloc(sizeof(structstudent));/*計(jì)數(shù)變量加一,且將新數(shù)據(jù)賦于申請(qǐng)的空間*//*錄入數(shù)據(jù)。賦值中只能傳遞值,若傳遞指針則該地址數(shù)據(jù)會(huì)被下次輸入洗掉*/puts("pleaseinputthe\n\nStudentNumber");strcpy(stu[cou]->cnm,(*loop)(2));gotoxy(1,8);puts("\n\nName");strcpy(stu[cou]->nam,(*loop)(2));puts("\n\nDorm");strcpy(stu[cou]->dom,(*loop)(2));puts("\n\nNativePlace");strcpy(stu[cou]->nat,(*loop)(2));puts("\n\nAddress");strcpy(stu[cou]->add,(*loop)(2));puts("\n\nTelephone");strcpy(stu[cou]->tel,(*loop)(2));puts("\n\nQQ");strcpy(stu[cou]->qq,(*loop)(2));/*確認(rèn)錄入*/
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年高壓液壓柱塞泵馬達(dá)項(xiàng)目發(fā)展計(jì)劃
- 2025年度新能源材料研發(fā)保密與共享合同
- 2025年度綠色建筑項(xiàng)目財(cái)產(chǎn)贈(zèng)與合同
- 2025年(半)干式煙氣脫硫成套設(shè)備項(xiàng)目建議書(shū)
- 2025年度出境領(lǐng)隊(duì)帶團(tuán)操作規(guī)范合同范本
- 水務(wù)生態(tài)保護(hù)規(guī)劃計(jì)劃
- 2025年食品分離機(jī)械項(xiàng)目合作計(jì)劃書(shū)
- 持續(xù)改進(jìn)教學(xué)工作的機(jī)制計(jì)劃
- 高危行業(yè)的安全防控計(jì)劃
- 合理安排急診排班的重要性計(jì)劃
- 靜脈用藥安全輸注藥護(hù)專家指引
- 華住酒店管理制度
- 《婚姻家庭糾紛調(diào)解》課件
- 教育科學(xué)與兒童心理學(xué)
- 2022高速公路隧道工程施工管理標(biāo)準(zhǔn)化手冊(cè)
- 雨水花園設(shè)計(jì)
- 年智慧水廠大數(shù)據(jù)信息化建設(shè)和應(yīng)用方案
- 光伏電纜橋架敷設(shè)施工方案
- 文山-硯山天然氣支線管道工程項(xiàng)目環(huán)境影響報(bào)告書(shū)
- 新選供應(yīng)商初期考察表模板
- 工人工資結(jié)清證明范本
評(píng)論
0/150
提交評(píng)論