Advertisement
dakodev

Untitled

Nov 17th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char cn[96];
  5. char sig[96];
  6.  
  7. int toint(char c) {
  8. if (c <= '9')
  9. return c - '0';
  10. else
  11. return 10 + (c - 'A');
  12. }
  13.  
  14. void sw(int *x, int *y, int *i) {
  15. (*i)++;
  16. int c = toint(cn[*i]);
  17.  
  18. (*i) += 2;
  19. int c2 = cn[*i];
  20. if (c2 == 'x')
  21. y = x;
  22. (*i)++;
  23. c2 = toint(cn[*i]);
  24. (*i) += 2;
  25. int war = toint(cn[*i]);
  26.  
  27. int currentbit = ((*y) >> c2) & 1;
  28. if (currentbit == war) {
  29. (*x) ^= 1UL << c;
  30. }
  31. }
  32.  
  33. void sg(int *x, int *res, int *i, int bit) {
  34. (*i)++;
  35. int c = toint(sig[*i]);
  36. int currentbit = ((*x) >> c) & 1;
  37. (*i)--;
  38. if (currentbit == 1) {
  39. *res = ((1 << bit) | (*res));
  40. }
  41. }
  42.  
  43. void dsg(int *x, int *res, int *i, int bit) {
  44. (*i)++;
  45. int c = toint(sig[*i]);
  46. int currentbit = ((*res) >> bit) & 1;
  47. (*i)--;
  48. if (currentbit == 1) {
  49. *x = ((1 << c) | (*x));
  50. }
  51. }
  52.  
  53. int main(void) {
  54. char what[16];
  55. scanf("%s", what);
  56. int n = 0;
  57. char c;
  58. while ((c = getchar()) != ']') {
  59. sig[n++] = c;
  60. }
  61.  
  62. for (int i = 0; i < 4; i++)
  63. getchar(); // _bin
  64.  
  65. int l = 0;
  66. if (getchar() != '\n') {
  67. while ((c = getchar()) != '\n') {
  68. cn[l++] = c;
  69. }
  70. }
  71.  
  72. if (!strcmp(what, "encode")) {
  73. for (int i = 0; i < 7; i++)
  74. getchar();
  75.  
  76. int x, y, res = 0;
  77. scanf("%d", &y);
  78. getchar();
  79. scanf("%d", &x);
  80.  
  81. for (int i = 0; i < l; i++) {
  82. int c = cn[i];
  83. if (c == 'x') {
  84. sw(&x, &y, &i);
  85. } else if (c == 'y') {
  86. sw(&y, &x, &i);
  87. }
  88. }
  89.  
  90. int bit = 0;
  91. for (int i = n - 1; i > 0; i--) {
  92. int c = sig[i];
  93. if (c == 'y') {
  94. sg(&y, &res, &i, bit);
  95. bit++;
  96. } else if (c == 'x') {
  97. sg(&x, &res, &i, bit);
  98. bit++;
  99. }
  100. }
  101.  
  102. printf("%d\n", res);
  103.  
  104. return 0;
  105. } else {
  106. // decode
  107. int res;
  108. scanf("%d", &res);
  109. int x = 0, y = 0;
  110.  
  111. int bit = 0;
  112. for (int i = n - 1; i > 0; i--) {
  113. int c = sig[i];
  114. if (c == 'y') {
  115. dsg(&y, &res, &i, bit);
  116. bit++;
  117. } else if (c == 'x') {
  118. dsg(&x, &res, &i, bit);
  119. bit++;
  120. }
  121. }
  122.  
  123. for (int i = 0; i < l; i++) {
  124. int c = cn[i];
  125. if (c == 'x') {
  126. sw(&x, &y, &i);
  127. } else if (c == 'y') {
  128. sw(&y, &x, &i);
  129. }
  130. }
  131.  
  132. printf("(y,x)=(%d,%d)\n", y, x);
  133. }
  134.  
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement