Справочник функций

Ваш аккаунт

Войти через: 
Забыли пароль?
Регистрация
Информацию о новых материалах можно получать и без регистрации:

Почтовая рассылка

Подписчиков: -1
Последний выпуск: 19.06.2015

помогите, пожалуйста,с программами

10.0K
20 февраля 2005 года
riichi
1 / / 20.02.2005
я попытался переписать кодировщик и декодировщик хафмана с С на С плюс плюс но ничего из этого не вышло. Помогите мне привести в божеский вид эти программы. Очень нужно.
Это кодировщик:

#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <stdlib.h>

#define VERBOSE /* If defined, prints verbose
program progress when it's
running... */

short father[512];
unsigned short code[256], heap_length;
unsigned long compress_charcount, file_size, heap[257], loop;
unsigned char code_length[256];
long frequency_count[512];
unsigned int thebyte = 0;
short loop1;
unsigned short current_code;

unsigned short current_length, dvalue;
unsigned long curbyte = 0;
short curbit = 7;

float savings;
unsigned short header_charcount;
unsigned long output_characters;

unsigned short loop2;
unsigned short current_bit;

unsigned short bitcode;
short parent;


void main (int argc,char *argv)
{
unsigned short generate_code_table ();
void build_code_tree ();
void build_initial_heap ();
void compress_image ();
void compression_report ();
void get_frequency_count ();


if (argc != 3)
{
cout<<"Benutzung: hpk <quelldatei> <ausgabedatei>\n";
exit(1);
}

cout<<"\nhpk: Huffman Package Program."
"\n Richard Peters. Version 1.0\n\n";

ifstream ifile(argv[1]);
ofstream ofile(argv[2]);

if (!ifile){
cout<<"Fehler! Datei "<<argv[1]<<" nicht gefunden.\n";
exit(1);
}

ifile.seekg(2);
file_size = (unsigned long) ifile.tellg();

#ifdef VERBOSE
cout<<"(1) Getting Frequency Counts.\n";
#endif

ifile.seekg(0);
for (loop = 0; loop < file_size; loop++){
frequency_count[ifile.get()]++;
}

#ifdef VERBOSE
cout<<"(2) Building Initial Heap.\n";
#endif

build_initial_heap ();

#ifdef VERBOSE
cout<<"(3) Building the Code Tree.\n";
#endif

build_code_tree ();

#ifdef VERBOSE
cout<<"(4) Generating the Code Table.\n";
#endif

if(!generate_code_table())
cout<<"ERROR! Code Value Out of Range. Cannot Compress.\n";
else{
#ifdef VERBOSE
cout<<"(5) Compressing & Creating the Output File.\n";
#endif
ofile.write((char *)&file_size, sizeof (file_size));
ofile.write((char *)&code, 256);
ofile.write((char *)&code_length, 256);
ifile.seekg(0);
for (loop = 0L; loop < file_size; loop++){
dvalue = (unsigned short) ifile.get();
current_code = code[dvalue];
current_length = (unsigned short) code_length[dvalue];
for (loop1 = current_length-1; loop1 >= 0; --loop1){
if ((current_code >> loop1) & 1)
thebyte |= (char) (1 << curbit);
if (--curbit < 0){
ofile.put((char)thebyte);
thebyte = 0;
curbyte++;
curbit = 7;
}
}
}
ofile.put((char)thebyte);
compress_charcount = ++curbyte;

ofile.close();

#ifdef VERBOSE
header_charcount = 768 + sizeof (file_size);
output_characters = (unsigned long) header_charcount + compress_charcount;

cout<< "\nRaw characters : "<<file_size;
cout<< "\nHeader characters : "<<header_charcount;
cout<< "\nCompressed characters : "<<compress_charcount;
cout<< "\nTotal output characters : "<<output_characters;

savings = 100 - ((float) output_characters / (float) file_size) * 100;
cout<< "\nPercentage savings : "<<savings;
#endif
ifile.close();

unsigned short generate_code_table ()
{
unsigned short loop;
unsigned short current_length;
unsigned short current_bit;

unsigned short bitcode;
short parent;


for (loop2 = 0; loop2 < 256; loop2++)
if (frequency_count[loop2])
{
current_length = bitcode = 0;
current_bit = 1;
parent = father[loop2];

while (parent)
{
if (parent < 0)
{
bitcode += current_bit;
parent = -parent;
}
parent = father[parent];
current_bit <<= 1;
current_length++;
}

code[loop2] = bitcode;

if (current_length > 16)
return (0);
else
code_length[loop] = (unsigned char) current_length;
}
else
code[loop2] = code_length[loop2] = 0;

return (1);
};

void build_code_tree ()
{
void reheap();

unsigned short findex;
unsigned long heap_value;


while (heap_length != 1)
{
heap_value = heap[1];
heap[1] = heap[heap_length--];

reheap (1);
findex = heap_length + 255;

frequency_count[findex] = frequency_count[heap[1]] +
frequency_count[heap_value];
father[heap_value] = findex;
father[heap[1]] = -findex;
heap[1] = findex;

reheap (1);
}

father[256] = 0;
};

void reheap (unsigned short heap_entry)
{
unsigned short index;
unsigned short flag = 1;

unsigned long heap_value;


heap_value = heap[heap_entry];

while ((heap_entry <= (heap_length >> 1)) && (flag))
{
index = heap_entry << 1;

if (index < heap_length)
if (frequency_count[heap[index]] >= frequency_count[heap[index+1]])
index++;

if (frequency_count[heap_value] < frequency_count[heap[index]])
flag--;
else
{
heap[heap_entry] = heap[index];
heap_entry = index;
}
}

heap[heap_entry] = heap_value;
};

void build_initial_heap ()
{
void reheap();

unsigned short loop;

heap_length = 0;

for (loop = 0; loop < 256; loop++)
if (frequency_count[loop])
heap[++heap_length] = (unsigned long) loop;

for (loop = heap_length; loop > 0; loop--)
reheap (loop);
};

}
}

