工程业务风险:数据结构/C++高手请进>>>>>>>>>

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/09 10:30:00
题目1 算术表达式计算 题目2 停车场管理模拟
题目3 最小生成树问题 题目4 哈夫曼编/译码器设计
题目5 校园导游系统模拟 题目6 稀疏矩阵运算
题目7 银行业务模拟 题目8 航空订票模拟
我想求教一个上面8个题目中任意一个的程序代码,希望那个高手能帮我设计编写一个程序,急用谢谢.(为表是感谢,如果程序合格,赠送四代QQ宠物蛋一个,高属性哦 1200++)
希望能给我写出设计的思路,偶也好理解学习,尽量弄懂.....谢谢了

#include<iostream.h>
#include<stdio.h>
#include<malloc.h>

typedef struct bnode{

bnode * lchild;
bnode * rchild;
char data;
int weight;
}* btree;
struct elemtype{//哈夫曼编码表元素
char ch;
int weight;
int code[10];

};
int N=0,m=0;
int CreateBitree(btree &T,FILE *fp){//前序还原
char ch;
ch=getc(fp);
if(ch=='#') T=NULL;
else{
T=(bnode*)new bnode;
if(!T)return 0;
T->data=ch;
CreateBitree(T->lchild,fp);
CreateBitree(T->rchild,fp);
}
return 1;
}
void preordertraverse(btree T,char *str){//前序遍历哈夫曼树
if(T!=NULL){
cout<<T->data;str[m]=T->data;m++;
preordertraverse(T->lchild,str);
preordertraverse(T->rchild,str);
}

}
void savetree(char *str){将树存入文件
FILE *fp=fopen("1.txt","w");
for(int i=0;i<m;i++){
fputc(str[i],fp);}

fclose(fp);
}
void createhuffman(btree& T,elemtype ** a,int n){创建哈夫曼树
int i=0,j,k1,k2;btree * b;btree q;
b=(btree * )malloc(n*sizeof(btree));
for(i=0;i<n;i++){
b[i]=(bnode *)malloc(sizeof(bnode));
b[i]->data=a[i]->ch;
b[i]->weight=a[i]->weight;
b[i]->lchild=b[i]->rchild=NULL;

}
for(i=1;i<n;i++){
k1=-1;
for(j=0;j<n;j++){
if(b[j]!=NULL&&k1==-1){k1=j;continue;}
if(b[j]!=NULL){k2=j;break;}
}
for(j=0;j<n;j++){
if(b[j]!=NULL){
if(b[j]->weight<=b[k1]->weight){
k1=j;}

}
}
if(k1==k2){
for(j=0;j<n;j++){
if(b[j]!=NULL&&j!=k1){k2=j;break;}
}
}
for(j=0;j<n;j++){
if(b[j]!=NULL&&j!=k1){
if(b[j]->weight<=b[k2]->weight){
k2=j;}
}
}

q=(bnode *)malloc(sizeof(bnode));
q->data='#';q->lchild=b[k1];q->rchild=b[k2];
q->weight=b[k1]->weight+b[k2]->weight;
b[k1]=q;b[k2]=NULL;
}
free(b);
T=q;
}
void huffmancoding(btree T,int len,elemtype **codes){生成编码表
static int a[10];
if(T!=NULL){
if(T->lchild==NULL&&T->rchild==NULL){
int i;
printf("%c------",T->data);
codes[N]=(elemtype *)malloc(sizeof(elemtype));
codes[N]->ch=T->data;codes[N]->weight=0;
for(i=0;i<10;i++)
codes[N]->code[i]=2;
for(i=0;i<len;i++){
printf("%d",a[i]);
codes[N]->code[i]=a[i];
}
N++;
printf("\n");
}
else{
a[len]=0;huffmancoding(T->lchild,len+1,codes);
a[len]=1;huffmancoding(T->rchild,len+1,codes);
}
}
}
void transcodes(btree T,char *str){译码
btree p=T;int i=0;

while(str[i]!=0){
if(p!=NULL){
if(p->lchild==NULL&&p->rchild==NULL){
cout<<p->data;
p=T;

}
else{
if(str[i]=='0'){p=p->lchild;}
if(str[i]=='1'){p=p->rchild;}
i++;
if(str[i]==0){cout<<p->data<<endl;break;}

}

}
}
}

void coding(char *str,elemtype **codes){编码函数
int i=0,j;FILE *fp=NULL;

fp=fopen("2.txt","w");
while(str[i]!=0){
for(j=0;j<=N;j++){
if(str[i]==codes[j]->ch){

for(int k=0;k<10;k++){
if(codes[j]->code[k]==2)break;

cout<<codes[j]->code[k];

fputc(codes[j]->code[k]+48,fp);
}
break;
}
}

if(j>N)
cout<<"文中没有此字母"<<endl;

i++;
}
if(fp!=NULL){fputc(10,fp);fclose(fp);}

}
void initlize(char * str,btree & T,elemtype ** codes,char *s){//函数调用及初始输入字符串中各字符的权值
int i=0,j,n=0,k=0;elemtype* a[20];

for(j=0;j<20;j++)
a[j]=NULL;
while(str[i]!=0){
for(j=0;j<i;j++){
if(str[j]==str[i]){
for(int h=0;h<n;h++){
if(a[h]->ch==str[j])break;}
a[h]->weight++;break;}
}
if(j>=i){
a[k]=(elemtype *)new elemtype;a[k]->ch=str[i];a[k]->weight=1;n++;k++;}
i++;
}
cout<<n<<endl;
for(j=0;j<n;j++){
cout<<a[j]->ch<<" "<<a[j]->weight<<endl;}

createhuffman(T,a,n);
preordertraverse(T,s);
cout<<endl;
savetree(s);
k=0;
cout<<"字符编码表如下:"<<endl;
huffmancoding(T,k,codes);
}

//////////////////////////////////////////////////////////////////////////////////

void main(){
btree t=NULL;
elemtype* b[26];//存放编码表
char str[100];//存放输入字符串
char s[50];//存放哈夫曼树
cout<<"请输入要编码的文本:(不包括空格)"<<endl;
cin>>str;
initlize(str,t,b,s);
coding(str,b);
cout<<endl;
for(int i=0;i<N;i++){
cout<<b[i]->ch<<b[i]->weight<<" ";
for(int k=0;k<10;k++){
if(b[i]->code[k]==2)break;
cout<<b[i]->code[k];}
cout<<endl;}
cout<<"请输入要解码的0 1串"<<endl;
cin>>str;
transcodes(t,str);
char c;
cin>>c;
}