Sie sind auf Seite 1von 9

#include<stdio.

h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<alloc.h>
#include<string.h>
#define MAX 100
#define INFINITY 1000
#define MEMBER 1
#define NONMEMBER 0
struct addr
{
char node;
char addr[10];
};
struct addr nw[16]={
{'A',"0001"},
{'B',"0010"},
{'C',"0011"},
{'D',"0100"},
{'E',"0101"},
{'F',"0110"},
{'G',"0111"},
{'H',"1000"},
{'I',"1001"},
{'J',"1010"},
{'K',"1011"},
{'L',"1100"},
{'M',"1101"},
{'N',"1110"},
{'O',"1111"},
{'P',"0000"}
};
struct shortpath
{
char route;
struct shortpath *link;

};
struct pck
{
char trg[10];
int count;
char src[10];
char data[20];
}pck1;
void showbits(int n)
{
int i,k,andmask;
for(i=3;i>=0;i--)
{
andmask=1<<i;
k=n&andmask;
k==0? printf("0"):printf("1");
}
}
int sp(int w[][MAX],int nodes,int s,int t, int precede[])
{
int distance[MAX],perm[MAX];
int current,i,k,dc;
int small,new1;
for(i=0;i<nodes;i++)
{
perm[i]=NONMEMBER;
distance[i]=INFINITY;
}
perm[i]=MEMBER;
distance[s]=0;
current=s;
while(current!=t)
{
small=INFINITY;
dc=distance[current];
for(i=0;i<nodes;i++)
{

if(perm[i]==NONMEMBER)
{
new1=dc+w[current][i];
if(new1<distance[i])
{
distance[i]=new1;
precede[i]=current;
}
if(distance[i]<small)
{
small=distance[i];
k=i;
}
}
}
current=k;
perm[current]=MEMBER;
}
return distance[t];
}
void main()
{
int nodes,i,j,edges,wt,s,t,k=1,x,y,l,pck2;
char n1,n2,dummy,nod[20],pck3[10];
int w[MAX][MAX],dist,path[MAX];
struct shortpath *sph;
struct shortpath *last;
struct shortpath *st;
struct shortpath *end;
char src1,trg1;
char xor1[10];
char trim[MAX];
clrscr();
printf("Enter the Number of nodes in the Network\n");
scanf("%d",&nodes);
printf("Enter the nodes\n");
for(i=0;i<nodes;i++)
{
scanf("%c%c",&dummy,&nod[i]);
nod[i]=toupper(nod[i]);

}
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
w[i][j]=INFINITY;
w[i][j]=0;
}
printf("\nEnter the Number of edges in the Network\n");
scanf("%d",&edges);
printf("Enter the details of all the edges of the Network\n");
for(i=0;i<edges;i++)
{
printf("edge#%d:",i+1);
scanf("%c%c%c%d",&dummy,&n1,&n2,&wt);
n1=toupper(n1)-'A';
n2=toupper(n2)-'A';
w[n1][n2]=w[n2][n1]=wt;
}
printf("\nEnter the source node\t\t\t");
scanf("%c%c",&dummy,&s);
s=toupper(s)-'A';
printf("\nEnter the end node\t\t\t");
scanf("%c%c",&dummy,&t);
t=toupper(t)-'A';
dist=sp(w,nodes,s,t,path);
printf("\nSHORTEST PATH FROM %c to %c:\t\t",s+'A',t+'A');
i=t;
printf("%c<==",t+'A');
sph=(struct shortpath *) malloc(sizeof(struct shortpath *));
sph->route=t + 'A';
trg1=t+'A';
sph->link=NULL;
last=sph;
end=last;
while(path[i]!=s)
{
printf("%c<==",path[i]+'A');
sph=(struct shortpath *) malloc(sizeof(struct shortpath *));
sph->route=path[i]+'A';
sph->link =last;

last=sph;
i=path[i];
}
printf("%c",s+'A');
src1=s+'A';
sph=(struct shortpath *) malloc(sizeof(struct shortpath *));
sph->route=s + 'A';
sph->link =last;
last =sph;
/*contructing pck at src*/
printf("\n\n\t\tPacket is to be formed and Transmitted\n\n\n\n");
printf("\n\nEnter the data to be trasmitted\t\t");
scanf("%s",pck1.data);
pck1.count=0;
st=last;
i=0;
while(i<16)
{
if(src1==nw[i].node)
strcpy(pck1.src,nw[i].addr);
if(trg1==nw[i].node)
strcpy(pck1.trg,nw[i].addr);
i++;
}
printf("\nPacket at Source node %c\t\t\t" ,s+'A');
printf("%s ",pck1.trg);
showbits(pck1.count);
printf(" %s %s\n",pck1.src,pck1.data);
st=last->link;
while(st!=end)
{
i=0;
pck1.count++;
while(i<16)
{
if(st->route==nw[i].node)
{
strcpy(xor1,nw[i].addr); break;
}
i++;

}
for(i=0;pck1.src[i]!='\0';i++)
{
if(pck1.src[i] == xor1[i])
pck1.src[i]='0';
else
pck1.src[i]='1';
}
i=0,j=0;
while(pck1.src[i]!='\0')
{
trim[j]=pck1.src[i];
i++;
j++;
}
trim[j]='\0';
strcpy(pck1.src,trim);
printf("\nPacket at Node %c\t\t\t",st->route);
printf("%s ",pck1.trg);
showbits(pck1.count);
printf(" %s %s\n",pck1.src,pck1.data);
st=st->link;
}
printf("\nPacket at Target node %c\t\t\t",t+'A');
printf("%s ",pck1.trg);
showbits(pck1.count);
printf(" %s %s\n",pck1.src,pck1.data);
/*Reconstruction path*/
printf("\n\n\n\n\t\Reconstruction of path to be done\n\n\n\n");
printf("\n\nEnter the Distance\t\t\t");
scanf("%d",&pck1.count);
pck2=pck1.count;
printf("\n\nEnter the end result of XOR\t\t");
scanf("%s",&pck1.src);
strcpy(pck3,pck1.src);
if(pck1.count==0)
{
printf("\n\nThe Reconstruction Path is\t\t");
printf("%c<==",s+'A');
printf("%c",t+'A'); getch(); exit(0);

}
else
for(y=0;y<16;y++)
{
if(y!=t)
{
dist=sp(w,nodes,t,s=nod[y]-'A',path);
i=s;
while(path[i]!=t)
{
sph=(struct shortpath *) malloc(sizeof(struct shortpath *));
if(k==1)
{
sph->route=path[i]+'A';
sph->link =NULL;
last=sph;
end =last;
k=0;
}
else
{
sph->route=path[i]+'A';
sph->link =last;
last=sph;
}
i=path[i];
}
k=1;
pck2=pck1.count;
strcpy(pck1.src,pck3);
st=last;
while(st!=NULL)
{
pck2--;
i=0;
while(i<16)
{
if(st->route==nw[i].node)
{
strcpy(xor1,nw[i].addr); break;

}
i++;
}
for(i=0;pck1.src[i]!='\0';i++)
{
if(pck1.src[i] == xor1[i])
pck1.src[i]='0';
else
pck1.src[i]='1';
}
i=0,j=0;
while(pck1.src[i]!='\0')
{
trim[j]=pck1.src[i];
i++;
j++;
}
trim[j]='\0';
strcpy(pck1.src,trim);
st=st->link;
}
if(st==NULL && pck2==0)
{
x=0;
while(x<16)
{
if(strcmp(nw[x].addr,pck1.src)==0)
{
if(nw[x].node==s+'A')
{
printf("\n\nThe Reconstruction Path is\t\t");
l=s;
printf("%c<==",s+'A');
while(path[l]!=t)
{
printf("%c<==",path[l]+'A');
l=path[l];
}
printf("%c",t+'A'); getch(); exit(0);
}

}
x++;
}
}
}
printf("\nThere is No Path");
}
getch();
}

Das könnte Ihnen auch gefallen