Mlxa

Z CRYpto.cpp

Dec 14th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cwchar>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <cassert>
  6. #include <clocale>
  7. //typedef unsigned char Char;
  8. #define Char wchar_t
  9.  
  10. void igets(Char* str)
  11. { fgetws(str, 1024, stdin); }
  12.  
  13. using namespace std;
  14. const int bNum(4), bSize(1<<10), cSize(1<<5);
  15. wchar_t AlS('a'), AlE('z');
  16.  
  17. Char buff[bNum][bSize];
  18. Char comm[cSize];
  19.  
  20. #define rangeBuff(i, num) for (int i(0); i < wcslen(buff[num]); ++i)
  21. #define ifeq(s1, s2) if(!wcscmp(s1, s2))
  22. #define R wscanf
  23. #define W wprintf
  24.  
  25. void input(Char* str, int count)
  26. {
  27.     fgetws(str, count, stdin);
  28.     str[wcslen(str) - 1] = 0;
  29. }
  30.  
  31. void Clear()
  32. {
  33.     for (int i(0); i < bNum; ++i) for (int j(0); j < bSize; ++j)
  34.         buff[i][j] = 0;
  35.     for (int i(0); i < cSize; ++i)
  36.         comm[i] = 0;
  37. }
  38.  
  39. void ruDowner(Char& a) { if (a >= L'А' and a <= L'Я') a += 32; }
  40. void enDowner(Char& a) { if (a >= L'A' and a <= L'Z') a += 32; }
  41. void letterDown(Char& a) { ruDowner(a); enDowner(a); }
  42.  
  43. void shift(Char& a, int s)
  44. {
  45.     s += a;
  46.     int L = AlE - AlS + 1;
  47.     if (a >= AlS and a <= AlE)
  48.     {
  49.         while (s < AlS) s += L;
  50.         while (s > AlE) s -= L;
  51.         assert(s >= AlS and s <= AlE);
  52.         a = (Char)s;
  53.     }
  54. }
  55.  
  56. void shift(Char& a, Char s, int sign)
  57. {
  58.     assert(s >= AlS and s <= AlE);
  59.     int d = sign * (s - AlS);
  60.     shift(a, d);
  61. }
  62.  
  63. void norm(int num)
  64. { rangeBuff(i, num) letterDown(buff[num][i]); }
  65.  
  66. void caesar(int num, int step)
  67. {
  68.     W(L"Num = %d, step = %d\n", num, step);
  69.     norm(num);
  70.     rangeBuff(i, num) shift(buff[num][i], step);
  71. }
  72.  
  73. int main()
  74. {
  75.     setlocale(0, "");
  76.     int a[8] = {};
  77.     while( wcscmp(comm, L"end") and R(L"%ls", comm) > 0 )
  78.     {
  79.         W(L"You entered: %ls;\n", comm);
  80.         ifeq(comm, L"read")  { R(L"%d%*c", &(a[0])); W(L"Arg = %d\n", a[0]); input(buff[a[0]], bSize); W(L"Readed: %ls;\n", buff[a[0]]); }
  81.         ifeq(comm, L"print") { R(L"%d", &(a[0])); W(L"Buff[%d] = %ls;\n", a[0], buff[a[0]]); }
  82.         ifeq(comm, L"caesar") { R(L"%d%d%*c", &(a[0]), &(a[1]) ); caesar(a[0], a[1]); }
  83.     }
  84.     return 0;
  85. }
Add Comment
Please, Sign In to add comment