哈夫曼编码译码器实验报告

《数据结构》课程设计报告

QFileInfo hufFileInfo; };

HuffView负责具体画图

class HuffView : public QGraphicsView {

Q_OBJECT public:

explicit HuffView(QWidget *parent = 0); //矩形长度常量

enum{ RectLen = 50 }; //清空

void clear(); public slots:

//画图。参数依次为:树的节点,x坐标,y坐标,层树。 void initScene( TreeNode*, int, int, int); protected:

//重写双击事件,允许放大缩小

void mouseDoubleClickEvent(QMouseEvent *event); private:

//画矩形,画线

void drawRect(TreeNode*, int, int); void drawLine(int,int,int,int); QGraphicsScene* scene; };

Coder主要算法:

 参数验证

 读文本文件,计算输入次数,构建charTable  根据charTable构造huffman树

 将charTable复制到临时字符表tmp,使用自定义的快速排序对tmp排序  根据排序后的tmp,构造TreeNode*数组tmp2  在treeNode*数组tmp2上构造huffman树  将构造好的huffman树的根节点指针赋值

 根据huffman树构造subTable,得到字符-编码的键值对  输出img文件,输出编码后的huf文件

Decoder要主算法

 参数验证

 读取img文件,构造charTable  通过charTable构造huffman树

 根据huffman树,解码文件,输出

//display mapping table

QByteArray tmpda = tmpFileName.toLatin1(); const char* _tmpda = tmpda.data();

FILE* tmpfp = fopen(_tmpda, "r"); unlink(_tmpda);

QTextStream* tmpFile = new QTextStream(tmpfp); textEdit->clear();

textEdit->setText(tmpFile->readAll()); fclose(tmpfp); delete tmpFile;

//display the rate showRate(); //load OK

QString* _msg = new QString(txtFileInfo.fileName()); emit(loadFileOk(*_msg)); }

void MainWidget::openExportFileDialog(){

if(txtFileInfo.canonicalFilePath().isEmpty() txtFileInfo.canonicalFilePath().isNull()) {

QMessageBox qm;

qm.setWindowTitle(tr("Warning"));

qm.setText("You should import a file first."); qm.setIcon(QMessageBox::Warning); qm.exec(); return; }

bool ok;

QString outFileName = QInputDialog::getText(this, tr("Input name:"), tr("File name:"), QLineEdit::Normal, tr(""), &ok); if(!ok ) return;

if(outFileName.isEmpty() || outFileName.isNull()) outFileName = hufFileInfo.fileName()+".txt"; QString tmp = outFileName;

QString tmp1 = imgFileInfo.canonicalFilePath(); QString tmp2 = hufFileInfo.canonicalFilePath();

QString cmd = tr("./decoder -o ") + tmp.replace(" ").replace("(","\\(").replace(")","\\)")+tr(" ") + tmp1.replace(" ", ").replace("(","\\(").replace(")","\\)") + tr(" ") + tmp2.replace(" ", ").replace("(","\\(").replace(")","\\)"); QByteArray da = cmd.toLatin1(); const char* _da = da.data(); system(_da);

emit(exportFileOk(outFileName)); }

the file ",


© 2024 实用范文网 | 联系我们: webmaster# 6400.net.cn