Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define Rows 9
  6. #define Col 9
  7.  
  8.  
  9.  
  10. int arr[9],arr1[9];
  11. int b[30],a[30];
  12. int lrc[9][9];
  13. char chr;
  14. int temp,temp1,i,j,u,divisor;
  15.  
  16. vector<int>crc;
  17. vector<int>check;
  18. vector<int>d;
  19. vector<int>bit;
  20.  
  21.  
  22.  
  23. void binary(int x)
  24. {
  25. int rem;
  26. int ctr=0,i=1;
  27. do
  28. {
  29. rem=x%2;
  30. arr[i]=rem;
  31. if(rem==1)
  32. {
  33. ctr++;
  34. }
  35. x=x/2;
  36. i++;
  37. }
  38. while(x!=0);
  39. if(ctr%2==0)
  40. {
  41. arr[0]=0;
  42. }
  43.  
  44. else
  45. {
  46. arr[0]=1;
  47. }
  48. }
  49.  
  50. void parity(int a[])
  51. {
  52. int count;
  53. count=0;
  54. for(i=0; i<8; i++)
  55. {
  56. if(a[i]==1)
  57. count++;
  58. }
  59. if(count%2==0)
  60. a[8]=0;
  61. else
  62. a[8]=1;
  63. count=0;
  64. printf("Receiver Side :\n");
  65. printf("\n\nVRC : \n");
  66. for(i=0; i<9; i++)
  67. {
  68. if(i==8)
  69. printf(" | ");
  70. printf("%d ",a[i]);
  71. }
  72. }
  73.  
  74. void parity1(int a[Rows][Col],int l)
  75. {
  76. int p[9];
  77. for( i=0; i<8; i++)
  78. {
  79.  
  80. int counter=0;
  81. for(j=0; j<l; j++)
  82. {
  83. if(a[j][i]==1)
  84. {
  85. counter++;
  86. }
  87. }
  88. if(counter%2==1)
  89. {
  90. p[i]=1;
  91. }
  92. else
  93. {
  94. p[i]=0;
  95. }
  96. }
  97. printf("Receiver Side :\n");
  98. printf("\n\nLRC : \n");
  99.  
  100. for(int i=0; i<8; i++)
  101. {
  102. printf("%d ",p[i]);
  103. }
  104. }
  105.  
  106. void binary1(int n)
  107. {
  108. // array to store binary number
  109. int binaryNum[1000];
  110.  
  111. // counter for binary array
  112. int i = 0;
  113. while (n > 0)
  114. {
  115.  
  116. // storing remainder in binary array
  117. binaryNum[i] = n % 2;
  118. n = n / 2;
  119. i++;
  120. }
  121.  
  122. // printing binary array in reverse order
  123. for (int j = i - 1; j >= 0; j--)
  124. {
  125. int p=binaryNum[j];
  126. d.push_back(p);
  127.  
  128. }
  129. }
  130.  
  131. void C()
  132. {
  133. int p,f[20],n[50],div[50],q,temp,quotient[20],z[10],n1[50],f1[20];
  134. for(i=0; i<crc.size(); i++)
  135. {
  136. n[i]=crc[i];
  137. }
  138. for(i=0; i<d.size(); i++)
  139. {
  140. div[i]=d[i];
  141. }
  142. int v=crc.size()+d.size();
  143. for(i=crc.size(); i<v; i++)
  144. {
  145. n[i]=0;
  146. }
  147.  
  148. for(i=0; i<crc.size(); i++)
  149. {
  150. temp=i;
  151. if(n[i]==1)
  152. {
  153. for (j=0; j<d.size(); j++)
  154. {
  155. if (n[temp]==div[j])
  156. {
  157. n[temp]=0;
  158. f[j]=0;
  159. }
  160. else
  161. {
  162. n[temp]=1;
  163. f[j]=1;
  164. }
  165. temp=temp+1;
  166.  
  167. }
  168. quotient[i]=1;
  169. }
  170. else
  171. quotient[i]=0;
  172. }
  173. printf("Receiver Side :\n");
  174. printf("\n\nCRC : \n");
  175.  
  176. for(i=1;i<d.size();i++)
  177. {
  178. cout<<f[i];
  179. }
  180. cout<<endl;
  181.  
  182.  
  183. for(i=1;i<d.size();i++)
  184. {
  185. check.push_back(f[i]);
  186. }
  187. for(i=0;i<check.size();i++)
  188. {
  189. n1[i]=check[i];
  190. }
  191.  
  192. for(i=0; i<check.size(); i++)
  193. {
  194. temp=i;
  195. if(n1[i]==1)
  196. {
  197. for (j=0; j<d.size(); j++)
  198. {
  199. if (n1[temp]==div[j])
  200. {
  201. n1[temp]=0;
  202. f1[j]=0;
  203. }
  204. else
  205. {
  206. n1[temp]=1;
  207. f1[j]=1;
  208. }
  209. temp=temp+1;
  210.  
  211. }
  212. quotient[i]=1;
  213. }
  214. else
  215. quotient[i]=0;
  216. }
  217. /*for(j=0; j<d.size(); j++)
  218. printf("%d",f1[j]);*/
  219. }
  220.  
  221. int main()
  222. {
  223.  
  224. string s;
  225. int x;
  226.  
  227. cout<<"Choose the operation: "<<endl;
  228. cout<<"1: Press '1' for Bit stuffing."<<endl;
  229. cout<<"2: Press '2' for VRC."<<endl;
  230. cout<<"3: Press '3' for LRC."<<endl;
  231. cout<<"4: Press '4' for CRC."<<endl;
  232. //scanf("%d",&x);
  233. cin>>x;
  234. cin.ignore();
  235. if(x==1)
  236. {
  237. cout<<"Enter the text: ";
  238. //cin.ignore();
  239. getline(cin,s);
  240. for(int m=0; m<s.length(); m++)
  241. {
  242. temp=s[m];
  243. binary(temp);
  244. printf("\nAscii value is : %d\n",temp);
  245. for(i=0,j=7; i<8; i++,j--)
  246. {
  247. a[i]=arr[j];
  248. cout<<a[i];
  249. }
  250. int x=0;
  251. int count=1;
  252. int y=0;
  253. while(x<8)
  254. {
  255. if(a[x]==1)
  256. {
  257. b[y]=a[x];
  258. for(int k=x+1; a[k]==1 && k<8 && count<5; k++)
  259. {
  260. y++;
  261. b[y]=a[k];
  262. count++;
  263. if(count==5)
  264. {
  265. y++;
  266. b[y]=0;
  267. }
  268. x=k;
  269. }
  270. }
  271. else
  272. {
  273. b[y]=a[x];
  274. }
  275. x++;
  276. y++;
  277. }
  278. cout<<endl;
  279. printf("After Bit Stuffing :");
  280. for(i=0; i<y; i++)
  281. printf("%d",b[i]);
  282. }
  283. }
  284. else if(x==2)
  285. {
  286. //cout<<s.length()<<endl;
  287. //int counter=0;
  288.  
  289. cout<<"Enter the text: ";
  290. //cin.ignore();
  291. getline(cin,s);
  292. for(int m=0; m<s.length(); m++)
  293. {
  294. //counter++;
  295. temp=s[m];
  296. binary(temp);
  297. printf("\nAscii value is : %d\n",temp);
  298. printf("\nBinary Form : ");
  299.  
  300. for(i=0; i<8; i++)
  301. {
  302. arr1[i]=arr[i];
  303. printf("%d ",arr[i]);
  304. }
  305.  
  306. printf("\n");
  307. parity(arr1);
  308. cout<<endl;
  309. }
  310. //cout<<"abul"<<endl;
  311. }
  312.  
  313. else if(x==3)
  314. {
  315. cout<<"Enter the text: ";
  316. //cin.ignore();
  317. getline(cin,s);
  318. int k=0;
  319. for(int m=0; m<s.length(); m++)
  320. {
  321. temp=s[m];
  322. binary(temp);
  323. printf("\nAscii value is : %d\n",temp);
  324. lrc[k][0]=0;
  325. //printf("\nBinary Form : ");
  326. for(j=1,u=7; j<8; j++)
  327. {
  328. lrc[k][j]=arr[u];
  329. u--;
  330. //printf("%d ",arr[j]);
  331. }
  332. k++;
  333.  
  334.  
  335. //printf("\n");
  336.  
  337. }
  338.  
  339.  
  340. parity1(lrc,s.length());
  341. }
  342. else
  343. {
  344. cout<<"Enter the text: ";
  345. //cin.ignore();
  346. getline(cin,s);
  347.  
  348. for(i=0; i<s.length(); i++)
  349. {
  350. temp=s[i];
  351. binary(temp);
  352. printf("\nAscii value is : %d\n",temp);
  353. for(j=7; j>=1; j--)
  354. {
  355. //cout<<arr[j];
  356. crc.push_back(arr[j]);
  357. check.push_back(arr[j]);
  358. }
  359. /*for(j=0;j<crc.size();j++)
  360. {
  361. cout<<crc[j];
  362. }*/
  363. }
  364. cout<<"Enter divisor: ";
  365. cin>>divisor;
  366. binary1(divisor);
  367. cout<<endl;
  368. C();
  369. //cout<<d.size()<<endl;
  370.  
  371. }
  372. return 0;
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement