主题:我发个实验大家分享一下,看能不能再改善一下,有一处错了,不知道怎么改啊!
编译错误在说明语法错误{MGaph *createmgraph()},望大家指点指点!
建立一个有向网络,求出指定起点(由键盘输入)到终点(指定)的最短路径;
#include<stdio.h>
#define MAXV 100
#define MAX 10000
typedef struct
{
char vexs[MAXV];
int edges[MAXV][MAXV];
int n,e;
}MGraph;
typedef struct
{
int path[MAXV][MAXV];
int ad[MAXV][MAXV];
}Chain;
[b]MGaph *createmgraph()[/b]
{
MGraph g;
int i,j,k;
int Adjmatrix[MAXV][MAXV];
printf("Please enter Apex number and side number of the graph.\n");
scanf("%d%d",&g->n,&g->e);
printf("Please enter the information of the graph.\n");
for(j=0;j<g->n;j++)
scanf("%c",g->vexs[j]);
for(i=0;i<g->n;i++)
for(j=0;j<g->n;j++)
g->edges[i][j]=0;
printf("Please enter the weight of each side.\n");
for(k=0;k<g->e;k++)
{
scanf("%d%d%d",&i,&j,&Adjmatrix[i][j]);
g->edges[i][j]=Adjmatrix[i][j];
}
return g;
}
Chain floyd(MGraph *g)
{
int i,j k,wm;
Chain h;
int path[][MAXV],ad[][MAXV]
for(i=0;i<g->n;i++)
for(j=0;j<g->n;j++)
{
h->ad[i][j]=g->edges[i][j];
if(i==j) h->path[i][j]=-1;
else if(h->ad[i][j]<MAX) h->path[i][j]=i;
else h->path[i][j]=-1;
}
for(k=0;k<g->n;k++)
for(i=0;i<g->n;i++)
for(j=0;j<g->n;j++)
if(h->ad[i][k]+h->ad[k][j]<h->ad[i][j])
{
h->ad[i][j]=h->ad[i][k]+h->ad[k][j];
h->path[i][j]=path[k][j];
}
return h;
}
void output(int m,int n,int path[m][n])
{ int p[MAXV],q[MAXV];
p[0]=n;
for(i=0;i<MAX;i++)
{
p[i]=path[m][n];
while(p[i]=m) break;
}
for(j=0;j<=i;j++)
{
q[j]=p[i--];
printf("V%d",&q[j]);
}
}
void main()
{
MGraph *t;
Chain *r;
int m,n;
printf("Now you can create MGraph.\n");
t=createmgraph();
r=floyd(t);
printf("OK,you have done it!\nInput the start&end vextex.\n");
scanf("%d%d",&m,&n);
printf("The shortest value from Vm to Vn is:%d\n",r->ad[m][n]);
printf("And the shortest path from Vm to Vn is:\n");
output(m,n,r->path[m][n]);
}
建立一个有向网络,求出指定起点(由键盘输入)到终点(指定)的最短路径;
#include<stdio.h>
#define MAXV 100
#define MAX 10000
typedef struct
{
char vexs[MAXV];
int edges[MAXV][MAXV];
int n,e;
}MGraph;
typedef struct
{
int path[MAXV][MAXV];
int ad[MAXV][MAXV];
}Chain;
[b]MGaph *createmgraph()[/b]
{
MGraph g;
int i,j,k;
int Adjmatrix[MAXV][MAXV];
printf("Please enter Apex number and side number of the graph.\n");
scanf("%d%d",&g->n,&g->e);
printf("Please enter the information of the graph.\n");
for(j=0;j<g->n;j++)
scanf("%c",g->vexs[j]);
for(i=0;i<g->n;i++)
for(j=0;j<g->n;j++)
g->edges[i][j]=0;
printf("Please enter the weight of each side.\n");
for(k=0;k<g->e;k++)
{
scanf("%d%d%d",&i,&j,&Adjmatrix[i][j]);
g->edges[i][j]=Adjmatrix[i][j];
}
return g;
}
Chain floyd(MGraph *g)
{
int i,j k,wm;
Chain h;
int path[][MAXV],ad[][MAXV]
for(i=0;i<g->n;i++)
for(j=0;j<g->n;j++)
{
h->ad[i][j]=g->edges[i][j];
if(i==j) h->path[i][j]=-1;
else if(h->ad[i][j]<MAX) h->path[i][j]=i;
else h->path[i][j]=-1;
}
for(k=0;k<g->n;k++)
for(i=0;i<g->n;i++)
for(j=0;j<g->n;j++)
if(h->ad[i][k]+h->ad[k][j]<h->ad[i][j])
{
h->ad[i][j]=h->ad[i][k]+h->ad[k][j];
h->path[i][j]=path[k][j];
}
return h;
}
void output(int m,int n,int path[m][n])
{ int p[MAXV],q[MAXV];
p[0]=n;
for(i=0;i<MAX;i++)
{
p[i]=path[m][n];
while(p[i]=m) break;
}
for(j=0;j<=i;j++)
{
q[j]=p[i--];
printf("V%d",&q[j]);
}
}
void main()
{
MGraph *t;
Chain *r;
int m,n;
printf("Now you can create MGraph.\n");
t=createmgraph();
r=floyd(t);
printf("OK,you have done it!\nInput the start&end vextex.\n");
scanf("%d%d",&m,&n);
printf("The shortest value from Vm to Vn is:%d\n",r->ad[m][n]);
printf("And the shortest path from Vm to Vn is:\n");
output(m,n,r->path[m][n]);
}