Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
25
Добавлен:
02.05.2014
Размер:
206.85 Кб
Скачать

Main.Cpp

#include <vcl.h>

#include <math.h>

#include <stdio.h>

#pragma hdrstop

#include "Main.h"

#pragma package(smart_init)

#pragma resource "*.dfm"

TfrmMain *frmMain;

int *TfrmMain::ansi2ascii(AnsiString ansiInput)

{

char *asciiChar;

asciiChar = ansiInput.c_str();

int *asciiInt;

asciiInt = new int [ansiInput.Length()+1];

for (int i=0;i<=ansiInput.Length();i++)

{

asciiInt[i] = asciiChar[i];

}

return asciiInt;

};

int asciiSymbol2koi(int asciiSymbol)

{

switch (asciiSymbol)

{

case 'A' : return 101; break;

case 'B' : return 102; break;

case 'C' : return 103; break;

case 'D' : return 104; break;

case 'E' : return 105; break;

case 'F' : return 106; break;

case 'G' : return 107; break;

case 'H' : return 110; break;

case 'I' : return 111; break;

case 'J' : return 112; break;

case 'K' : return 113; break;

case 'L' : return 114; break;

case 'M' : return 115; break;

case 'N' : return 116; break;

case 'O' : return 117; break;

case 'P' : return 120; break;

case 'Q' : return 121; break;

case 'R' : return 122; break;

case 'S' : return 123; break;

case 'T' : return 124; break;

case 'U' : return 125; break;

case 'V' : return 126; break;

case 'W' : return 127; break;

case 'X' : return 130; break;

case 'Y' : return 131; break;

case 'Z' : return 132; break;

case ' ' : return 39; break;

default : return 0;

}

}

int *TfrmMain::ascii2koi(int *asciiInput)

{

int Length=0;

while(asciiInput[Length]!='\0') Length++;

int *koi;

koi = new int [Length+1];

for (int i=0;i<=Length;i++)

koi[i] = asciiSymbol2koi(asciiInput[i]);

return koi;

};

int *int2bit(int number, int N)

{

int *bit;

bit = new int[N+1];

for (int i=0;i<=N;i++) bit[i]=0;

int i=N;

int tmp=0;

while (number!=0)

{

tmp = fmod(number,2);

bit[i]=tmp;

number = number/2;

i--;

}

return bit;

}

int **TfrmMain::koi2bit(int *koiInput, int N)

{

int Length=0;

while(koiInput[Length]!=0) Length++;

int **bits;

bits = new int *[Length+1];

for (int i=0;i<=Length;i++)

{

bits[i] = int2bit(koiInput[i],N);

}

return bits;

};

void TfrmMain::ShowBits(TMemo *Memo, int **bits, int Length, int N)

{

for (int i=0;i<=Length;i++)

{

AnsiString temp="";

for (int j=1;j<=N;j++)

temp += IntToStr(bits[i][j]);

Memo->Lines->Add(temp);

}

}

int xor(int x, int y)

{

if ((!x)&&(!y)) return 0;

if ((!x)&&(y)) return 1;

if ((x)&&(!y)) return 1;

if ((x)&&(y)) return 0;

}

int **TfrmMain::hammingMethod(int **bits,int Length, int N)

{

int **hamming = new int *[Length+1];

for (int i=0;i<=Length;i++)

hamming[i] = new int[4];

for (int i=0; i<=Length; i++)

{

int **t;

t = new int *[N+1];

for (int j=1; j<=N; j++)

if (bits[i][j]==1)

{

int tmp = j;

if (j==1) tmp = 3;

if (j==2) tmp = 5;

if (j==3) tmp = 6;

if (j==4) tmp = 7;

if (j==5) tmp = 9;

if (j==6) tmp = 10;

if (j==7) tmp = 11;

if (j==8) tmp = 12;

t[j-1] = int2bit(tmp,4);

}

else

t[j-1] = int2bit(0,4);

t[N] = int2bit(0,4);

int *sum;

sum = new int[5];

for (int j=0; j<=4; j++)

sum[j] = 0;

for (int k=0; k<=4; k++)

for (int j=0; j<=N; j++)

sum[k] = xor(sum[k],t[j][k]);

hamming[i] = sum;

}

return hamming;

}

int **TfrmMain::InvertBit(int **bits,int Length, int N)

{

int **invert;

invert = new int *[Length+1];

for (int i=0;i<=Length;i++)

{

invert [i] = new int[N+1];

for (int j=1;j<=N;j++)

invert[i][1+N-j] = bits[i][j];

}

return invert;

}

void SaveToFileHamming(AnsiString FileName, int **bits, int **hamming, int Length)

{

FILE *file;

file = fopen(FileName.c_str(),"w");

for (int i=0; i<=Length; i++)

{

fprintf(file,"%i",hamming[i][4]);

fprintf(file,"%i",hamming[i][3]);

fprintf(file,"%i",bits[i][8]);

fprintf(file,"%i",hamming[i][2]);

fprintf(file,"%i",bits[i][7]);

fprintf(file,"%i",bits[i][6]);

fprintf(file,"%i",bits[i][5]);

fprintf(file,"%i",hamming[i][1]);

fprintf(file,"%i",bits[i][4]);

fprintf(file,"%i",bits[i][3]);

fprintf(file,"%i",bits[i][2]);

fprintf(file,"%i",bits[i][1]);

fprintf(file,"\n");

}

fclose(file);

}

__fastcall TfrmMain::TfrmMain(TComponent* Owner)

: TForm(Owner)

{

hammingOutputFile = "";

InputFile="hamming.input.txt";

mmInput->Lines->LoadFromFile(InputFile);

ansiMessage = mmInput->Lines->Text;

asciiMessage = ansi2ascii(ansiMessage);

koiMessage = ascii2koi(asciiMessage);

bitMessage = koi2bit(koiMessage, 8);

ShowBits(mmOutput,bitMessage,ansiMessage.Length()-1,8);

int **invBitMessage;

invBitMessage = InvertBit(bitMessage,ansiMessage.Length()-1,8);

hamming = hammingMethod(invBitMessage,ansiMessage.Length()-1,8);

ShowBits(mmMethod,hamming,ansiMessage.Length()-1,4);

SaveToFileHamming("hamming.output.txt", bitMessage, hamming, ansiMessage.Length()-1);

}

void __fastcall TfrmMain::btnCloseClick(TObject *Sender)

{

Close();

}

Листинг программы, реализующей код Хэмминга. Декодер.

Decoder.cpp

#include <vcl.h>

#pragma hdrstop

USEFORM("Main.cpp", frmMain);

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)

{

try

{

Application->Initialize();

Application->CreateForm(__classid(TfrmMain), &frmMain);

Application->Run();

}

catch (Exception &exception)

{

Application->ShowException(&exception);

}

catch (...)

{

try

{

throw Exception("");

}

catch (Exception &exception)

{

Application->ShowException(&exception);

}

}

return 0;

}

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.