Sunteți pe pagina 1din 12

Darshan M Sonde

CCN LAB PROGRAMS

// program to stuff and destuff bits #include <iostream.h> #include <string.h> #include <conio.h> void stuff(); void destuff(); char bits[100]; //MSG format : 01111110 ..message.. 01111110 void main() { char ch[100]; clrscr(); cout<<"Enter Bits:"; cin>>ch; strcpy(bits,"01111110"); strcat(bits,ch); strcat(bits,"01111110"); cout<<"\ndata with flag:"<<bits; stuff(); cout<<"\n Stuffed data:"<<bits; destuff(); cout<<"\nDestuffed data:"<<bits; getch();

void stuff() { int n=strlen(bits),oneCount=0; for(int i=8;i<n-8;i++) //neglect first & last 8 flag bits { if(bits[i]=='1') { oneCount++; if(oneCount==5) { for(int j=n;j>i;j--) //copy array from i:n to j+1:n+1 bits[j+1]=bits[j]; //make room to stuff i.e shift right bits[i+1]='0'; //stuff '0' n++; oneCount=0; } } else oneCount=0; } } void destuff() { //function similar to stuffing int n=strlen(bits),oneCount=0; //remove first and last 8 bits = flag for(int i=0;i<n-16;i++) bits[i]=bits[i+8]; bits[i]='\0'; n=strlen(bits);

Darshan M Sonde

CCN LAB PROGRAMS

for(i=0;i<n;i++) { if(bits[i]=='1') { oneCount++; if(oneCount==5) { for(int j=i+1;j<n;j++) //copy array from i+2:n to i+1:n-1 bits[j]=bits[j+1];//copy such that '0' is destuffed n--; oneCount=0; } } else oneCount=0; }

OUTPUT Enter Bits:111111 data with flag: 0111111011111101111110 Stuffed data: 01111110111110101111110 Destuffed data: 111111

Darshan M Sonde

CCN LAB PROGRAMS

// program to stuff and destuff bits #include <iostream.h> #include <string.h> #include <conio.h> #include <stdlib.h> void stuff(); void destuff(); char bits[100]; //MSG format : DLE STX ..message.. DLE ETX void main() { char ch[100]; clrscr(); cout<<"Enter chars:"; cin>>ch; bits[0]=0x10;//DLE = bits[1]=0x02;//STX = strcat(bits,ch); int n=strlen(bits); bits[n]=0x10;//DLE = bits[n+1]=0x03;//ETX bits[n+2]='\0'; bits[4]=0x10;//put a = random DLE in message

} void stuff() { int n=strlen(bits); for(int i=2;i<n-2;i++) //neglect first & last 2 flag bits { if(bits[i]==0x10||bits[i]==0x02||bits[i]==0x03) {//if you get FLAG(STX,ETX) or ESC in msg stuff a ESC for(int j=n;j>=i;j--) bits[j+1]=bits[j]; //make room to stuff i.e shift right bits[i]=0x10;//stuff DLE n++; i++; } } } void destuff() { //function similar to stuffing int n=strlen(bits); //remove first and last 2 bits = flag for(int i=0;i<n-4;i++) bits[i]=bits[i+2]; bits[i]='\0'; n=strlen(bits); for(i=0;i<n;i++) { if(bits[i]==0x10) { for(int j=i+1;j<n;j++) bits[j]=bits[j+1]; n--; } }

cout<<"\ndata with flag:"<<bits; stuff(); cout<<"\n Stuffed data:"<<bits; destuff(); cout<<"\nDestuffed data:"<<bits; getch();

//copy such that DLE is destuffed

Darshan M Sonde

CCN LAB PROGRAMS

OUTPUT: aim is to transmit and receive mesage Enter chars:message data with flag: mesage Stuffed data: mesage Destuffed data: mesage

Darshan M Sonde

CCN LAB PROGRAMS

//program for finding shortest path //using function given in CCN - TANENBAUM #include <stdio.h> #include <iostream.h> #include <conio.h> #include <stdlib.h> #define MAX_NODES 100 #define INFINITY 10000 int n,dist[MAX_NODES][MAX_NODES]; void shortest_path(int s,int t,int path[]) { enum lbl{permanent,tentative} ; struct state { int predecessor; int length; lbl label; }state[MAX_NODES]; int i, k, min; struct state *p; for(p=&state[0]; p<&state[n];p++) { p->predecessor = -1; p->length = INFINITY; p->label = tentative; } state[t].length = 0; state[t].label = permanent; k=t; do { for(i=0;i<n;i++) if(dist[k][i] != 0 && state[i].label == tentative) { if(state[k].length+dist[k][i]<state[i].length) { state[i].predecessor = k; state[i].length = state[k].length + dist[k][i]; } } k=0;min=INFINITY; for(i=0;i<n;i++) if(state[i].label == tentative && state[i].length<min) { min = state[i].length; k=i; } state[k].label = permanent; }while( k!= s); i=0;k=s; do{ path[i++]=k;k=state[k].predecessor; }while(k>=0);

Darshan M Sonde

CCN LAB PROGRAMS

int main() { int i,j,tmp,tmp2,p[MAX_NODES]; for(i=0;i<MAX_NODES;i++) for(j=0;j<MAX_NODES;j++) dist[i][j]=INFINITY; printf("\nEnter no of nodes:"); scanf("%d",&n); cout<<"Enter src,dest,distance(-1,x,x to end):\n"; do{ cin>>i>>j>>tmp; //i=src, j=dest, tmp=distance if(i>=0) dist[i][j] = dist[j][i] = tmp; }while(i>=0); cout<<"\nSource node:"; cin>>tmp; cout<<"Destination:"; cin>>tmp2; shortest_path(tmp,tmp2,p); //p is path returned from function printf("\n"); for(i=0;i<n;i++) { printf("%d ",p[i]); if(p[i]==tmp2) //if p[i]==destination stop. break; } getch(); return 0;

OUTPUT

1 1

2 3

Shortest path connect

Enter no of nodes:5 Enter src,dest,distance(-1,x,x to end): 0 1 1 1 2 2 2 3 3 3 4 1 4 1 4 1 4 1 -1 0 0 Source node:0 Destination:3 0 1 4 3

Darshan M Sonde

CCN LAB PROGRAMS

//program for encryption and decryption using substitution #include <iostream.h> #include <string.h> #include <conio.h> void main() { char msg[100]; char table[26] = "qwertyuiopasdfghjklzxcvbnm";//look up table cout<<"\nEnter msg:"; cin>>msg; //ENCRYPTION------------------------------------------int i; for(i=0;i<strlen(msg);i++) msg[i] = table[msg[i]-'a'];//get the look up value cout<<"Encrypted message:"<<msg; //DECRYPTION------------------------------------------for(i=0;i<strlen(msg);i++) { for(int j=0;j<26;j++) if(msg[i]==table[j])break; //gets position of char i inside table into j //ex: pos of q=0 w=1 e=2 .....m=25 ,which correspond to a,b,c...z msg[i]=j+'a'; } cout<<"\nDecrypted message:"<<msg; getch();

OUTPUT Enter msg:message Encrypted message:dtllqut Decrypted message:message

Darshan M Sonde

CCN LAB PROGRAMS

//program for encryption and decryption of a message using transposition #include <iostream.h> #include <conio.h> #include <string.h> #define NCRYPT 0 #define DCRYPT 1 void crypt(int en); char msg[100],key[80]; char msgCopy[100]; int n,m,pos[80]; int i,j,count; void main() { cout<<"\nEnter Message:"; cin.getline(msg,99); cout<<"Enter keyword:"; cin>>key; n=strlen(key),m=strlen(msg); strcpy(msgCopy,msg); for(i=0;i<n;i++) { count=1; for(j=0;j<n;j++) if(key[j]<key[i]) //get the no of chars less than key[i] count++; //this gives position of key[i] pos[count-1]=i; //now store offset required, to output msg[pos } crypt(NCRYPT); cout<<"\nEncrypted Message:"<<msg; strcpy(msgCopy,msg); crypt(DCRYPT); cout<<"\nDecrypted Message:"<<msg; getch();

[i]].

} void crypt(int en) { j=0; //count is position of cursor for(i=0;i<n;i++) { count=0; while(count+pos[i]<m) { if(en==NCRYPT) msg[j++]=msgCopy[count+pos[i]];//encrypt else msg[count+pos[i]]=msgCopy[j++]; //just reverse of encryption line above count+=n; } } } OUTPUT Enter Message:message Enter Keyword:key Encrypted Message:eamsesg Decrypted Message:message

Darshan M Sonde

CCN LAB PROGRAMS

//minimum spanning tree #include <stdio.h> #include <conio.h> //using prim's algorithm //in_tree is set of selected nodes, initially make any node // selected/attached(=1) to tree //while(tree is not complete) //{ // find least cost edge with one node free(=0)& one node attached to tree(=1) // ^^^^^^^^^^ // if(no such node) error in network // add edge to tree or display // delete edge from network //} #define MAX 100 #define INF 10000 int n,edges[MAX][MAX],in_tree[MAX]; void main() { int i,j; for(i=0;i<MAX;i++) for(j=0;j<MAX;j++) { edges[i][j]=INF; in_tree[i]=0; } printf("\nEnter no of nodes:"); scanf("%d",&n); printf("\nEnter a,b,dist (-1,x,x to end):\n"); while(1) { int tmp; scanf("%d%d%d",&i,&j,&tmp); if(i<0)break; edges[i][j]=edges[j][i]=tmp; } //find tree in_tree[0]=1; int count=0,min,a,b,cost=0; while(count<n-1) { min=INF; for(i=0;i<n;i++) for(j=0;j<n;j++) if(in_tree[j]&&!in_tree[i]&&min>edges[i][j]) { //i th node is not attached //j th node is in tree min=edges[i][j]; a=i;b=j; } if(min==INF) { printf("Error in network:count=%d",count); break; } count++; in_tree[a]=1; //attach node to tree printf("\n%d--%d",a,b); cost+=edges[a][b]; edges[a][b]=edges[b][a]=INF; } printf("\ntotal cost=%d",cost); getch(); }

Darshan M Sonde

CCN LAB PROGRAMS

10

0 10 5 25 6

28 1 14 16 2 12 5

0 10 14 6 25 4 22 3 Min spanning tree 1 16 2 12

24 18 4 22 3 tree

Enter no of nodes:7 Enter a,b,dist(-1,x,x to end): 0 1 28 1 2 16 2 3 12 3 4 22 4 5 25 5 0 10 4 6 24 6 3 18 6 1 14 -1 0 0 50 45 34 23 12 61 total cost=99

Darshan M Sonde

CCN LAB PROGRAMS

11

// crc program #include <stdio.h> #include <conio.h> void disp(long); void main() { unsigned long data=0; unsigned long data_copy; unsigned long CRC_CODE=0x88108000; //ccitt polynomial x^16 + x^12 + x^5 + 1 = 1000 1000 0001 0000 1 //CRC code is in MSB 17 bits, hence remainder in MSB 16 bits int n,i,ch; clrscr(); printf("\nEnter data length(1-16):"); scanf("%d",&n); printf("Enter msg:"); for(i=0;i<n;i++) { data<<=1; ch=getche(); ch-='0'; if(ch) data|=1; //input to LSB bit, //if 0 no need to add it is zero from shifting } data<<=16; //padding 16 zeros data_copy=data; //crc-generation for(i=0;i<16;i++) { ch=(data>>31)&1; //check if msb is '1' if(ch) data=data^CRC_CODE; data<<=1; } printf("\nchecksum code :"); disp(data>>16); data = data_copy^(data>>16); //data>>16 because remainder is in MSB 16 bits printf("\ngenerated data+checksum:"); disp(data); //crc-checking printf("\nchecking crc of data...:"); data = data ^ 0x0002; //introduce error for(i=0;i<16;i++) { ch=(data>>31)&1; //check if msb is '1' if(ch) data=data^CRC_CODE; data<<=1; } if(data==0) printf("NO ERRORS FOUND"); else printf("ERROR DETECTED"); getch();

Darshan M Sonde

CCN LAB PROGRAMS

12

void disp(long val) { //displays contents of val all 32 bits int i,ch; for(i=0;i<32;i++) { ch=val>>(31-i); ch=ch&1; if(ch) printf("1"); else printf("0"); } } OUTPUT Enter dta length(1-16):4 Enter msg:1101 checksum code :0000.0000.0000.0000.1101.0001.1010.1101 generated data+checksum:0000.0000.0000.1101.1101.0001.1010.1101 checking crc of data :ERROR DETECTED //commenting the line in program used to introduce error gives checking crc of data...:NO ERROR DETECTED //the . in above output is.just.for.clarity

S-ar putea să vă placă și