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

ООП / ООП / oop-ekz-tasks / Семинар 7

.doc
Скачиваний:
11
Добавлен:
18.02.2017
Размер:
48.13 Кб
Скачать

Семинар 7.

#include <iostream>

#include <fstream>

#include <stdlib.h>

#include <string.h>

using namespace std;

const int MaxA = 1000;

class SrcCreate

{

char * name;

int size;

int Current;

int Position;

char Rx [MaxA];

char Tx [MaxA];

public:

SrcCreate (char * w, int SIZE);

void AddMsg (char * w);

~ SrcCreate ();

};

SrcCreate :: SrcCreate (char *w, int SIZE)

{

name = w;

ofstream out (name);

Current = 0;

Position = 0;

if (!out)

{

cout << "Cannot open file! " << w << endl;

exit (1);

}

size = SIZE;

};

SrcCreate :: ~SrcCreate ()

{

ofstream out (name, ios_base::app);

if (Position != 0)

{

out << "Rx: ";

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

{

out << Rx [2 * j] << Rx [2 * j + 1] << " ";

}

out << endl;

out << "Tx: ";

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

{

out << Tx [2 * j] << Tx [2 * j + 1] << " ";

}

out << endl << endl;

}

out.close ();

}

void SrcCreate :: AddMsg (char * w)

{

ofstream out (name, ios_base::app);

Current ++;

int i = 0;

if (Current % 2 == 1)

{

while (i < strlen (w))

{

while (i < strlen (w) && 3 * Position < size)

{

if (((s [i] - ' ') + 32) % 16 > 9)

{

Rx [2 * Position + 1] = (((s [i] - ' ') + 32) % 16 - 10) + 'A';

}

else

{

Rx [2 * Position + 1] = (((s [i] - ' ') + 32) % 16) + '0';

}

Tx [2 * Position + 1] = ' ';

if ((((w [i] - ' ') + 32) / 16) %16 > 9)

{

Rx [2 * Position] = ((((w [i] - ' ') + 32) / 16) %16 - 10) + 'A';

}

else

{

Rx [2 * Position] = ((((w [i] - ' ') + 32) / 16) %16) + '0';

}

Tx [2 * Position] = ' ';

i ++;

Position ++;

}

if (3 * Position == size)

{

Position = 0;

out << "Rx: ";

for (int j = 0; j < size / 3; j ++)

{

out << Rx [2 * j] << Rx [2 * j + 1] << " ";

}

out << endl;

out << "Tx: ";

for (int j = 0; j < size / 3; j ++)

{

out << Tx [2 * j] << Tx [2 * j + 1] << " ";

}

out << endl << endl;

}

}

}

else if (Current % 2 == 0)

{

while (i < strlen (w))

{

while (i < strlen (w) && 3 * Position < size)

{

if (((w [i] - ' ') + 32) % 16 > 9)

{

Tx [2 * Position + 1] = (((w [i] - ' ') + 32) % 16 - 10) + 'A';

}

else

{

Tx [2 * Position + 1] = (((w [i] - ' ') + 32) % 16) + '0';

}

Rx [2 * Position + 1] = ' ';

if ((((w [i] - ' ') + 32) / 16) %16 > 9)

{

Tx [2 * Position] = ((((w[i] - ' ') + 32) / 16) %16 - 10) + 'A';

}

else

{

Tx [2 * Position] = ((((w [i] - ' ') + 32) / 16) %16) + '0';

}

Rx [2 * Position] = ' ';

i ++;

Position ++;

}

if (3 * Position == size)

{

Position = 0;

out << "Rx: ";

for (int j = 0; j < size / 3; j ++)

{

out << Rx [2 * j] << Rx [2 * j + 1] << " ";

}

out << endl;

out << "Tx: ";

for (int j = 0; j < size / 3; j ++)

{

out << Tx [2 * j] << Tx [2 * j + 1] << " ";

}

out << endl << endl;

}

}

}

}

class SrcConvert

{

char * name1;

char * name2;

char Rx [MaxA];

char Tx [MaxA];

char temp [33];

int Position;

int kol;

public:

SrcConvert (char * w1, char * w2);

};

SrcConvert :: SrcConvert(char *w1, char *w2)

{

name1 = w1;

name2 = w2;

ifstream in (name1);

if (!in)

{

cout << "Cannot open file!" << endl;

exit (1);

}

ofstream out (name2);

if (!out)

{

cout << "Cannot open file!" << endl;

exit (1);

}

Position = -1;

kol = 0;

while (!in.eof ())

{

in.getline (Rx, 1024, '\n');

in.getline (Tx, 1024, '\n');

int i = 0;

while (3 * i + 2 < strlen (Rx))

{

if (Tx [3 * i + 4] == ' ' && Position != 0)

{

if (Position != -1)

{

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

{

out << temp [2 * j] << temp [2 * j + 1] << " ";

}

if (kol < 16)

for (int j = kol + 1 ; j < 16; j ++)

{

out << " ";

}

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

{

int temp2 = ((temp [2 * j] >= 'A') * (temp [2 * j] - 'A' + 10) + (temp [2 * j] <= '9') * (temp [2 * j] - '0'))* 16 + (temp [2 * j + 1] >= 'A') * (temp [2 * j + 1] - 'A' + 10) + ((temp [2 * j + 1] <= '9') * (temp [2 * j + 1] - '0'));

if (temp2 < 32) out << '.';

else out << (char)((temp2 - 32) + ' ');

}

out << endl << endl;

}

out << "Src->Dst" << endl;

Position = 0;

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

temp [j] = ' ';

kol = 0;

temp [2 * kol] = Rx [3 * i + 4];

temp [2 * kol + 1] = Rx [3 * i + 5];

}

else if (Rx [3 * i + 4] == ' ' && Position != 1)

{

if (Position != -1)

{

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

{

out << temp [2 * j] << temp [2 * j + 1] << " ";

}

if (kol < 16)

for (int j = kol + 1 ; j < 16; j ++)

{

out << " ";

}

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

{

int temp2 = ((temp [2 * j] >= 'A') * (temp [2 * j] - 'A' + 10) + (temp [2 * j] <= '9') * (temp [2 * j] - '0'))* 16 + (temp [2 * j + 1] >= 'A') * (temp [2 * j + 1] - 'A' + 10) + ((temp [2 * j + 1] <= '9') * (temp [2 * j + 1] - '0'));

if (temp2 < 32) out << '.';

else out << (char)((temp2 - 32) + ' ');

}

out << endl <<endl;

}

out << "Src<-Dst" << endl;

Position = 1;

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

temp [j] = ' ';

kol = 0;

temp [2 * kol] = Tx [3 * i + 4];

temp [2 * kol + 1] = Tx [3 * i + 5];

}

else if (Tx [3 * i + 5] == ' ')

{

kol ++;

if (kol == 16)

{

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

{

out << temp [2 * j] << temp [2 * j + 1] << " ";

}

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

{

int temp2 = ((temp [2 * j] >= 'A') * (temp [2 * j] - 'A' + 10) + (temp [2 * j] <= '9') * (temp [2 * j] - '0'))* 16 + (temp [2 * j + 1] >= 'A') * (temp [2 * j + 1] - 'A' + 10) + ((temp [2 * j + 1] <= '9') * (temp [2 * j + 1] - '0'));

if (temp2 < 32) out << '.';

else out << (char)((temp2 - 32) + ' ');

}

kol = 0;

out << endl;

}

temp [2 * kol] = Rx [3 * i + 4];

temp [2 * kol + 1] = Rx [3 * i + 5];

}

else if (Rx [3 * i + 5] == ' ')

{

kol ++;

if (kol == 16)

{

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

{

out << temp [2 * j] << temp [2 * j + 1] << " ";

}

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

{

int temp2 = ((temp [2 * j] >= 'A') * (temp [2 * j] - 'A' + 10) + (temp [2 * j] <= '9') * (temp [2 * j] - '0'))* 16 + (temp [2 * j + 1] >= 'A') * (temp [2 * j + 1] - 'A' + 10) + ((temp [2 * j + 1] <= '9') * (temp [2 * j + 1] - '0'));

if (temp2 < 32) out << '.';

else out << (char)((temp2 - 32) + ' ');

}

out << endl;

kol = 0;

}

temp [2 * kol] = Tx [3 * i + 4];

temp [2 * kol + 1] = Tx [3 * i + 5];

}

i ++;

}

in.getline (Tx, 1024, '\n');

}

if (kol > 0)

{

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

{

out << temp [2 * j] << temp [2 * j + 1] << " ";

}

if (kol < 16)

for (int j = kol + 1 ; j <= 16; j ++)

{

out << " ";

}

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

{

int temp2 = ((temp [2 * j] >= 'A') * (temp [2 * j] - 'A' + 10) + (temp [2 * j] <= '9') * (temp [2 * j] - '0'))* 16 + (temp [2 * j + 1] >= 'A') * (temp [2 * j + 1] - 'A' + 10) + ((temp [2 * j + 1] <= '9') * (temp [2 * j + 1] - '0'));

if (temp2 < 32) out << '.';

else out << (char)((temp2 - 32) + ' ');

}

}

in.close ();

out.close ();

}

int main ()

{

{

SrcCreate src ("C:\\output.txt", 42);

src.AddMsg ("Hello!");

src.AddMsg ("Hi!Guy!");

src.AddMsg ("How are you?");

src.AddMsg ("I'm well, and how are you?");

src.AddMsg ("I’m Well! Bye!");

src.AddMsg ("See you!");

}

SrcConvert dst ("D:\\OOP\output.txt", "D:\\OOP\input.txt");

return 0;

}