Huffman main procedure
Submitted by Daryle on Thu, 05/17/2012 - 13:24
Filename: | huffman.cpp |
Project Name: | Huffman Encoding Trees |
Language: | C++ |
Description: |
This program reads a text file of the format: "c0.ffffN" where "c" is a single ASCII character, "0.ffff" is a floating point number, and N is a new-line character. The program creates a binary tree, called the "List Tree", of Symbol objects for each line/character in the file. Once all lines have been read, and the binary tree constructed, the Huffman table is generated. This process involves: searching the List Tree for the lowest probability symbol, marking this symbol as read, and then moving it to a new tree called the Huffman Tree. Once all symbols have been marked are moved in this manner, the new tree is outputted to a second file, and the program terminates. |
Code:
#include <assert.h> #include <new.h> #include <stdlib.h> #include <fstream.h> #include <iostream.h> #include <iomanip.h> #include <string.h> #include "Symbol.h" /*=========================================================== * Name: Daryle Niedermayer * Date: November 1, 1997 * Desc: This program reads a text file of the format: "c0.ffffN" where "c" is a single ASCII character, "ffff" is a floating point number, and N is a new- line character. The program creates a binary tree, called the "List Tree", of Symbol objects for each line/character in the file. Once all lines have been read, and the binary tree constructed, the Huffman table is generated. This process involves: searching the List Tree for the lowest probability symbol, marking this symbol as read, and then moving it to a new tree called the Huffman Tree. Once all symbols have been marked are moved in this manner, the new tree is outputted to a second file, and the program terminates. ==============================================================*/ int main() { int Count=0; //Count of symbols to process char Infile[40]; //Name of Input file to read char Outfile[40]; //Name of Output file to write char Buffer[80]; char CurrentCharacter; //Values for the current read line float CurrentProb; Tree List; //Create the ListTree Tree Huffman; //Create the Huffman Table ifstream INPUT; //Declare input and output filestreams ofstream OUTPUT; /*============================================= == SET UP FILE STREAMS FOR READING AND WRITING =============================================*/ cout << "\nEnter an input filename: "; cin >> Infile; cout << "\nEnter an output filename: "; cin >> Outfile; INPUT.open(Infile,ios::in); OUTPUT.open(Outfile,ios::out); /*============================================= == BEGIN CREATING LIST TREE =============================================*/ while (INPUT.eof() == false) { INPUT.getline(Buffer,80,'\n'); CurrentCharacter=Buffer[0]; CurrentProb=atof(Buffer+1); istream &flush(); List.insertSymbol(CurrentCharacter,CurrentProb); } /*============================================= == MOVE SYMBOLS TO HUFFMAN TREE & ENCODE =============================================*/ List.moveSymbols(&Huffman); Huffman.assignBits(); Huffman.printTree(); /*============================================= == WRITE ENCODING TABLE FILE =============================================*/ Huffman.writeTable(&OUTPUT); return 0; }