// SIMULATE CYCLIC REDUNDANCY CHECK (CRC) #include<iostream.h> #include<conio.h> #include<math.h> #include<stdlib.h> #include<graphics.h> #include<DOS.h> int digitinbin(int number) { int i=0; int n=number; while(n!=0) { n=n/2; i++; } return i; } int bintodec(int *a,int d) { int num=0; for(int i=0;i<d;i++) { if(a[i]==1) num=num+pow(2,i); } return num; } int* dectobin(int n) { int *A=new int[32]; int i=0; int num=n; while(num!=0) { A[i]=num%2; num=num/2; i++; } return A; } int sender(int mess,int poly) { int k=0,num1; int d1=digitinbin(poly); mess=mess*pow(2,d1-1); int d2=digitinbin(mess); int *B=dectobin(mess); int counter=0; int times=d2-d1+1; int *temp=new int[d1]; int a=pow(2,d1-1); while(counter<times) { int j=0; if(counter==0) { for(int i=times-counter-1;i<d2-counter;i++) { temp[j]=B[i]; j++; } num1=bintodec(temp,j); } int num2=poly; if(counter!=0) num1=k; if(num1>=a) k=num1^num2; else k=num1; if(d2-d1-counter-1>=0) { if(B[d2-d1-counter-1]==1) k=k*2+1; else k=k*2; } counter++; } return (k^mess); } void receiver(int mess,int poly) { int k=0,num1; int d1=digitinbin(poly); int d2=digitinbin(mess); int *B=dectobin(mess); int counter=0; int times=d2-d1+1; int *temp=new int[d1]; int a=pow(2,d1-1); while(counter<times) { int j=0; if(counter==0) { for(int i=times-counter-1;i<d2-counter;i++) { temp[j]=B[i]; j++; } num1=bintodec(temp,j); } int num2=poly; if(counter!=0) num1=k; if(num1>=a) k=num1^num2; else k=num1; if(d2-d1-counter-1>=0) { if(B[d2-d1-counter-1]==1) k=k*2+1; else k=k*2; } counter++; } if(k==0) cout<<"MESSAGE TRANSMITTED SUCCESSFULLY\n"<<endl; else cout<<"ERROR IN TRANSMISSION\n"<<endl; } void main() { clrscr(); int message,genpoly,ch=1; cout<<"enter the number to be transmitted"<<endl; cin>>message; cout<<"Enter the generator polynomial number"<<endl; cin>>genpoly; int gp=sender(message,genpoly); while(ch==1 || ch==2) { cout<<"MENU"<<endl; cout<<"1-SUCCESSFUL TRANSMISSION"<<endl; cout<<"2-ERROR IN TRANSMISSION"<<endl; cout<<"PRESS ANY OTHER KEY TO EXIT"<<endl; cout<<"Enter your choice"<<endl; cin>>ch; if(ch==1) { cout<<"TRANSMITTING MESSAGE .......\n"<<endl; delay(5000); receiver(gp,genpoly); int *B=dectobin(message); int dig=digitinbin(message); cout<<"The transmitted message is "; for(int i=dig-1;i>=0;i--) cout<<B[i]; cout<<"\n"<<endl; } else if(ch==2) { cout<<"TRANSMITTING MESSAGE .......\n"<<endl; delay(5000); receiver(gp+1,genpoly); cout<<endl; } } cout<<"PROGRAM ENDS"<<endl; getch(); } /* OUTPUT enter the number to be transmitted 729 Enter the generator polynomial number 19 MENU 1-SUCCESSFUL TRANSMISSION 2-ERROR IN TRANSMISSION PRESS ANY OTHER KEY TO EXIT Enter your choice 1 TRANSMITTING MESSAGE ....... MESSAGE TRANSMITTED SUCCESSFULLY The transmitted message is 1011011001 MENU 1-SUCCESSFUL TRANSMISSION 2-ERROR IN TRANSMISSION PRESS ANY OTHER KEY TO EXIT Enter your choice 2 TRANSMITTING MESSAGE ....... ERROR IN TRANSMISSION MENU 1-SUCCESSFUL TRANSMISSION 2-ERROR IN TRANSMISSION PRESS ANY OTHER KEY TO EXIT Enter your choice 4 PROGRAM ENDS */
Saturday, March 22, 2014
Cyclic Redundancy Check (CRC) Program in C++
Labels:
c plus plus,
computer networks,
cpp program,
networks
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment