Есть два файла, которые написаны на C++. Один из них кодирует символьную последовательность и записывает результат в файл, другой декодирует файл и выводит в консоль закодированную строку. Необходимо переписать программы на язык C#. Требования: 1) .NET 5.0 2) Версия языка от 8 3) Должны использоваться только стандартные библиотеки 4) Результат вашей работы - два Console Appliacation, в каждом из которых стандартный Program.cs и в нём методы переписанные с C++. На своё усмотрение можете сделать отдельный класс с переписанными методами, если вам это необходимо. 5) Логика работы должна быть абсолютно идентичная файлам на C++. Как пример: если я закодировал строку программой encode.cpp и получил на выходе бинарный файл. То если закину этот файл декодер на C#, то должна получиться изначальная строка. encode.cpp // ACIS encoding #include <stdio.h> #include <fcntl.h> #include <io.h> #include <time.h> const unsigned int t = 10; const unsigned int MSB = 1 << (t - 1); const unsigned int hint = 1 << (t - 1); // half interval const unsigned int qint = 1 << (t - 2); // quarter interval const int N = 3; // alphabet size unsigned int Q[N + 1] = { 0, 10, 20, 30 }; unsigned int low, high, S; // state unsigned int vcount = 0; // the number of + or - subintervals selected FILE* F = stdout; void InitEncoder() { low = 0, high = 1 << t, S = 0; _setmode(_fileno(F), _O_BINARY); } int byte = 1; void OutputBit(int b) { byte <<= 1; byte |= b & 1; if (byte & 0x100) fwrite(&byte, 1, 1, F), byte = 1; } void EncodeFinal() // non-homophonic implementation { unsigned int L = 0, b; const unsigned int eint = 1 << (t - 3); // 1/8 of full interval while (L < low) L += eint; if (L & MSB) b = 1; else b = 0; OutputBit(b); for (; S; S--) OutputBit(1 - b); L <<= 1; if (L & MSB) b = 1; else b = 0; OutputBit(b); L <<= 1; if (L & MSB) b = 1; else b = 0; OutputBit(b); if (byte > 1) { while (!(byte & 0x100)) byte <<= 1; fwrite(&byte, 1, 1, F), byte = 1; } } void ScaleInterval() // a version from ACR { unsigned int L, R; L = low, R = high - low; while (R <= qint) { if ((L + R) <= hint) { OutputBit(0); for (; S; S--) OutputBit(1); } else if (hint <= L) { OutputBit(1); for (; S; S--) OutputBit(0); L -= hint; } else S++, L -= qint; L <<= 1, R <<= 1; } low = L, high = L + R; } unsigned int x = time(0); unsigned int GetRandom() { const unsigned int a = 1664525, c = 1013904223; x = a * x + c; return x; } int Select(unsigned int a, unsigned int b, unsigned int c) { unsigned int sum = a + b + c, bound = 1, r; while (sum > bound) bound <<= 1; while (1) { r = GetRandom() & (bound - 1); if (r < a) return 0; if (r < a + b) return 1; if (r < sum) return -1; } } void Encode(unsigned int q1, unsigned int q2, unsigned int den) { unsigned int d = high - low; unsigned int Il, Rl, Ih, Rh; Il = (d * q1) / den, Rl = (d * q1) % den; Ih = (d * q2) / den, Rh = (d * q2) % den; unsigned int vplus = Rh, vminus = Rl ? den - Rl : 0; unsigned int v = (d * q2) - (d * q1) - vplus - vminus; int c = Select(v, vplus, vminus); if (c == 0) // v is selected high = low + Ih, low += (Rl ? Il + 1 : Il), ScaleInterval(); else if (c > 0) // v+ is selected low += Ih, high = low + 1, vcount++, ScaleInterval(), Encode(0, Rh, den); else // v- is selected low += Il, high = low + 1, vcount++, ScaleInterval(), Encode(Rl, den, den); } int main(int argc, char* argv[]) { int i; char c; F = fopen("acis.bin", "wb"); InitEncoder(); for (i = 0; c = argv[1][i]; i++) Encode(Q[c - 'a'], Q[c - 'a' + 1], Q[N]); EncodeFinal(); fclose(F); fprintf(stderr, "V+ or V- selected: %d\n", vcount); return 0; } C // ACIS encoding #include <stdio.h> #include <fcntl.h> #include <io.h> #include <time.h> const unsigned int t = 10; const unsigned int MSB = 1 << (t - 1); const unsigned int hint = 1 << (t - 1); // half interval const unsigned int qint = 1 << (t - 2); // quarter interval const int N = 3; // alphabet size unsigned int Q[N + 1] = { 0, 10, 20, 30 }; unsigned int low, high, S; // state unsigned int vcount = 0; // the number of + or - subintervals selected FILE* F = stdout; void InitEncoder() { low = 0, high = 1 << t, S = 0; _setmode(_fileno(F), _O_BINARY); } int byte = 1; void OutputBit(int b) { byte <<= 1; byte |= b & 1; if (byte & 0x100) fwrite(&byte, 1, 1, F), byte = 1; } void EncodeFinal() // non-homophonic implementation { unsigned int L = 0, b; const unsigned int eint = 1 << (t - 3); // 1/8 of full interval while (L < low) L += eint; if (L & MSB) b = 1; else b = 0; OutputBit(b); for (; S; S--) OutputBit(1 - b); L <<= 1; if (L & MSB) b = 1; else b = 0; OutputBit(b); L <<= 1; if (L & MSB) b = 1; else b = 0; OutputBit(b); if (byte > 1) { while (!(byte & 0x100)) byte <<= 1; fwrite(&byte, 1, 1, F), byte = 1; } } void ScaleInterval() // a version from ACR { unsigned int L, R; L = low, R = high - low; while (R <= qint) { if ((L + R) <= hint) { OutputBit(0); for (; S; S--) OutputBit(1); } else if (hint <= L) { OutputBit(1); for (; S; S--) OutputBit(0); L -= hint; } else S++, L -= qint; L <<= 1, R <<= 1; } low = L, high = L + R; } unsigned int x = time(0); unsigned int GetRandom() { const unsigned int a = 1664525, c = 1013904223; x = a * x + c; return x; } int Select(unsigned int a, unsigned int b, unsigned int c) { unsigned int sum = a + b + c, bound = 1, r; while (sum > bound) bound <<= 1; while (1) { r = GetRandom() & (bound - 1); if (r < a) return 0; if (r < a + b) return 1; if (r < sum) return -1; } } void Encode(unsigned int q1, unsigned int q2, unsigned int den) { unsigned int d = high - low; unsigned int Il, Rl, Ih, Rh; Il = (d * q1) / den, Rl = (d * q1) % den; Ih = (d * q2) / den, Rh = (d * q2) % den; unsigned int vplus = Rh, vminus = Rl ? den - Rl : 0; unsigned int v = (d * q2) - (d * q1) - vplus - vminus; int c = Select(v, vplus, vminus); if (c == 0) // v is selected high = low + Ih, low += (Rl ? Il + 1 : Il), ScaleInterval(); else if (c > 0) // v+ is selected low += Ih, high = low + 1, vcount++, ScaleInterval(), Encode(0, Rh, den); else // v- is selected low += Il, high = low + 1, vcount++, ScaleInterval(), Encode(Rl, den, den); } int main(int argc, char* argv[]) { int i; char c; F = fopen("acis.bin", "wb"); InitEncoder(); for (i = 0; c = argv[1][i]; i++) Encode(Q[c - 'a'], Q[c - 'a' + 1], Q[N]); EncodeFinal(); fclose(F); fprintf(stderr, "V+ or V- selected: %d\n", vcount); return 0; } decode.cpp // ACIS decoding #pragma warning(suppress : 4996) #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <io.h> const unsigned int t = 10; // t <= 16 const unsigned int MSB = 1 << (t - 1); const unsigned int hint = 1 << (t - 1); // half interval const unsigned int qint = 1 << (t - 2); // quarter interval const int N = 3; // alphabet size unsigned int Q[N + 1] = { 0, 10, 20, 30 }; unsigned int cur, low, high; // state FILE* F = stdin; int byte = 0x10000; int NextCodeBit() { if (byte & 0x10000) byte = 0x100, fread(&byte, 1, 1, F); byte <<= 1; if (byte & 0x100) return 1; else return 0; } void InitDecoder() { _setmode(_fileno(F), _O_BINARY); cur = 0; for (int i = 0; i < t; i++) cur = (cur << 1) + NextCodeBit(); low = 0, high = 1 << t; } void ScaleIntervalDec() // a version from ACR { unsigned int L, R, V; L = low, R = high - low, V = cur; while (R <= qint) { if ((L + R) <= hint); // no action else if (hint <= L) L -= hint, V -= hint; else L -= qint, V -= qint; L <<= 1, R <<= 1, V = (V << 1) + NextCodeBit(); } low = L, high = L + R, cur = V; } int Decode(unsigned int q[], unsigned int den) { unsigned int d = high - low; unsigned int Il, Rl, Ih, Rh, Ic; int u = 0; Ic = ((cur - low) * den) / d; while (Ic >= q[u + 1]) u++; Ih = (d * q[u + 1]) / den, Rh = (d * q[u + 1]) % den; high = low + Ih; if (cur == high) { low = cur, high = cur + 1, ScaleIntervalDec(); unsigned int qp[3] = { 0, Rh, den }; u = u + Decode(qp, den); } else { Il = (d * q[u]) / den, Rl = (d * q[u]) % den; low = low + Il; if (Rl != 0) low++; ScaleIntervalDec(); } return u; } int main(int argc, char* argv[]) { char c; F = fopen("acis.bin", "rb"); InitDecoder(); printf("Decode string:"); while (!feof(F)) c = Decode(Q, Q[N]), printf("%c", c + 'a'); if (byte < 0x10000) c = Decode(Q, Q[N]), printf("%c", c + 'a'); printf("\n"); return 0; } C // ACIS decoding #pragma warning(suppress : 4996) #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <io.h> const unsigned int t = 10; // t <= 16 const unsigned int MSB = 1 << (t - 1); const unsigned int hint = 1 << (t - 1); // half interval const unsigned int qint = 1 << (t - 2); // quarter interval const int N = 3; // alphabet size unsigned int Q[N + 1] = { 0, 10, 20, 30 }; unsigned int cur, low, high; // state FILE* F = stdin; int byte = 0x10000; int NextCodeBit() { if (byte & 0x10000) byte = 0x100, fread(&byte, 1, 1, F); byte <<= 1; if (byte & 0x100) return 1; else return 0; } void InitDecoder() { _setmode(_fileno(F), _O_BINARY); cur = 0; for (int i = 0; i < t; i++) cur = (cur << 1) + NextCodeBit(); low = 0, high = 1 << t; } void ScaleIntervalDec() // a version from ACR { unsigned int L, R, V; L = low, R = high - low, V = cur; while (R <= qint) { if ((L + R) <= hint); // no action else if (hint <= L) L -= hint, V -= hint; else L -= qint, V -= qint; L <<= 1, R <<= 1, V = (V << 1) + NextCodeBit(); } low = L, high = L + R, cur = V; } int Decode(unsigned int q[], unsigned int den) { unsigned int d = high - low; unsigned int Il, Rl, Ih, Rh, Ic; int u = 0; Ic = ((cur - low) * den) / d; while (Ic >= q[u + 1]) u++; Ih = (d * q[u + 1]) / den, Rh = (d * q[u + 1]) % den; high = low + Ih; if (cur == high) { low = cur, high = cur + 1, ScaleIntervalDec(); unsigned int qp[3] = { 0, Rh, den }; u = u + Decode(qp, den); } else { Il = (d * q[u]) / den, Rl = (d * q[u]) % den; low = low + Il; if (Rl != 0) low++; ScaleIntervalDec(); } return u; } int main(int argc, char* argv[]) { char c; F = fopen("acis.bin", "rb"); InitDecoder(); printf("Decode string:"); while (!feof(F)) c = Decode(Q, Q[N]), printf("%c", c + 'a'); if (byte < 0x10000) c = Decode(Q, Q[N]), printf("%c", c + 'a'); printf("\n"); return 0; } По цене предлагайте в Telegram @byl3x