數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)_第1頁
數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)_第2頁
數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)_第3頁
數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)_第4頁
數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)1.赫夫曼編碼器設(shè)計(jì)一個利用赫夫曼算法的編碼和譯碼系統(tǒng),重復(fù)地顯示并處理以下項(xiàng)目,直到選擇退出為止。要求:1)將權(quán)值數(shù)據(jù)存放在數(shù)據(jù)文件(文件名為data.txt,位于執(zhí)行程序的當(dāng)前目錄中)2)初始化:鍵盤輸入字符集大小26、26個字符和26個權(quán)值(統(tǒng)計(jì)一篇英文文章中26個字母),建立哈夫曼樹;3)編碼:利用建好的哈夫曼樹生成哈夫曼編碼;4)輸出編碼(首先實(shí)現(xiàn)屏幕輸出,然后實(shí)現(xiàn)文件輸出);5)界面優(yōu)化設(shè)計(jì)。代碼如下:#include<stdio.h>#include<string.h>#include<stdlib.h>#include<malloc.h>#defineN200typedefstructHTNode//結(jié)構(gòu)體{ intWeight; charch; intParent,Lchild,Rchild;}HTNode;typedefchar**HCode;voidSave(intn,HTNode*HT)//把權(quán)值保存到文件{ FILE*fp; inti; if((fp=fopen("data.txt","wb"))==NULL) { printf("cannotopenfile\n"); return; } for(i=0;i<n;i++) if(fwrite(&HT[i].Weight,sizeof(structHTNode),1,fp)!=1) printf("filewriteerror\n"); fclose(fp); system("cls"); printf("保存成功!");}voidCreate_H(intn,intm,HTNode*HT)//建立赫夫曼樹,進(jìn)行編碼{ intw,k,j; charc; for(k=1;k<=m;k++) { if(k<=n) { printf("\n請輸入權(quán)值和字符(用空格隔開):"); scanf("%d",&w); scanf("%c",&c); HT[k].ch=c; HT[k].Weight=w; } elseHT[k].Weight=0; HT[k].Parent=HT[k].Lchild=HT[k].Rchild=0; } intp1,p2,w1,w2; for(k=n+1;k<=m;k++) { p1=0;p2=0; w1=32767;w2=32767; for(j=1;j<=k-1;j++) { if(HT[j].Parent==0) { if(HT[j].Weight<w1)//尋找最小權(quán)值 { w2=w1;p2=p1; w1=HT[j].Weight; p1=j; } elseif(HT[j].Weight<w2) { w2=HT[j].Weight; p2=j; } } } HT[k].Lchild=p1;HT[k].Rchild=p2; HT[k].Weight=HT[p1].Weight+HT[p2].Weight; HT[p1].Parent=k;HT[p2].Parent=k; } printf("輸入成功!");}voidCoding_H(intn,HTNode*HT)//對結(jié)點(diǎn)進(jìn)行譯碼{ intk,sp,fp,p; char*cd; HCodeHC;HC=(HCode)malloc((n+1)*sizeof(char*)); cd=(char*)malloc(n*sizeof(char)); cd[n-1]='\0'; printf("************************\n"); printf("CharCoding\n"); for(k=1;k<=n;k++) { sp=n-1;p=k;fp=HT[k].Parent; for(;fp!=0;p=fp,fp=HT[fp].Parent) if(HT[fp].Lchild==p) cd[--sp]='0'; else cd[--sp]='1'; HC[k]=(char*)malloc((n-sp)*sizeof(char)); strcpy(HC[k],&cd[sp]); printf("%c%s\n",HT[k].ch,HC[k]); } printf("************************\n"); free(cd);}voidRead(intn,HTNode*HT)//從文件中讀出數(shù)據(jù){ inti; FILE*fp; if((fp=fopen("data.txt","rb"))==NULL) { printf("cannotopenfile\n"); exit(0); } for(i=0;i<n;i++) {fread(&HT[i].Weight,sizeof(structHTNode),1,fp); // printf("%d\n",HT[i].Weight); } Coding_H(n,HT); fclose(fp);}voidPrint_H(intm,HTNode*HT)//輸出赫夫曼造樹過程{ intk; printf("************************\n"); printf("NumWeightParLChRCh\n"); for(k=1;k<=m;k++) { printf("%d",k); printf("%d",HT[k].Weight); printf("%d",HT[k].Parent); printf("%d",HT[k].Lchild); printf("%d\n",HT[k].Rchild); } printf("************************\n");}voidDecode(intm,HTNode*HT)//對輸入的電文進(jìn)行譯碼{ inti,j=0; chara[10]; charendflag='2'; i=m; printf("輸入發(fā)送的編碼,以‘2’結(jié)束:"); scanf("%s",&a); printf("譯碼后的字符:"); while(a[j]!='2') { if(a[j]=='0') i=HT[i].Lchild; elsei=HT[i].Rchild; if(HT[i].Lchild==0)//HT[i]是葉結(jié)點(diǎn) { printf("%c",HT[i].ch); i=m;//回到根結(jié)點(diǎn) } j++; } printf("\n"); if(HT[i].Lchild!=0&&a[j]!='2') printf("ERROR");}intmain()//主函數(shù){ intn,m,c; HTNodeHT[N]; do { system("color2f");//運(yùn)行環(huán)境背景顏色. printf("\n\n\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n\t\t"); printf("\n\t\t\t赫夫曼編譯碼系統(tǒng)\t\t\t"); printf("\n\n\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n\t\t"); printf("\n\t\t\t1.輸入權(quán)值、字母\n\t\t\t2.把數(shù)據(jù)寫入文件\n\t\t\t3.輸出赫夫曼編碼表\n\t\t\t"); printf("4.輸出赫夫曼譯碼表\n\t\t\t5.輸入編碼并譯碼.\n\t\t\t6.從文件中讀出數(shù)據(jù)\n\t\t\t7.退出"); printf("\n\n\t\t\t請選擇:"); scanf("%d",&c); switch(c) { case1:system("cls");printf("輸入多少結(jié)點(diǎn):"); scanf("%d",&n);m=2*n-1;Create_H(n,m,HT);break; case2:system("cls");Save(n,HT);break; case3:system("cls");Print_H(m,HT);break; case4:system("cls");Coding_H(n,HT);break; case5:system("cls");Decode(m,HT);break; case6:system("cls");Read(n,HT);break; case7:system("cls");exit(0); } }while(1); return0;}運(yùn)行界面如下:2.學(xué)生成績管理(鏈表實(shí)現(xiàn))要求:實(shí)現(xiàn)如下功能:增加、查找、刪除、輸出、退出。代碼如下:#include<stdio.h>#include<string.h>#include<stdlib.h>typedefstructscore//定義成績信息結(jié)構(gòu)體{ charNumber[20]; charName[20]; charChinese[20]; charEnglish[20]; charMath[20];}score;typedefstructnode_score//定義成績信息鏈表結(jié)點(diǎn),包括數(shù)據(jù)域和指針域{ scoredata; structnode_score*next;}node_score,*p_node_score;p_node_scoreheadScore;//定義鏈表的頭指針為全局變量voidPrintScore(scores)//輸出信息函數(shù){ printf("%10s",s.Number); printf("|%-6s",s.Name); printf("|%-3s",s.Chinese); printf("|%-3s",s.English);printf("|%-3s\n",s.Math);}voidView()//輸出函數(shù){p_node_scorepNodeScore;pNodeScore=headScore; printf("學(xué)號|姓名|語文成績|英語成績|高數(shù)成績\n"); while(pNodeScore!=NULL) { PrintScore(pNodeScore->data);//輸出學(xué)生信息和成績信息 pNodeScore=pNodeScore->next; }}voidAdd(){p_node_scorepNodeScore;//定義一個節(jié)點(diǎn) pNodeScore=(p_node_score)malloc(sizeof(node_score));//為節(jié)點(diǎn)分配存儲空間 printf("請輸入學(xué)號:"); scanf("%s",pNodeScore->data.Number); printf("請輸入姓名:"); scanf("%s",pNodeScore->data.Name); printf("請輸入語文成績:"); scanf("%s",pNodeScore->data.Chinese); printf("請輸入英語成績:"); scanf("%s",pNodeScore->data.English); printf("請輸入高數(shù)成績:"); scanf("%s",pNodeScore->data.Math); if(headScore==NULL) {//如果頭結(jié)點(diǎn)為空 headScore=pNodeScore; pNodeScore->next=NULL; } else {//如果頭結(jié)點(diǎn)不為空 pNodeScore->next=headScore; headScore=pNodeScore;//將頭結(jié)點(diǎn)新結(jié)點(diǎn) }}voidInput(){ intn,i; printf("輸入幾個學(xué)生的數(shù)據(jù):"); scanf("%d",&n); for(i=0;i<n;i++) Add(); printf("輸入成功!");}intDelete(){ p_node_scorepNodeScore,p1;//p1為pNodeScore的前驅(qū)p1=headScore; if(p1==NULL) { printf("成績表中沒有數(shù)據(jù)!請先添加數(shù)據(jù)!\n"); return0; } charDeleteNumber[20];printf("請數(shù)入要刪除的學(xué)生學(xué)號:"); scanf("%s",DeleteNumber); if(strcmp(p1->data.Number,DeleteNumber)==0) {//如果要刪除的結(jié)點(diǎn)在第一個 headScore=p1->next;pNodeScore=p1; printf("學(xué)號為%s的學(xué)生信息已經(jīng)刪除!\n",DeleteNumber); return0; } else { pNodeScore=p1->next;while(pNodeScore!=NULL) { if(strcmp(pNodeScore->data.Number,DeleteNumber)==0) { p1->next=pNodeScore->next; printf("學(xué)號為%s的學(xué)生信息已經(jīng)刪除!\n",DeleteNumber); return0; } else {//否則,結(jié)點(diǎn)向下一個,p1仍為pNodeScore的前驅(qū) p1=pNodeScore; pNodeScore=pNodeScore->next; } } } printf("沒有此學(xué)號的學(xué)生!");}intChange(){p_node_scorepNodeScore;pNodeScore=headScore; if(pNodeScore==NULL) { printf("成績表中沒有數(shù)據(jù)!請先添加數(shù)據(jù)!\n"); return0; } charEditNumber[20]; printf("請輸入你要修改的學(xué)生學(xué)號:"); scanf("%s",EditNumber); while(pNodeScore!=NULL) { if(strcmp(pNodeScore->data.Number,EditNumber)==0) {//用strcmp比較兩字符串是否相等,相等則返回0 printf("原來的學(xué)生成績信息如下:\n");//輸出原來的成績信息printf("學(xué)號|姓名|語文成績|英語成績|高數(shù)成績\n"); PrintScore(pNodeScore->data); printf("語文新成績:");scanf("%s",pNodeScore->data.Chinese);printf("英語新成績:");scanf("%s",pNodeScore->data.English); printf("高數(shù)新成績:");scanf("%s",pNodeScore->data.Math); printf("成績已經(jīng)修改!"); return0; } pNodeScore=pNodeScore->next;//如果不相等,pNodeScore則指向下一個結(jié)點(diǎn) } printf("沒有此學(xué)號的學(xué)生!\n");//如果找到最后都沒有,則輸出沒有此學(xué)號的學(xué)生 }intFind(){p_node_scorepNodeScore;pNodeScore=headScore; if(pNodeScore==NULL) { printf("成績表中沒有數(shù)據(jù)!請先添加數(shù)據(jù)!\n"); return0; } charFindNumber[20]; printf("請輸入你要查找的學(xué)生學(xué)號:"); scanf("%s",FindNumber); while(pNodeScore!=NULL) { if(strcmp(pNodeScore->data.Number,FindNumber)==0) { printf("你要查找的學(xué)生成績信息如下:\n");printf("學(xué)號|姓名|語文成績|英語成績|高數(shù)成績\n"); PrintScore(pNodeScore->data); return0; } pNodeScore=p

溫馨提示

  • 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

提交評論