Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sstream>
  6. #include <bitset>
  7. //#include"complex.h"
  8. using namespace std;
  9.  
  10. int debug;
  11. int decision;
  12. string inp;
  13. string originalBits, encodedBits;
  14. int redundantBits;
  15.  
  16. void Start();
  17. void OriginalCodeStuff();
  18. void Encode();
  19. void Decode();
  20. string MakeCharToAscii();
  21. void SetRedundantBitsSize();
  22. void SetDecodeRedundantBits();
  23. void DecodeSetOriginalBitsString();
  24.  
  25. string MakeCharToAscii(char c)
  26. {
  27. string ret;
  28. char buffer[33];
  29. _itoa_s(c, buffer, 2);
  30. ret += "0";
  31. ret += buffer;
  32. return ret;
  33. }
  34. void OriginalCodeStuff()
  35. {
  36. originalBits = "";
  37. for (int i = 0; i < inp.size(); i++)
  38. {
  39. originalBits += MakeCharToAscii(inp[i]);
  40. }
  41. cout << "\nOriginal code: " << originalBits << endl;
  42.  
  43. }
  44. int Power(int a, int b)
  45. {
  46. int ret = 1;
  47. for (int i = 0; i < b; i++)
  48. ret *= a;
  49. return ret;
  50. }
  51. void SetRedundantBitsSize()
  52. {
  53. redundantBits = 0;
  54. while (originalBits.size() + redundantBits + 1 > Power(2, redundantBits))
  55. redundantBits++;
  56. cout << "No of redundant bits: " << redundantBits << endl;
  57. }
  58. void SetDecodeRedundantBits()
  59. {
  60. int i = 0;
  61. for (;encodedBits.size() + 1 > Power(2, i); i++);
  62. redundantBits = i;
  63. cout << "No of redundant bits: " << redundantBits << endl;
  64. }
  65. bool IsBinaryNumber(int i)
  66. {
  67. while (i > 1)
  68. {
  69. if (i % 2 != 0)
  70. return false;
  71. i /= 2;
  72. }
  73. return true;
  74. }
  75. void SetNormalBitsToEncoded()
  76. {
  77. for (int i = 0, origIt = 0; i < encodedBits.size(); i++)
  78. {
  79. int j = i + 1;
  80. if (!IsBinaryNumber(j))
  81. {
  82. if (origIt >= originalBits.size())
  83. {
  84. cout << "Error, Original Iterator larger than Encoded size" << endl;
  85. return;
  86. }
  87. encodedBits[i] = originalBits[origIt];
  88. origIt++;
  89. }
  90.  
  91. }
  92. }
  93. void DecodeSetOriginalBitsString()
  94. {
  95. originalBits = string(encodedBits.size() - redundantBits , '0');
  96. for (int i = 0, j = 0; i < encodedBits.size(); i++)
  97. {
  98. if (IsBinaryNumber(i + 1))
  99. continue;
  100. if (j < originalBits.size())
  101. {
  102. originalBits[j] = encodedBits[i];
  103. j++;
  104. }
  105. }
  106. }
  107. void OutputOriginalText()
  108. {
  109. stringstream sstream(originalBits);
  110. string output;
  111. while (sstream.good())
  112. {
  113. bitset<8> bits;
  114. sstream >> bits;
  115. char c = char(bits.to_ulong());
  116. output += c;
  117. }
  118. cout << "Original text: " << output << endl;
  119. }
  120. void ds()
  121. {
  122. if (debug == 1)
  123. cout << "\n";
  124. }
  125. void dm(string s)
  126. {
  127. if (debug == 1)
  128. cout << "s";
  129. }
  130. void dm(int s)
  131. {
  132. if (debug == 1)
  133. cout << s;
  134. }
  135. int CountParity(int i, int sz)
  136. {
  137. int ret = 0;
  138. int szTemp = sz;
  139. if (i == sz - 1)
  140. {
  141. i++;
  142. szTemp--;
  143. }
  144. for (; szTemp > 0 && i < encodedBits.size(); i++, szTemp--)
  145. {
  146. ret += encodedBits[i] == '1' ? 1 : 0;
  147. }
  148. return ret;
  149. }
  150. void SetParityBitsToEncoded()
  151. {
  152. for (int i = 0, origIt = 0; i < encodedBits.size(); i++)
  153. {
  154. int j = i + 1;
  155. if (IsBinaryNumber(j))
  156. {
  157. int count = 0;
  158. int currectIndex = i;
  159. while (currectIndex < encodedBits.size())
  160. {
  161. count += CountParity(currectIndex, j);
  162. currectIndex += 2*j;
  163. }
  164. encodedBits[i] = count % 2 == 0 ? '0' : '1';
  165.  
  166. }
  167.  
  168. }
  169. }
  170. void EncodedCodeStuff()
  171. {
  172. encodedBits = "";
  173. // m + r + 1 <= 2^r
  174. SetRedundantBitsSize();
  175. encodedBits = string(redundantBits + originalBits.size(), '0');
  176. SetNormalBitsToEncoded();
  177. SetParityBitsToEncoded();
  178. cout << "Encoded code: " << encodedBits << endl;
  179.  
  180. }
  181. void Encode()
  182. {
  183. cout << "\nEnter message: ";
  184. cin >> inp;
  185. if (inp.size() == 1 && inp == "3")
  186. Start();
  187. OriginalCodeStuff();
  188. EncodedCodeStuff();
  189. cout << endl;
  190. Start();
  191. }
  192. void FindErrors(string o)
  193. {
  194. int e = 0;
  195. for (int i = 0; i < o.size(); i++)
  196. {
  197. if (o[i] != encodedBits[i])
  198. e |= (i + 1);
  199. }
  200. if (e != 0)
  201. encodedBits[e-1] = encodedBits[e-1] == '1' ? '0' : '1';
  202. if (e != 0)
  203. cout << "Error found, correct code is: " << encodedBits << endl;
  204. }
  205. void Decode()
  206. {
  207. cout << "\nEnter code: ";
  208. cin >> inp;
  209. if (inp.size() == 1 && inp == "3")
  210. Start();
  211. encodedBits = inp;
  212. SetDecodeRedundantBits();
  213. string temp = encodedBits;
  214. SetParityBitsToEncoded();
  215. FindErrors(temp);
  216.  
  217. DecodeSetOriginalBitsString();
  218. cout << "Original: " << originalBits << endl;
  219.  
  220. OutputOriginalText();
  221. cout << endl;
  222. Start();
  223. }
  224. void Start()
  225. {
  226. cout << "Enter '1' for Encoding\nEnter '2' for Decoding\nEnter '3' at any point to restart" << endl;
  227. cin >> decision;
  228. if (decision == 1)
  229. Encode();
  230. else if (decision == 2)
  231. Decode();
  232. else if (decision == 3)
  233. Start();
  234. }
  235.  
  236. void main()
  237. {
  238.  
  239. debug = 0;
  240. Start();
  241.  
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement