Здравствуйте. Требуется помощь в обнаружении ошибки. Возникает путаница с символами во время построения дерева. Не могу определить источник такой вот проблемы. Заранее спасибо.
Код:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <fstream.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
struct TTable{
char buk;
char* kod;
TTable* left;
TTable* right;
};
struct symb{
char buk;
char* kod;
int povt;
TTable* table;
};
symb abc[41];
void symb_initiate();
int FindLeast(int count, int index)
{
int i;
int min=0;
if(index==0) min=1;
for (i=1; i<count; i++)
{
if (i!=index)
if (abc[i].povt<abc[min].povt)
min=i;
}
return min;
}
void PreInit(int count)
{
int ind1, ind2;
TTable *table;
while (count>1)
{
ind1=FindLeast(count,-1);
ind2=FindLeast(count,ind1);
table=new TTable;
table->buk='no';
table->left=abc[ind1].table;
table->right=abc[ind2].table;
abc[ind1].buk='no';
abc[ind1].povt+=abc[ind2].povt;
abc[ind1].table=table;
if (ind2!=(--count))
abc[ind2]=abc[count];
}
}
void RecursMake(TTable* tbl, char* kodd,symb* tekst, int j)
{
if(tbl->buk!='no')
{
tbl->kod=kodd;
tekst[j].buk=tbl->buk;
tekst[j].kod=tbl->kod;
j++;
}
else
{
RecursMake(tbl->left, kodd+'0',tekst,j);
RecursMake(tbl->right, kodd+'1',tekst,j);
}
}
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
odOpenFile->FileName="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnOpenFileClick(TObject *Sender)
{
odOpenFile->Execute();
ifstream file (odOpenFile->FileName.c_str());
char text[1000];
symb_initiate();
for(int i=0;i<40;i++)
abc[i].povt=0;
while(!file.eof())
{ char a;
a=file.get();
for(int j=0;j<40;j++)
if (a==abc[j].buk)
abc[j].povt++;}
int count =40;
int i=0;
while(i<count)
{
if (abc[i].povt==0)
{
abc[i]=abc[--count];
;
}
else
i++;
}
symb* tekst=new symb [count];
int x=count;
for(i=0;i<count;i++)
{
abc[i].table=new TTable;
abc[i].table->left=0;
abc[i].table->right=0;
abc[i].table->buk=abc[i].buk;
}
PreInit(count);
RecursMake(abc[0].table,"",tekst,0);
for(int i=0;i<x;i++)
Tabl->Lines->Add(tekst[i].buk+tekst[i].kod);
}
void symb_initiate()
{
abc[0].buk='a';
abc[1].buk='b';
abc[2].buk='c';
abc[3].buk='d';
abc[4].buk='e';
abc[5].buk='f';
abc[6].buk='g';
abc[7].buk='h';
abc[8].buk='i';
abc[9].buk='j';
abc[10].buk='k';
abc[11].buk='l';
abc[12].buk='m';
abc[13].buk='n';
abc[14].buk='o';
abc[15].buk='p';
abc[16].buk='q';
abc[17].buk='r';
abc[18].buk='s';
abc[19].buk='t';
abc[20].buk='u';
abc[21].buk='w';
abc[22].buk='x';
abc[23].buk='y';
abc[24].buk='z';
abc[25].buk='1';
abc[26].buk='2';
abc[27].buk='3';
abc[28].buk='4';
abc[29].buk='5';
abc[30].buk='6';
abc[31].buk='7';
abc[32].buk='8';
abc[33].buk='9';
abc[34].buk='0';
abc[35].buk=' ';
abc[36].buk='!';
abc[37].buk='?';
abc[38].buk='.';
abc[39].buk=',';
}
//---------------------------------------------------------------------------