#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include "ctype.h"
#include <stdio.h>
#include "BaseOperator.h"
#include "SaveType.h"
#define n 27
void display1();
void main()
{
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! 欢迎进入哈夫曼编_译码操作界面! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! !"<<endl;
cout<<"! 制作单位:计算机科学与技术(2)班 !"<<endl;
cout<<"! 学号:2003040140221 !"<<endl;
cout<<"! 作者:刘金宏 !"<<endl;
cout<<"! !"<<endl;
cout<<"! 提示:按ENTER进入操作! !"<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
getchar();
display1();
}
void display1(){
char i;
char ch;
Huffman huffman;
HuffmanTree HT;
FILE *fp;
loop:
system("cls");
cout<<endl;
cout<<"特别提示::请用'#'代替空格键!!"<<endl;
cout<<"*******************************************************************************"<<endl;
cout<<"* 0 : 退出 *"<<endl;
cout<<"* *"<<endl;
cout<<"* *"<<endl;
cout<<"* 1 : 编码! 4 : 译码 *"<<endl;
cout<<"* *"<<endl;
cout<<"* *"<<endl;
cout<<"* 2 :重建哈夫曼树! 5 :查看你的所有编码或译码! *"<<endl;
cout<<"* *"<<endl;
cout<<"* *"<<endl;
cout<<"* 3 :查看哈夫曼树! 6 : 删除你的所有编码或译码! *"<<endl;
cout<<"* *"<<endl;
cout<<"*******************************************************************************"<<endl;
cout<<endl;cout<<endl;
cout<<" 从上面选择一个你所要的操作!"<<endl;
cout<<endl;
cin>>i;
system("cls");
switch(i){
case '0':exit(0);
case '1':{ //编码
fp=fopen("TreeCode.exe","rb"); //打开存放哈夫曼树的文件
huffman.InitHuffmanTree(HT,n); //初始化一个哈夫曼存储结构用来存放读入的文件
for(i=1;i<=2*n-1;i++){
fread(&HT[i],sizeof(struct HTNode),1,fp); //读入,并存放在以HT命名的结构中
}
huffman.HuffmanEnconding(HT,n); //编码并保存在一个‘TextFile.text’的文件
huffman.HuffmanDeconding(HT,n); //把刚刚的编码译成字符以便确定是否编码成功
cout<<endl;
cout<<" 你刚才建立的编码列表"<<endl;cout<<endl;
fp=fopen("TextFile.text","r");
ch=fgetc(fp);
while(ch!=EOF){
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);cout<<endl;cout<<endl;cout<<endl;
cout<<" 按ENTER进入主操作界面!"<<endl;
getchar();
goto loop;
}
case '2':{ //建立哈夫曼树
loop3:
cout<<endl;
cout<<" 你确定要重新建立一个哈夫曼树吗? Y/N"<<endl;
cin>>ch;
system("cls"); cout<<endl;cout<<endl;
switch(ch){
case 'Y':
case 'y':{
huffman.InitHuffmanTree(HT,n); //初始化
cout<<" 建立哈夫曼树!"<<endl;cout<<endl;
huffman.CreatHuffmanTree(HT,n); //建立哈夫曼树
huffman.SaveHuffmanTree(HT,n); //保存新建的哈夫曼树
break;
}
case 'N':
case 'n':{
cout<<" 你取消了重建哈夫曼树的操作!"<<endl;
break;
}
default: goto loop3;
}
cout<<"按ENTER进入主操作界面!"<<endl;
getchar();
goto loop;
}
case '3':{ //查看哈夫曼树
fp=fopen("TreeCode.exe","rb"); //打开存放哈夫曼树的文件
huffman.InitHuffmanTree(HT,n); //初始化一个哈夫曼存储结构用来存放读入的文件
for(i=1;i<=2*n-1;i++)
fread(&HT[i],sizeof(struct HTNode),1,fp); //读入,并存放在以HT命名的结构中
cout<<endl<<" 你的哈夫曼树如下:"<<endl;
huffman.SaveTreePrint(HT,n); //用凹凸法显示哈夫曼树
cout<<"按ENTER进入主操作界面!"<<endl;
getchar();
goto loop;
}
case '4':{ //译码
fp=fopen("TreeCode.exe","rb"); //打开存放哈夫曼树的文件
huffman.InitHuffmanTree(HT,n); //初始化一个哈夫曼存储结构用来存放读入的文件
for(i=1;i<=2*n-1;i++){
fread(&HT[i],sizeof(struct HTNode),1,fp); //读入,并存放在以HT命名的结构中
}
huffman.HuffmanDeconding(HT,n); //进行译码操作
cout<<" 全部译码为:"<<endl;
fp=fopen("AllTextFile.text","r"); //从文件中读入你刚才输入的字符的全部译码
ch=fgetc(fp);
while(ch!=EOF){
putchar(ch);
ch=fgetc(fp);
}
cout<<endl;cout<<endl;cout<<endl;
cout<<"按ENTER进入主操作界面!"<<endl;
getchar();
goto loop;
}
case '5':{ //查看
loop1:
cout<<" 你要查看所有的编码还是译码?"<<endl;cout<<endl;
cout<<" 1.查看编码 2.查看译码 "<<endl;cout<<endl;
cin>>ch;
switch(ch){
case '1':{
cout<<"你所编译的全部编码是:"<<endl;cout<<endl;
if(!(fp=fopen("AllCodeFile.text","r")))
cout<<" 文件不存在!"<<endl;cout<<endl;
ch=fgetc(fp);
while(ch!=EOF){
putchar(ch);
ch=fgetc(fp);
}
break;
}
case '2':{
cout<<"你所翻译的全部代码是:"<<endl;cout<<endl;
if(!(fp=fopen("AllTextFile.text","r")))
cout<<" 文件不存在!"<<endl;cout<<endl;
ch=fgetc(fp);
while(