基本要求
一个完整的系统应具有以下功能:
(a) I:初始化(Initialization)。从终端读入字符集大小 n ,以及 n 个字符和 n 个权值,建立哈夫曼树,并将它存于文件 hfmTree 中。
(b) E:编码(Encoding)。利用已建好的哈夫曼树(如不在内存,则从文件hfmTree中读入),对文件 T oBeTran 中的正文进行编码,然后将结果存入文件 CodeFile 中。
(c)D:译码(Decoding)。利用已建好的哈夫曼树将文件CodeFile 中的代码进行译码,结果存入文件 TextFile 中。
(d)P:打印代码文件(Print)。将文件 CodeFile 以紧凑格式显示在终端上,每行50个代码。
(e)T:印哈夫曼树(Tree printing)。将已在内存中的哈夫曼树以直观的方式显示在终端上,同时将此字符形式的哈夫曼树写入文件 TreePrint中。
我的程序的错误:在运行中如果实现了 译码 功能 ,再去实现打印代码功能就不能实现 如果实现了 打印代码功能 ,再去实现译码功能 就不能实现了;小弟实在不知道哪里错啦 求解啊!
下面是我的代码
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include <iomanip.h>
using namespace std;
typedef struct{
char ch;
int weight;
int parent,lchild,rchild;
}htnode,*hfmtree;
typedef char **hfmcode;
hfmtree HT;//全局变量
int select(hfmtree &HT,int n )
{
int flag;
double j=100000;
for(int i=1;i<=n;i++)
{
if(HT[i].weight<=j&&HT[i].parent==0)
{
j=HT[i].weight;
flag=i;
}
}
HT[flag].parent=1;
return flag;
}
void htcoding(hfmtree &HT,int n)
{
if(n<1)
return;
int m=2*n-1;
HT=new htnode [m+1];
for(int i=1;i<=n;++i)
{
cout<<"qingshuru"<<endl;
cin>>HT[i].ch;
cin>>HT[i].weight;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
HT[i].ch='0';
HT[i].weight=0;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
int s1=select(HT,i-1);
int s2=select(HT,i-1);
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;
}
}
void Tcode(hfmcode &hc,int n,hfmtree &HT)
{
hc=new char*[n+1];
char *cd= new char[n];
cd[n-1]='\0';
int c,f;
for(int i=1;i<=n;++i)
{
int start=n-1;
for( 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];
strcpy(hc[i],&cd[start]);
cout<<hc[i]<<endl;
}
delete cd;
}
void Convert_tree(unsigned char T[100][100],int s,int *i,int j)
{
int k,l;
l=++(*i);
for(k=0;k<s;k++)
T[l][k]=' ';
T[l][k]=HT[j].weight;
if(HT[j].lchild) //有左孩子
Convert_tree(T,s+1,i,HT[j].lchild);
if(HT[j].rchild)
Convert_tree(T,s+1,i,HT[j].rchild);
T[l][++k]='\0';
}
int main()
{
char code[100],h[100],hl[100],kk[100];
int n,i,j,k,l;
ifstream input_file;
ofstream output_file;
char choice,str[100];
hfmcode HC;
cout<<"\n";
while(choice!='Q'&&choice!='q')
{
cout<<" "<<"*************************赫夫曼编码/译码打印系统*************************\n";
cout<<" "<<"I--------------------------------------------------Init\n";
cout<<" "<<"C--------------------------------------------------Coding\n";
cout<<" "<<"D--------------------------------------------------Decoding\n";
cout<<" "<<"O--------------------------------------------------OutputCode\n";
cout<<" "<<"P--------------------------------------------------PrintHfmTree\n";
cout<<" "<<"Q--------------------------------------------------Exit\n";
cout<<"请输入您要操作的步骤:";
cin>>choice;
if(choice=='I'||choice=='i')
{
cout<<"请输入字符个数:";
cin>>n;
// hfmcoding(HT,HC,n);
htcoding(HT, n);
Tcode(HC,n,HT);
cout<<"hao"<<endl;
for(i=1;i<=n;++i)
{
cout<<HT[i].ch<<":"<<HC[i]<<endl;
}
output_file.open("hfmTree.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=1;i<=n;i++)
{
output_file<<"("<<HT[i].ch<<HC[i]<<")";
}
output_file.close();
cout<<"赫夫曼树已经创建完毕,并且已经放入hfmTree.txt文件中!"<<endl;
}
else if(choice=='C'||choice=='c')
{
printf("请输入字符:");
gets(str);
output_file.open("ToBeTran.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
output_file<<str<<endl;
output_file.close();
output_file.open("CodeFile.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=0;i<strlen(str);i++){
for(j=0;j<=n;++j)
{
if(HT[j].ch==str[i])
{
output_file<<HC[j];
break;
}
}
}
output_file.close();
cout<<"\n";
cout<<"编码完毕,并且已经存入CodeFile.txt文件!\n";
}
else if(choice=='O'||choice=='o')
{
input_file.open("CodeFile.txt");
if(!input_file)
{
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>code;
cout<<"编码码值为:\n";
cout.setf(ios::left);
cout<<setw(50)<<code<<endl;
input_file.close();
}
else if(choice=='D'||choice=='d')
{
input_file.open("CodeFile.txt");
if(!input_file){
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>h;
input_file.close();
output_file.open("Textfile.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
k=0;
while(h[k]!='\0')
{
for(i=1;i<=n;i++)
{
l=k;
for(j=0;j<strlen(HC[i]);j++,l++){
hl[j]=h[l];
}
hl[j]='\0';
if(strcmp(HC[i],hl)==0)
{
output_file<<HT[i].ch;
k=k+strlen(HC[i]);
break;
}
}
}
output_file.close();
input_file.open("Textfile.txt");
if(!input_file){
cout<<"can't oen file!"<<endl;
return 1;
}
input_file>>h;
cout<<h<<endl;
input_file.close();
cout<<"译码结束,字符已经存入Textfile.txt文件中!"<<endl;
}
else if(choice=='P'||choice=='p'){
unsigned char T[100][100];
int i,j,m=0;
FILE *fp;
Convert_tree(T,0,&m,2*n-1);
if((fp=fopen("treeprint.txt","wb+"))==NULL)
printf("Open file treeprint.txt error!\n");
printf("\n以凹入表形式打印已建好的赫夫曼树:\n");
for(i=1;i<=2*n-1;i++)
{
for (j=0;T[i][j]!=0;j++)
{
if(T[i][j]==' ') {printf(" ");fputc(T[i][j],fp);}
else
{printf("%d",T[i][j]);fprintf(fp,"%d\n",T[i][j]);}
}
printf("\n");
}
fclose(fp);
printf("\n此字符形式的哈夫曼树已写入文件treeprint.txt中.\n\n");
}
else if(choice=='Q'||choice=='q')
{
exit(0);
}
else
{
cout<<"您没有输入正确的步骤,请重新输入!"<<endl;
}
cout<<endl;
}
return 0;
}
9 个解决方案
#1
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include <iomanip.h>
using namespace std;
typedef struct{
char ch;
int weight;
int parent,lchild,rchild;
}htnode,*hfmtree;
typedef char **hfmcode;
hfmtree HT;//全局变量
int select(hfmtree &HT,int n )
{
int flag;
double j=100000;
for(int i=1;i<=n;i++)
{
if(HT[i].weight<=j&&HT[i].parent==0)
{
j=HT[i].weight;
flag=i;
}
}
HT[flag].parent=1;
return flag;
}
void htcoding(hfmtree &HT,int n)
{
if(n<1)
return;
int m=2*n-1;
HT=new htnode [m+1];
for(int i=1;i<=n;++i)
{
cout<<"qingshuru"<<endl;
cin>>HT[i].ch;
cin>>HT[i].weight;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
HT[i].ch='0';
HT[i].weight=0;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
int s1=select(HT,i-1);
int s2=select(HT,i-1);
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;
}
}
void Tcode(hfmcode &hc,int n,hfmtree &HT)
{
hc=new char*[n+1];
char *cd= new char[n];
cd[n-1]='\0';
int c,f;
for(int i=1;i<=n;++i)
{
int start=n-1;
for( 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];
strcpy(hc[i],&cd[start]);
cout<<hc[i]<<endl;
}
delete cd;
}
void Convert_tree(unsigned char T[100][100],int s,int *i,int j)
{
int k,l;
l=++(*i);
for(k=0;k<s;k++)
T[l][k]=' ';
T[l][k]=HT[j].weight;
if(HT[j].lchild) //有左孩子
Convert_tree(T,s+1,i,HT[j].lchild);
if(HT[j].rchild)
Convert_tree(T,s+1,i,HT[j].rchild);
T[l][++k]='\0';
}
int main()
{
char code[100],h[100],hl[100],kk[100];
int n,i,j,k,l;
ifstream input_file;
ofstream output_file;
char choice,str[100];
hfmcode HC;
cout<<"\n";
while(choice!='Q'&&choice!='q')
{
cout<<" "<<"*************************赫夫曼编码/译码打印系统*************************\n";
cout<<" "<<"I--------------------------------------------------Init\n";
cout<<" "<<"C--------------------------------------------------Coding\n";
cout<<" "<<"D--------------------------------------------------Decoding\n";
cout<<" "<<"O--------------------------------------------------OutputCode\n";
cout<<" "<<"P--------------------------------------------------PrintHfmTree\n";
cout<<" "<<"Q--------------------------------------------------Exit\n";
cout<<"请输入您要操作的步骤:";
cin>>choice;
if(choice=='I'||choice=='i')
{
cout<<"请输入字符个数:";
cin>>n;
// hfmcoding(HT,HC,n);
htcoding(HT, n);
Tcode(HC,n,HT);
cout<<"hao"<<endl;
for(i=1;i<=n;++i)
{
cout<<HT[i].ch<<":"<<HC[i]<<endl;
}
output_file.open("hfmTree.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=1;i<=n;i++)
{
output_file<<"("<<HT[i].ch<<HC[i]<<")";
}
output_file.close();
cout<<"赫夫曼树已经创建完毕,并且已经放入hfmTree.txt文件中!"<<endl;
}
else if(choice=='C'||choice=='c')
{
printf("请输入字符:");
gets(str);
output_file.open("ToBeTran.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
output_file<<str<<endl;
output_file.close();
output_file.open("CodeFile.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=0;i<strlen(str);i++){
for(j=0;j<=n;++j)
{
if(HT[j].ch==str[i])
{
output_file<<HC[j];
break;
}
}
}
output_file.close();
cout<<"\n";
cout<<"编码完毕,并且已经存入CodeFile.txt文件!\n";
}
else if(choice=='O'||choice=='o')
{
input_file.open("CodeFile.txt");
if(!input_file)
{
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>code;
cout<<"编码码值为:\n";
cout.setf(ios::left);
cout<<setw(50)<<code<<endl;
input_file.close();
}
else if(choice=='D'||choice=='d')
{
input_file.open("CodeFile.txt");
if(!input_file){
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>h;
input_file.close();
output_file.open("Textfile.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
k=0;
while(h[k]!='\0')
{
for(i=1;i<=n;i++)
{
l=k;
for(j=0;j<strlen(HC[i]);j++,l++){
hl[j]=h[l];
}
hl[j]='\0';
if(strcmp(HC[i],hl)==0)
{
output_file<<HT[i].ch;
k=k+strlen(HC[i]);
break;
}
}
}
output_file.close();
input_file.open("Textfile.txt");
if(!input_file){
cout<<"can't oen file!"<<endl;
return 1;
}
input_file>>h;
cout<<h<<endl;
input_file.close();
cout<<"译码结束,字符已经存入Textfile.txt文件中!"<<endl;
}
else if(choice=='P'||choice=='p'){
unsigned char T[100][100];
int i,j,m=0;
FILE *fp;
Convert_tree(T,0,&m,2*n-1);
if((fp=fopen("treeprint.txt","wb+"))==NULL)
printf("Open file treeprint.txt error!\n");
printf("\n以凹入表形式打印已建好的赫夫曼树:\n");
for(i=1;i<=2*n-1;i++)
{
for (j=0;T[i][j]!=0;j++)
{
if(T[i][j]==' ') {printf(" ");fputc(T[i][j],fp);}
else
{printf("%d",T[i][j]);fprintf(fp,"%d\n",T[i][j]);}
}
printf("\n");
}
fclose(fp);
printf("\n此字符形式的哈夫曼树已写入文件treeprint.txt中.\n\n");
}
else if(choice=='Q'||choice=='q')
{
exit(0);
}
else
{
cout<<"您没有输入正确的步骤,请重新输入!"<<endl;
}
cout<<endl;
}
return 0;
}
#2
这么长的代码还不排版,不排版就算了后面竟然这么大一片空白,所以我就没有怎么看.....
#3
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include <iomanip.h>
using namespace std;
typedef struct{
char ch;
int weight;
int parent,lchild,rchild;
}htnode,*hfmtree;
typedef char **hfmcode;
hfmtree HT;//全局变量
int select(hfmtree &HT,int n )
{
int flag;
double j=100000;
for(int i=1;i<=n;i++)
{
if(HT[i].weight<=j&&HT[i].parent==0)
{
j=HT[i].weight;
flag=i;
}
}
HT[flag].parent=1;
return flag;
}
void htcoding(hfmtree &HT,int n)
{
if(n<1)
return;
int m=2*n-1;
HT=new htnode [m+1];
for(int i=1;i<=n;++i)
{
cout<<"qingshuru"<<endl;
cin>>HT[i].ch;
cin>>HT[i].weight;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
HT[i].ch='0';
HT[i].weight=0;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
int s1=select(HT,i-1);
int s2=select(HT,i-1);
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;
}
}
void Tcode(hfmcode &hc,int n,hfmtree &HT)
{
hc=new char*[n+1];
char *cd= new char[n];
cd[n-1]='\0';
int c,f;
for(int i=1;i<=n;++i)
{
int start=n-1;
for( 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];
strcpy(hc[i],&cd[start]);
cout<<hc[i]<<endl;
}
delete cd;
}
void Convert_tree(unsigned char T[100][100],int s,int *i,int j)
{
int k,l;
l=++(*i);
for(k=0;k<s;k++)
T[l][k]=' ';
T[l][k]=HT[j].weight;
if(HT[j].lchild) //有左孩子
Convert_tree(T,s+1,i,HT[j].lchild);
if(HT[j].rchild)
Convert_tree(T,s+1,i,HT[j].rchild);
T[l][++k]='\0';
}
int main()
{
char code[100],h[100],hl[100],kk[100];
int n,i,j,k,l;
ifstream input_file;
ofstream output_file;
char choice,str[100];
hfmcode HC;
cout<<"\n";
while(choice!='Q'&&choice!='q')
{
cout<<" "<<"*************************赫夫曼编码/译码打印系统*************************\n";
cout<<" "<<"I--------------------------------------------------Init\n";
cout<<" "<<"C--------------------------------------------------Coding\n";
cout<<" "<<"D--------------------------------------------------Decoding\n";
cout<<" "<<"O--------------------------------------------------OutputCode\n";
cout<<" "<<"P--------------------------------------------------PrintHfmTree\n";
cout<<" "<<"Q--------------------------------------------------Exit\n";
cout<<"请输入您要操作的步骤:";
cin>>choice;
if(choice=='I'||choice=='i')
{
cout<<"请输入字符个数:";
cin>>n;
// hfmcoding(HT,HC,n);
htcoding(HT, n);
Tcode(HC,n,HT);
cout<<"hao"<<endl;
for(i=1;i<=n;++i)
{
cout<<HT[i].ch<<":"<<HC[i]<<endl;
}
output_file.open("hfmTree.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=1;i<=n;i++)
{
output_file<<"("<<HT[i].ch<<HC[i]<<")";
}
output_file.close();
cout<<"赫夫曼树已经创建完毕,并且已经放入hfmTree.txt文件中!"<<endl;
}
else if(choice=='C'||choice=='c')
{
printf("请输入字符:");
gets(str);
output_file.open("ToBeTran.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
output_file<<str<<endl;
output_file.close();
output_file.open("CodeFile.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=0;i<strlen(str);i++){
for(j=0;j<=n;++j)
{
if(HT[j].ch==str[i])
{
output_file<<HC[j];
break;
}
}
}
output_file.close();
cout<<"\n";
cout<<"编码完毕,并且已经存入CodeFile.txt文件!\n";
}
else if(choice=='O'||choice=='o')
{
input_file.open("CodeFile.txt");
if(!input_file)
{
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>code;
cout<<"编码码值为:\n";
cout.setf(ios::left);
cout<<setw(50)<<code<<endl;
input_file.close();
}
else if(choice=='D'||choice=='d')
{
input_file.open("CodeFile.txt");
if(!input_file){
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>h;
input_file.close();
output_file.open("Textfile.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
k=0;
while(h[k]!='\0')
{
for(i=1;i<=n;i++)
{
l=k;
for(j=0;j<strlen(HC[i]);j++,l++){
hl[j]=h[l];
}
hl[j]='\0';
if(strcmp(HC[i],hl)==0)
{
output_file<<HT[i].ch;
k=k+strlen(HC[i]);
break;
}
}
}
output_file.close();
input_file.open("Textfile.txt");
if(!input_file){
cout<<"can't oen file!"<<endl;
return 1;
}
input_file>>h;
cout<<h<<endl;
input_file.close();
cout<<"译码结束,字符已经存入Textfile.txt文件中!"<<endl;
}
else if(choice=='P'||choice=='p'){
unsigned char T[100][100];
int i,j,m=0;
FILE *fp;
Convert_tree(T,0,&m,2*n-1);
if((fp=fopen("treeprint.txt","wb+"))==NULL)
printf("Open file treeprint.txt error!\n");
printf("\n以凹入表形式打印已建好的赫夫曼树:\n");
for(i=1;i<=2*n-1;i++)
{
for (j=0;T[i][j]!=0;j++)
{
if(T[i][j]==' ') {printf(" ");fputc(T[i][j],fp);}
else
{printf("%d",T[i][j]);fprintf(fp,"%d\n",T[i][j]);}
}
printf("\n");
}
fclose(fp);
printf("\n此字符形式的哈夫曼树已写入文件treeprint.txt中.\n\n");
}
else if(choice=='Q'||choice=='q')
{
exit(0);
}
else
{
cout<<"您没有输入正确的步骤,请重新输入!"<<endl;
}
cout<<endl;
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include <iomanip.h>
using namespace std;
typedef struct{
char ch;
int weight;
int parent,lchild,rchild;
}htnode,*hfmtree;
typedef char **hfmcode;
hfmtree HT;//全局变量
int select(hfmtree &HT,int n )
{
int flag;
double j=100000;
for(int i=1;i<=n;i++)
{
if(HT[i].weight<=j&&HT[i].parent==0)
{
j=HT[i].weight;
flag=i;
}
}
HT[flag].parent=1;
return flag;
}
void htcoding(hfmtree &HT,int n)
{
if(n<1)
return;
int m=2*n-1;
HT=new htnode [m+1];
for(int i=1;i<=n;++i)
{
cout<<"qingshuru"<<endl;
cin>>HT[i].ch;
cin>>HT[i].weight;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
HT[i].ch='0';
HT[i].weight=0;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
int s1=select(HT,i-1);
int s2=select(HT,i-1);
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;
}
}
void Tcode(hfmcode &hc,int n,hfmtree &HT)
{
hc=new char*[n+1];
char *cd= new char[n];
cd[n-1]='\0';
int c,f;
for(int i=1;i<=n;++i)
{
int start=n-1;
for( 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];
strcpy(hc[i],&cd[start]);
cout<<hc[i]<<endl;
}
delete cd;
}
void Convert_tree(unsigned char T[100][100],int s,int *i,int j)
{
int k,l;
l=++(*i);
for(k=0;k<s;k++)
T[l][k]=' ';
T[l][k]=HT[j].weight;
if(HT[j].lchild) //有左孩子
Convert_tree(T,s+1,i,HT[j].lchild);
if(HT[j].rchild)
Convert_tree(T,s+1,i,HT[j].rchild);
T[l][++k]='\0';
}
int main()
{
char code[100],h[100],hl[100],kk[100];
int n,i,j,k,l;
ifstream input_file;
ofstream output_file;
char choice,str[100];
hfmcode HC;
cout<<"\n";
while(choice!='Q'&&choice!='q')
{
cout<<" "<<"*************************赫夫曼编码/译码打印系统*************************\n";
cout<<" "<<"I--------------------------------------------------Init\n";
cout<<" "<<"C--------------------------------------------------Coding\n";
cout<<" "<<"D--------------------------------------------------Decoding\n";
cout<<" "<<"O--------------------------------------------------OutputCode\n";
cout<<" "<<"P--------------------------------------------------PrintHfmTree\n";
cout<<" "<<"Q--------------------------------------------------Exit\n";
cout<<"请输入您要操作的步骤:";
cin>>choice;
if(choice=='I'||choice=='i')
{
cout<<"请输入字符个数:";
cin>>n;
// hfmcoding(HT,HC,n);
htcoding(HT, n);
Tcode(HC,n,HT);
cout<<"hao"<<endl;
for(i=1;i<=n;++i)
{
cout<<HT[i].ch<<":"<<HC[i]<<endl;
}
output_file.open("hfmTree.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=1;i<=n;i++)
{
output_file<<"("<<HT[i].ch<<HC[i]<<")";
}
output_file.close();
cout<<"赫夫曼树已经创建完毕,并且已经放入hfmTree.txt文件中!"<<endl;
}
else if(choice=='C'||choice=='c')
{
printf("请输入字符:");
gets(str);
output_file.open("ToBeTran.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
output_file<<str<<endl;
output_file.close();
output_file.open("CodeFile.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=0;i<strlen(str);i++){
for(j=0;j<=n;++j)
{
if(HT[j].ch==str[i])
{
output_file<<HC[j];
break;
}
}
}
output_file.close();
cout<<"\n";
cout<<"编码完毕,并且已经存入CodeFile.txt文件!\n";
}
else if(choice=='O'||choice=='o')
{
input_file.open("CodeFile.txt");
if(!input_file)
{
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>code;
cout<<"编码码值为:\n";
cout.setf(ios::left);
cout<<setw(50)<<code<<endl;
input_file.close();
}
else if(choice=='D'||choice=='d')
{
input_file.open("CodeFile.txt");
if(!input_file){
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>h;
input_file.close();
output_file.open("Textfile.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
k=0;
while(h[k]!='\0')
{
for(i=1;i<=n;i++)
{
l=k;
for(j=0;j<strlen(HC[i]);j++,l++){
hl[j]=h[l];
}
hl[j]='\0';
if(strcmp(HC[i],hl)==0)
{
output_file<<HT[i].ch;
k=k+strlen(HC[i]);
break;
}
}
}
output_file.close();
input_file.open("Textfile.txt");
if(!input_file){
cout<<"can't oen file!"<<endl;
return 1;
}
input_file>>h;
cout<<h<<endl;
input_file.close();
cout<<"译码结束,字符已经存入Textfile.txt文件中!"<<endl;
}
else if(choice=='P'||choice=='p'){
unsigned char T[100][100];
int i,j,m=0;
FILE *fp;
Convert_tree(T,0,&m,2*n-1);
if((fp=fopen("treeprint.txt","wb+"))==NULL)
printf("Open file treeprint.txt error!\n");
printf("\n以凹入表形式打印已建好的赫夫曼树:\n");
for(i=1;i<=2*n-1;i++)
{
for (j=0;T[i][j]!=0;j++)
{
if(T[i][j]==' ') {printf(" ");fputc(T[i][j],fp);}
else
{printf("%d",T[i][j]);fprintf(fp,"%d\n",T[i][j]);}
}
printf("\n");
}
fclose(fp);
printf("\n此字符形式的哈夫曼树已写入文件treeprint.txt中.\n\n");
}
else if(choice=='Q'||choice=='q')
{
exit(0);
}
else
{
cout<<"您没有输入正确的步骤,请重新输入!"<<endl;
}
cout<<endl;
}
return 0;
}
#4
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include <iomanip.h>
using namespace std;
typedef struct{
char ch;
int weight;
int parent,lchild,rchild;
}htnode,*hfmtree;
typedef char **hfmcode;
hfmtree HT;//全局变量
int select(hfmtree &HT,int n )
{
int flag;
double j=100000;
for(int i=1;i<=n;i++)
{
if(HT[i].weight<=j&&HT[i].parent==0)
{
j=HT[i].weight;
flag=i;
}
}
HT[flag].parent=1;
return flag;
}
void htcoding(hfmtree &HT,int n)
{
if(n<1)
return;
int m=2*n-1;
HT=new htnode [m+1];
for(int i=1;i<=n;++i)
{
cout<<"qingshuru"<<endl;
cin>>HT[i].ch;
cin>>HT[i].weight;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
HT[i].ch='0';
HT[i].weight=0;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
int s1=select(HT,i-1);
int s2=select(HT,i-1);
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;
}
}
void Tcode(hfmcode &hc,int n,hfmtree &HT)
{
hc=new char*[n+1];
char *cd= new char[n];
cd[n-1]='\0';
int c,f;
for(int i=1;i<=n;++i)
{
int start=n-1;
for( 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];
strcpy(hc[i],&cd[start]);
cout<<hc[i]<<endl;
}
delete cd;
}
void Convert_tree(unsigned char T[100][100],int s,int *i,int j)
{
int k,l;
l=++(*i);
for(k=0;k<s;k++)
T[l][k]=' ';
T[l][k]=HT[j].weight;
if(HT[j].lchild) //有左孩子
Convert_tree(T,s+1,i,HT[j].lchild);
if(HT[j].rchild)
Convert_tree(T,s+1,i,HT[j].rchild);
T[l][++k]='\0';
}
int main()
{
char code[100],h[100],hl[100],kk[100];
int n,i,j,k,l;
ifstream input_file;
ofstream output_file;
char choice,str[100];
hfmcode HC;
cout<<"\n";
while(choice!='Q'&&choice!='q')
{
cout<<" "<<"*************************赫夫曼编码/译码打印系统*************************\n";
cout<<" "<<"I--------------------------------------------------Init\n";
cout<<" "<<"C--------------------------------------------------Coding\n";
cout<<" "<<"D--------------------------------------------------Decoding\n";
cout<<" "<<"O--------------------------------------------------OutputCode\n";
cout<<" "<<"P--------------------------------------------------PrintHfmTree\n";
cout<<" "<<"Q--------------------------------------------------Exit\n";
cout<<"请输入您要操作的步骤:";
cin>>choice;
if(choice=='I'||choice=='i')
{
cout<<"请输入字符个数:";
cin>>n;
// hfmcoding(HT,HC,n);
htcoding(HT, n);
Tcode(HC,n,HT);
cout<<"hao"<<endl;
for(i=1;i<=n;++i)
{
cout<<HT[i].ch<<":"<<HC[i]<<endl;
}
output_file.open("hfmTree.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=1;i<=n;i++)
{
output_file<<"("<<HT[i].ch<<HC[i]<<")";
}
output_file.close();
cout<<"赫夫曼树已经创建完毕,并且已经放入hfmTree.txt文件中!"<<endl;
}
else if(choice=='C'||choice=='c')
{
printf("请输入字符:");
gets(str);
output_file.open("ToBeTran.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
output_file<<str<<endl;
output_file.close();
output_file.open("CodeFile.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=0;i<strlen(str);i++){
for(j=0;j<=n;++j)
{
if(HT[j].ch==str[i])
{
output_file<<HC[j];
break;
}
}
}
output_file.close();
cout<<"\n";
cout<<"编码完毕,并且已经存入CodeFile.txt文件!\n";
}
else if(choice=='O'||choice=='o')
{
input_file.open("CodeFile.txt");
if(!input_file)
{
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>code;
cout<<"编码码值为:\n";
cout.setf(ios::left);
cout<<setw(50)<<code<<endl;
input_file.close();
}
else if(choice=='D'||choice=='d')
{
input_file.open("CodeFile.txt");
if(!input_file){
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>h;
input_file.close();
output_file.open("Textfile.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
k=0;
while(h[k]!='\0')
{
for(i=1;i<=n;i++)
{
l=k;
for(j=0;j<strlen(HC[i]);j++,l++){
hl[j]=h[l];
}
hl[j]='\0';
if(strcmp(HC[i],hl)==0)
{
output_file<<HT[i].ch;
k=k+strlen(HC[i]);
break;
}
}
}
output_file.close();
input_file.open("Textfile.txt");
if(!input_file){
cout<<"can't oen file!"<<endl;
return 1;
}
input_file>>h;
cout<<h<<endl;
input_file.close();
cout<<"译码结束,字符已经存入Textfile.txt文件中!"<<endl;
}
else if(choice=='P'||choice=='p'){
unsigned char T[100][100];
int i,j,m=0;
FILE *fp;
Convert_tree(T,0,&m,2*n-1);
if((fp=fopen("treeprint.txt","wb+"))==NULL)
printf("Open file treeprint.txt error!\n");
printf("\n以凹入表形式打印已建好的赫夫曼树:\n");
for(i=1;i<=2*n-1;i++)
{
for (j=0;T[i][j]!=0;j++)
{
if(T[i][j]==' ') {printf(" ");fputc(T[i][j],fp);}
else
{printf("%d",T[i][j]);fprintf(fp,"%d\n",T[i][j]);}
}
printf("\n");
}
fclose(fp);
printf("\n此字符形式的哈夫曼树已写入文件treeprint.txt中.\n\n");
}
else if(choice=='Q'||choice=='q')
{
exit(0);
}
else
{
cout<<"您没有输入正确的步骤,请重新输入!"<<endl;
}
cout<<endl;
}
return 0;
}
#5
不好意思啊
这是排好的
这是排好的
#6
下断点,用少量数据进行单步调试
只要问题是固定的,都可以定位到具体哪一行出的问题——除非你不知道每一行代码是什么作用
只要问题是固定的,都可以定位到具体哪一行出的问题——除非你不知道每一行代码是什么作用
#7
读书患不多,思义患不明。患足已不学,既学患不行。
#8
#9
如果排版没错,铁定少个"}"
#1
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include <iomanip.h>
using namespace std;
typedef struct{
char ch;
int weight;
int parent,lchild,rchild;
}htnode,*hfmtree;
typedef char **hfmcode;
hfmtree HT;//全局变量
int select(hfmtree &HT,int n )
{
int flag;
double j=100000;
for(int i=1;i<=n;i++)
{
if(HT[i].weight<=j&&HT[i].parent==0)
{
j=HT[i].weight;
flag=i;
}
}
HT[flag].parent=1;
return flag;
}
void htcoding(hfmtree &HT,int n)
{
if(n<1)
return;
int m=2*n-1;
HT=new htnode [m+1];
for(int i=1;i<=n;++i)
{
cout<<"qingshuru"<<endl;
cin>>HT[i].ch;
cin>>HT[i].weight;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
HT[i].ch='0';
HT[i].weight=0;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
int s1=select(HT,i-1);
int s2=select(HT,i-1);
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;
}
}
void Tcode(hfmcode &hc,int n,hfmtree &HT)
{
hc=new char*[n+1];
char *cd= new char[n];
cd[n-1]='\0';
int c,f;
for(int i=1;i<=n;++i)
{
int start=n-1;
for( 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];
strcpy(hc[i],&cd[start]);
cout<<hc[i]<<endl;
}
delete cd;
}
void Convert_tree(unsigned char T[100][100],int s,int *i,int j)
{
int k,l;
l=++(*i);
for(k=0;k<s;k++)
T[l][k]=' ';
T[l][k]=HT[j].weight;
if(HT[j].lchild) //有左孩子
Convert_tree(T,s+1,i,HT[j].lchild);
if(HT[j].rchild)
Convert_tree(T,s+1,i,HT[j].rchild);
T[l][++k]='\0';
}
int main()
{
char code[100],h[100],hl[100],kk[100];
int n,i,j,k,l;
ifstream input_file;
ofstream output_file;
char choice,str[100];
hfmcode HC;
cout<<"\n";
while(choice!='Q'&&choice!='q')
{
cout<<" "<<"*************************赫夫曼编码/译码打印系统*************************\n";
cout<<" "<<"I--------------------------------------------------Init\n";
cout<<" "<<"C--------------------------------------------------Coding\n";
cout<<" "<<"D--------------------------------------------------Decoding\n";
cout<<" "<<"O--------------------------------------------------OutputCode\n";
cout<<" "<<"P--------------------------------------------------PrintHfmTree\n";
cout<<" "<<"Q--------------------------------------------------Exit\n";
cout<<"请输入您要操作的步骤:";
cin>>choice;
if(choice=='I'||choice=='i')
{
cout<<"请输入字符个数:";
cin>>n;
// hfmcoding(HT,HC,n);
htcoding(HT, n);
Tcode(HC,n,HT);
cout<<"hao"<<endl;
for(i=1;i<=n;++i)
{
cout<<HT[i].ch<<":"<<HC[i]<<endl;
}
output_file.open("hfmTree.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=1;i<=n;i++)
{
output_file<<"("<<HT[i].ch<<HC[i]<<")";
}
output_file.close();
cout<<"赫夫曼树已经创建完毕,并且已经放入hfmTree.txt文件中!"<<endl;
}
else if(choice=='C'||choice=='c')
{
printf("请输入字符:");
gets(str);
output_file.open("ToBeTran.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
output_file<<str<<endl;
output_file.close();
output_file.open("CodeFile.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=0;i<strlen(str);i++){
for(j=0;j<=n;++j)
{
if(HT[j].ch==str[i])
{
output_file<<HC[j];
break;
}
}
}
output_file.close();
cout<<"\n";
cout<<"编码完毕,并且已经存入CodeFile.txt文件!\n";
}
else if(choice=='O'||choice=='o')
{
input_file.open("CodeFile.txt");
if(!input_file)
{
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>code;
cout<<"编码码值为:\n";
cout.setf(ios::left);
cout<<setw(50)<<code<<endl;
input_file.close();
}
else if(choice=='D'||choice=='d')
{
input_file.open("CodeFile.txt");
if(!input_file){
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>h;
input_file.close();
output_file.open("Textfile.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
k=0;
while(h[k]!='\0')
{
for(i=1;i<=n;i++)
{
l=k;
for(j=0;j<strlen(HC[i]);j++,l++){
hl[j]=h[l];
}
hl[j]='\0';
if(strcmp(HC[i],hl)==0)
{
output_file<<HT[i].ch;
k=k+strlen(HC[i]);
break;
}
}
}
output_file.close();
input_file.open("Textfile.txt");
if(!input_file){
cout<<"can't oen file!"<<endl;
return 1;
}
input_file>>h;
cout<<h<<endl;
input_file.close();
cout<<"译码结束,字符已经存入Textfile.txt文件中!"<<endl;
}
else if(choice=='P'||choice=='p'){
unsigned char T[100][100];
int i,j,m=0;
FILE *fp;
Convert_tree(T,0,&m,2*n-1);
if((fp=fopen("treeprint.txt","wb+"))==NULL)
printf("Open file treeprint.txt error!\n");
printf("\n以凹入表形式打印已建好的赫夫曼树:\n");
for(i=1;i<=2*n-1;i++)
{
for (j=0;T[i][j]!=0;j++)
{
if(T[i][j]==' ') {printf(" ");fputc(T[i][j],fp);}
else
{printf("%d",T[i][j]);fprintf(fp,"%d\n",T[i][j]);}
}
printf("\n");
}
fclose(fp);
printf("\n此字符形式的哈夫曼树已写入文件treeprint.txt中.\n\n");
}
else if(choice=='Q'||choice=='q')
{
exit(0);
}
else
{
cout<<"您没有输入正确的步骤,请重新输入!"<<endl;
}
cout<<endl;
}
return 0;
}
#2
这么长的代码还不排版,不排版就算了后面竟然这么大一片空白,所以我就没有怎么看.....
#3
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include <iomanip.h>
using namespace std;
typedef struct{
char ch;
int weight;
int parent,lchild,rchild;
}htnode,*hfmtree;
typedef char **hfmcode;
hfmtree HT;//全局变量
int select(hfmtree &HT,int n )
{
int flag;
double j=100000;
for(int i=1;i<=n;i++)
{
if(HT[i].weight<=j&&HT[i].parent==0)
{
j=HT[i].weight;
flag=i;
}
}
HT[flag].parent=1;
return flag;
}
void htcoding(hfmtree &HT,int n)
{
if(n<1)
return;
int m=2*n-1;
HT=new htnode [m+1];
for(int i=1;i<=n;++i)
{
cout<<"qingshuru"<<endl;
cin>>HT[i].ch;
cin>>HT[i].weight;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
HT[i].ch='0';
HT[i].weight=0;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
int s1=select(HT,i-1);
int s2=select(HT,i-1);
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;
}
}
void Tcode(hfmcode &hc,int n,hfmtree &HT)
{
hc=new char*[n+1];
char *cd= new char[n];
cd[n-1]='\0';
int c,f;
for(int i=1;i<=n;++i)
{
int start=n-1;
for( 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];
strcpy(hc[i],&cd[start]);
cout<<hc[i]<<endl;
}
delete cd;
}
void Convert_tree(unsigned char T[100][100],int s,int *i,int j)
{
int k,l;
l=++(*i);
for(k=0;k<s;k++)
T[l][k]=' ';
T[l][k]=HT[j].weight;
if(HT[j].lchild) //有左孩子
Convert_tree(T,s+1,i,HT[j].lchild);
if(HT[j].rchild)
Convert_tree(T,s+1,i,HT[j].rchild);
T[l][++k]='\0';
}
int main()
{
char code[100],h[100],hl[100],kk[100];
int n,i,j,k,l;
ifstream input_file;
ofstream output_file;
char choice,str[100];
hfmcode HC;
cout<<"\n";
while(choice!='Q'&&choice!='q')
{
cout<<" "<<"*************************赫夫曼编码/译码打印系统*************************\n";
cout<<" "<<"I--------------------------------------------------Init\n";
cout<<" "<<"C--------------------------------------------------Coding\n";
cout<<" "<<"D--------------------------------------------------Decoding\n";
cout<<" "<<"O--------------------------------------------------OutputCode\n";
cout<<" "<<"P--------------------------------------------------PrintHfmTree\n";
cout<<" "<<"Q--------------------------------------------------Exit\n";
cout<<"请输入您要操作的步骤:";
cin>>choice;
if(choice=='I'||choice=='i')
{
cout<<"请输入字符个数:";
cin>>n;
// hfmcoding(HT,HC,n);
htcoding(HT, n);
Tcode(HC,n,HT);
cout<<"hao"<<endl;
for(i=1;i<=n;++i)
{
cout<<HT[i].ch<<":"<<HC[i]<<endl;
}
output_file.open("hfmTree.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=1;i<=n;i++)
{
output_file<<"("<<HT[i].ch<<HC[i]<<")";
}
output_file.close();
cout<<"赫夫曼树已经创建完毕,并且已经放入hfmTree.txt文件中!"<<endl;
}
else if(choice=='C'||choice=='c')
{
printf("请输入字符:");
gets(str);
output_file.open("ToBeTran.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
output_file<<str<<endl;
output_file.close();
output_file.open("CodeFile.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=0;i<strlen(str);i++){
for(j=0;j<=n;++j)
{
if(HT[j].ch==str[i])
{
output_file<<HC[j];
break;
}
}
}
output_file.close();
cout<<"\n";
cout<<"编码完毕,并且已经存入CodeFile.txt文件!\n";
}
else if(choice=='O'||choice=='o')
{
input_file.open("CodeFile.txt");
if(!input_file)
{
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>code;
cout<<"编码码值为:\n";
cout.setf(ios::left);
cout<<setw(50)<<code<<endl;
input_file.close();
}
else if(choice=='D'||choice=='d')
{
input_file.open("CodeFile.txt");
if(!input_file){
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>h;
input_file.close();
output_file.open("Textfile.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
k=0;
while(h[k]!='\0')
{
for(i=1;i<=n;i++)
{
l=k;
for(j=0;j<strlen(HC[i]);j++,l++){
hl[j]=h[l];
}
hl[j]='\0';
if(strcmp(HC[i],hl)==0)
{
output_file<<HT[i].ch;
k=k+strlen(HC[i]);
break;
}
}
}
output_file.close();
input_file.open("Textfile.txt");
if(!input_file){
cout<<"can't oen file!"<<endl;
return 1;
}
input_file>>h;
cout<<h<<endl;
input_file.close();
cout<<"译码结束,字符已经存入Textfile.txt文件中!"<<endl;
}
else if(choice=='P'||choice=='p'){
unsigned char T[100][100];
int i,j,m=0;
FILE *fp;
Convert_tree(T,0,&m,2*n-1);
if((fp=fopen("treeprint.txt","wb+"))==NULL)
printf("Open file treeprint.txt error!\n");
printf("\n以凹入表形式打印已建好的赫夫曼树:\n");
for(i=1;i<=2*n-1;i++)
{
for (j=0;T[i][j]!=0;j++)
{
if(T[i][j]==' ') {printf(" ");fputc(T[i][j],fp);}
else
{printf("%d",T[i][j]);fprintf(fp,"%d\n",T[i][j]);}
}
printf("\n");
}
fclose(fp);
printf("\n此字符形式的哈夫曼树已写入文件treeprint.txt中.\n\n");
}
else if(choice=='Q'||choice=='q')
{
exit(0);
}
else
{
cout<<"您没有输入正确的步骤,请重新输入!"<<endl;
}
cout<<endl;
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include <iomanip.h>
using namespace std;
typedef struct{
char ch;
int weight;
int parent,lchild,rchild;
}htnode,*hfmtree;
typedef char **hfmcode;
hfmtree HT;//全局变量
int select(hfmtree &HT,int n )
{
int flag;
double j=100000;
for(int i=1;i<=n;i++)
{
if(HT[i].weight<=j&&HT[i].parent==0)
{
j=HT[i].weight;
flag=i;
}
}
HT[flag].parent=1;
return flag;
}
void htcoding(hfmtree &HT,int n)
{
if(n<1)
return;
int m=2*n-1;
HT=new htnode [m+1];
for(int i=1;i<=n;++i)
{
cout<<"qingshuru"<<endl;
cin>>HT[i].ch;
cin>>HT[i].weight;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
HT[i].ch='0';
HT[i].weight=0;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
int s1=select(HT,i-1);
int s2=select(HT,i-1);
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;
}
}
void Tcode(hfmcode &hc,int n,hfmtree &HT)
{
hc=new char*[n+1];
char *cd= new char[n];
cd[n-1]='\0';
int c,f;
for(int i=1;i<=n;++i)
{
int start=n-1;
for( 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];
strcpy(hc[i],&cd[start]);
cout<<hc[i]<<endl;
}
delete cd;
}
void Convert_tree(unsigned char T[100][100],int s,int *i,int j)
{
int k,l;
l=++(*i);
for(k=0;k<s;k++)
T[l][k]=' ';
T[l][k]=HT[j].weight;
if(HT[j].lchild) //有左孩子
Convert_tree(T,s+1,i,HT[j].lchild);
if(HT[j].rchild)
Convert_tree(T,s+1,i,HT[j].rchild);
T[l][++k]='\0';
}
int main()
{
char code[100],h[100],hl[100],kk[100];
int n,i,j,k,l;
ifstream input_file;
ofstream output_file;
char choice,str[100];
hfmcode HC;
cout<<"\n";
while(choice!='Q'&&choice!='q')
{
cout<<" "<<"*************************赫夫曼编码/译码打印系统*************************\n";
cout<<" "<<"I--------------------------------------------------Init\n";
cout<<" "<<"C--------------------------------------------------Coding\n";
cout<<" "<<"D--------------------------------------------------Decoding\n";
cout<<" "<<"O--------------------------------------------------OutputCode\n";
cout<<" "<<"P--------------------------------------------------PrintHfmTree\n";
cout<<" "<<"Q--------------------------------------------------Exit\n";
cout<<"请输入您要操作的步骤:";
cin>>choice;
if(choice=='I'||choice=='i')
{
cout<<"请输入字符个数:";
cin>>n;
// hfmcoding(HT,HC,n);
htcoding(HT, n);
Tcode(HC,n,HT);
cout<<"hao"<<endl;
for(i=1;i<=n;++i)
{
cout<<HT[i].ch<<":"<<HC[i]<<endl;
}
output_file.open("hfmTree.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=1;i<=n;i++)
{
output_file<<"("<<HT[i].ch<<HC[i]<<")";
}
output_file.close();
cout<<"赫夫曼树已经创建完毕,并且已经放入hfmTree.txt文件中!"<<endl;
}
else if(choice=='C'||choice=='c')
{
printf("请输入字符:");
gets(str);
output_file.open("ToBeTran.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
output_file<<str<<endl;
output_file.close();
output_file.open("CodeFile.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=0;i<strlen(str);i++){
for(j=0;j<=n;++j)
{
if(HT[j].ch==str[i])
{
output_file<<HC[j];
break;
}
}
}
output_file.close();
cout<<"\n";
cout<<"编码完毕,并且已经存入CodeFile.txt文件!\n";
}
else if(choice=='O'||choice=='o')
{
input_file.open("CodeFile.txt");
if(!input_file)
{
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>code;
cout<<"编码码值为:\n";
cout.setf(ios::left);
cout<<setw(50)<<code<<endl;
input_file.close();
}
else if(choice=='D'||choice=='d')
{
input_file.open("CodeFile.txt");
if(!input_file){
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>h;
input_file.close();
output_file.open("Textfile.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
k=0;
while(h[k]!='\0')
{
for(i=1;i<=n;i++)
{
l=k;
for(j=0;j<strlen(HC[i]);j++,l++){
hl[j]=h[l];
}
hl[j]='\0';
if(strcmp(HC[i],hl)==0)
{
output_file<<HT[i].ch;
k=k+strlen(HC[i]);
break;
}
}
}
output_file.close();
input_file.open("Textfile.txt");
if(!input_file){
cout<<"can't oen file!"<<endl;
return 1;
}
input_file>>h;
cout<<h<<endl;
input_file.close();
cout<<"译码结束,字符已经存入Textfile.txt文件中!"<<endl;
}
else if(choice=='P'||choice=='p'){
unsigned char T[100][100];
int i,j,m=0;
FILE *fp;
Convert_tree(T,0,&m,2*n-1);
if((fp=fopen("treeprint.txt","wb+"))==NULL)
printf("Open file treeprint.txt error!\n");
printf("\n以凹入表形式打印已建好的赫夫曼树:\n");
for(i=1;i<=2*n-1;i++)
{
for (j=0;T[i][j]!=0;j++)
{
if(T[i][j]==' ') {printf(" ");fputc(T[i][j],fp);}
else
{printf("%d",T[i][j]);fprintf(fp,"%d\n",T[i][j]);}
}
printf("\n");
}
fclose(fp);
printf("\n此字符形式的哈夫曼树已写入文件treeprint.txt中.\n\n");
}
else if(choice=='Q'||choice=='q')
{
exit(0);
}
else
{
cout<<"您没有输入正确的步骤,请重新输入!"<<endl;
}
cout<<endl;
}
return 0;
}
#4
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include <iomanip.h>
using namespace std;
typedef struct{
char ch;
int weight;
int parent,lchild,rchild;
}htnode,*hfmtree;
typedef char **hfmcode;
hfmtree HT;//全局变量
int select(hfmtree &HT,int n )
{
int flag;
double j=100000;
for(int i=1;i<=n;i++)
{
if(HT[i].weight<=j&&HT[i].parent==0)
{
j=HT[i].weight;
flag=i;
}
}
HT[flag].parent=1;
return flag;
}
void htcoding(hfmtree &HT,int n)
{
if(n<1)
return;
int m=2*n-1;
HT=new htnode [m+1];
for(int i=1;i<=n;++i)
{
cout<<"qingshuru"<<endl;
cin>>HT[i].ch;
cin>>HT[i].weight;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
HT[i].ch='0';
HT[i].weight=0;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(i=n+1;i<m+1;i++)
{
int s1=select(HT,i-1);
int s2=select(HT,i-1);
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;
}
}
void Tcode(hfmcode &hc,int n,hfmtree &HT)
{
hc=new char*[n+1];
char *cd= new char[n];
cd[n-1]='\0';
int c,f;
for(int i=1;i<=n;++i)
{
int start=n-1;
for( 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];
strcpy(hc[i],&cd[start]);
cout<<hc[i]<<endl;
}
delete cd;
}
void Convert_tree(unsigned char T[100][100],int s,int *i,int j)
{
int k,l;
l=++(*i);
for(k=0;k<s;k++)
T[l][k]=' ';
T[l][k]=HT[j].weight;
if(HT[j].lchild) //有左孩子
Convert_tree(T,s+1,i,HT[j].lchild);
if(HT[j].rchild)
Convert_tree(T,s+1,i,HT[j].rchild);
T[l][++k]='\0';
}
int main()
{
char code[100],h[100],hl[100],kk[100];
int n,i,j,k,l;
ifstream input_file;
ofstream output_file;
char choice,str[100];
hfmcode HC;
cout<<"\n";
while(choice!='Q'&&choice!='q')
{
cout<<" "<<"*************************赫夫曼编码/译码打印系统*************************\n";
cout<<" "<<"I--------------------------------------------------Init\n";
cout<<" "<<"C--------------------------------------------------Coding\n";
cout<<" "<<"D--------------------------------------------------Decoding\n";
cout<<" "<<"O--------------------------------------------------OutputCode\n";
cout<<" "<<"P--------------------------------------------------PrintHfmTree\n";
cout<<" "<<"Q--------------------------------------------------Exit\n";
cout<<"请输入您要操作的步骤:";
cin>>choice;
if(choice=='I'||choice=='i')
{
cout<<"请输入字符个数:";
cin>>n;
// hfmcoding(HT,HC,n);
htcoding(HT, n);
Tcode(HC,n,HT);
cout<<"hao"<<endl;
for(i=1;i<=n;++i)
{
cout<<HT[i].ch<<":"<<HC[i]<<endl;
}
output_file.open("hfmTree.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=1;i<=n;i++)
{
output_file<<"("<<HT[i].ch<<HC[i]<<")";
}
output_file.close();
cout<<"赫夫曼树已经创建完毕,并且已经放入hfmTree.txt文件中!"<<endl;
}
else if(choice=='C'||choice=='c')
{
printf("请输入字符:");
gets(str);
output_file.open("ToBeTran.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
output_file<<str<<endl;
output_file.close();
output_file.open("CodeFile.txt");
if(!output_file){
cout<<"can't oen file!"<<endl;
return 1;
}
for(i=0;i<strlen(str);i++){
for(j=0;j<=n;++j)
{
if(HT[j].ch==str[i])
{
output_file<<HC[j];
break;
}
}
}
output_file.close();
cout<<"\n";
cout<<"编码完毕,并且已经存入CodeFile.txt文件!\n";
}
else if(choice=='O'||choice=='o')
{
input_file.open("CodeFile.txt");
if(!input_file)
{
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>code;
cout<<"编码码值为:\n";
cout.setf(ios::left);
cout<<setw(50)<<code<<endl;
input_file.close();
}
else if(choice=='D'||choice=='d')
{
input_file.open("CodeFile.txt");
if(!input_file){
cout<<"can't open file!"<<endl;
return 1;
}
input_file>>h;
input_file.close();
output_file.open("Textfile.txt");
if(!output_file)
{
cout<<"can't oen file!"<<endl;
return 1;
}
k=0;
while(h[k]!='\0')
{
for(i=1;i<=n;i++)
{
l=k;
for(j=0;j<strlen(HC[i]);j++,l++){
hl[j]=h[l];
}
hl[j]='\0';
if(strcmp(HC[i],hl)==0)
{
output_file<<HT[i].ch;
k=k+strlen(HC[i]);
break;
}
}
}
output_file.close();
input_file.open("Textfile.txt");
if(!input_file){
cout<<"can't oen file!"<<endl;
return 1;
}
input_file>>h;
cout<<h<<endl;
input_file.close();
cout<<"译码结束,字符已经存入Textfile.txt文件中!"<<endl;
}
else if(choice=='P'||choice=='p'){
unsigned char T[100][100];
int i,j,m=0;
FILE *fp;
Convert_tree(T,0,&m,2*n-1);
if((fp=fopen("treeprint.txt","wb+"))==NULL)
printf("Open file treeprint.txt error!\n");
printf("\n以凹入表形式打印已建好的赫夫曼树:\n");
for(i=1;i<=2*n-1;i++)
{
for (j=0;T[i][j]!=0;j++)
{
if(T[i][j]==' ') {printf(" ");fputc(T[i][j],fp);}
else
{printf("%d",T[i][j]);fprintf(fp,"%d\n",T[i][j]);}
}
printf("\n");
}
fclose(fp);
printf("\n此字符形式的哈夫曼树已写入文件treeprint.txt中.\n\n");
}
else if(choice=='Q'||choice=='q')
{
exit(0);
}
else
{
cout<<"您没有输入正确的步骤,请重新输入!"<<endl;
}
cout<<endl;
}
return 0;
}
#5
不好意思啊
这是排好的
这是排好的
#6
下断点,用少量数据进行单步调试
只要问题是固定的,都可以定位到具体哪一行出的问题——除非你不知道每一行代码是什么作用
只要问题是固定的,都可以定位到具体哪一行出的问题——除非你不知道每一行代码是什么作用
#7
读书患不多,思义患不明。患足已不学,既学患不行。
#8
#9
如果排版没错,铁定少个"}"