您的位置:程序门 -> 产品/厂家 -> ibm 开发者大会



麻烦各位来帮忙看看   急!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


麻烦各位来帮忙看看 急![已结贴,结贴人:skylctu]
发表于:2007-12-29 21:15:32 楼主
      我是个学生,这个题是我的气模实验设计,要不是时间太紧我是不打算求人的,各位前辈帮忙看看。
从终端读入字符集大小为n(即字符的个数),逐一输入n个字符和相应的n个权值(即字符出现的频度),建立哈夫曼树,进行编码并且输出。
注:可用c或c++编写。
1、设计一个图的类,采用临接表法进行存储,该图每个结点的数据类型类模板的模板参数进行定义(注:需先设计一个结点类node);
2、为该类分别设计一个实现深度优先搜索和广度优先搜索的成员函数,并要输出搜索结果;
注:
        1、为了让你设计的图类拥有数据,可以设计一个成员函数,用于构造你自己预先设计好的图;
        2、要求的图如下,也可以自己构造图,但是需要注意的是,图不能是退化的单链表:
发表于:2007-12-29 21:21:121楼 得分:0
建立哈夫曼树,还有图类啊...
发表于:2007-12-29 21:26:392楼 得分:50
c/c++ code
#include"iostream.h" #include"cstring" #include"stdlib.h" #include"stdio.h" typedef struct { int weight; int parent,lchild,rchild; char c; }htnode, *huffmantree; typedef char** huffmancode; typedef struct { int w; int n; }small; typedef struct { char c; int w1; }text; text* input(int& n) { text *p; cout << "please input the number of the chars"<<endl; cin >> n; p=new text[n]; cout <<"please input the chars:"<<endl; forint i=0; i<n; i++) scanf("%c", &p[i].c); cout << "please input the weights:"<<endl; forint j=0; j<n; j++) { cin >> p[j].w1; if(p[j].w1 <=0) { cout <<"input error,please continue."<<endl; j=0; continue; } } return p; } void bubble(small a[],int n) { int i,j,t,temp1,temp2; for(i=1;i<=n-1;i++) { t=n-i; for(j=0;j<t-1;j++) if(a[j].w>a[j+1].w) { temp1=a[j].w ;temp2=a[j].n; a[j].w =a[j+1].w ;a[j].n =a[j+1].n ; a[j+1].w =temp1;a[j+1].n =temp2; } } } huffmancode scoding(huffmantree& ht,int n) { int k=0,start,x; char *cd; huffmancode hc; hc=(huffmancode)malloc((n+1)*sizeofchar*)); cd=new char[n]; cd[n-1]='\0'; forint i=1;i<=n;++i) { x=0; start=n-1; forint c=i,f=ht[i].parent;f!=0;c=f,f=ht[f].parent) if(ht[f].lchild==c) cd[--start]='0'; else cd[--start]='1'; hc[i]=new char[n-start]; forchar *l=hc[i],k=start;k<=n-2;k++,x++) l[x]=cd[k]; l[x]='\0'; } delete[] cd; return hc; } void select(huffmantree p,int i,int& a,int& b) { small *q=new small[i]; int m=0; forint j=1;j<=i;j++) { if(p[j].parent ==0) { q[m].w =p[j].weight; q[m].n =j; m++; } } bubble(q,m); if(q[0].n <q[1].n) { a=q[0].n; b=q[1].n; } else { a=q[1].n; b=q[0].n; } } void bulidtree(huffmantree& ht,text *w,int n) { file *fp; int s1,s2; huffmantree p; int m,i; if(n<=1) return ; m=2*n-1; ht=new htnode[m+1]; if((fp=fopen("hfmtree.txt","w"))==null) { cout<<"can't open file"; exit(0); } for(p=ht,i=1;i<=n;++i) { p[i].c =w[i-1].c; p[i].weight =w[i-1].w1; p[i].parent =0; p[i].lchild =0; p[i].rchild =0; } for(;i<=m;++i) { p[i].c =null; p[i].weight =0; p[i].parent =0; p[i].lchild =0; p[i].rchild =0; } for(i=n+1;i<=m;i++) { select(ht,i-1,s1,s2); ht[s1].parent=i;ht[s2].parent=i; ht[i].lchild=s1;ht[i].rchild=s2; ht[i].weight=ht[s1].weight+ht[s2].weight; } forint j=1;j<=m;j++) { fprintf(fp,"%c %d %d %d %d",ht[j].c ,ht[j].weight ,ht[j].parent ,ht[j].lchild ,ht[j].rchild ); } } text* readingtree(huffmancode& h,int& n,huffmantree& p) { char c; int i=1,a,b,e,d,j=0; text* r; n=0; file *fp; if((fp=fopen("hfmtree.txt","r"))==null) { cout << "can't open this file."<<endl; exit(0); } while(fscanf(fp,"%c %d %d %d %d",&c ,&a,&b ,&e ,&d )!=eof&&c !=null) n++; rewind(fp); r=new text[n]; p=new htnode[2*n-1]; while(fscanf(fp,"%c %d %d %d %d", &p[i].c, &p[i].weight, &p[i].parent, &p[i].lchild, &p[i].rchild)!=eof) { if(j<=n) { r[j].c =p[i].c; r[j].w1 =p[i].weight; } j++; i++; } h=scoding(p,n); fclose(fp); return r; } void encoding(huffmancode p1,int n,text *r1) { bool k=1; file *fp,*fp1; char c; if((fp=fopen("codefile.txt","w"))==null) { cout<<"can't open file"; exit(0); } if((fp1=fopen("tobetran.txt","r"))==null) { cout << "can't open this file."<<endl; exit(0); } while((c=fgetc(fp1))!=eof) { forint i=0;i<n;i++) { if(c==r1[i].c) { fputs(p1[i+1],fp); k=0; } } if(k==1) fputc(c,fp); k=1; } fclose(fp); } void decoding(huffmancode p1,int n,text *r2) {//随机读写 bool k=1; file* fp,*fp1; char ch; int i=0; if((fp1=fopen("textfile.txt","w"))==null) { cout << "can't open this file."<<endl; exit(0); } if((fp=fopen("codefile.txt","r"))==null) { cout << "can't open this file."<<endl; exit(0); } while(ch!=eof) { while(i<n&&ch!=eof) { forint j=0;i<n&&j<(signed )strlen(p1[i+1]);j++) { ch=fgetc(fp); if(ch!=p1[i+1][j]&&ch!=eof) { if(ch!='1'&&ch!='0') { fputc(ch,fp1); i=0; j=0; k=0; break; } fseek(fp,-(j+1),1); i++; j=0; k=0; break; } } if(k==1&&ch!=eof) { fputc(r2[i].c ,fp1); i=0; } k=1; } } fclose(fp); fclose(fp1); } void print() { int i=1; char ch; file *fp,*fp1; if((fp=fopen("codefile.txt","r"))==null) { cout << "can't open this file."<<endl; exit(0); } if((fp1=fopen("codeprin.txt","w"))==null) { cout << "can't open this file."<<endl; exit(0); } while((ch=fgetc(fp))!=eof) { cout <<ch; fputc(ch,fp1); if(i%50==0) { cout <<endl; } i++; } cout <<endl; fclose(fp); fclose(fp1); } htnode findboot(huffmantree ht,int n) { forint i=0; i<2*n-1)&&ht[i].parent!=0; i++); return ht[i]; } void printspace(int i,file *fp1) { forint j=0;j<i;j++) { cout<<" "; fputc(' ', fp1); } } void treeprinting(htnode ht, huffmantree h, int i, file* fp) { if(ht.weight !=0) { if(ht.lchild ) treeprinting(h[ht.lchild],h,i+2,fp) ; printspace(i,fp); cout<<ht.weight <<endl; fprintf(fp,"%d", ht.weight ); fputc('\n',fp); if(ht.rchild) treeprinting(h[ht.rchild],h,i+2,fp) ; } }
发表于:2007-12-29 21:28:463楼 得分:50
c/c++ code
//以前数据结构课的作业:很长的... void main() { file* fp; char c; htnode u; huffmancode p; text *r; huffmantree q; int n, i=0, j=0; cout<<" *******************"<<endl; cout <<" * 哈夫曼编/译码器 * "<<endl; cout<<" *******************"<<endl; cout<<endl; cout<<"----------------------------------------------------------------------"<<endl; cout<<"initialization-i"<<" encoding-e"<<" decoding-d"<<endl; cout<<"print-p"<<" tree printing-t"<<" quit-q"<<endl; cout<<"----------------------------------------------------------------------"<<endl; cout<<"please choose:"<<endl; while(c!='q') { cin >> c; if(c=='i') { r=input(n); bulidtree(q,r,n); p=scoding(q,n); cout<<"the huffmantree has been made!"<<endl; i=1; } else if(c=='e') { if(i==0) { cout<<"there are not a huffmantree in the memory!"<<endl <<"do you want to call hfmtree?(y/n)"<<endl; cin >> c; if(c=='y') { r=readingtree(p,n,q); encoding(p,n,r); i=1; cout<<"encoding has ended!"<<endl; } else cout<<"ok,you must want to initialization a huffmantree.please input 'i'."<<endl; } else { encoding(p,n,r); cout<<"encoding has ended!"<<endl; } } else if(c=='d') { if(i==0) { cout<<"there are not a huffmantree in the memory!"<<endl <<"do you want to call hfmtree?(y/n)"<<endl; cin >>c; if(c=='y') { r=readingtree(p,n,q); decoding(p,n,r); i=1; cout<<"dencoding has ended!"<<endl; } else cout<<"ok,you must want to initialization a huffmantree.please input 'i'."<<endl; } else { decoding(p,n,r); cout<<"decoding has ended!"<<endl; } } else if(c=='p') { print(); } else if(c=='t') { if((fp=fopen("treeprint.txt","w"))==null) { cout << "can't open this file."<<endl; exit(0); } if(i==0) { cout<<"there are not a huffmantree in the memory!"<<endl <<"do you want to call hfmtree?(y/n)"<<endl; cin >>c; if(c=='y') { readingtree(p,n,q); u=findboot(q,n); treeprinting(u,q,j,fp); i=1; cout<<"treeprinting has ended!"<<endl; } else cout<<"ok,you must want to initialization a huffmantree.please input 'i'."<<endl; } else { u=findboot(q,n); treeprinting(u,q,j,fp); i=1; cout<<"treeprinting has ended!"<<endl; } fclose(fp); } else if(c!='q') cout<<"input error!!!"<<endl; cout<<"----------------------------------------------------------------------"<<endl; cout<<"initialization-i"<<" encoding-e"<<" decoding-d"<<endl; cout<<"print-p"<<" tree printing-t"<<" quit-q"<<endl; cout<<"----------------------------------------------------------------------"<<endl; cout<<"please choose:"<<endl; } cout<<"the programme has ended!"<<endl; }
发表于:2007-12-29 22:06:274楼 得分:0
不好意思啊,刚才那个题写漏了一个题号,大家从新来看看
第一题:
              从终端读入字符集大小为n(即字符的个数),逐一输入n个字符和相应的n个权值(即字符出现的频度),建立哈夫曼树,进行编码并且输出。  
              注:可用c或c++编写。  
第二题:
              1、设计一个图的类,采用临接表法进行存储,该图每个结点的数据类型类模板的模板参数进行定义(注:需先设计一个结点类node);  
              2为该类分别设计一个实现深度优先搜索和广度优先搜索的成员函数,并要输出搜索结果;  
注:  
                1、为了让你设计的图类拥有数据,可以设计一个成员函数,用于构造你自己预先设计好的图;  
                2、要求的图如下,也可以自己构造图,但是需要注意的是,图不能是退化的单链表:
发表于:2007-12-29 22:11:375楼 得分:0
飘过顶。


快速检索

最新资讯
热门点击