А это декодировщик:


#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#define VERBOSE /* Wenn definiert, zeichnet
programm progress, wenn diese
lauft... */

short decomp_tree[512];
unsigned short code[256];
unsigned long file_size;
unsigned char code_length[256];

unsigned short loop1;
unsigned short current_index;
unsigned short loop;
unsigned short current_node = 1;

unsigned short cindex = 1;
char curchar;
short bitshift;
unsigned long charcount = 0;

/**************************************************************************

MAIN ()

This is the main program. It performs the Huffman decoding procedure in
2 separate steps.

**************************************************************************/

void main (int argc, char *argv)
{
if (argc != 3){
cout<<"Benutzung: hpkd <quelldatei> <ausgabedatei>\n";
exit(1);
}

cout<< "\nhpkd: Huffman Package Decoding Program.";
cout<< "\n Stefan Richter. Version 1.0\n\n";

ifstream ifile(argv[1]);
ofstream ofile(argv[2]);

if (!ifile){
cout<<"Fehler! Datei "<<argv[1]<<" nicht gefunden.\n";
exit(1);
}

ifile.read((char *)(&file_size), sizeof (file_size));
ifile.read((char *)(&code), 256);
ifile.read((char *)(&code_length), 256);

#ifdef VERBOSE
cout<<"(1) Bau des Baums.\n";
#endif

decomp_tree[1] = 1;
for(loop = 0; loop < 256; loop++){
if(code_length[loop]){
current_index = 1;
for(loop1 = code_length[loop] - 1; loop1 > 0; loop1--){
current_index = (decomp_tree[current_index]<<1) + ((code[loop]>>loop1)&1);
if (!(decomp_tree[current_index])) decomp_tree[current_index] = ++current_node;
}
decomp_tree[(decomp_tree[current_index]<<1) + (code[loop] & 1)] = -loop;
}
}

#ifdef VERBOSE
cout<<"(2) Dekomprimierung und Erstellung der Ausgabedatei.\n";
#endif

if (!ofile){
cout<<"\nFehler! Kann nicht Ausgabendatei "<<argv[2]<<" erstellen.\n";
ifile.close();
}
while (charcount < file_size){
curchar = ifile.get();
for (bitshift = 7; bitshift >= 0; --bitshift){
cindex = (cindex << 1) + ((curchar >> bitshift) & 1);
if (decomp_tree[cindex] <= 0){
ofile.put((char)-decomp_tree[cindex]);
if ((++charcount) == file_size) bitshift = 0;
else cindex = 1;
}
else cindex = decomp_tree[cindex];
}
}
ifile.close();
ofile.close();
}

Благодарю зарание.
Предложения так-же можно посылать на [email]riichi@list.ru[/email]
Реклама на сайте | Обмен ссылками | Ссылки | Экспорт (RSS) | Контакты
Добавить статью | Добавить исходник | Добавить хостинг-провайдера | Добавить сайт в каталог