Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define Rows 9
- #define Col 9
- int arr[9],arr1[9];
- int b[30],a[30];
- int lrc[9][9];
- char chr;
- int temp,temp1,i,j,u,divisor;
- vector<int>crc;
- vector<int>check;
- vector<int>d;
- vector<int>bit;
- void binary(int x)
- {
- int rem;
- int ctr=0,i=1;
- do
- {
- rem=x%2;
- arr[i]=rem;
- if(rem==1)
- {
- ctr++;
- }
- x=x/2;
- i++;
- }
- while(x!=0);
- if(ctr%2==0)
- {
- arr[0]=0;
- }
- else
- {
- arr[0]=1;
- }
- }
- void parity(int a[])
- {
- int count;
- count=0;
- for(i=0; i<8; i++)
- {
- if(a[i]==1)
- count++;
- }
- if(count%2==0)
- a[8]=0;
- else
- a[8]=1;
- count=0;
- printf("Receiver Side :\n");
- printf("\n\nVRC : \n");
- for(i=0; i<9; i++)
- {
- if(i==8)
- printf(" | ");
- printf("%d ",a[i]);
- }
- }
- void parity1(int a[Rows][Col],int l)
- {
- int p[9];
- for( i=0; i<8; i++)
- {
- int counter=0;
- for(j=0; j<l; j++)
- {
- if(a[j][i]==1)
- {
- counter++;
- }
- }
- if(counter%2==1)
- {
- p[i]=1;
- }
- else
- {
- p[i]=0;
- }
- }
- printf("Receiver Side :\n");
- printf("\n\nLRC : \n");
- for(int i=0; i<8; i++)
- {
- printf("%d ",p[i]);
- }
- }
- void binary1(int n)
- {
- // array to store binary number
- int binaryNum[1000];
- // counter for binary array
- int i = 0;
- while (n > 0)
- {
- // storing remainder in binary array
- binaryNum[i] = n % 2;
- n = n / 2;
- i++;
- }
- // printing binary array in reverse order
- for (int j = i - 1; j >= 0; j--)
- {
- int p=binaryNum[j];
- d.push_back(p);
- }
- }
- void C()
- {
- int p,f[20],n[50],div[50],q,temp,quotient[20],z[10],n1[50],f1[20];
- for(i=0; i<crc.size(); i++)
- {
- n[i]=crc[i];
- }
- for(i=0; i<d.size(); i++)
- {
- div[i]=d[i];
- }
- int v=crc.size()+d.size();
- for(i=crc.size(); i<v; i++)
- {
- n[i]=0;
- }
- for(i=0; i<crc.size(); i++)
- {
- temp=i;
- if(n[i]==1)
- {
- for (j=0; j<d.size(); j++)
- {
- if (n[temp]==div[j])
- {
- n[temp]=0;
- f[j]=0;
- }
- else
- {
- n[temp]=1;
- f[j]=1;
- }
- temp=temp+1;
- }
- quotient[i]=1;
- }
- else
- quotient[i]=0;
- }
- printf("Receiver Side :\n");
- printf("\n\nCRC : \n");
- for(i=1;i<d.size();i++)
- {
- cout<<f[i];
- }
- cout<<endl;
- for(i=1;i<d.size();i++)
- {
- check.push_back(f[i]);
- }
- for(i=0;i<check.size();i++)
- {
- n1[i]=check[i];
- }
- for(i=0; i<check.size(); i++)
- {
- temp=i;
- if(n1[i]==1)
- {
- for (j=0; j<d.size(); j++)
- {
- if (n1[temp]==div[j])
- {
- n1[temp]=0;
- f1[j]=0;
- }
- else
- {
- n1[temp]=1;
- f1[j]=1;
- }
- temp=temp+1;
- }
- quotient[i]=1;
- }
- else
- quotient[i]=0;
- }
- /*for(j=0; j<d.size(); j++)
- printf("%d",f1[j]);*/
- }
- int main()
- {
- string s;
- int x;
- cout<<"Choose the operation: "<<endl;
- cout<<"1: Press '1' for Bit stuffing."<<endl;
- cout<<"2: Press '2' for VRC."<<endl;
- cout<<"3: Press '3' for LRC."<<endl;
- cout<<"4: Press '4' for CRC."<<endl;
- //scanf("%d",&x);
- cin>>x;
- cin.ignore();
- if(x==1)
- {
- cout<<"Enter the text: ";
- //cin.ignore();
- getline(cin,s);
- for(int m=0; m<s.length(); m++)
- {
- temp=s[m];
- binary(temp);
- printf("\nAscii value is : %d\n",temp);
- for(i=0,j=7; i<8; i++,j--)
- {
- a[i]=arr[j];
- cout<<a[i];
- }
- int x=0;
- int count=1;
- int y=0;
- while(x<8)
- {
- if(a[x]==1)
- {
- b[y]=a[x];
- for(int k=x+1; a[k]==1 && k<8 && count<5; k++)
- {
- y++;
- b[y]=a[k];
- count++;
- if(count==5)
- {
- y++;
- b[y]=0;
- }
- x=k;
- }
- }
- else
- {
- b[y]=a[x];
- }
- x++;
- y++;
- }
- cout<<endl;
- printf("After Bit Stuffing :");
- for(i=0; i<y; i++)
- printf("%d",b[i]);
- }
- }
- else if(x==2)
- {
- //cout<<s.length()<<endl;
- //int counter=0;
- cout<<"Enter the text: ";
- //cin.ignore();
- getline(cin,s);
- for(int m=0; m<s.length(); m++)
- {
- //counter++;
- temp=s[m];
- binary(temp);
- printf("\nAscii value is : %d\n",temp);
- printf("\nBinary Form : ");
- for(i=0; i<8; i++)
- {
- arr1[i]=arr[i];
- printf("%d ",arr[i]);
- }
- printf("\n");
- parity(arr1);
- cout<<endl;
- }
- //cout<<"abul"<<endl;
- }
- else if(x==3)
- {
- cout<<"Enter the text: ";
- //cin.ignore();
- getline(cin,s);
- int k=0;
- for(int m=0; m<s.length(); m++)
- {
- temp=s[m];
- binary(temp);
- printf("\nAscii value is : %d\n",temp);
- lrc[k][0]=0;
- //printf("\nBinary Form : ");
- for(j=1,u=7; j<8; j++)
- {
- lrc[k][j]=arr[u];
- u--;
- //printf("%d ",arr[j]);
- }
- k++;
- //printf("\n");
- }
- parity1(lrc,s.length());
- }
- else
- {
- cout<<"Enter the text: ";
- //cin.ignore();
- getline(cin,s);
- for(i=0; i<s.length(); i++)
- {
- temp=s[i];
- binary(temp);
- printf("\nAscii value is : %d\n",temp);
- for(j=7; j>=1; j--)
- {
- //cout<<arr[j];
- crc.push_back(arr[j]);
- check.push_back(arr[j]);
- }
- /*for(j=0;j<crc.size();j++)
- {
- cout<<crc[j];
- }*/
- }
- cout<<"Enter divisor: ";
- cin>>divisor;
- binary1(divisor);
- cout<<endl;
- C();
- //cout<<d.size()<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement